OSDN Git Service

* config/alpha/alpha.md, config/arm/arm.c, config/arm/arm.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    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 "rtl.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "real.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "except.h"
36 #include "function.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "toplev.h"
41 #include "basic-block.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "target-def.h"
45
46 #ifndef CHECK_STACK_LIMIT
47 #define CHECK_STACK_LIMIT -1
48 #endif
49
50 /* Processor costs (relative to an add) */
51 static const 
52 struct processor_costs size_cost = {    /* costs for tunning for size */
53   2,                                    /* cost of an add instruction */
54   3,                                    /* cost of a lea instruction */
55   2,                                    /* variable shift costs */
56   3,                                    /* constant shift costs */
57   3,                                    /* cost of starting a multiply */
58   0,                                    /* cost of multiply per each bit set */
59   3,                                    /* cost of a divide/mod */
60   3,                                    /* cost of movsx */
61   3,                                    /* cost of movzx */
62   0,                                    /* "large" insn */
63   2,                                    /* MOVE_RATIO */
64   2,                                    /* cost for loading QImode using movzbl */
65   {2, 2, 2},                            /* cost of loading integer registers
66                                            in QImode, HImode and SImode.
67                                            Relative to reg-reg move (2).  */
68   {2, 2, 2},                            /* cost of storing integer registers */
69   2,                                    /* cost of reg,reg fld/fst */
70   {2, 2, 2},                            /* cost of loading fp registers
71                                            in SFmode, DFmode and XFmode */
72   {2, 2, 2},                            /* cost of loading integer registers */
73   3,                                    /* cost of moving MMX register */
74   {3, 3},                               /* cost of loading MMX registers
75                                            in SImode and DImode */
76   {3, 3},                               /* cost of storing MMX registers
77                                            in SImode and DImode */
78   3,                                    /* cost of moving SSE register */
79   {3, 3, 3},                            /* cost of loading SSE registers
80                                            in SImode, DImode and TImode */
81   {3, 3, 3},                            /* cost of storing SSE registers
82                                            in SImode, DImode and TImode */
83   3,                                    /* MMX or SSE register to integer */
84 };
85 /* Processor costs (relative to an add) */
86 static const 
87 struct processor_costs i386_cost = {    /* 386 specific costs */
88   1,                                    /* cost of an add instruction */
89   1,                                    /* cost of a lea instruction */
90   3,                                    /* variable shift costs */
91   2,                                    /* constant shift costs */
92   6,                                    /* cost of starting a multiply */
93   1,                                    /* cost of multiply per each bit set */
94   23,                                   /* cost of a divide/mod */
95   3,                                    /* cost of movsx */
96   2,                                    /* cost of movzx */
97   15,                                   /* "large" insn */
98   3,                                    /* MOVE_RATIO */
99   4,                                    /* cost for loading QImode using movzbl */
100   {2, 4, 2},                            /* cost of loading integer registers
101                                            in QImode, HImode and SImode.
102                                            Relative to reg-reg move (2).  */
103   {2, 4, 2},                            /* cost of storing integer registers */
104   2,                                    /* cost of reg,reg fld/fst */
105   {8, 8, 8},                            /* cost of loading fp registers
106                                            in SFmode, DFmode and XFmode */
107   {8, 8, 8},                            /* cost of loading integer registers */
108   2,                                    /* cost of moving MMX register */
109   {4, 8},                               /* cost of loading MMX registers
110                                            in SImode and DImode */
111   {4, 8},                               /* cost of storing MMX registers
112                                            in SImode and DImode */
113   2,                                    /* cost of moving SSE register */
114   {4, 8, 16},                           /* cost of loading SSE registers
115                                            in SImode, DImode and TImode */
116   {4, 8, 16},                           /* cost of storing SSE registers
117                                            in SImode, DImode and TImode */
118   3,                                    /* MMX or SSE register to integer */
119 };
120
121 static const 
122 struct processor_costs i486_cost = {    /* 486 specific costs */
123   1,                                    /* cost of an add instruction */
124   1,                                    /* cost of a lea instruction */
125   3,                                    /* variable shift costs */
126   2,                                    /* constant shift costs */
127   12,                                   /* cost of starting a multiply */
128   1,                                    /* cost of multiply per each bit set */
129   40,                                   /* cost of a divide/mod */
130   3,                                    /* cost of movsx */
131   2,                                    /* cost of movzx */
132   15,                                   /* "large" insn */
133   3,                                    /* MOVE_RATIO */
134   4,                                    /* cost for loading QImode using movzbl */
135   {2, 4, 2},                            /* cost of loading integer registers
136                                            in QImode, HImode and SImode.
137                                            Relative to reg-reg move (2).  */
138   {2, 4, 2},                            /* cost of storing integer registers */
139   2,                                    /* cost of reg,reg fld/fst */
140   {8, 8, 8},                            /* cost of loading fp registers
141                                            in SFmode, DFmode and XFmode */
142   {8, 8, 8},                            /* cost of loading integer registers */
143   2,                                    /* cost of moving MMX register */
144   {4, 8},                               /* cost of loading MMX registers
145                                            in SImode and DImode */
146   {4, 8},                               /* cost of storing MMX registers
147                                            in SImode and DImode */
148   2,                                    /* cost of moving SSE register */
149   {4, 8, 16},                           /* cost of loading SSE registers
150                                            in SImode, DImode and TImode */
151   {4, 8, 16},                           /* cost of storing SSE registers
152                                            in SImode, DImode and TImode */
153   3                                     /* MMX or SSE register to integer */
154 };
155
156 static const 
157 struct processor_costs pentium_cost = {
158   1,                                    /* cost of an add instruction */
159   1,                                    /* cost of a lea instruction */
160   4,                                    /* variable shift costs */
161   1,                                    /* constant shift costs */
162   11,                                   /* cost of starting a multiply */
163   0,                                    /* cost of multiply per each bit set */
164   25,                                   /* cost of a divide/mod */
165   3,                                    /* cost of movsx */
166   2,                                    /* cost of movzx */
167   8,                                    /* "large" insn */
168   6,                                    /* MOVE_RATIO */
169   6,                                    /* cost for loading QImode using movzbl */
170   {2, 4, 2},                            /* cost of loading integer registers
171                                            in QImode, HImode and SImode.
172                                            Relative to reg-reg move (2).  */
173   {2, 4, 2},                            /* cost of storing integer registers */
174   2,                                    /* cost of reg,reg fld/fst */
175   {2, 2, 6},                            /* cost of loading fp registers
176                                            in SFmode, DFmode and XFmode */
177   {4, 4, 6},                            /* cost of loading integer registers */
178   8,                                    /* cost of moving MMX register */
179   {8, 8},                               /* cost of loading MMX registers
180                                            in SImode and DImode */
181   {8, 8},                               /* cost of storing MMX registers
182                                            in SImode and DImode */
183   2,                                    /* cost of moving SSE register */
184   {4, 8, 16},                           /* cost of loading SSE registers
185                                            in SImode, DImode and TImode */
186   {4, 8, 16},                           /* cost of storing SSE registers
187                                            in SImode, DImode and TImode */
188   3                                     /* MMX or SSE register to integer */
189 };
190
191 static const 
192 struct processor_costs pentiumpro_cost = {
193   1,                                    /* cost of an add instruction */
194   1,                                    /* cost of a lea instruction */
195   1,                                    /* variable shift costs */
196   1,                                    /* constant shift costs */
197   4,                                    /* cost of starting a multiply */
198   0,                                    /* cost of multiply per each bit set */
199   17,                                   /* cost of a divide/mod */
200   1,                                    /* cost of movsx */
201   1,                                    /* cost of movzx */
202   8,                                    /* "large" insn */
203   6,                                    /* MOVE_RATIO */
204   2,                                    /* cost for loading QImode using movzbl */
205   {4, 4, 4},                            /* cost of loading integer registers
206                                            in QImode, HImode and SImode.
207                                            Relative to reg-reg move (2).  */
208   {2, 2, 2},                            /* cost of storing integer registers */
209   2,                                    /* cost of reg,reg fld/fst */
210   {2, 2, 6},                            /* cost of loading fp registers
211                                            in SFmode, DFmode and XFmode */
212   {4, 4, 6},                            /* cost of loading integer registers */
213   2,                                    /* cost of moving MMX register */
214   {2, 2},                               /* cost of loading MMX registers
215                                            in SImode and DImode */
216   {2, 2},                               /* cost of storing MMX registers
217                                            in SImode and DImode */
218   2,                                    /* cost of moving SSE register */
219   {2, 2, 8},                            /* cost of loading SSE registers
220                                            in SImode, DImode and TImode */
221   {2, 2, 8},                            /* cost of storing SSE registers
222                                            in SImode, DImode and TImode */
223   3                                     /* MMX or SSE register to integer */
224 };
225
226 static const 
227 struct processor_costs k6_cost = {
228   1,                                    /* cost of an add instruction */
229   2,                                    /* cost of a lea instruction */
230   1,                                    /* variable shift costs */
231   1,                                    /* constant shift costs */
232   3,                                    /* cost of starting a multiply */
233   0,                                    /* cost of multiply per each bit set */
234   18,                                   /* cost of a divide/mod */
235   2,                                    /* cost of movsx */
236   2,                                    /* cost of movzx */
237   8,                                    /* "large" insn */
238   4,                                    /* MOVE_RATIO */
239   3,                                    /* cost for loading QImode using movzbl */
240   {4, 5, 4},                            /* cost of loading integer registers
241                                            in QImode, HImode and SImode.
242                                            Relative to reg-reg move (2).  */
243   {2, 3, 2},                            /* cost of storing integer registers */
244   4,                                    /* cost of reg,reg fld/fst */
245   {6, 6, 6},                            /* cost of loading fp registers
246                                            in SFmode, DFmode and XFmode */
247   {4, 4, 4},                            /* cost of loading integer registers */
248   2,                                    /* cost of moving MMX register */
249   {2, 2},                               /* cost of loading MMX registers
250                                            in SImode and DImode */
251   {2, 2},                               /* cost of storing MMX registers
252                                            in SImode and DImode */
253   2,                                    /* cost of moving SSE register */
254   {2, 2, 8},                            /* cost of loading SSE registers
255                                            in SImode, DImode and TImode */
256   {2, 2, 8},                            /* cost of storing SSE registers
257                                            in SImode, DImode and TImode */
258   6                                     /* MMX or SSE register to integer */
259 };
260
261 static const 
262 struct processor_costs athlon_cost = {
263   1,                                    /* cost of an add instruction */
264   2,                                    /* cost of a lea instruction */
265   1,                                    /* variable shift costs */
266   1,                                    /* constant shift costs */
267   5,                                    /* cost of starting a multiply */
268   0,                                    /* cost of multiply per each bit set */
269   42,                                   /* cost of a divide/mod */
270   1,                                    /* cost of movsx */
271   1,                                    /* cost of movzx */
272   8,                                    /* "large" insn */
273   9,                                    /* MOVE_RATIO */
274   4,                                    /* cost for loading QImode using movzbl */
275   {4, 5, 4},                            /* cost of loading integer registers
276                                            in QImode, HImode and SImode.
277                                            Relative to reg-reg move (2).  */
278   {2, 3, 2},                            /* cost of storing integer registers */
279   4,                                    /* cost of reg,reg fld/fst */
280   {6, 6, 20},                           /* cost of loading fp registers
281                                            in SFmode, DFmode and XFmode */
282   {4, 4, 16},                           /* cost of loading integer registers */
283   2,                                    /* cost of moving MMX register */
284   {2, 2},                               /* cost of loading MMX registers
285                                            in SImode and DImode */
286   {2, 2},                               /* cost of storing MMX registers
287                                            in SImode and DImode */
288   2,                                    /* cost of moving SSE register */
289   {2, 2, 8},                            /* cost of loading SSE registers
290                                            in SImode, DImode and TImode */
291   {2, 2, 8},                            /* cost of storing SSE registers
292                                            in SImode, DImode and TImode */
293   6                                     /* MMX or SSE register to integer */
294 };
295
296 static const 
297 struct processor_costs pentium4_cost = {
298   1,                                    /* cost of an add instruction */
299   1,                                    /* cost of a lea instruction */
300   8,                                    /* variable shift costs */
301   8,                                    /* constant shift costs */
302   30,                                   /* cost of starting a multiply */
303   0,                                    /* cost of multiply per each bit set */
304   112,                                  /* cost of a divide/mod */
305   1,                                    /* cost of movsx */
306   1,                                    /* cost of movzx */
307   16,                                   /* "large" insn */
308   6,                                    /* MOVE_RATIO */
309   2,                                    /* cost for loading QImode using movzbl */
310   {4, 5, 4},                            /* cost of loading integer registers
311                                            in QImode, HImode and SImode.
312                                            Relative to reg-reg move (2).  */
313   {2, 3, 2},                            /* cost of storing integer registers */
314   2,                                    /* cost of reg,reg fld/fst */
315   {2, 2, 6},                            /* cost of loading fp registers
316                                            in SFmode, DFmode and XFmode */
317   {4, 4, 6},                            /* cost of loading integer registers */
318   2,                                    /* cost of moving MMX register */
319   {2, 2},                               /* cost of loading MMX registers
320                                            in SImode and DImode */
321   {2, 2},                               /* cost of storing MMX registers
322                                            in SImode and DImode */
323   12,                                   /* cost of moving SSE register */
324   {12, 12, 12},                         /* cost of loading SSE registers
325                                            in SImode, DImode and TImode */
326   {2, 2, 8},                            /* cost of storing SSE registers
327                                            in SImode, DImode and TImode */
328   10,                                   /* MMX or SSE register to integer */
329 };
330
331 const struct processor_costs *ix86_cost = &pentium_cost;
332
333 /* Processor feature/optimization bitmasks.  */
334 #define m_386 (1<<PROCESSOR_I386)
335 #define m_486 (1<<PROCESSOR_I486)
336 #define m_PENT (1<<PROCESSOR_PENTIUM)
337 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
338 #define m_K6  (1<<PROCESSOR_K6)
339 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
340 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
341
342 const int x86_use_leave = m_386 | m_K6 | m_ATHLON;
343 const int x86_push_memory = m_386 | m_K6 | m_ATHLON | m_PENT4;
344 const int x86_zero_extend_with_and = m_486 | m_PENT;
345 const int x86_movx = m_ATHLON | m_PPRO | m_PENT4 /* m_386 | m_K6 */;
346 const int x86_double_with_add = ~m_386;
347 const int x86_use_bit_test = m_386;
348 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON | m_K6;
349 const int x86_cmove = m_PPRO | m_ATHLON | m_PENT4;
350 const int x86_3dnow_a = m_ATHLON;
351 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON | m_PENT4;
352 const int x86_branch_hints = m_PENT4;
353 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4;
354 const int x86_partial_reg_stall = m_PPRO;
355 const int x86_use_loop = m_K6;
356 const int x86_use_fiop = ~(m_PPRO | m_ATHLON | m_PENT);
357 const int x86_use_mov0 = m_K6;
358 const int x86_use_cltd = ~(m_PENT | m_K6);
359 const int x86_read_modify_write = ~m_PENT;
360 const int x86_read_modify = ~(m_PENT | m_PPRO);
361 const int x86_split_long_moves = m_PPRO;
362 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486;
363 const int x86_single_stringop = m_386 | m_PENT4;
364 const int x86_qimode_math = ~(0);
365 const int x86_promote_qi_regs = 0;
366 const int x86_himode_math = ~(m_PPRO);
367 const int x86_promote_hi_regs = m_PPRO;
368 const int x86_sub_esp_4 = m_ATHLON | m_PPRO | m_PENT4;
369 const int x86_sub_esp_8 = m_ATHLON | m_PPRO | m_386 | m_486 | m_PENT4;
370 const int x86_add_esp_4 = m_ATHLON | m_K6 | m_PENT4;
371 const int x86_add_esp_8 = m_ATHLON | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4;
372 const int x86_integer_DFmode_moves = ~(m_ATHLON | m_PENT4);
373 const int x86_partial_reg_dependency = m_ATHLON | m_PENT4;
374 const int x86_memory_mismatch_stall = m_ATHLON | m_PENT4;
375 const int x86_accumulate_outgoing_args = m_ATHLON | m_PENT4 | m_PPRO;
376 const int x86_prologue_using_move = m_ATHLON | m_PENT4 | m_PPRO;
377 const int x86_epilogue_using_move = m_ATHLON | m_PENT4 | m_PPRO;
378
379 /* In case the avreage insn count for single function invocation is
380    lower than this constant, emit fast (but longer) prologue and
381    epilogue code.  */
382 #define FAST_PROLOGUE_INSN_COUNT 30
383 /* Set by prologue expander and used by epilogue expander to determine
384    the style used.  */
385 static int use_fast_prologue_epilogue;
386
387 #define AT_BP(mode) (gen_rtx_MEM ((mode), hard_frame_pointer_rtx))
388
389 static const char *const hi_reg_name[] = HI_REGISTER_NAMES; /* names for 16 bit regs */
390 static const char *const qi_reg_name[] = QI_REGISTER_NAMES; /* names for 8 bit regs (low) */
391 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES; /* names for 8 bit regs (high) */
392
393 /* Array of the smallest class containing reg number REGNO, indexed by
394    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
395
396 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
397 {
398   /* ax, dx, cx, bx */
399   AREG, DREG, CREG, BREG,
400   /* si, di, bp, sp */
401   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
402   /* FP registers */
403   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
404   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
405   /* arg pointer */
406   NON_Q_REGS,
407   /* flags, fpsr, dirflag, frame */
408   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
409   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
410   SSE_REGS, SSE_REGS,
411   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
412   MMX_REGS, MMX_REGS,
413   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
414   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
415   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
416   SSE_REGS, SSE_REGS,
417 };
418
419 /* The "default" register map used in 32bit mode.  */
420
421 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
422 {
423   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
424   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
425   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
426   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
427   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
428   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
429   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
430 };
431
432 static int x86_64_int_parameter_registers[6] = {5 /*RDI*/, 4 /*RSI*/,
433                                                 1 /*RDX*/, 2 /*RCX*/,
434                                                 FIRST_REX_INT_REG /*R8 */,
435                                                 FIRST_REX_INT_REG + 1 /*R9 */};
436 static int x86_64_int_return_registers[4] = {0 /*RAX*/, 1 /*RDI*/, 5, 4};
437
438 /* The "default" register map used in 64bit mode.  */
439 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
440 {
441   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
442   33, 34, 35, 36, 37, 38, 39, 40        /* fp regs */
443   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
444   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
445   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
446   8,9,10,11,12,13,14,15,                /* extended integer registers */
447   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
448 };
449
450 /* Define the register numbers to be used in Dwarf debugging information.
451    The SVR4 reference port C compiler uses the following register numbers
452    in its Dwarf output code:
453         0 for %eax (gcc regno = 0)
454         1 for %ecx (gcc regno = 2)
455         2 for %edx (gcc regno = 1)
456         3 for %ebx (gcc regno = 3)
457         4 for %esp (gcc regno = 7)
458         5 for %ebp (gcc regno = 6)
459         6 for %esi (gcc regno = 4)
460         7 for %edi (gcc regno = 5)
461    The following three DWARF register numbers are never generated by
462    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
463    believes these numbers have these meanings.
464         8  for %eip    (no gcc equivalent)
465         9  for %eflags (gcc regno = 17)
466         10 for %trapno (no gcc equivalent)
467    It is not at all clear how we should number the FP stack registers
468    for the x86 architecture.  If the version of SDB on x86/svr4 were
469    a bit less brain dead with respect to floating-point then we would
470    have a precedent to follow with respect to DWARF register numbers
471    for x86 FP registers, but the SDB on x86/svr4 is so completely
472    broken with respect to FP registers that it is hardly worth thinking
473    of it as something to strive for compatibility with.
474    The version of x86/svr4 SDB I have at the moment does (partially)
475    seem to believe that DWARF register number 11 is associated with
476    the x86 register %st(0), but that's about all.  Higher DWARF
477    register numbers don't seem to be associated with anything in
478    particular, and even for DWARF regno 11, SDB only seems to under-
479    stand that it should say that a variable lives in %st(0) (when
480    asked via an `=' command) if we said it was in DWARF regno 11,
481    but SDB still prints garbage when asked for the value of the
482    variable in question (via a `/' command).
483    (Also note that the labels SDB prints for various FP stack regs
484    when doing an `x' command are all wrong.)
485    Note that these problems generally don't affect the native SVR4
486    C compiler because it doesn't allow the use of -O with -g and
487    because when it is *not* optimizing, it allocates a memory
488    location for each floating-point variable, and the memory
489    location is what gets described in the DWARF AT_location
490    attribute for the variable in question.
491    Regardless of the severe mental illness of the x86/svr4 SDB, we
492    do something sensible here and we use the following DWARF
493    register numbers.  Note that these are all stack-top-relative
494    numbers.
495         11 for %st(0) (gcc regno = 8)
496         12 for %st(1) (gcc regno = 9)
497         13 for %st(2) (gcc regno = 10)
498         14 for %st(3) (gcc regno = 11)
499         15 for %st(4) (gcc regno = 12)
500         16 for %st(5) (gcc regno = 13)
501         17 for %st(6) (gcc regno = 14)
502         18 for %st(7) (gcc regno = 15)
503 */
504 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
505 {
506   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
507   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
508   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
509   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
510   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
511   -1, -1, -1, -1, -1, -1, -1, -1,       /* extemded integer registers */
512   -1, -1, -1, -1, -1, -1, -1, -1,       /* extemded SSE registers */
513 };
514
515 /* Test and compare insns in i386.md store the information needed to
516    generate branch and scc insns here.  */
517
518 struct rtx_def *ix86_compare_op0 = NULL_RTX;
519 struct rtx_def *ix86_compare_op1 = NULL_RTX;
520
521 #define MAX_386_STACK_LOCALS 3
522 /* Size of the register save area.  */
523 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
524
525 /* Define the structure for the machine field in struct function.  */
526 struct machine_function
527 {
528   rtx stack_locals[(int) MAX_MACHINE_MODE][MAX_386_STACK_LOCALS];
529   int save_varrargs_registers;
530   int accesses_prev_frame;
531 };
532
533 #define ix86_stack_locals (cfun->machine->stack_locals)
534 #define ix86_save_varrargs_registers (cfun->machine->save_varrargs_registers)
535
536 /* Structure describing stack frame layout.
537    Stack grows downward:
538
539    [arguments]
540                                               <- ARG_POINTER
541    saved pc
542
543    saved frame pointer if frame_pointer_needed
544                                               <- HARD_FRAME_POINTER
545    [saved regs]
546
547    [padding1]          \
548                         )
549    [va_arg registers]  (
550                         > to_allocate         <- FRAME_POINTER
551    [frame]             (
552                         )
553    [padding2]          /
554   */
555 struct ix86_frame
556 {
557   int nregs;
558   int padding1;
559   int va_arg_size;
560   HOST_WIDE_INT frame;
561   int padding2;
562   int outgoing_arguments_size;
563   int red_zone_size;
564
565   HOST_WIDE_INT to_allocate;
566   /* The offsets relative to ARG_POINTER.  */
567   HOST_WIDE_INT frame_pointer_offset;
568   HOST_WIDE_INT hard_frame_pointer_offset;
569   HOST_WIDE_INT stack_pointer_offset;
570 };
571
572 /* Code model option as passed by user.  */
573 const char *ix86_cmodel_string;
574 /* Parsed value.  */
575 enum cmodel ix86_cmodel;
576
577 /* which cpu are we scheduling for */
578 enum processor_type ix86_cpu;
579
580 /* which instruction set architecture to use.  */
581 int ix86_arch;
582
583 /* Strings to hold which cpu and instruction set architecture  to use.  */
584 const char *ix86_cpu_string;            /* for -mcpu=<xxx> */
585 const char *ix86_arch_string;           /* for -march=<xxx> */
586
587 /* # of registers to use to pass arguments.  */
588 const char *ix86_regparm_string;
589
590 /* ix86_regparm_string as a number */
591 int ix86_regparm;
592
593 /* Alignment to use for loops and jumps:  */
594
595 /* Power of two alignment for loops.  */
596 const char *ix86_align_loops_string;
597
598 /* Power of two alignment for non-loop jumps.  */
599 const char *ix86_align_jumps_string;
600
601 /* Power of two alignment for stack boundary in bytes.  */
602 const char *ix86_preferred_stack_boundary_string;
603
604 /* Preferred alignment for stack boundary in bits.  */
605 int ix86_preferred_stack_boundary;
606
607 /* Values 1-5: see jump.c */
608 int ix86_branch_cost;
609 const char *ix86_branch_cost_string;
610
611 /* Power of two alignment for functions.  */
612 const char *ix86_align_funcs_string;
613
614 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
615 static char internal_label_prefix[16];
616 static int internal_label_prefix_len;
617 \f
618 static int local_symbolic_operand PARAMS ((rtx, enum machine_mode));
619 static void output_pic_addr_const PARAMS ((FILE *, rtx, int));
620 static void put_condition_code PARAMS ((enum rtx_code, enum machine_mode,
621                                        int, int, FILE *));
622 static rtx ix86_expand_int_compare PARAMS ((enum rtx_code, rtx, rtx));
623 static enum rtx_code ix86_prepare_fp_compare_args PARAMS ((enum rtx_code,
624                                                            rtx *, rtx *));
625 static rtx gen_push PARAMS ((rtx));
626 static int memory_address_length PARAMS ((rtx addr));
627 static int ix86_flags_dependant PARAMS ((rtx, rtx, enum attr_type));
628 static int ix86_agi_dependant PARAMS ((rtx, rtx, enum attr_type));
629 static int ix86_safe_length PARAMS ((rtx));
630 static enum attr_memory ix86_safe_memory PARAMS ((rtx));
631 static enum attr_pent_pair ix86_safe_pent_pair PARAMS ((rtx));
632 static enum attr_ppro_uops ix86_safe_ppro_uops PARAMS ((rtx));
633 static void ix86_dump_ppro_packet PARAMS ((FILE *));
634 static void ix86_reorder_insn PARAMS ((rtx *, rtx *));
635 static rtx * ix86_pent_find_pair PARAMS ((rtx *, rtx *, enum attr_pent_pair,
636                                          rtx));
637 static void ix86_init_machine_status PARAMS ((struct function *));
638 static void ix86_mark_machine_status PARAMS ((struct function *));
639 static void ix86_free_machine_status PARAMS ((struct function *));
640 static int ix86_split_to_parts PARAMS ((rtx, rtx *, enum machine_mode));
641 static int ix86_safe_length_prefix PARAMS ((rtx));
642 static int ix86_nsaved_regs PARAMS((void));
643 static void ix86_emit_save_regs PARAMS((void));
644 static void ix86_emit_save_regs_using_mov PARAMS ((rtx, HOST_WIDE_INT));
645 static void ix86_emit_restore_regs_using_mov PARAMS ((rtx, int, int));
646 static void ix86_set_move_mem_attrs_1 PARAMS ((rtx, rtx, rtx, rtx, rtx));
647 static void ix86_sched_reorder_pentium PARAMS((rtx *, rtx *));
648 static void ix86_sched_reorder_ppro PARAMS((rtx *, rtx *));
649 static HOST_WIDE_INT ix86_GOT_alias_set PARAMS ((void));
650 static void ix86_adjust_counter PARAMS ((rtx, HOST_WIDE_INT));
651 static rtx ix86_expand_aligntest PARAMS ((rtx, int));
652 static void ix86_expand_strlensi_unroll_1 PARAMS ((rtx, rtx));
653 static int ix86_issue_rate PARAMS ((void));
654 static int ix86_adjust_cost PARAMS ((rtx, rtx, rtx, int));
655 static void ix86_sched_init PARAMS ((FILE *, int, int));
656 static int ix86_sched_reorder PARAMS ((FILE *, int, rtx *, int *, int));
657 static int ix86_variable_issue PARAMS ((FILE *, int, rtx, int));
658
659 struct ix86_address
660 {
661   rtx base, index, disp;
662   HOST_WIDE_INT scale;
663 };
664
665 static int ix86_decompose_address PARAMS ((rtx, struct ix86_address *));
666
667 struct builtin_description;
668 static rtx ix86_expand_sse_comi PARAMS ((const struct builtin_description *,
669                                          tree, rtx));
670 static rtx ix86_expand_sse_compare PARAMS ((const struct builtin_description *,
671                                             tree, rtx));
672 static rtx ix86_expand_unop1_builtin PARAMS ((enum insn_code, tree, rtx));
673 static rtx ix86_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx, int));
674 static rtx ix86_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
675 static rtx ix86_expand_store_builtin PARAMS ((enum insn_code, tree, int));
676 static rtx safe_vector_operand PARAMS ((rtx, enum machine_mode));
677 static enum rtx_code ix86_fp_compare_code_to_integer PARAMS ((enum rtx_code));
678 static void ix86_fp_comparison_codes PARAMS ((enum rtx_code code,
679                                               enum rtx_code *,
680                                               enum rtx_code *,
681                                               enum rtx_code *));
682 static rtx ix86_expand_fp_compare PARAMS ((enum rtx_code, rtx, rtx, rtx,
683                                           rtx *, rtx *));
684 static int ix86_fp_comparison_arithmetics_cost PARAMS ((enum rtx_code code));
685 static int ix86_fp_comparison_fcomi_cost PARAMS ((enum rtx_code code));
686 static int ix86_fp_comparison_sahf_cost PARAMS ((enum rtx_code code));
687 static int ix86_fp_comparison_cost PARAMS ((enum rtx_code code));
688 static int ix86_save_reg PARAMS ((int, int));
689 static void ix86_compute_frame_layout PARAMS ((struct ix86_frame *));
690 static int ix86_comp_type_attributes PARAMS ((tree, tree));
691 const struct attribute_spec ix86_attribute_table[];
692 static tree ix86_handle_cdecl_attribute PARAMS ((tree *, tree, tree, int, bool *));
693 static tree ix86_handle_regparm_attribute PARAMS ((tree *, tree, tree, int, bool *));
694
695 #ifdef DO_GLOBAL_CTORS_BODY
696 static void ix86_svr3_asm_out_constructor PARAMS ((rtx, int));
697 #endif
698
699 /* Register class used for passing given 64bit part of the argument.
700    These represent classes as documented by the PS ABI, with the exception
701    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
702    use SF or DFmode move instead of DImode to avoid reformating penalties.
703
704    Similary we play games with INTEGERSI_CLASS to use cheaper SImode moves
705    whenever possible (upper half does contain padding).
706  */
707 enum x86_64_reg_class
708   {
709     X86_64_NO_CLASS,
710     X86_64_INTEGER_CLASS,
711     X86_64_INTEGERSI_CLASS,
712     X86_64_SSE_CLASS,
713     X86_64_SSESF_CLASS,
714     X86_64_SSEDF_CLASS,
715     X86_64_SSEUP_CLASS,
716     X86_64_X87_CLASS,
717     X86_64_X87UP_CLASS,
718     X86_64_MEMORY_CLASS
719   };
720 const char * const x86_64_reg_class_name[] =
721    {"no", "integer", "integerSI", "sse", "sseSF", "sseDF", "sseup", "x87", "x87up", "no"};
722
723 #define MAX_CLASSES 4
724 static int classify_argument PARAMS ((enum machine_mode, tree,
725                                       enum x86_64_reg_class [MAX_CLASSES],
726                                       int));
727 static int examine_argument PARAMS ((enum machine_mode, tree, int, int *,
728                                      int *));
729 static rtx construct_container PARAMS ((enum machine_mode, tree, int, int, int,
730                                         int *, int));
731 static enum x86_64_reg_class merge_classes PARAMS ((enum x86_64_reg_class,
732                                                     enum x86_64_reg_class));
733 \f
734 /* Initialize the GCC target structure.  */
735 #undef TARGET_ATTRIBUTE_TABLE
736 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
737 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
738 #  undef TARGET_MERGE_DECL_ATTRIBUTES
739 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
740 #endif
741
742 #undef TARGET_COMP_TYPE_ATTRIBUTES
743 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
744
745 #undef TARGET_INIT_BUILTINS
746 #define TARGET_INIT_BUILTINS ix86_init_builtins
747
748 #undef TARGET_EXPAND_BUILTIN
749 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
750
751 #if defined (OSF_OS) || defined (TARGET_OSF1ELF)
752    static void ix86_osf_output_function_prologue PARAMS ((FILE *,
753                                                           HOST_WIDE_INT));
754 #  undef TARGET_ASM_FUNCTION_PROLOGUE
755 #  define TARGET_ASM_FUNCTION_PROLOGUE ix86_osf_output_function_prologue
756 #endif
757
758 #undef TARGET_ASM_OPEN_PAREN
759 #define TARGET_ASM_OPEN_PAREN ""
760 #undef TARGET_ASM_CLOSE_PAREN
761 #define TARGET_ASM_CLOSE_PAREN ""
762
763 #undef TARGET_SCHED_ADJUST_COST
764 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
765 #undef TARGET_SCHED_ISSUE_RATE
766 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
767 #undef TARGET_SCHED_VARIABLE_ISSUE
768 #define TARGET_SCHED_VARIABLE_ISSUE ix86_variable_issue
769 #undef TARGET_SCHED_INIT
770 #define TARGET_SCHED_INIT ix86_sched_init
771 #undef TARGET_SCHED_REORDER
772 #define TARGET_SCHED_REORDER ix86_sched_reorder
773
774 struct gcc_target targetm = TARGET_INITIALIZER;
775 \f
776 /* Sometimes certain combinations of command options do not make
777    sense on a particular target machine.  You can define a macro
778    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
779    defined, is executed once just after all the command options have
780    been parsed.
781
782    Don't use this macro to turn on various extra optimizations for
783    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
784
785 void
786 override_options ()
787 {
788   int i;
789   /* Comes from final.c -- no real reason to change it.  */
790 #define MAX_CODE_ALIGN 16
791
792   static struct ptt
793     {
794       const struct processor_costs *cost;       /* Processor costs */
795       const int target_enable;                  /* Target flags to enable.  */
796       const int target_disable;                 /* Target flags to disable.  */
797       const int align_loop;                     /* Default alignments.  */
798       const int align_jump;
799       const int align_func;
800       const int branch_cost;
801     }
802   const processor_target_table[PROCESSOR_max] =
803     {
804       {&i386_cost, 0, 0, 2, 2, 2, 1},
805       {&i486_cost, 0, 0, 4, 4, 4, 1},
806       {&pentium_cost, 0, 0, -4, -4, -4, 1},
807       {&pentiumpro_cost, 0, 0, 4, -4, 4, 1},
808       {&k6_cost, 0, 0, -5, -5, 4, 1},
809       {&athlon_cost, 0, 0, 4, -4, 4, 1},
810       {&pentium4_cost, 0, 0, 2, 2, 2, 1}
811     };
812
813   static struct pta
814     {
815       const char *const name;           /* processor name or nickname.  */
816       const enum processor_type processor;
817     }
818   const processor_alias_table[] =
819     {
820       {"i386", PROCESSOR_I386},
821       {"i486", PROCESSOR_I486},
822       {"i586", PROCESSOR_PENTIUM},
823       {"pentium", PROCESSOR_PENTIUM},
824       {"i686", PROCESSOR_PENTIUMPRO},
825       {"pentiumpro", PROCESSOR_PENTIUMPRO},
826       {"k6", PROCESSOR_K6},
827       {"athlon", PROCESSOR_ATHLON},
828       {"pentium4", PROCESSOR_PENTIUM4},
829     };
830
831   int const pta_size = sizeof (processor_alias_table) / sizeof (struct pta);
832
833 #ifdef SUBTARGET_OVERRIDE_OPTIONS
834   SUBTARGET_OVERRIDE_OPTIONS;
835 #endif
836
837   ix86_arch = PROCESSOR_I386;
838   ix86_cpu = (enum processor_type) TARGET_CPU_DEFAULT;
839
840   if (ix86_cmodel_string != 0)
841     {
842       if (!strcmp (ix86_cmodel_string, "small"))
843         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
844       else if (flag_pic)
845         sorry ("Code model %s not supported in PIC mode", ix86_cmodel_string);
846       else if (!strcmp (ix86_cmodel_string, "32"))
847         ix86_cmodel = CM_32;
848       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
849         ix86_cmodel = CM_KERNEL;
850       else if (!strcmp (ix86_cmodel_string, "medium") && !flag_pic)
851         ix86_cmodel = CM_MEDIUM;
852       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
853         ix86_cmodel = CM_LARGE;
854       else
855         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
856     }
857   else
858     {
859       ix86_cmodel = CM_32;
860       if (TARGET_64BIT)
861         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
862     }
863   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
864     error ("Code model `%s' not supported in the %s bit mode.",
865            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
866   if (ix86_cmodel == CM_LARGE)
867     sorry ("Code model `large' not supported yet.");
868   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
869     sorry ("%i-bit mode not compiled in.",
870            (target_flags & MASK_64BIT) ? 64 : 32);
871
872   if (ix86_arch_string != 0)
873     {
874       for (i = 0; i < pta_size; i++)
875         if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
876           {
877             ix86_arch = processor_alias_table[i].processor;
878             /* Default cpu tuning to the architecture.  */
879             ix86_cpu = ix86_arch;
880             break;
881           }
882
883       if (i == pta_size)
884         error ("bad value (%s) for -march= switch", ix86_arch_string);
885     }
886
887   if (ix86_cpu_string != 0)
888     {
889       for (i = 0; i < pta_size; i++)
890         if (! strcmp (ix86_cpu_string, processor_alias_table[i].name))
891           {
892             ix86_cpu = processor_alias_table[i].processor;
893             break;
894           }
895       if (i == pta_size)
896         error ("bad value (%s) for -mcpu= switch", ix86_cpu_string);
897     }
898
899   if (optimize_size)
900     ix86_cost = &size_cost;
901   else
902     ix86_cost = processor_target_table[ix86_cpu].cost;
903   target_flags |= processor_target_table[ix86_cpu].target_enable;
904   target_flags &= ~processor_target_table[ix86_cpu].target_disable;
905
906   /* Arrange to set up i386_stack_locals for all functions.  */
907   init_machine_status = ix86_init_machine_status;
908   mark_machine_status = ix86_mark_machine_status;
909   free_machine_status = ix86_free_machine_status;
910
911   /* Validate -mregparm= value.  */
912   if (ix86_regparm_string)
913     {
914       i = atoi (ix86_regparm_string);
915       if (i < 0 || i > REGPARM_MAX)
916         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
917       else
918         ix86_regparm = i;
919     }
920   else
921    if (TARGET_64BIT)
922      ix86_regparm = REGPARM_MAX;
923
924   /* If the user has provided any of the -malign-* options,
925      warn and use that value only if -falign-* is not set.
926      Remove this code in GCC 3.2 or later.  */
927   if (ix86_align_loops_string)
928     {
929       warning ("-malign-loops is obsolete, use -falign-loops");
930       if (align_loops == 0)
931         {
932           i = atoi (ix86_align_loops_string);
933           if (i < 0 || i > MAX_CODE_ALIGN)
934             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
935           else
936             align_loops = 1 << i;
937         }
938     }
939
940   if (ix86_align_jumps_string)
941     {
942       warning ("-malign-jumps is obsolete, use -falign-jumps");
943       if (align_jumps == 0)
944         {
945           i = atoi (ix86_align_jumps_string);
946           if (i < 0 || i > MAX_CODE_ALIGN)
947             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
948           else
949             align_jumps = 1 << i;
950         }
951     }
952
953   if (ix86_align_funcs_string)
954     {
955       warning ("-malign-functions is obsolete, use -falign-functions");
956       if (align_functions == 0)
957         {
958           i = atoi (ix86_align_funcs_string);
959           if (i < 0 || i > MAX_CODE_ALIGN)
960             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
961           else
962             align_functions = 1 << i;
963         }
964     }
965
966   /* Default align_* from the processor table.  */
967 #define abs(n) (n < 0 ? -n : n)
968   if (align_loops == 0)
969     align_loops = 1 << abs (processor_target_table[ix86_cpu].align_loop);
970   if (align_jumps == 0)
971     align_jumps = 1 << abs (processor_target_table[ix86_cpu].align_jump);
972   if (align_functions == 0)
973     align_functions = 1 << abs (processor_target_table[ix86_cpu].align_func);
974
975   /* Validate -mpreferred-stack-boundary= value, or provide default.
976      The default of 128 bits is for Pentium III's SSE __m128, but we
977      don't want additional code to keep the stack aligned when
978      optimizing for code size.  */
979   ix86_preferred_stack_boundary = (optimize_size
980                                    ? TARGET_64BIT ? 64 : 32
981                                    : 128);
982   if (ix86_preferred_stack_boundary_string)
983     {
984       i = atoi (ix86_preferred_stack_boundary_string);
985       if (i < (TARGET_64BIT ? 3 : 2) || i > 12)
986         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
987                TARGET_64BIT ? 3 : 2);
988       else
989         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
990     }
991
992   /* Validate -mbranch-cost= value, or provide default.  */
993   ix86_branch_cost = processor_target_table[ix86_cpu].branch_cost;
994   if (ix86_branch_cost_string)
995     {
996       i = atoi (ix86_branch_cost_string);
997       if (i < 0 || i > 5)
998         error ("-mbranch-cost=%d is not between 0 and 5", i);
999       else
1000         ix86_branch_cost = i;
1001     }
1002
1003   /* Keep nonleaf frame pointers.  */
1004   if (TARGET_OMIT_LEAF_FRAME_POINTER)
1005     flag_omit_frame_pointer = 1;
1006
1007   /* If we're doing fast math, we don't care about comparison order
1008      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1009   if (flag_unsafe_math_optimizations)
1010     target_flags &= ~MASK_IEEE_FP;
1011
1012   if (TARGET_64BIT)
1013     {
1014       if (TARGET_ALIGN_DOUBLE)
1015         error ("-malign-double makes no sense in the 64bit mode.");
1016       if (TARGET_RTD)
1017         error ("-mrtd calling convention not supported in the 64bit mode.");
1018       /* Enable by default the SSE and MMX builtins.  */
1019       target_flags |= MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE;
1020      }
1021
1022   /* It makes no sense to ask for just SSE builtins, so MMX is also turned
1023      on by -msse.  */
1024   if (TARGET_SSE)
1025     target_flags |= MASK_MMX;
1026
1027   /* If it has 3DNow! it also has MMX so MMX is also turned on by -m3dnow */
1028   if (TARGET_3DNOW)
1029     {
1030       target_flags |= MASK_MMX;
1031       /* If we are targetting the Athlon architecture, enable the 3Dnow/MMX
1032          extensions it adds.  */
1033       if (x86_3dnow_a & (1 << ix86_arch))
1034         target_flags |= MASK_3DNOW_A;
1035     }
1036   if ((x86_accumulate_outgoing_args & CPUMASK)
1037       && !(target_flags & MASK_NO_ACCUMULATE_OUTGOING_ARGS)
1038       && !optimize_size)
1039     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1040
1041   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1042   {
1043     char *p;
1044     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1045     p = strchr (internal_label_prefix, 'X');
1046     internal_label_prefix_len = p - internal_label_prefix;
1047     *p = '\0';
1048   }
1049 }
1050 \f
1051 void
1052 optimization_options (level, size)
1053      int level;
1054      int size ATTRIBUTE_UNUSED;
1055 {
1056   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
1057      make the problem with not enough registers even worse.  */
1058 #ifdef INSN_SCHEDULING
1059   if (level > 1)
1060     flag_schedule_insns = 0;
1061 #endif
1062   if (TARGET_64BIT && optimize >= 1)
1063     flag_omit_frame_pointer = 1;
1064   if (TARGET_64BIT)
1065     {
1066       flag_pcc_struct_return = 0;
1067       flag_asynchronous_unwind_tables = 1;
1068     }
1069 }
1070 \f
1071 /* Table of valid machine attributes.  */
1072 const struct attribute_spec ix86_attribute_table[] =
1073 {
1074   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1075   /* Stdcall attribute says callee is responsible for popping arguments
1076      if they are not variable.  */
1077   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1078   /* Cdecl attribute says the callee is a normal C declaration */
1079   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1080   /* Regparm attribute specifies how many integer arguments are to be
1081      passed in registers.  */
1082   { "regparm",   1, 1, false, true,  true,  ix86_handle_regparm_attribute },
1083 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
1084   { "dllimport", 0, 0, false, false, false, ix86_handle_dll_attribute },
1085   { "dllexport", 0, 0, false, false, false, ix86_handle_dll_attribute },
1086   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
1087 #endif
1088   { NULL,        0, 0, false, false, false, NULL }
1089 };
1090
1091 /* Handle a "cdecl" or "stdcall" attribute;
1092    arguments as in struct attribute_spec.handler.  */
1093 static tree
1094 ix86_handle_cdecl_attribute (node, name, args, flags, no_add_attrs)
1095      tree *node;
1096      tree name;
1097      tree args ATTRIBUTE_UNUSED;
1098      int flags ATTRIBUTE_UNUSED;
1099      bool *no_add_attrs;
1100 {
1101   if (TREE_CODE (*node) != FUNCTION_TYPE
1102       && TREE_CODE (*node) != METHOD_TYPE
1103       && TREE_CODE (*node) != FIELD_DECL
1104       && TREE_CODE (*node) != TYPE_DECL)
1105     {
1106       warning ("`%s' attribute only applies to functions",
1107                IDENTIFIER_POINTER (name));
1108       *no_add_attrs = true;
1109     }
1110
1111   if (TARGET_64BIT)
1112     {
1113       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1114       *no_add_attrs = true;
1115     }
1116
1117   return NULL_TREE;
1118 }
1119
1120 /* Handle a "regparm" attribute;
1121    arguments as in struct attribute_spec.handler.  */
1122 static tree
1123 ix86_handle_regparm_attribute (node, name, args, flags, no_add_attrs)
1124      tree *node;
1125      tree name;
1126      tree args;
1127      int flags ATTRIBUTE_UNUSED;
1128      bool *no_add_attrs;
1129 {
1130   if (TREE_CODE (*node) != FUNCTION_TYPE
1131       && TREE_CODE (*node) != METHOD_TYPE
1132       && TREE_CODE (*node) != FIELD_DECL
1133       && TREE_CODE (*node) != TYPE_DECL)
1134     {
1135       warning ("`%s' attribute only applies to functions",
1136                IDENTIFIER_POINTER (name));
1137       *no_add_attrs = true;
1138     }
1139   else
1140     {
1141       tree cst;
1142
1143       cst = TREE_VALUE (args);
1144       if (TREE_CODE (cst) != INTEGER_CST)
1145         {
1146           warning ("`%s' attribute requires an integer constant argument",
1147                    IDENTIFIER_POINTER (name));
1148           *no_add_attrs = true;
1149         }
1150       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
1151         {
1152           warning ("argument to `%s' attribute larger than %d",
1153                    IDENTIFIER_POINTER (name), REGPARM_MAX);
1154           *no_add_attrs = true;
1155         }
1156     }
1157
1158   return NULL_TREE;
1159 }
1160
1161 #if defined (OSF_OS) || defined (TARGET_OSF1ELF)
1162
1163 /* Generate the assembly code for function entry.  FILE is a stdio
1164    stream to output the code to.  SIZE is an int: how many units of
1165    temporary storage to allocate.
1166
1167    Refer to the array `regs_ever_live' to determine which registers to
1168    save; `regs_ever_live[I]' is nonzero if register number I is ever
1169    used in the function.  This function is responsible for knowing
1170    which registers should not be saved even if used.
1171
1172    We override it here to allow for the new profiling code to go before
1173    the prologue and the old mcount code to go after the prologue (and
1174    after %ebx has been set up for ELF shared library support).  */
1175
1176 static void
1177 ix86_osf_output_function_prologue (file, size)
1178      FILE *file;
1179      HOST_WIDE_INT size;
1180 {
1181   char *prefix = "";
1182   char *lprefix = LPREFIX;
1183   int labelno = profile_label_no;
1184
1185 #ifdef OSF_OS
1186
1187   if (TARGET_UNDERSCORES)
1188     prefix = "_";
1189
1190   if (profile_flag && OSF_PROFILE_BEFORE_PROLOGUE)
1191     {
1192       if (!flag_pic && !HALF_PIC_P ())
1193         {
1194           fprintf (file, "\tmovl $%sP%d,%%edx\n", lprefix, labelno);
1195           fprintf (file, "\tcall *%s_mcount_ptr\n", prefix);
1196         }
1197
1198       else if (HALF_PIC_P ())
1199         {
1200           rtx symref;
1201
1202           HALF_PIC_EXTERNAL ("_mcount_ptr");
1203           symref = HALF_PIC_PTR (gen_rtx_SYMBOL_REF (Pmode,
1204                                                      "_mcount_ptr"));
1205
1206           fprintf (file, "\tmovl $%sP%d,%%edx\n", lprefix, labelno);
1207           fprintf (file, "\tmovl %s%s,%%eax\n", prefix,
1208                    XSTR (symref, 0));
1209           fprintf (file, "\tcall *(%%eax)\n");
1210         }
1211
1212       else
1213         {
1214           static int call_no = 0;
1215
1216           fprintf (file, "\tcall %sPc%d\n", lprefix, call_no);
1217           fprintf (file, "%sPc%d:\tpopl %%eax\n", lprefix, call_no);
1218           fprintf (file, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-%sPc%d],%%eax\n",
1219                    lprefix, call_no++);
1220           fprintf (file, "\tleal %sP%d@GOTOFF(%%eax),%%edx\n",
1221                    lprefix, labelno);
1222           fprintf (file, "\tmovl %s_mcount_ptr@GOT(%%eax),%%eax\n",
1223                    prefix);
1224           fprintf (file, "\tcall *(%%eax)\n");
1225         }
1226     }
1227
1228 #else  /* !OSF_OS */
1229
1230   if (profile_flag && OSF_PROFILE_BEFORE_PROLOGUE)
1231     {
1232       if (!flag_pic)
1233         {
1234           fprintf (file, "\tmovl $%sP%d,%%edx\n", lprefix, labelno);
1235           fprintf (file, "\tcall *%s_mcount_ptr\n", prefix);
1236         }
1237
1238       else
1239         {
1240           static int call_no = 0;
1241
1242           fprintf (file, "\tcall %sPc%d\n", lprefix, call_no);
1243           fprintf (file, "%sPc%d:\tpopl %%eax\n", lprefix, call_no);
1244           fprintf (file, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-%sPc%d],%%eax\n",
1245                    lprefix, call_no++);
1246           fprintf (file, "\tleal %sP%d@GOTOFF(%%eax),%%edx\n",
1247                    lprefix, labelno);
1248           fprintf (file, "\tmovl %s_mcount_ptr@GOT(%%eax),%%eax\n",
1249                    prefix);
1250           fprintf (file, "\tcall *(%%eax)\n");
1251         }
1252     }
1253 #endif /* !OSF_OS */
1254
1255   function_prologue (file, size);
1256 }
1257
1258 #endif  /* OSF_OS || TARGET_OSF1ELF */
1259
1260 /* Return 0 if the attributes for two types are incompatible, 1 if they
1261    are compatible, and 2 if they are nearly compatible (which causes a
1262    warning to be generated).  */
1263
1264 static int
1265 ix86_comp_type_attributes (type1, type2)
1266      tree type1;
1267      tree type2;
1268 {
1269   /* Check for mismatch of non-default calling convention.  */
1270   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
1271
1272   if (TREE_CODE (type1) != FUNCTION_TYPE)
1273     return 1;
1274
1275   /* Check for mismatched return types (cdecl vs stdcall).  */
1276   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
1277       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
1278     return 0;
1279   return 1;
1280 }
1281 \f
1282 /* Value is the number of bytes of arguments automatically
1283    popped when returning from a subroutine call.
1284    FUNDECL is the declaration node of the function (as a tree),
1285    FUNTYPE is the data type of the function (as a tree),
1286    or for a library call it is an identifier node for the subroutine name.
1287    SIZE is the number of bytes of arguments passed on the stack.
1288
1289    On the 80386, the RTD insn may be used to pop them if the number
1290      of args is fixed, but if the number is variable then the caller
1291      must pop them all.  RTD can't be used for library calls now
1292      because the library is compiled with the Unix compiler.
1293    Use of RTD is a selectable option, since it is incompatible with
1294    standard Unix calling sequences.  If the option is not selected,
1295    the caller must always pop the args.
1296
1297    The attribute stdcall is equivalent to RTD on a per module basis.  */
1298
1299 int
1300 ix86_return_pops_args (fundecl, funtype, size)
1301      tree fundecl;
1302      tree funtype;
1303      int size;
1304 {
1305   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
1306
1307     /* Cdecl functions override -mrtd, and never pop the stack.  */
1308   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
1309
1310     /* Stdcall functions will pop the stack if not variable args.  */
1311     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype)))
1312       rtd = 1;
1313
1314     if (rtd
1315         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
1316             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
1317                 == void_type_node)))
1318       return size;
1319   }
1320
1321   /* Lose any fake structure return argument.  */
1322   if (aggregate_value_p (TREE_TYPE (funtype))
1323       && !TARGET_64BIT)
1324     return GET_MODE_SIZE (Pmode);
1325
1326     return 0;
1327 }
1328 \f
1329 /* Argument support functions.  */
1330
1331 /* Return true when register may be used to pass function parameters.  */
1332 bool
1333 ix86_function_arg_regno_p (regno)
1334      int regno;
1335 {
1336   int i;
1337   if (!TARGET_64BIT)
1338     return regno < REGPARM_MAX || (TARGET_SSE && SSE_REGNO_P (regno));
1339   if (SSE_REGNO_P (regno) && TARGET_SSE)
1340     return true;
1341   /* RAX is used as hidden argument to va_arg functions.  */
1342   if (!regno)
1343     return true;
1344   for (i = 0; i < REGPARM_MAX; i++)
1345     if (regno == x86_64_int_parameter_registers[i])
1346       return true;
1347   return false;
1348 }
1349
1350 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1351    for a call to a function whose data type is FNTYPE.
1352    For a library call, FNTYPE is 0.  */
1353
1354 void
1355 init_cumulative_args (cum, fntype, libname)
1356      CUMULATIVE_ARGS *cum;      /* Argument info to initialize */
1357      tree fntype;               /* tree ptr for function decl */
1358      rtx libname;               /* SYMBOL_REF of library name or 0 */
1359 {
1360   static CUMULATIVE_ARGS zero_cum;
1361   tree param, next_param;
1362
1363   if (TARGET_DEBUG_ARG)
1364     {
1365       fprintf (stderr, "\ninit_cumulative_args (");
1366       if (fntype)
1367         fprintf (stderr, "fntype code = %s, ret code = %s",
1368                  tree_code_name[(int) TREE_CODE (fntype)],
1369                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
1370       else
1371         fprintf (stderr, "no fntype");
1372
1373       if (libname)
1374         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
1375     }
1376
1377   *cum = zero_cum;
1378
1379   /* Set up the number of registers to use for passing arguments.  */
1380   cum->nregs = ix86_regparm;
1381   cum->sse_nregs = SSE_REGPARM_MAX;
1382   if (fntype && !TARGET_64BIT)
1383     {
1384       tree attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (fntype));
1385
1386       if (attr)
1387         cum->nregs = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1388     }
1389   cum->maybe_vaarg = false;
1390
1391   /* Determine if this function has variable arguments.  This is
1392      indicated by the last argument being 'void_type_mode' if there
1393      are no variable arguments.  If there are variable arguments, then
1394      we won't pass anything in registers */
1395
1396   if (cum->nregs)
1397     {
1398       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
1399            param != 0; param = next_param)
1400         {
1401           next_param = TREE_CHAIN (param);
1402           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
1403             {
1404               if (!TARGET_64BIT)
1405                 cum->nregs = 0;
1406               cum->maybe_vaarg = true;
1407             }
1408         }
1409     }
1410   if ((!fntype && !libname)
1411       || (fntype && !TYPE_ARG_TYPES (fntype)))
1412     cum->maybe_vaarg = 1;
1413
1414   if (TARGET_DEBUG_ARG)
1415     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
1416
1417   return;
1418 }
1419
1420 /* x86-64 register passing impleemntation.  See x86-64 ABI for details.  Goal
1421    of this code is to classify each 8bytes of incoming argument by the register
1422    class and assign registers accordingly.  */
1423
1424 /* Return the union class of CLASS1 and CLASS2.
1425    See the x86-64 PS ABI for details.  */
1426
1427 static enum x86_64_reg_class
1428 merge_classes (class1, class2)
1429      enum x86_64_reg_class class1, class2;
1430 {
1431   /* Rule #1: If both classes are equal, this is the resulting class.  */
1432   if (class1 == class2)
1433     return class1;
1434
1435   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
1436      the other class.  */
1437   if (class1 == X86_64_NO_CLASS)
1438     return class2;
1439   if (class2 == X86_64_NO_CLASS)
1440     return class1;
1441
1442   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
1443   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
1444     return X86_64_MEMORY_CLASS;
1445
1446   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
1447   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
1448       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
1449     return X86_64_INTEGERSI_CLASS;
1450   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
1451       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
1452     return X86_64_INTEGER_CLASS;
1453
1454   /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used.  */
1455   if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
1456       || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
1457     return X86_64_MEMORY_CLASS;
1458
1459   /* Rule #6: Otherwise class SSE is used.  */
1460   return X86_64_SSE_CLASS;
1461 }
1462
1463 /* Classify the argument of type TYPE and mode MODE.
1464    CLASSES will be filled by the register class used to pass each word
1465    of the operand.  The number of words is returned.  In case the parameter
1466    should be passed in memory, 0 is returned. As a special case for zero
1467    sized containers, classes[0] will be NO_CLASS and 1 is returned.
1468
1469    BIT_OFFSET is used internally for handling records and specifies offset
1470    of the offset in bits modulo 256 to avoid overflow cases.
1471
1472    See the x86-64 PS ABI for details.
1473 */
1474
1475 static int
1476 classify_argument (mode, type, classes, bit_offset)
1477      enum machine_mode mode;
1478      tree type;
1479      enum x86_64_reg_class classes[MAX_CLASSES];
1480      int bit_offset;
1481 {
1482   int bytes =
1483     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
1484   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1485
1486   if (type && AGGREGATE_TYPE_P (type))
1487     {
1488       int i;
1489       tree field;
1490       enum x86_64_reg_class subclasses[MAX_CLASSES];
1491
1492       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
1493       if (bytes > 16)
1494         return 0;
1495
1496       for (i = 0; i < words; i++)
1497         classes[i] = X86_64_NO_CLASS;
1498
1499       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
1500          signalize memory class, so handle it as special case.  */
1501       if (!words)
1502         {
1503           classes[0] = X86_64_NO_CLASS;
1504           return 1;
1505         }
1506
1507       /* Classify each field of record and merge classes.  */
1508       if (TREE_CODE (type) == RECORD_TYPE)
1509         {
1510           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1511             {
1512               if (TREE_CODE (field) == FIELD_DECL)
1513                 {
1514                   int num;
1515
1516                   /* Bitfields are always classified as integer.  Handle them
1517                      early, since later code would consider them to be
1518                      misaligned integers.  */
1519                   if (DECL_BIT_FIELD (field))
1520                     {
1521                       for (i = int_bit_position (field) / 8 / 8;
1522                            i < (int_bit_position (field)
1523                                 + tree_low_cst (DECL_SIZE (field), 0)
1524                                 + 63) / 8 / 8; i++)
1525                         classes[i] =
1526                           merge_classes (X86_64_INTEGER_CLASS,
1527                                          classes[i]);
1528                     }
1529                   else
1530                     {
1531                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
1532                                                TREE_TYPE (field), subclasses,
1533                                                (int_bit_position (field)
1534                                                 + bit_offset) % 256);
1535                       if (!num)
1536                         return 0;
1537                       for (i = 0; i < num; i++)
1538                         {
1539                           int pos =
1540                             (int_bit_position (field) + bit_offset) / 8 / 8;
1541                           classes[i + pos] =
1542                             merge_classes (subclasses[i], classes[i + pos]);
1543                         }
1544                     }
1545                 }
1546             }
1547         }
1548       /* Arrays are handled as small records.  */
1549       else if (TREE_CODE (type) == ARRAY_TYPE)
1550         {
1551           int num;
1552           num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
1553                                    TREE_TYPE (type), subclasses, bit_offset);
1554           if (!num)
1555             return 0;
1556
1557           /* The partial classes are now full classes.  */
1558           if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
1559             subclasses[0] = X86_64_SSE_CLASS;
1560           if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
1561             subclasses[0] = X86_64_INTEGER_CLASS;
1562
1563           for (i = 0; i < words; i++)
1564             classes[i] = subclasses[i % num];
1565         }
1566       /* Unions are similar to RECORD_TYPE but offset is always 0.  */
1567       else if (TREE_CODE (type) == UNION_TYPE)
1568         {
1569           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1570             {
1571               if (TREE_CODE (field) == FIELD_DECL)
1572                 {
1573                   int num;
1574                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
1575                                            TREE_TYPE (field), subclasses,
1576                                            bit_offset);
1577                   if (!num)
1578                     return 0;
1579                   for (i = 0; i < num; i++)
1580                     classes[i] = merge_classes (subclasses[i], classes[i]);
1581                 }
1582             }
1583         }
1584       else
1585         abort ();
1586
1587       /* Final merger cleanup.  */
1588       for (i = 0; i < words; i++)
1589         {
1590           /* If one class is MEMORY, everything should be passed in
1591              memory.  */
1592           if (classes[i] == X86_64_MEMORY_CLASS)
1593             return 0;
1594
1595           /* The X86_64_SSEUP_CLASS should be always preceeded by
1596              X86_64_SSE_CLASS.  */
1597           if (classes[i] == X86_64_SSEUP_CLASS
1598               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
1599             classes[i] = X86_64_SSE_CLASS;
1600
1601           /*  X86_64_X87UP_CLASS should be preceeded by X86_64_X87_CLASS.  */
1602           if (classes[i] == X86_64_X87UP_CLASS
1603               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
1604             classes[i] = X86_64_SSE_CLASS;
1605         }
1606       return words;
1607     }
1608
1609   /* Compute alignment needed.  We align all types to natural boundaries with
1610      exception of XFmode that is aligned to 64bits.  */
1611   if (mode != VOIDmode && mode != BLKmode)
1612     {
1613       int mode_alignment = GET_MODE_BITSIZE (mode);
1614
1615       if (mode == XFmode)
1616         mode_alignment = 128;
1617       else if (mode == XCmode)
1618         mode_alignment = 256;
1619       /* Missalignmed fields are always returned in memory.  */
1620       if (bit_offset % mode_alignment)
1621         return 0;
1622     }
1623
1624   /* Classification of atomic types.  */
1625   switch (mode)
1626     {
1627     case DImode:
1628     case SImode:
1629     case HImode:
1630     case QImode:
1631     case CSImode:
1632     case CHImode:
1633     case CQImode:
1634       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
1635         classes[0] = X86_64_INTEGERSI_CLASS;
1636       else
1637         classes[0] = X86_64_INTEGER_CLASS;
1638       return 1;
1639     case CDImode:
1640     case TImode:
1641       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
1642       return 2;
1643     case CTImode:
1644       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
1645       classes[2] = classes[3] = X86_64_INTEGER_CLASS;
1646       return 4;
1647     case SFmode:
1648       if (!(bit_offset % 64))
1649         classes[0] = X86_64_SSESF_CLASS;
1650       else
1651         classes[0] = X86_64_SSE_CLASS;
1652       return 1;
1653     case DFmode:
1654       classes[0] = X86_64_SSEDF_CLASS;
1655       return 1;
1656     case TFmode:
1657       classes[0] = X86_64_X87_CLASS;
1658       classes[1] = X86_64_X87UP_CLASS;
1659       return 2;
1660     case TCmode:
1661       classes[0] = X86_64_X87_CLASS;
1662       classes[1] = X86_64_X87UP_CLASS;
1663       classes[2] = X86_64_X87_CLASS;
1664       classes[3] = X86_64_X87UP_CLASS;
1665       return 4;
1666     case DCmode:
1667       classes[0] = X86_64_SSEDF_CLASS;
1668       classes[1] = X86_64_SSEDF_CLASS;
1669       return 2;
1670     case SCmode:
1671       classes[0] = X86_64_SSE_CLASS;
1672       return 1;
1673     case BLKmode:
1674       return 0;
1675     default:
1676       abort ();
1677     }
1678 }
1679
1680 /* Examine the argument and return set number of register required in each
1681    class.  Return 0 ifif parameter should be passed in memory.  */
1682 static int
1683 examine_argument (mode, type, in_return, int_nregs, sse_nregs)
1684      enum machine_mode mode;
1685      tree type;
1686      int *int_nregs, *sse_nregs;
1687      int in_return;
1688 {
1689   enum x86_64_reg_class class[MAX_CLASSES];
1690   int n = classify_argument (mode, type, class, 0);
1691
1692   *int_nregs = 0;
1693   *sse_nregs = 0;
1694   if (!n)
1695     return 0;
1696   for (n--; n >= 0; n--)
1697     switch (class[n])
1698       {
1699       case X86_64_INTEGER_CLASS:
1700       case X86_64_INTEGERSI_CLASS:
1701         (*int_nregs)++;
1702         break;
1703       case X86_64_SSE_CLASS:
1704       case X86_64_SSESF_CLASS:
1705       case X86_64_SSEDF_CLASS:
1706         (*sse_nregs)++;
1707         break;
1708       case X86_64_NO_CLASS:
1709       case X86_64_SSEUP_CLASS:
1710         break;
1711       case X86_64_X87_CLASS:
1712       case X86_64_X87UP_CLASS:
1713         if (!in_return)
1714           return 0;
1715         break;
1716       case X86_64_MEMORY_CLASS:
1717         abort ();
1718       }
1719   return 1;
1720 }
1721 /* Construct container for the argument used by GCC interface.  See
1722    FUNCTION_ARG for the detailed description.  */
1723 static rtx
1724 construct_container (mode, type, in_return, nintregs, nsseregs, intreg, sse_regno)
1725      enum machine_mode mode;
1726      tree type;
1727      int in_return;
1728      int nintregs, nsseregs;
1729      int *intreg, sse_regno;
1730 {
1731   enum machine_mode tmpmode;
1732   int bytes =
1733     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
1734   enum x86_64_reg_class class[MAX_CLASSES];
1735   int n;
1736   int i;
1737   int nexps = 0;
1738   int needed_sseregs, needed_intregs;
1739   rtx exp[MAX_CLASSES];
1740   rtx ret;
1741
1742   n = classify_argument (mode, type, class, 0);
1743   if (TARGET_DEBUG_ARG)
1744     {
1745       if (!n)
1746         fprintf (stderr, "Memory class\n");
1747       else
1748         {
1749           fprintf (stderr, "Classes:");
1750           for (i = 0; i < n; i++)
1751             {
1752               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
1753             }
1754            fprintf (stderr, "\n");
1755         }
1756     }
1757   if (!n)
1758     return NULL;
1759   if (!examine_argument (mode, type, in_return, &needed_intregs, &needed_sseregs))
1760     return NULL;
1761   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
1762     return NULL;
1763
1764   /* First construct simple cases.  Avoid SCmode, since we want to use
1765      single register to pass this type.  */
1766   if (n == 1 && mode != SCmode)
1767     switch (class[0])
1768       {
1769       case X86_64_INTEGER_CLASS:
1770       case X86_64_INTEGERSI_CLASS:
1771         return gen_rtx_REG (mode, intreg[0]);
1772       case X86_64_SSE_CLASS:
1773       case X86_64_SSESF_CLASS:
1774       case X86_64_SSEDF_CLASS:
1775         return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
1776       case X86_64_X87_CLASS:
1777         return gen_rtx_REG (mode, FIRST_STACK_REG);
1778       case X86_64_NO_CLASS:
1779         /* Zero sized array, struct or class.  */
1780         return NULL;
1781       default:
1782         abort ();
1783       }
1784   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS)
1785     return gen_rtx_REG (TImode, SSE_REGNO (sse_regno));
1786   if (n == 2
1787       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
1788     return gen_rtx_REG (TFmode, FIRST_STACK_REG);
1789   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
1790       && class[1] == X86_64_INTEGER_CLASS
1791       && (mode == CDImode || mode == TImode)
1792       && intreg[0] + 1 == intreg[1])
1793     return gen_rtx_REG (mode, intreg[0]);
1794   if (n == 4
1795       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS
1796       && class[2] == X86_64_X87_CLASS && class[3] == X86_64_X87UP_CLASS)
1797     return gen_rtx_REG (TCmode, FIRST_STACK_REG);
1798
1799   /* Otherwise figure out the entries of the PARALLEL.  */
1800   for (i = 0; i < n; i++)
1801     {
1802       switch (class[i])
1803         {
1804           case X86_64_NO_CLASS:
1805             break;
1806           case X86_64_INTEGER_CLASS:
1807           case X86_64_INTEGERSI_CLASS:
1808             /* Merge TImodes on aligned occassions here too.  */
1809             if (i * 8 + 8 > bytes)
1810               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
1811             else if (class[i] == X86_64_INTEGERSI_CLASS)
1812               tmpmode = SImode;
1813             else
1814               tmpmode = DImode;
1815             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
1816             if (tmpmode == BLKmode)
1817               tmpmode = DImode;
1818             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
1819                                                gen_rtx_REG (tmpmode, *intreg),
1820                                                GEN_INT (i*8));
1821             intreg++;
1822             break;
1823           case X86_64_SSESF_CLASS:
1824             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
1825                                                gen_rtx_REG (SFmode,
1826                                                             SSE_REGNO (sse_regno)),
1827                                                GEN_INT (i*8));
1828             sse_regno++;
1829             break;
1830           case X86_64_SSEDF_CLASS:
1831             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
1832                                                gen_rtx_REG (DFmode,
1833                                                             SSE_REGNO (sse_regno)),
1834                                                GEN_INT (i*8));
1835             sse_regno++;
1836             break;
1837           case X86_64_SSE_CLASS:
1838             if (i < n && class[i + 1] == X86_64_SSEUP_CLASS)
1839               tmpmode = TImode, i++;
1840             else
1841               tmpmode = DImode;
1842             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
1843                                                gen_rtx_REG (tmpmode,
1844                                                             SSE_REGNO (sse_regno)),
1845                                                GEN_INT (i*8));
1846             sse_regno++;
1847             break;
1848           default:
1849             abort ();
1850         }
1851     }
1852   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
1853   for (i = 0; i < nexps; i++)
1854     XVECEXP (ret, 0, i) = exp [i];
1855   return ret;
1856 }
1857
1858 /* Update the data in CUM to advance over an argument
1859    of mode MODE and data type TYPE.
1860    (TYPE is null for libcalls where that information may not be available.)  */
1861
1862 void
1863 function_arg_advance (cum, mode, type, named)
1864      CUMULATIVE_ARGS *cum;      /* current arg information */
1865      enum machine_mode mode;    /* current arg mode */
1866      tree type;                 /* type of the argument or 0 if lib support */
1867      int named;                 /* whether or not the argument was named */
1868 {
1869   int bytes =
1870     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
1871   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1872
1873   if (TARGET_DEBUG_ARG)
1874     fprintf (stderr,
1875              "function_adv (sz=%d, wds=%2d, nregs=%d, mode=%s, named=%d)\n\n",
1876              words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
1877   if (TARGET_64BIT)
1878     {
1879       int int_nregs, sse_nregs;
1880       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
1881         cum->words += words;
1882       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
1883         {
1884           cum->nregs -= int_nregs;
1885           cum->sse_nregs -= sse_nregs;
1886           cum->regno += int_nregs;
1887           cum->sse_regno += sse_nregs;
1888         }
1889       else
1890         cum->words += words;
1891     }
1892   else
1893     {
1894       if (TARGET_SSE && mode == TImode)
1895         {
1896           cum->sse_words += words;
1897           cum->sse_nregs -= 1;
1898           cum->sse_regno += 1;
1899           if (cum->sse_nregs <= 0)
1900             {
1901               cum->sse_nregs = 0;
1902               cum->sse_regno = 0;
1903             }
1904         }
1905       else
1906         {
1907           cum->words += words;
1908           cum->nregs -= words;
1909           cum->regno += words;
1910
1911           if (cum->nregs <= 0)
1912             {
1913               cum->nregs = 0;
1914               cum->regno = 0;
1915             }
1916         }
1917     }
1918   return;
1919 }
1920
1921 /* Define where to put the arguments to a function.
1922    Value is zero to push the argument on the stack,
1923    or a hard register in which to store the argument.
1924
1925    MODE is the argument's machine mode.
1926    TYPE is the data type of the argument (as a tree).
1927     This is null for libcalls where that information may
1928     not be available.
1929    CUM is a variable of type CUMULATIVE_ARGS which gives info about
1930     the preceding args and about the function being called.
1931    NAMED is nonzero if this argument is a named parameter
1932     (otherwise it is an extra parameter matching an ellipsis).  */
1933
1934 struct rtx_def *
1935 function_arg (cum, mode, type, named)
1936      CUMULATIVE_ARGS *cum;      /* current arg information */
1937      enum machine_mode mode;    /* current arg mode */
1938      tree type;                 /* type of the argument or 0 if lib support */
1939      int named;                 /* != 0 for normal args, == 0 for ... args */
1940 {
1941   rtx ret   = NULL_RTX;
1942   int bytes =
1943     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
1944   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1945
1946   /* Handle an hidden AL argument containing number of registers for varargs
1947      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
1948      any AL settings.  */
1949   if (mode == VOIDmode)
1950     {
1951       if (TARGET_64BIT)
1952         return GEN_INT (cum->maybe_vaarg
1953                         ? (cum->sse_nregs < 0
1954                            ? SSE_REGPARM_MAX
1955                            : cum->sse_regno)
1956                         : -1);
1957       else
1958         return constm1_rtx;
1959     }
1960   if (TARGET_64BIT)
1961     ret = construct_container (mode, type, 0, cum->nregs, cum->sse_nregs,
1962                                &x86_64_int_parameter_registers [cum->regno],
1963                                cum->sse_regno);
1964   else
1965     switch (mode)
1966       {
1967         /* For now, pass fp/complex values on the stack.  */
1968       default:
1969         break;
1970
1971       case BLKmode:
1972       case DImode:
1973       case SImode:
1974       case HImode:
1975       case QImode:
1976         if (words <= cum->nregs)
1977           ret = gen_rtx_REG (mode, cum->regno);
1978         break;
1979       case TImode:
1980         if (cum->sse_nregs)
1981           ret = gen_rtx_REG (mode, cum->sse_regno);
1982         break;
1983       }
1984
1985   if (TARGET_DEBUG_ARG)
1986     {
1987       fprintf (stderr,
1988                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d",
1989                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
1990
1991       if (ret)
1992         fprintf (stderr, ", reg=%%e%s", reg_names[ REGNO(ret) ]);
1993       else
1994         fprintf (stderr, ", stack");
1995
1996       fprintf (stderr, " )\n");
1997     }
1998
1999   return ret;
2000 }
2001
2002 /* Gives the alignment boundary, in bits, of an argument with the specified mode
2003    and type.   */
2004
2005 int
2006 ix86_function_arg_boundary (mode, type)
2007      enum machine_mode mode;
2008      tree type;
2009 {
2010   int align;
2011   if (!TARGET_64BIT)
2012     return PARM_BOUNDARY;
2013   if (type)
2014     align = TYPE_ALIGN (type);
2015   else
2016     align = GET_MODE_ALIGNMENT (mode);
2017   if (align < PARM_BOUNDARY)
2018     align = PARM_BOUNDARY;
2019   if (align > 128)
2020     align = 128;
2021   return align;
2022 }
2023
2024 /* Return true if N is a possible register number of function value.  */
2025 bool
2026 ix86_function_value_regno_p (regno)
2027      int regno;
2028 {
2029   if (!TARGET_64BIT)
2030     {
2031       return ((regno) == 0
2032               || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
2033               || ((regno) == FIRST_SSE_REG && TARGET_SSE));
2034     }
2035   return ((regno) == 0 || (regno) == FIRST_FLOAT_REG
2036           || ((regno) == FIRST_SSE_REG && TARGET_SSE)
2037           || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387));
2038 }
2039
2040 /* Define how to find the value returned by a function.
2041    VALTYPE is the data type of the value (as a tree).
2042    If the precise function being called is known, FUNC is its FUNCTION_DECL;
2043    otherwise, FUNC is 0.  */
2044 rtx
2045 ix86_function_value (valtype)
2046      tree valtype;
2047 {
2048   if (TARGET_64BIT)
2049     {
2050       rtx ret = construct_container (TYPE_MODE (valtype), valtype, 1,
2051                                      REGPARM_MAX, SSE_REGPARM_MAX,
2052                                      x86_64_int_return_registers, 0);
2053       /* For zero sized structures, construct_continer return NULL, but we need
2054          to keep rest of compiler happy by returning meaningfull value.  */
2055       if (!ret)
2056         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
2057       return ret;
2058     }
2059   else
2060     return gen_rtx_REG (TYPE_MODE (valtype), VALUE_REGNO (TYPE_MODE (valtype)));
2061 }
2062
2063 /* Return false ifif type is returned in memory.  */
2064 int
2065 ix86_return_in_memory (type)
2066      tree type;
2067 {
2068   int needed_intregs, needed_sseregs;
2069   if (TARGET_64BIT)
2070     {
2071       return !examine_argument (TYPE_MODE (type), type, 1,
2072                                 &needed_intregs, &needed_sseregs);
2073     }
2074   else
2075     {
2076       if (TYPE_MODE (type) == BLKmode
2077           || (VECTOR_MODE_P (TYPE_MODE (type))
2078               && int_size_in_bytes (type) == 8)
2079           || (int_size_in_bytes (type) > 12 && TYPE_MODE (type) != TImode
2080               && TYPE_MODE (type) != TFmode
2081               && !VECTOR_MODE_P (TYPE_MODE (type))))
2082         return 1;
2083       return 0;
2084     }
2085 }
2086
2087 /* Define how to find the value returned by a library function
2088    assuming the value has mode MODE.  */
2089 rtx
2090 ix86_libcall_value (mode)
2091    enum machine_mode mode;
2092 {
2093   if (TARGET_64BIT)
2094     {
2095       switch (mode)
2096         {
2097           case SFmode:
2098           case SCmode:
2099           case DFmode:
2100           case DCmode:
2101             return gen_rtx_REG (mode, FIRST_SSE_REG);
2102           case TFmode:
2103           case TCmode:
2104             return gen_rtx_REG (mode, FIRST_FLOAT_REG);
2105           default:
2106             return gen_rtx_REG (mode, 0);
2107         }
2108     }
2109   else
2110    return gen_rtx_REG (mode, VALUE_REGNO (mode));
2111 }
2112 \f
2113 /* Create the va_list data type.  */
2114
2115 tree
2116 ix86_build_va_list ()
2117 {
2118   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
2119
2120   /* For i386 we use plain pointer to argument area.  */
2121   if (!TARGET_64BIT)
2122     return build_pointer_type (char_type_node);
2123
2124   record = make_lang_type (RECORD_TYPE);
2125   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2126
2127   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"), 
2128                       unsigned_type_node);
2129   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"), 
2130                       unsigned_type_node);
2131   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
2132                       ptr_type_node);
2133   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
2134                       ptr_type_node);
2135
2136   DECL_FIELD_CONTEXT (f_gpr) = record;
2137   DECL_FIELD_CONTEXT (f_fpr) = record;
2138   DECL_FIELD_CONTEXT (f_ovf) = record;
2139   DECL_FIELD_CONTEXT (f_sav) = record;
2140
2141   TREE_CHAIN (record) = type_decl;
2142   TYPE_NAME (record) = type_decl;
2143   TYPE_FIELDS (record) = f_gpr;
2144   TREE_CHAIN (f_gpr) = f_fpr;
2145   TREE_CHAIN (f_fpr) = f_ovf;
2146   TREE_CHAIN (f_ovf) = f_sav;
2147
2148   layout_type (record);
2149
2150   /* The correct type is an array type of one element.  */
2151   return build_array_type (record, build_index_type (size_zero_node));
2152 }
2153
2154 /* Perform any needed actions needed for a function that is receiving a
2155    variable number of arguments. 
2156
2157    CUM is as above.
2158
2159    MODE and TYPE are the mode and type of the current parameter.
2160
2161    PRETEND_SIZE is a variable that should be set to the amount of stack
2162    that must be pushed by the prolog to pretend that our caller pushed
2163    it.
2164
2165    Normally, this macro will push all remaining incoming registers on the
2166    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2167
2168 void
2169 ix86_setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2170      CUMULATIVE_ARGS *cum;
2171      enum machine_mode mode;
2172      tree type;
2173      int *pretend_size ATTRIBUTE_UNUSED;
2174      int no_rtl;
2175
2176 {
2177   CUMULATIVE_ARGS next_cum;
2178   rtx save_area = NULL_RTX, mem;
2179   rtx label;
2180   rtx label_ref;
2181   rtx tmp_reg;
2182   rtx nsse_reg;
2183   int set;
2184   tree fntype;
2185   int stdarg_p;
2186   int i;
2187
2188   if (!TARGET_64BIT)
2189     return;
2190
2191   /* Indicate to allocate space on the stack for varargs save area.  */
2192   ix86_save_varrargs_registers = 1;
2193
2194   fntype = TREE_TYPE (current_function_decl);
2195   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
2196               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2197                   != void_type_node));
2198
2199   /* For varargs, we do not want to skip the dummy va_dcl argument.
2200      For stdargs, we do want to skip the last named argument.  */
2201   next_cum = *cum;
2202   if (stdarg_p)
2203     function_arg_advance (&next_cum, mode, type, 1);
2204
2205   if (!no_rtl)
2206     save_area = frame_pointer_rtx;
2207
2208   set = get_varargs_alias_set ();
2209
2210   for (i = next_cum.regno; i < ix86_regparm; i++)
2211     {
2212       mem = gen_rtx_MEM (Pmode,
2213                          plus_constant (save_area, i * UNITS_PER_WORD));
2214       set_mem_alias_set (mem, set);
2215       emit_move_insn (mem, gen_rtx_REG (Pmode,
2216                                         x86_64_int_parameter_registers[i]));
2217     }
2218
2219   if (next_cum.sse_nregs)
2220     {
2221       /* Now emit code to save SSE registers.  The AX parameter contains number
2222          of SSE parameter regsiters used to call this function.  We use
2223          sse_prologue_save insn template that produces computed jump across
2224          SSE saves.  We need some preparation work to get this working.  */
2225
2226       label = gen_label_rtx ();
2227       label_ref = gen_rtx_LABEL_REF (Pmode, label);
2228
2229       /* Compute address to jump to :
2230          label - 5*eax + nnamed_sse_arguments*5  */
2231       tmp_reg = gen_reg_rtx (Pmode);
2232       nsse_reg = gen_reg_rtx (Pmode);
2233       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
2234       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
2235                               gen_rtx_MULT (Pmode, nsse_reg,
2236                                             GEN_INT (4))));
2237       if (next_cum.sse_regno)
2238         emit_move_insn
2239           (nsse_reg,
2240            gen_rtx_CONST (DImode,
2241                           gen_rtx_PLUS (DImode,
2242                                         label_ref,
2243                                         GEN_INT (next_cum.sse_regno * 4))));
2244       else
2245         emit_move_insn (nsse_reg, label_ref);
2246       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
2247
2248       /* Compute address of memory block we save into.  We always use pointer
2249          pointing 127 bytes after first byte to store - this is needed to keep
2250          instruction size limited by 4 bytes.  */
2251       tmp_reg = gen_reg_rtx (Pmode);
2252       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
2253                               plus_constant (save_area,
2254                                              8 * REGPARM_MAX + 127)));
2255       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
2256       set_mem_alias_set (mem, set);
2257       set_mem_align (mem, BITS_PER_WORD);
2258
2259       /* And finally do the dirty job!  */
2260       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
2261                                         GEN_INT (next_cum.sse_regno), label));
2262     }
2263
2264 }
2265
2266 /* Implement va_start.  */
2267
2268 void
2269 ix86_va_start (stdarg_p, valist, nextarg)
2270      int stdarg_p;
2271      tree valist;
2272      rtx nextarg;
2273 {
2274   HOST_WIDE_INT words, n_gpr, n_fpr;
2275   tree f_gpr, f_fpr, f_ovf, f_sav;
2276   tree gpr, fpr, ovf, sav, t;
2277
2278   /* Only 64bit target needs something special.  */
2279   if (!TARGET_64BIT)
2280     {
2281       std_expand_builtin_va_start (stdarg_p, valist, nextarg);
2282       return;
2283     }
2284
2285   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2286   f_fpr = TREE_CHAIN (f_gpr);
2287   f_ovf = TREE_CHAIN (f_fpr);
2288   f_sav = TREE_CHAIN (f_ovf);
2289
2290   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2291   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2292   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2293   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2294   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2295
2296   /* Count number of gp and fp argument registers used.  */
2297   words = current_function_args_info.words;
2298   n_gpr = current_function_args_info.regno;
2299   n_fpr = current_function_args_info.sse_regno;
2300
2301   if (TARGET_DEBUG_ARG)
2302     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
2303              (int)words, (int)n_gpr, (int)n_fpr);
2304
2305   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
2306              build_int_2 (n_gpr * 8, 0));
2307   TREE_SIDE_EFFECTS (t) = 1;
2308   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2309
2310   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
2311              build_int_2 (n_fpr * 16 + 8*REGPARM_MAX, 0));
2312   TREE_SIDE_EFFECTS (t) = 1;
2313   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2314
2315   /* Find the overflow area.  */
2316   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
2317   if (words != 0)
2318     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
2319                build_int_2 (words * UNITS_PER_WORD, 0));
2320   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2321   TREE_SIDE_EFFECTS (t) = 1;
2322   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2323
2324   /* Find the register save area.
2325      Prologue of the function save it right above stack frame.  */
2326   t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
2327   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
2328   TREE_SIDE_EFFECTS (t) = 1;
2329   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2330 }
2331
2332 /* Implement va_arg.  */
2333 rtx
2334 ix86_va_arg (valist, type)
2335      tree valist, type;
2336 {
2337   static int intreg[6] = { 0, 1, 2, 3, 4, 5 };
2338   tree f_gpr, f_fpr, f_ovf, f_sav;
2339   tree gpr, fpr, ovf, sav, t;
2340   int size, rsize;
2341   rtx lab_false, lab_over = NULL_RTX;
2342   rtx addr_rtx, r;
2343   rtx container;
2344
2345   /* Only 64bit target needs something special.  */
2346   if (!TARGET_64BIT)
2347     {
2348       return std_expand_builtin_va_arg (valist, type);
2349     }
2350
2351   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2352   f_fpr = TREE_CHAIN (f_gpr);
2353   f_ovf = TREE_CHAIN (f_fpr);
2354   f_sav = TREE_CHAIN (f_ovf);
2355
2356   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2357   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2358   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2359   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2360   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2361
2362   size = int_size_in_bytes (type);
2363   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2364
2365   container = construct_container (TYPE_MODE (type), type, 0,
2366                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
2367   /*
2368    * Pull the value out of the saved registers ...
2369    */
2370
2371   addr_rtx = gen_reg_rtx (Pmode);
2372
2373   if (container)
2374     {
2375       rtx int_addr_rtx, sse_addr_rtx;
2376       int needed_intregs, needed_sseregs;
2377       int need_temp;
2378
2379       lab_over = gen_label_rtx ();
2380       lab_false = gen_label_rtx ();
2381
2382       examine_argument (TYPE_MODE (type), type, 0,
2383                         &needed_intregs, &needed_sseregs);
2384
2385
2386       need_temp = ((needed_intregs && TYPE_ALIGN (type) > 64)
2387                    || TYPE_ALIGN (type) > 128);
2388
2389       /* In case we are passing structure, verify that it is consetuctive block
2390          on the register save area.  If not we need to do moves.  */
2391       if (!need_temp && !REG_P (container))
2392         {
2393           /* Verify that all registers are strictly consetuctive  */
2394           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
2395             {
2396               int i;
2397
2398               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
2399                 {
2400                   rtx slot = XVECEXP (container, 0, i);
2401                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int)i
2402                       || INTVAL (XEXP (slot, 1)) != i * 16)
2403                     need_temp = 1;
2404                 }
2405             }
2406           else
2407             {
2408               int i;
2409
2410               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
2411                 {
2412                   rtx slot = XVECEXP (container, 0, i);
2413                   if (REGNO (XEXP (slot, 0)) != (unsigned int)i
2414                       || INTVAL (XEXP (slot, 1)) != i * 8)
2415                     need_temp = 1;
2416                 }
2417             }
2418         }
2419       if (!need_temp)
2420         {
2421           int_addr_rtx = addr_rtx;
2422           sse_addr_rtx = addr_rtx;
2423         }
2424       else
2425         {
2426           int_addr_rtx = gen_reg_rtx (Pmode);
2427           sse_addr_rtx = gen_reg_rtx (Pmode);
2428         }
2429       /* First ensure that we fit completely in registers.  */
2430       if (needed_intregs)
2431         {
2432           emit_cmp_and_jump_insns (expand_expr
2433                                    (gpr, NULL_RTX, SImode, EXPAND_NORMAL),
2434                                    GEN_INT ((REGPARM_MAX - needed_intregs +
2435                                              1) * 8), GE, const1_rtx, SImode,
2436                                    1, 1, lab_false);
2437         }
2438       if (needed_sseregs)
2439         {
2440           emit_cmp_and_jump_insns (expand_expr
2441                                    (fpr, NULL_RTX, SImode, EXPAND_NORMAL),
2442                                    GEN_INT ((SSE_REGPARM_MAX -
2443                                              needed_sseregs + 1) * 16 +
2444                                             REGPARM_MAX * 8), GE, const1_rtx,
2445                                    SImode, 1, 1, lab_false);
2446         }
2447
2448       /* Compute index to start of area used for integer regs.  */
2449       if (needed_intregs)
2450         {
2451           t = build (PLUS_EXPR, ptr_type_node, sav, gpr);
2452           r = expand_expr (t, int_addr_rtx, Pmode, EXPAND_NORMAL);
2453           if (r != int_addr_rtx)
2454             emit_move_insn (int_addr_rtx, r);
2455         }
2456       if (needed_sseregs)
2457         {
2458           t = build (PLUS_EXPR, ptr_type_node, sav, fpr);
2459           r = expand_expr (t, sse_addr_rtx, Pmode, EXPAND_NORMAL);
2460           if (r != sse_addr_rtx)
2461             emit_move_insn (sse_addr_rtx, r);
2462         }
2463       if (need_temp)
2464         {
2465           int i;
2466           rtx mem;
2467
2468           /* Never use the memory itself, as it has the alias set.  */
2469           addr_rtx = XEXP (assign_temp (type, 0, 1, 0), 0);
2470           mem = gen_rtx_MEM (BLKmode, addr_rtx);
2471           set_mem_alias_set (mem, get_varargs_alias_set ());
2472           set_mem_align (mem, BITS_PER_UNIT);
2473
2474           for (i = 0; i < XVECLEN (container, 0); i++)
2475             {
2476               rtx slot = XVECEXP (container, 0, i);
2477               rtx reg = XEXP (slot, 0);
2478               enum machine_mode mode = GET_MODE (reg);
2479               rtx src_addr;
2480               rtx src_mem;
2481               int src_offset;
2482               rtx dest_mem;
2483
2484               if (SSE_REGNO_P (REGNO (reg)))
2485                 {
2486                   src_addr = sse_addr_rtx;
2487                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
2488                 }
2489               else
2490                 {
2491                   src_addr = int_addr_rtx;
2492                   src_offset = REGNO (reg) * 8;
2493                 }
2494               src_mem = gen_rtx_MEM (mode, src_addr);
2495               set_mem_alias_set (src_mem, get_varargs_alias_set ());
2496               src_mem = adjust_address (src_mem, mode, src_offset);
2497               dest_mem = adjust_address (mem, mode, INTVAL (XEXP (slot, 1)));
2498               emit_move_insn (dest_mem, src_mem);
2499             }
2500         }
2501
2502       if (needed_intregs)
2503         {
2504           t =
2505             build (PLUS_EXPR, TREE_TYPE (gpr), gpr,
2506                    build_int_2 (needed_intregs * 8, 0));
2507           t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
2508           TREE_SIDE_EFFECTS (t) = 1;
2509           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2510         }
2511       if (needed_sseregs)
2512         {
2513           t =
2514             build (PLUS_EXPR, TREE_TYPE (fpr), fpr,
2515                    build_int_2 (needed_sseregs * 16, 0));
2516           t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
2517           TREE_SIDE_EFFECTS (t) = 1;
2518           expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2519         }
2520
2521       emit_jump_insn (gen_jump (lab_over));
2522       emit_barrier ();
2523       emit_label (lab_false);
2524     }
2525
2526   /* ... otherwise out of the overflow area.  */
2527
2528   /* Care for on-stack alignment if needed.  */
2529   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64)
2530     t = ovf;
2531   else
2532     {
2533       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
2534       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align - 1, 0));
2535       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align, -1));
2536     }
2537   t = save_expr (t);
2538
2539   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
2540   if (r != addr_rtx)
2541     emit_move_insn (addr_rtx, r);
2542
2543   t =
2544     build (PLUS_EXPR, TREE_TYPE (t), t,
2545            build_int_2 (rsize * UNITS_PER_WORD, 0));
2546   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2547   TREE_SIDE_EFFECTS (t) = 1;
2548   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2549
2550   if (container)
2551     emit_label (lab_over);
2552
2553   return addr_rtx;
2554 }
2555 \f
2556 /* Return nonzero if OP is general operand representable on x86_64.  */
2557
2558 int
2559 x86_64_general_operand (op, mode)
2560      rtx op;
2561      enum machine_mode mode;
2562 {
2563   if (!TARGET_64BIT)
2564     return general_operand (op, mode);
2565   if (nonimmediate_operand (op, mode))
2566     return 1;
2567   return x86_64_sign_extended_value (op);
2568 }
2569
2570 /* Return nonzero if OP is general operand representable on x86_64
2571    as eighter sign extended or zero extended constant.  */
2572
2573 int
2574 x86_64_szext_general_operand (op, mode)
2575      rtx op;
2576      enum machine_mode mode;
2577 {
2578   if (!TARGET_64BIT)
2579     return general_operand (op, mode);
2580   if (nonimmediate_operand (op, mode))
2581     return 1;
2582   return x86_64_sign_extended_value (op) || x86_64_zero_extended_value (op);
2583 }
2584
2585 /* Return nonzero if OP is nonmemory operand representable on x86_64.  */
2586
2587 int
2588 x86_64_nonmemory_operand (op, mode)
2589      rtx op;
2590      enum machine_mode mode;
2591 {
2592   if (!TARGET_64BIT)
2593     return nonmemory_operand (op, mode);
2594   if (register_operand (op, mode))
2595     return 1;
2596   return x86_64_sign_extended_value (op);
2597 }
2598
2599 /* Return nonzero if OP is nonmemory operand acceptable by movabs patterns.  */
2600
2601 int
2602 x86_64_movabs_operand (op, mode)
2603      rtx op;
2604      enum machine_mode mode;
2605 {
2606   if (!TARGET_64BIT || !flag_pic)
2607     return nonmemory_operand (op, mode);
2608   if (register_operand (op, mode) || x86_64_sign_extended_value (op))
2609     return 1;
2610   if (CONSTANT_P (op) && !symbolic_reference_mentioned_p (op))
2611     return 1;
2612   return 0;
2613 }
2614
2615 /* Return nonzero if OP is nonmemory operand representable on x86_64.  */
2616
2617 int
2618 x86_64_szext_nonmemory_operand (op, mode)
2619      rtx op;
2620      enum machine_mode mode;
2621 {
2622   if (!TARGET_64BIT)
2623     return nonmemory_operand (op, mode);
2624   if (register_operand (op, mode))
2625     return 1;
2626   return x86_64_sign_extended_value (op) || x86_64_zero_extended_value (op);
2627 }
2628
2629 /* Return nonzero if OP is immediate operand representable on x86_64.  */
2630
2631 int
2632 x86_64_immediate_operand (op, mode)
2633      rtx op;
2634      enum machine_mode mode;
2635 {
2636   if (!TARGET_64BIT)
2637     return immediate_operand (op, mode);
2638   return x86_64_sign_extended_value (op);
2639 }
2640
2641 /* Return nonzero if OP is immediate operand representable on x86_64.  */
2642
2643 int
2644 x86_64_zext_immediate_operand (op, mode)
2645      rtx op;
2646      enum machine_mode mode ATTRIBUTE_UNUSED;
2647 {
2648   return x86_64_zero_extended_value (op);
2649 }
2650
2651 /* Return nonzero if OP is (const_int 1), else return zero.  */
2652
2653 int
2654 const_int_1_operand (op, mode)
2655      rtx op;
2656      enum machine_mode mode ATTRIBUTE_UNUSED;
2657 {
2658   return (GET_CODE (op) == CONST_INT && INTVAL (op) == 1);
2659 }
2660
2661 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
2662    reference and a constant.  */
2663
2664 int
2665 symbolic_operand (op, mode)
2666      register rtx op;
2667      enum machine_mode mode ATTRIBUTE_UNUSED;
2668 {
2669   switch (GET_CODE (op))
2670     {
2671     case SYMBOL_REF:
2672     case LABEL_REF:
2673       return 1;
2674
2675     case CONST:
2676       op = XEXP (op, 0);
2677       if (GET_CODE (op) == SYMBOL_REF
2678           || GET_CODE (op) == LABEL_REF
2679           || (GET_CODE (op) == UNSPEC
2680               && (XINT (op, 1) == 6
2681                   || XINT (op, 1) == 7
2682                   || XINT (op, 1) == 15)))
2683         return 1;
2684       if (GET_CODE (op) != PLUS
2685           || GET_CODE (XEXP (op, 1)) != CONST_INT)
2686         return 0;
2687
2688       op = XEXP (op, 0);
2689       if (GET_CODE (op) == SYMBOL_REF
2690           || GET_CODE (op) == LABEL_REF)
2691         return 1;
2692       /* Only @GOTOFF gets offsets.  */
2693       if (GET_CODE (op) != UNSPEC
2694           || XINT (op, 1) != 7)
2695         return 0;
2696
2697       op = XVECEXP (op, 0, 0);
2698       if (GET_CODE (op) == SYMBOL_REF
2699           || GET_CODE (op) == LABEL_REF)
2700         return 1;
2701       return 0;
2702
2703     default:
2704       return 0;
2705     }
2706 }
2707
2708 /* Return true if the operand contains a @GOT or @GOTOFF reference.  */
2709
2710 int
2711 pic_symbolic_operand (op, mode)
2712      register rtx op;
2713      enum machine_mode mode ATTRIBUTE_UNUSED;
2714 {
2715   if (GET_CODE (op) != CONST)
2716     return 0;
2717   op = XEXP (op, 0);
2718   if (TARGET_64BIT)
2719     {
2720       if (GET_CODE (XEXP (op, 0)) == UNSPEC)
2721         return 1;
2722     }
2723   else 
2724     {
2725       if (GET_CODE (op) == UNSPEC)
2726         return 1;
2727       if (GET_CODE (op) != PLUS
2728           || GET_CODE (XEXP (op, 1)) != CONST_INT)
2729         return 0;
2730       op = XEXP (op, 0);
2731       if (GET_CODE (op) == UNSPEC)
2732         return 1;
2733     }
2734   return 0;
2735 }
2736
2737 /* Return true if OP is a symbolic operand that resolves locally.  */
2738
2739 static int
2740 local_symbolic_operand (op, mode)
2741      rtx op;
2742      enum machine_mode mode ATTRIBUTE_UNUSED;
2743 {
2744   if (GET_CODE (op) == LABEL_REF)
2745     return 1;
2746
2747   if (GET_CODE (op) == CONST
2748       && GET_CODE (XEXP (op, 0)) == PLUS
2749       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
2750     op = XEXP (XEXP (op, 0), 0);
2751
2752   if (GET_CODE (op) != SYMBOL_REF)
2753     return 0;
2754
2755   /* These we've been told are local by varasm and encode_section_info
2756      respectively.  */
2757   if (CONSTANT_POOL_ADDRESS_P (op) || SYMBOL_REF_FLAG (op))
2758     return 1;
2759
2760   /* There is, however, a not insubstantial body of code in the rest of
2761      the compiler that assumes it can just stick the results of 
2762      ASM_GENERATE_INTERNAL_LABEL in a symbol_ref and have done.  */
2763   /* ??? This is a hack.  Should update the body of the compiler to
2764      always create a DECL an invoke ENCODE_SECTION_INFO.  */
2765   if (strncmp (XSTR (op, 0), internal_label_prefix,
2766                internal_label_prefix_len) == 0)
2767     return 1;
2768
2769   return 0;
2770 }
2771
2772 /* Test for a valid operand for a call instruction.  Don't allow the
2773    arg pointer register or virtual regs since they may decay into
2774    reg + const, which the patterns can't handle.  */
2775
2776 int
2777 call_insn_operand (op, mode)
2778      rtx op;
2779      enum machine_mode mode ATTRIBUTE_UNUSED;
2780 {
2781   /* Disallow indirect through a virtual register.  This leads to
2782      compiler aborts when trying to eliminate them.  */
2783   if (GET_CODE (op) == REG
2784       && (op == arg_pointer_rtx
2785           || op == frame_pointer_rtx
2786           || (REGNO (op) >= FIRST_PSEUDO_REGISTER
2787               && REGNO (op) <= LAST_VIRTUAL_REGISTER)))
2788     return 0;
2789
2790   /* Disallow `call 1234'.  Due to varying assembler lameness this
2791      gets either rejected or translated to `call .+1234'.  */
2792   if (GET_CODE (op) == CONST_INT)
2793     return 0;
2794
2795   /* Explicitly allow SYMBOL_REF even if pic.  */
2796   if (GET_CODE (op) == SYMBOL_REF)
2797     return 1;
2798
2799   /* Half-pic doesn't allow anything but registers and constants.
2800      We've just taken care of the later.  */
2801   if (HALF_PIC_P ())
2802     return register_operand (op, Pmode);
2803
2804   /* Otherwise we can allow any general_operand in the address.  */
2805   return general_operand (op, Pmode);
2806 }
2807
2808 int
2809 constant_call_address_operand (op, mode)
2810      rtx op;
2811      enum machine_mode mode ATTRIBUTE_UNUSED;
2812 {
2813   if (GET_CODE (op) == CONST
2814       && GET_CODE (XEXP (op, 0)) == PLUS
2815       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
2816     op = XEXP (XEXP (op, 0), 0);
2817   return GET_CODE (op) == SYMBOL_REF;
2818 }
2819
2820 /* Match exactly zero and one.  */
2821
2822 int
2823 const0_operand (op, mode)
2824      register rtx op;
2825      enum machine_mode mode;
2826 {
2827   return op == CONST0_RTX (mode);
2828 }
2829
2830 int
2831 const1_operand (op, mode)
2832      register rtx op;
2833      enum machine_mode mode ATTRIBUTE_UNUSED;
2834 {
2835   return op == const1_rtx;
2836 }
2837
2838 /* Match 2, 4, or 8.  Used for leal multiplicands.  */
2839
2840 int
2841 const248_operand (op, mode)
2842      register rtx op;
2843      enum machine_mode mode ATTRIBUTE_UNUSED;
2844 {
2845   return (GET_CODE (op) == CONST_INT
2846           && (INTVAL (op) == 2 || INTVAL (op) == 4 || INTVAL (op) == 8));
2847 }
2848
2849 /* True if this is a constant appropriate for an increment or decremenmt.  */
2850
2851 int
2852 incdec_operand (op, mode)
2853      register rtx op;
2854      enum machine_mode mode ATTRIBUTE_UNUSED;
2855 {
2856   /* On Pentium4, the inc and dec operations causes extra dependancy on flag
2857      registers, since carry flag is not set.  */
2858   if (TARGET_PENTIUM4 && !optimize_size)
2859     return 0;
2860   return op == const1_rtx || op == constm1_rtx;
2861 }
2862
2863 /* Return nonzero if OP is acceptable as operand of DImode shift
2864    expander.  */
2865
2866 int
2867 shiftdi_operand (op, mode)
2868      rtx op;
2869      enum machine_mode mode ATTRIBUTE_UNUSED;
2870 {
2871   if (TARGET_64BIT)
2872     return nonimmediate_operand (op, mode);
2873   else
2874     return register_operand (op, mode);
2875 }
2876
2877 /* Return false if this is the stack pointer, or any other fake
2878    register eliminable to the stack pointer.  Otherwise, this is
2879    a register operand.
2880
2881    This is used to prevent esp from being used as an index reg.
2882    Which would only happen in pathological cases.  */
2883
2884 int
2885 reg_no_sp_operand (op, mode)
2886      register rtx op;
2887      enum machine_mode mode;
2888 {
2889   rtx t = op;
2890   if (GET_CODE (t) == SUBREG)
2891     t = SUBREG_REG (t);
2892   if (t == stack_pointer_rtx || t == arg_pointer_rtx || t == frame_pointer_rtx)
2893     return 0;
2894
2895   return register_operand (op, mode);
2896 }
2897
2898 int
2899 mmx_reg_operand (op, mode)
2900      register rtx op;
2901      enum machine_mode mode ATTRIBUTE_UNUSED;
2902 {
2903   return MMX_REG_P (op);
2904 }
2905
2906 /* Return false if this is any eliminable register.  Otherwise
2907    general_operand.  */
2908
2909 int
2910 general_no_elim_operand (op, mode)
2911      register rtx op;
2912      enum machine_mode mode;
2913 {
2914   rtx t = op;
2915   if (GET_CODE (t) == SUBREG)
2916     t = SUBREG_REG (t);
2917   if (t == arg_pointer_rtx || t == frame_pointer_rtx
2918       || t == virtual_incoming_args_rtx || t == virtual_stack_vars_rtx
2919       || t == virtual_stack_dynamic_rtx)
2920     return 0;
2921   if (REG_P (t)
2922       && REGNO (t) >= FIRST_VIRTUAL_REGISTER
2923       && REGNO (t) <= LAST_VIRTUAL_REGISTER)
2924     return 0;
2925
2926   return general_operand (op, mode);
2927 }
2928
2929 /* Return false if this is any eliminable register.  Otherwise
2930    register_operand or const_int.  */
2931
2932 int
2933 nonmemory_no_elim_operand (op, mode)
2934      register rtx op;
2935      enum machine_mode mode;
2936 {
2937   rtx t = op;
2938   if (GET_CODE (t) == SUBREG)
2939     t = SUBREG_REG (t);
2940   if (t == arg_pointer_rtx || t == frame_pointer_rtx
2941       || t == virtual_incoming_args_rtx || t == virtual_stack_vars_rtx
2942       || t == virtual_stack_dynamic_rtx)
2943     return 0;
2944
2945   return GET_CODE (op) == CONST_INT || register_operand (op, mode);
2946 }
2947
2948 /* Return true if op is a Q_REGS class register.  */
2949
2950 int
2951 q_regs_operand (op, mode)
2952      register rtx op;
2953      enum machine_mode mode;
2954 {
2955   if (mode != VOIDmode && GET_MODE (op) != mode)
2956     return 0;
2957   if (GET_CODE (op) == SUBREG)
2958     op = SUBREG_REG (op);
2959   return QI_REG_P (op);
2960 }
2961
2962 /* Return true if op is a NON_Q_REGS class register.  */
2963
2964 int
2965 non_q_regs_operand (op, mode)
2966      register rtx op;
2967      enum machine_mode mode;
2968 {
2969   if (mode != VOIDmode && GET_MODE (op) != mode)
2970     return 0;
2971   if (GET_CODE (op) == SUBREG)
2972     op = SUBREG_REG (op);
2973   return NON_QI_REG_P (op);
2974 }
2975
2976 /* Return 1 if OP is a comparison that can be used in the CMPSS/CMPPS
2977    insns.  */
2978 int
2979 sse_comparison_operator (op, mode)
2980      rtx op;
2981      enum machine_mode mode ATTRIBUTE_UNUSED;
2982 {
2983   enum rtx_code code = GET_CODE (op);
2984   switch (code)
2985     {
2986     /* Operations supported directly.  */
2987     case EQ:
2988     case LT:
2989     case LE:
2990     case UNORDERED:
2991     case NE:
2992     case UNGE:
2993     case UNGT:
2994     case ORDERED:
2995       return 1;
2996     /* These are equivalent to ones above in non-IEEE comparisons.  */
2997     case UNEQ:
2998     case UNLT:
2999     case UNLE:
3000     case LTGT:
3001     case GE:
3002     case GT:
3003       return !TARGET_IEEE_FP;
3004     default:
3005       return 0;
3006     }
3007 }
3008 /* Return 1 if OP is a valid comparison operator in valid mode.  */
3009 int
3010 ix86_comparison_operator (op, mode)
3011      register rtx op;
3012      enum machine_mode mode;
3013 {
3014   enum machine_mode inmode;
3015   enum rtx_code code = GET_CODE (op);
3016   if (mode != VOIDmode && GET_MODE (op) != mode)
3017     return 0;
3018   if (GET_RTX_CLASS (code) != '<')
3019     return 0;
3020   inmode = GET_MODE (XEXP (op, 0));
3021
3022   if (inmode == CCFPmode || inmode == CCFPUmode)
3023     {
3024       enum rtx_code second_code, bypass_code;
3025       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
3026       return (bypass_code == NIL && second_code == NIL);
3027     }
3028   switch (code)
3029     {
3030     case EQ: case NE:
3031       return 1;
3032     case LT: case GE:
3033       if (inmode == CCmode || inmode == CCGCmode
3034           || inmode == CCGOCmode || inmode == CCNOmode)
3035         return 1;
3036       return 0;
3037     case LTU: case GTU: case LEU: case ORDERED: case UNORDERED: case GEU:
3038       if (inmode == CCmode)
3039         return 1;
3040       return 0;
3041     case GT: case LE:
3042       if (inmode == CCmode || inmode == CCGCmode || inmode == CCNOmode)
3043         return 1;
3044       return 0;
3045     default:
3046       return 0;
3047     }
3048 }
3049
3050 /* Return 1 if OP is a comparison operator that can be issued by fcmov.  */
3051
3052 int
3053 fcmov_comparison_operator (op, mode)
3054     register rtx op;
3055     enum machine_mode mode;
3056 {
3057   enum machine_mode inmode;
3058   enum rtx_code code = GET_CODE (op);
3059   if (mode != VOIDmode && GET_MODE (op) != mode)
3060     return 0;
3061   if (GET_RTX_CLASS (code) != '<')
3062     return 0;
3063   inmode = GET_MODE (XEXP (op, 0));
3064   if (inmode == CCFPmode || inmode == CCFPUmode)
3065     {
3066       enum rtx_code second_code, bypass_code;
3067       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
3068       if (bypass_code != NIL || second_code != NIL)
3069         return 0;
3070       code = ix86_fp_compare_code_to_integer (code);
3071     }
3072   /* i387 supports just limited amount of conditional codes.  */
3073   switch (code)
3074     {
3075     case LTU: case GTU: case LEU: case GEU:
3076       if (inmode == CCmode || inmode == CCFPmode || inmode == CCFPUmode)
3077         return 1;
3078       return 0;
3079     case ORDERED: case UNORDERED:
3080     case EQ: case NE:
3081       return 1;
3082     default:
3083       return 0;
3084     }
3085 }
3086
3087 /* Return 1 if OP is a binary operator that can be promoted to wider mode.  */
3088
3089 int
3090 promotable_binary_operator (op, mode)
3091      register rtx op;
3092      enum machine_mode mode ATTRIBUTE_UNUSED;
3093 {
3094   switch (GET_CODE (op))
3095     {
3096     case MULT:
3097       /* Modern CPUs have same latency for HImode and SImode multiply,
3098          but 386 and 486 do HImode multiply faster.  */
3099       return ix86_cpu > PROCESSOR_I486;
3100     case PLUS:
3101     case AND:
3102     case IOR:
3103     case XOR:
3104     case ASHIFT:
3105       return 1;
3106     default:
3107       return 0;
3108     }
3109 }
3110
3111 /* Nearly general operand, but accept any const_double, since we wish
3112    to be able to drop them into memory rather than have them get pulled
3113    into registers.  */
3114
3115 int
3116 cmp_fp_expander_operand (op, mode)
3117      register rtx op;
3118      enum machine_mode mode;
3119 {
3120   if (mode != VOIDmode && mode != GET_MODE (op))
3121     return 0;
3122   if (GET_CODE (op) == CONST_DOUBLE)
3123     return 1;
3124   return general_operand (op, mode);
3125 }
3126
3127 /* Match an SI or HImode register for a zero_extract.  */
3128
3129 int
3130 ext_register_operand (op, mode)
3131      register rtx op;
3132      enum machine_mode mode ATTRIBUTE_UNUSED;
3133 {
3134   int regno;
3135   if ((!TARGET_64BIT || GET_MODE (op) != DImode)
3136       && GET_MODE (op) != SImode && GET_MODE (op) != HImode)
3137     return 0;
3138
3139   if (!register_operand (op, VOIDmode))
3140     return 0;
3141
3142   /* Be curefull to accept only registers having upper parts.  */
3143   regno = REG_P (op) ? REGNO (op) : REGNO (SUBREG_REG (op));
3144   return (regno > LAST_VIRTUAL_REGISTER || regno < 4);
3145 }
3146
3147 /* Return 1 if this is a valid binary floating-point operation.
3148    OP is the expression matched, and MODE is its mode.  */
3149
3150 int
3151 binary_fp_operator (op, mode)
3152     register rtx op;
3153     enum machine_mode mode;
3154 {
3155   if (mode != VOIDmode && mode != GET_MODE (op))
3156     return 0;
3157
3158   switch (GET_CODE (op))
3159     {
3160     case PLUS:
3161     case MINUS:
3162     case MULT:
3163     case DIV:
3164       return GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT;
3165
3166     default:
3167       return 0;
3168     }
3169 }
3170
3171 int
3172 mult_operator(op, mode)
3173     register rtx op;
3174     enum machine_mode mode ATTRIBUTE_UNUSED;
3175 {
3176   return GET_CODE (op) == MULT;
3177 }
3178
3179 int
3180 div_operator(op, mode)
3181     register rtx op;
3182     enum machine_mode mode ATTRIBUTE_UNUSED;
3183 {
3184   return GET_CODE (op) == DIV;
3185 }
3186
3187 int
3188 arith_or_logical_operator (op, mode)
3189       rtx op;
3190       enum machine_mode mode;
3191 {
3192   return ((mode == VOIDmode || GET_MODE (op) == mode)
3193           && (GET_RTX_CLASS (GET_CODE (op)) == 'c'
3194               || GET_RTX_CLASS (GET_CODE (op)) == '2'));
3195 }
3196
3197 /* Returns 1 if OP is memory operand with a displacement.  */
3198
3199 int
3200 memory_displacement_operand (op, mode)
3201      register rtx op;
3202      enum machine_mode mode;
3203 {
3204   struct ix86_address parts;
3205
3206   if (! memory_operand (op, mode))
3207     return 0;
3208
3209   if (! ix86_decompose_address (XEXP (op, 0), &parts))
3210     abort ();
3211
3212   return parts.disp != NULL_RTX;
3213 }
3214
3215 /* To avoid problems when jump re-emits comparisons like testqi_ext_ccno_0,
3216    re-recognize the operand to avoid a copy_to_mode_reg that will fail.
3217
3218    ??? It seems likely that this will only work because cmpsi is an
3219    expander, and no actual insns use this.  */
3220
3221 int
3222 cmpsi_operand (op, mode)
3223       rtx op;
3224       enum machine_mode mode;
3225 {
3226   if (nonimmediate_operand (op, mode))
3227     return 1;
3228
3229   if (GET_CODE (op) == AND
3230       && GET_MODE (op) == SImode
3231       && GET_CODE (XEXP (op, 0)) == ZERO_EXTRACT
3232       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
3233       && GET_CODE (XEXP (XEXP (op, 0), 2)) == CONST_INT
3234       && INTVAL (XEXP (XEXP (op, 0), 1)) == 8
3235       && INTVAL (XEXP (XEXP (op, 0), 2)) == 8
3236       && GET_CODE (XEXP (op, 1)) == CONST_INT)
3237     return 1;
3238
3239   return 0;
3240 }
3241
3242 /* Returns 1 if OP is memory operand that can not be represented by the
3243    modRM array.  */
3244
3245 int
3246 long_memory_operand (op, mode)
3247      register rtx op;
3248      enum machine_mode mode;
3249 {
3250   if (! memory_operand (op, mode))
3251     return 0;
3252
3253   return memory_address_length (op) != 0;
3254 }
3255
3256 /* Return nonzero if the rtx is known aligned.  */
3257
3258 int
3259 aligned_operand (op, mode)
3260      rtx op;
3261      enum machine_mode mode;
3262 {
3263   struct ix86_address parts;
3264
3265   if (!general_operand (op, mode))
3266     return 0;
3267
3268   /* Registers and immediate operands are always "aligned".  */
3269   if (GET_CODE (op) != MEM)
3270     return 1;
3271
3272   /* Don't even try to do any aligned optimizations with volatiles.  */
3273   if (MEM_VOLATILE_P (op))
3274     return 0;
3275
3276   op = XEXP (op, 0);
3277
3278   /* Pushes and pops are only valid on the stack pointer.  */
3279   if (GET_CODE (op) == PRE_DEC
3280       || GET_CODE (op) == POST_INC)
3281     return 1;
3282
3283   /* Decode the address.  */
3284   if (! ix86_decompose_address (op, &parts))
3285     abort ();
3286
3287   /* Look for some component that isn't known to be aligned.  */
3288   if (parts.index)
3289     {
3290       if (parts.scale < 4
3291           && REGNO_POINTER_ALIGN (REGNO (parts.index)) < 32)
3292         return 0;
3293     }
3294   if (parts.base)
3295     {
3296       if (REGNO_POINTER_ALIGN (REGNO (parts.base)) < 32)
3297         return 0;
3298     }
3299   if (parts.disp)
3300     {
3301       if (GET_CODE (parts.disp) != CONST_INT
3302           || (INTVAL (parts.disp) & 3) != 0)
3303         return 0;
3304     }
3305
3306   /* Didn't find one -- this must be an aligned address.  */
3307   return 1;
3308 }
3309 \f
3310 /* Return true if the constant is something that can be loaded with
3311    a special instruction.  Only handle 0.0 and 1.0; others are less
3312    worthwhile.  */
3313
3314 int
3315 standard_80387_constant_p (x)
3316      rtx x;
3317 {
3318   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
3319     return -1;
3320   /* Note that on the 80387, other constants, such as pi, that we should support
3321      too.  On some machines, these are much slower to load as standard constant,
3322      than to load from doubles in memory.  */
3323   if (x == CONST0_RTX (GET_MODE (x)))
3324     return 1;
3325   if (x == CONST1_RTX (GET_MODE (x)))
3326     return 2;
3327   return 0;
3328 }
3329
3330 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
3331  */
3332 int
3333 standard_sse_constant_p (x)
3334      rtx x;
3335 {
3336   if (GET_CODE (x) != CONST_DOUBLE)
3337     return -1;
3338   return (x == CONST0_RTX (GET_MODE (x)));
3339 }
3340
3341 /* Returns 1 if OP contains a symbol reference */
3342
3343 int
3344 symbolic_reference_mentioned_p (op)
3345      rtx op;
3346 {
3347   register const char *fmt;
3348   register int i;
3349
3350   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
3351     return 1;
3352
3353   fmt = GET_RTX_FORMAT (GET_CODE (op));
3354   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3355     {
3356       if (fmt[i] == 'E')
3357         {
3358           register int j;
3359
3360           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3361             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3362               return 1;
3363         }
3364
3365       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
3366         return 1;
3367     }
3368
3369   return 0;
3370 }
3371
3372 /* Return 1 if it is appropriate to emit `ret' instructions in the
3373    body of a function.  Do this only if the epilogue is simple, needing a
3374    couple of insns.  Prior to reloading, we can't tell how many registers
3375    must be saved, so return 0 then.  Return 0 if there is no frame
3376    marker to de-allocate.
3377
3378    If NON_SAVING_SETJMP is defined and true, then it is not possible
3379    for the epilogue to be simple, so return 0.  This is a special case
3380    since NON_SAVING_SETJMP will not cause regs_ever_live to change
3381    until final, but jump_optimize may need to know sooner if a
3382    `return' is OK.  */
3383
3384 int
3385 ix86_can_use_return_insn_p ()
3386 {
3387   struct ix86_frame frame;
3388
3389 #ifdef NON_SAVING_SETJMP
3390   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
3391     return 0;
3392 #endif
3393 #ifdef FUNCTION_BLOCK_PROFILER_EXIT
3394   if (profile_block_flag == 2)
3395     return 0;
3396 #endif
3397
3398   if (! reload_completed || frame_pointer_needed)
3399     return 0;
3400
3401   /* Don't allow more than 32 pop, since that's all we can do
3402      with one instruction.  */
3403   if (current_function_pops_args
3404       && current_function_args_size >= 32768)
3405     return 0;
3406
3407   ix86_compute_frame_layout (&frame);
3408   return frame.to_allocate == 0 && frame.nregs == 0;
3409 }
3410 \f
3411 /* Return 1 if VALUE can be stored in the sign extended immediate field.  */
3412 int
3413 x86_64_sign_extended_value (value)
3414      rtx value;
3415 {
3416   switch (GET_CODE (value))
3417     {
3418       /* CONST_DOUBLES never match, since HOST_BITS_PER_WIDE_INT is known
3419          to be at least 32 and this all acceptable constants are
3420          represented as CONST_INT.  */
3421       case CONST_INT:
3422         if (HOST_BITS_PER_WIDE_INT == 32)
3423           return 1;
3424         else
3425           {
3426             HOST_WIDE_INT val = trunc_int_for_mode (INTVAL (value), DImode);
3427             return trunc_int_for_mode (val, SImode) == val;
3428           }
3429         break;
3430
3431       /* For certain code models, the symbolic references are known to fit.  */
3432       case SYMBOL_REF:
3433         return ix86_cmodel == CM_SMALL || ix86_cmodel == CM_KERNEL;
3434
3435       /* For certain code models, the code is near as well.  */
3436       case LABEL_REF:
3437         return ix86_cmodel != CM_LARGE && ix86_cmodel != CM_SMALL_PIC;
3438
3439       /* We also may accept the offsetted memory references in certain special
3440          cases.  */
3441       case CONST:
3442         if (GET_CODE (XEXP (value, 0)) == UNSPEC
3443             && XVECLEN (XEXP (value, 0), 0) == 1
3444             && XINT (XEXP (value, 0), 1) ==  15)
3445           return 1;
3446         else if (GET_CODE (XEXP (value, 0)) == PLUS)
3447           {
3448             rtx op1 = XEXP (XEXP (value, 0), 0);
3449             rtx op2 = XEXP (XEXP (value, 0), 1);
3450             HOST_WIDE_INT offset;
3451
3452             if (ix86_cmodel == CM_LARGE)
3453               return 0;
3454             if (GET_CODE (op2) != CONST_INT)
3455               return 0;
3456             offset = trunc_int_for_mode (INTVAL (op2), DImode);
3457             switch (GET_CODE (op1))
3458               {
3459                 case SYMBOL_REF:
3460                   /* For CM_SMALL assume that latest object is 1MB before
3461                      end of 31bits boundary.  We may also accept pretty
3462                      large negative constants knowing that all objects are
3463                      in the positive half of address space.  */
3464                   if (ix86_cmodel == CM_SMALL
3465                       && offset < 1024*1024*1024
3466                       && trunc_int_for_mode (offset, SImode) == offset)
3467                     return 1;
3468                   /* For CM_KERNEL we know that all object resist in the
3469                      negative half of 32bits address space.  We may not
3470                      accept negative offsets, since they may be just off
3471                      and we may accept pretty large possitive ones.  */
3472                   if (ix86_cmodel == CM_KERNEL
3473                       && offset > 0
3474                       && trunc_int_for_mode (offset, SImode) == offset)
3475                     return 1;
3476                   break;
3477                 case LABEL_REF:
3478                   /* These conditions are similar to SYMBOL_REF ones, just the
3479                      constraints for code models differ.  */
3480                   if ((ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM)
3481                       && offset < 1024*1024*1024
3482                       && trunc_int_for_mode (offset, SImode) == offset)
3483                     return 1;
3484                   if (ix86_cmodel == CM_KERNEL
3485                       && offset > 0
3486                       && trunc_int_for_mode (offset, SImode) == offset)
3487                     return 1;
3488                   break;
3489                 default:
3490                   return 0;
3491               }
3492           }
3493         return 0;
3494       default:
3495         return 0;
3496     }
3497 }
3498
3499 /* Return 1 if VALUE can be stored in the zero extended immediate field.  */
3500 int
3501 x86_64_zero_extended_value (value)
3502      rtx value;
3503 {
3504   switch (GET_CODE (value))
3505     {
3506       case CONST_DOUBLE:
3507         if (HOST_BITS_PER_WIDE_INT == 32)
3508           return  (GET_MODE (value) == VOIDmode
3509                    && !CONST_DOUBLE_HIGH (value));
3510         else
3511           return 0;
3512       case CONST_INT:
3513         if (HOST_BITS_PER_WIDE_INT == 32)
3514           return INTVAL (value) >= 0;
3515         else
3516           return !(INTVAL (value) & ~(HOST_WIDE_INT)0xffffffff);
3517         break;
3518
3519       /* For certain code models, the symbolic references are known to fit.  */
3520       case SYMBOL_REF:
3521         return ix86_cmodel == CM_SMALL;
3522
3523       /* For certain code models, the code is near as well.  */
3524       case LABEL_REF:
3525         return ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM;
3526
3527       /* We also may accept the offsetted memory references in certain special
3528          cases.  */
3529       case CONST:
3530         if (GET_CODE (XEXP (value, 0)) == PLUS)
3531           {
3532             rtx op1 = XEXP (XEXP (value, 0), 0);
3533             rtx op2 = XEXP (XEXP (value, 0), 1);
3534
3535             if (ix86_cmodel == CM_LARGE)
3536               return 0;
3537             switch (GET_CODE (op1))
3538               {
3539                 case SYMBOL_REF:
3540                     return 0;
3541                   /* For small code model we may accept pretty large possitive
3542                      offsets, since one bit is available for free.  Negative
3543                      offsets are limited by the size of NULL pointer area
3544                      specified by the ABI.  */
3545                   if (ix86_cmodel == CM_SMALL
3546                       && GET_CODE (op2) == CONST_INT
3547                       && trunc_int_for_mode (INTVAL (op2), DImode) > -0x10000
3548                       && (trunc_int_for_mode (INTVAL (op2), SImode)
3549                           == INTVAL (op2)))
3550                     return 1;
3551                   /* ??? For the kernel, we may accept adjustment of
3552                      -0x10000000, since we know that it will just convert
3553                      negative address space to possitive, but perhaps this
3554                      is not worthwhile.  */
3555                   break;
3556                 case LABEL_REF:
3557                   /* These conditions are similar to SYMBOL_REF ones, just the
3558                      constraints for code models differ.  */
3559                   if ((ix86_cmodel == CM_SMALL || ix86_cmodel == CM_MEDIUM)
3560                       && GET_CODE (op2) == CONST_INT
3561                       && trunc_int_for_mode (INTVAL (op2), DImode) > -0x10000
3562                       && (trunc_int_for_mode (INTVAL (op2), SImode)
3563                           == INTVAL (op2)))
3564                     return 1;
3565                   break;
3566                 default:
3567                   return 0;
3568               }
3569           }
3570         return 0;
3571       default:
3572         return 0;
3573     }
3574 }
3575
3576 /* Value should be nonzero if functions must have frame pointers.
3577    Zero means the frame pointer need not be set up (and parms may
3578    be accessed via the stack pointer) in functions that seem suitable.  */
3579
3580 int
3581 ix86_frame_pointer_required ()
3582 {
3583   /* If we accessed previous frames, then the generated code expects
3584      to be able to access the saved ebp value in our frame.  */
3585   if (cfun->machine->accesses_prev_frame)
3586     return 1;
3587
3588   /* Several x86 os'es need a frame pointer for other reasons,
3589      usually pertaining to setjmp.  */
3590   if (SUBTARGET_FRAME_POINTER_REQUIRED)
3591     return 1;
3592
3593   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
3594      the frame pointer by default.  Turn it back on now if we've not
3595      got a leaf function.  */
3596   if (TARGET_OMIT_LEAF_FRAME_POINTER && ! leaf_function_p ())
3597     return 1;
3598
3599   return 0;
3600 }
3601
3602 /* Record that the current function accesses previous call frames.  */
3603
3604 void
3605 ix86_setup_frame_addresses ()
3606 {
3607   cfun->machine->accesses_prev_frame = 1;
3608 }
3609 \f
3610 static char pic_label_name[32];
3611
3612 /* This function generates code for -fpic that loads %ebx with
3613    the return address of the caller and then returns.  */
3614
3615 void
3616 ix86_asm_file_end (file)
3617      FILE *file;
3618 {
3619   rtx xops[2];
3620
3621   if (! TARGET_DEEP_BRANCH_PREDICTION || pic_label_name[0] == 0)
3622     return;
3623
3624   /* ??? Binutils 2.10 and earlier has a linkonce elimination bug related
3625      to updating relocations to a section being discarded such that this
3626      doesn't work.  Ought to detect this at configure time.  */
3627 #if 0
3628   /* The trick here is to create a linkonce section containing the
3629      pic label thunk, but to refer to it with an internal label.
3630      Because the label is internal, we don't have inter-dso name
3631      binding issues on hosts that don't support ".hidden".
3632
3633      In order to use these macros, however, we must create a fake
3634      function decl.  */
3635   if (targetm.have_named_sections)
3636     {
3637       tree decl = build_decl (FUNCTION_DECL,
3638                               get_identifier ("i686.get_pc_thunk"),
3639                               error_mark_node);
3640       DECL_ONE_ONLY (decl) = 1;
3641       UNIQUE_SECTION (decl, 0);
3642       named_section (decl, NULL);
3643     }
3644   else
3645 #else
3646     text_section ();
3647 #endif
3648
3649   /* This used to call ASM_DECLARE_FUNCTION_NAME() but since it's an
3650      internal (non-global) label that's being emitted, it didn't make
3651      sense to have .type information for local labels.   This caused
3652      the SCO OpenServer 5.0.4 ELF assembler grief (why are you giving
3653      me debug info for a label that you're declaring non-global?) this
3654      was changed to call ASM_OUTPUT_LABEL() instead.  */
3655
3656   ASM_OUTPUT_LABEL (file, pic_label_name);
3657
3658   xops[0] = pic_offset_table_rtx;
3659   xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
3660   output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
3661   output_asm_insn ("ret", xops);
3662 }
3663
3664 void
3665 load_pic_register ()
3666 {
3667   rtx gotsym, pclab;
3668
3669   if (TARGET_64BIT)
3670     abort();
3671
3672   gotsym = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3673
3674   if (TARGET_DEEP_BRANCH_PREDICTION)
3675     {
3676       if (! pic_label_name[0])
3677         ASM_GENERATE_INTERNAL_LABEL (pic_label_name, "LPR", 0);
3678       pclab = gen_rtx_MEM (QImode, gen_rtx_SYMBOL_REF (Pmode, pic_label_name));
3679     }
3680   else
3681     {
3682       pclab = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
3683     }
3684
3685   emit_insn (gen_prologue_get_pc (pic_offset_table_rtx, pclab));
3686
3687   if (! TARGET_DEEP_BRANCH_PREDICTION)
3688     emit_insn (gen_popsi1 (pic_offset_table_rtx));
3689
3690   emit_insn (gen_prologue_set_got (pic_offset_table_rtx, gotsym, pclab));
3691 }
3692
3693 /* Generate an "push" pattern for input ARG.  */
3694
3695 static rtx
3696 gen_push (arg)
3697      rtx arg;
3698 {
3699   return gen_rtx_SET (VOIDmode,
3700                       gen_rtx_MEM (Pmode,
3701                                    gen_rtx_PRE_DEC (Pmode,
3702                                                     stack_pointer_rtx)),
3703                       arg);
3704 }
3705
3706 /* Return 1 if we need to save REGNO.  */
3707 static int
3708 ix86_save_reg (regno, maybe_eh_return)
3709      int regno;
3710      int maybe_eh_return;
3711 {
3712   if (flag_pic
3713       && ! TARGET_64BIT
3714       && regno == PIC_OFFSET_TABLE_REGNUM
3715       && (current_function_uses_pic_offset_table
3716           || current_function_uses_const_pool
3717           || current_function_calls_eh_return))
3718     return 1;
3719
3720   if (current_function_calls_eh_return && maybe_eh_return)
3721     {
3722       unsigned i;
3723       for (i = 0; ; i++)
3724         {
3725           unsigned test = EH_RETURN_DATA_REGNO(i);
3726           if (test == INVALID_REGNUM)
3727             break;
3728           if (test == (unsigned) regno)
3729             return 1;
3730         }
3731     }
3732
3733   return (regs_ever_live[regno]
3734           && !call_used_regs[regno]
3735           && !fixed_regs[regno]
3736           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
3737 }
3738
3739 /* Return number of registers to be saved on the stack.  */
3740
3741 static int
3742 ix86_nsaved_regs ()
3743 {
3744   int nregs = 0;
3745   int regno;
3746
3747   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
3748     if (ix86_save_reg (regno, true))
3749       nregs++;
3750   return nregs;
3751 }
3752
3753 /* Return the offset between two registers, one to be eliminated, and the other
3754    its replacement, at the start of a routine.  */
3755
3756 HOST_WIDE_INT
3757 ix86_initial_elimination_offset (from, to)
3758      int from;
3759      int to;
3760 {
3761   struct ix86_frame frame;
3762   ix86_compute_frame_layout (&frame);
3763
3764   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
3765     return frame.hard_frame_pointer_offset;
3766   else if (from == FRAME_POINTER_REGNUM
3767            && to == HARD_FRAME_POINTER_REGNUM)
3768     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
3769   else
3770     {
3771       if (to != STACK_POINTER_REGNUM)
3772         abort ();
3773       else if (from == ARG_POINTER_REGNUM)
3774         return frame.stack_pointer_offset;
3775       else if (from != FRAME_POINTER_REGNUM)
3776         abort ();
3777       else
3778         return frame.stack_pointer_offset - frame.frame_pointer_offset;
3779     }
3780 }
3781
3782 /* Fill structure ix86_frame about frame of currently computed function.  */
3783
3784 static void
3785 ix86_compute_frame_layout (frame)
3786      struct ix86_frame *frame;
3787 {
3788   HOST_WIDE_INT total_size;
3789   int stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
3790   int offset;
3791   int preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
3792   HOST_WIDE_INT size = get_frame_size ();
3793
3794   frame->nregs = ix86_nsaved_regs ();
3795   total_size = size;
3796
3797   /* Skip return value and save base pointer.  */
3798   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
3799
3800   frame->hard_frame_pointer_offset = offset;
3801
3802   /* Do some sanity checking of stack_alignment_needed and
3803      preferred_alignment, since i386 port is the only using those features
3804      that may break easily.  */
3805
3806   if (size && !stack_alignment_needed)
3807     abort ();
3808   if (preferred_alignment < STACK_BOUNDARY / BITS_PER_UNIT)
3809     abort ();
3810   if (preferred_alignment > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
3811     abort ();
3812   if (stack_alignment_needed > PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
3813     abort ();
3814
3815   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
3816     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
3817
3818   /* Register save area */
3819   offset += frame->nregs * UNITS_PER_WORD;
3820
3821   /* Va-arg area */
3822   if (ix86_save_varrargs_registers)
3823     {
3824       offset += X86_64_VARARGS_SIZE;
3825       frame->va_arg_size = X86_64_VARARGS_SIZE;
3826     }
3827   else
3828     frame->va_arg_size = 0;
3829
3830   /* Align start of frame for local function.  */
3831   frame->padding1 = ((offset + stack_alignment_needed - 1)
3832                      & -stack_alignment_needed) - offset;
3833
3834   offset += frame->padding1;
3835
3836   /* Frame pointer points here.  */
3837   frame->frame_pointer_offset = offset;
3838
3839   offset += size;
3840
3841   /* Add outgoing arguments area.  */
3842   if (ACCUMULATE_OUTGOING_ARGS)
3843     {
3844       offset += current_function_outgoing_args_size;
3845       frame->outgoing_arguments_size = current_function_outgoing_args_size;
3846     }
3847   else
3848     frame->outgoing_arguments_size = 0;
3849
3850   /* Align stack boundary.  */
3851   frame->padding2 = ((offset + preferred_alignment - 1)
3852                      & -preferred_alignment) - offset;
3853
3854   offset += frame->padding2;
3855
3856   /* We've reached end of stack frame.  */
3857   frame->stack_pointer_offset = offset;
3858
3859   /* Size prologue needs to allocate.  */
3860   frame->to_allocate =
3861     (size + frame->padding1 + frame->padding2
3862      + frame->outgoing_arguments_size + frame->va_arg_size);
3863
3864   if (TARGET_64BIT && TARGET_RED_ZONE && current_function_sp_is_unchanging
3865       && current_function_is_leaf)
3866     {
3867       frame->red_zone_size = frame->to_allocate;
3868       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
3869         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
3870     }
3871   else
3872     frame->red_zone_size = 0;
3873   frame->to_allocate -= frame->red_zone_size;
3874   frame->stack_pointer_offset -= frame->red_zone_size;
3875 #if 0
3876   fprintf (stderr, "nregs: %i\n", frame->nregs);
3877   fprintf (stderr, "size: %i\n", size);
3878   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
3879   fprintf (stderr, "padding1: %i\n", frame->padding1);
3880   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
3881   fprintf (stderr, "padding2: %i\n", frame->padding2);
3882   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
3883   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
3884   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
3885   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
3886            frame->hard_frame_pointer_offset);
3887   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
3888 #endif
3889 }
3890
3891 /* Emit code to save registers in the prologue.  */
3892
3893 static void
3894 ix86_emit_save_regs ()
3895 {
3896   register int regno;
3897   rtx insn;
3898
3899   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
3900     if (ix86_save_reg (regno, true))
3901       {
3902         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
3903         RTX_FRAME_RELATED_P (insn) = 1;
3904       }
3905 }
3906
3907 /* Emit code to save registers using MOV insns.  First register
3908    is restored from POINTER + OFFSET.  */
3909 static void
3910 ix86_emit_save_regs_using_mov (pointer, offset)
3911      rtx pointer;
3912      HOST_WIDE_INT offset;
3913 {
3914   int regno;
3915   rtx insn;
3916
3917   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
3918     if (ix86_save_reg (regno, true))
3919       {
3920         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
3921                                                Pmode, offset),
3922                                gen_rtx_REG (Pmode, regno));
3923         RTX_FRAME_RELATED_P (insn) = 1;
3924         offset += UNITS_PER_WORD;
3925       }
3926 }
3927
3928 /* Expand the prologue into a bunch of separate insns.  */
3929
3930 void
3931 ix86_expand_prologue ()
3932 {
3933   rtx insn;
3934   int pic_reg_used = (flag_pic && (current_function_uses_pic_offset_table
3935                                   || current_function_uses_const_pool)
3936                       && !TARGET_64BIT);
3937   struct ix86_frame frame;
3938   int use_mov = 0;
3939   HOST_WIDE_INT allocate;
3940
3941   if (!optimize_size)
3942     {
3943       use_fast_prologue_epilogue
3944          = !expensive_function_p (FAST_PROLOGUE_INSN_COUNT);
3945       if (TARGET_PROLOGUE_USING_MOVE)
3946         use_mov = use_fast_prologue_epilogue;
3947     }
3948   ix86_compute_frame_layout (&frame);
3949
3950   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
3951      slower on all targets.  Also sdb doesn't like it.  */
3952
3953   if (frame_pointer_needed)
3954     {
3955       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
3956       RTX_FRAME_RELATED_P (insn) = 1;
3957
3958       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
3959       RTX_FRAME_RELATED_P (insn) = 1;
3960     }
3961
3962   allocate = frame.to_allocate;
3963   /* In case we are dealing only with single register and empty frame,
3964      push is equivalent of the mov+add sequence.  */
3965   if (allocate == 0 && frame.nregs <= 1)
3966     use_mov = 0;
3967
3968   if (!use_mov)
3969     ix86_emit_save_regs ();
3970   else
3971     allocate += frame.nregs * UNITS_PER_WORD;
3972
3973   if (allocate == 0)
3974     ;
3975   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
3976     {
3977       insn = emit_insn (gen_pro_epilogue_adjust_stack
3978                         (stack_pointer_rtx, stack_pointer_rtx,
3979                          GEN_INT (-allocate)));
3980       RTX_FRAME_RELATED_P (insn) = 1;
3981     }
3982   else
3983     {
3984       /* ??? Is this only valid for Win32?  */
3985
3986       rtx arg0, sym;
3987
3988       if (TARGET_64BIT)
3989         abort();
3990
3991       arg0 = gen_rtx_REG (SImode, 0);
3992       emit_move_insn (arg0, GEN_INT (allocate));
3993
3994       sym = gen_rtx_MEM (FUNCTION_MODE,
3995                          gen_rtx_SYMBOL_REF (Pmode, "_alloca"));
3996       insn = emit_call_insn (gen_call (sym, const0_rtx, constm1_rtx));
3997
3998       CALL_INSN_FUNCTION_USAGE (insn)
3999         = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, arg0),
4000                              CALL_INSN_FUNCTION_USAGE (insn));
4001     }
4002   if (use_mov)
4003     {
4004       if (!frame_pointer_needed || !frame.to_allocate)
4005         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
4006       else
4007         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
4008                                        -frame.nregs * UNITS_PER_WORD);
4009     }
4010
4011 #ifdef SUBTARGET_PROLOGUE
4012   SUBTARGET_PROLOGUE;
4013 #endif
4014
4015   if (pic_reg_used)
4016     load_pic_register ();
4017
4018   /* If we are profiling, make sure no instructions are scheduled before
4019      the call to mcount.  However, if -fpic, the above call will have
4020      done that.  */
4021   if ((profile_flag || profile_block_flag) && ! pic_reg_used)
4022     emit_insn (gen_blockage ());
4023 }
4024
4025 /* Emit code to restore saved registers using MOV insns.  First register
4026    is restored from POINTER + OFFSET.  */
4027 static void
4028 ix86_emit_restore_regs_using_mov (pointer, offset, maybe_eh_return)
4029      rtx pointer;
4030      int offset;
4031      int maybe_eh_return;
4032 {
4033   int regno;
4034
4035   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4036     if (ix86_save_reg (regno, maybe_eh_return))
4037       {
4038         emit_move_insn (gen_rtx_REG (Pmode, regno),
4039                         adjust_address (gen_rtx_MEM (Pmode, pointer),
4040                                         Pmode, offset));
4041         offset += UNITS_PER_WORD;
4042       }
4043 }
4044
4045 /* Restore function stack, frame, and registers.  */
4046
4047 void
4048 ix86_expand_epilogue (style)
4049      int style;
4050 {
4051   int regno;
4052   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
4053   struct ix86_frame frame;
4054   HOST_WIDE_INT offset;
4055
4056   ix86_compute_frame_layout (&frame);
4057
4058   /* Calculate start of saved registers relative to ebp.  Special care
4059      must be taken for the normal return case of a function using
4060      eh_return: the eax and edx registers are marked as saved, but not
4061      restored along this path.  */
4062   offset = frame.nregs;
4063   if (current_function_calls_eh_return && style != 2)
4064     offset -= 2;
4065   offset *= -UNITS_PER_WORD;
4066
4067 #ifdef FUNCTION_BLOCK_PROFILER_EXIT
4068   if (profile_block_flag == 2)
4069     {
4070       FUNCTION_BLOCK_PROFILER_EXIT;
4071     }
4072 #endif
4073
4074   /* If we're only restoring one register and sp is not valid then
4075      using a move instruction to restore the register since it's
4076      less work than reloading sp and popping the register.
4077
4078      The default code result in stack adjustment using add/lea instruction,
4079      while this code results in LEAVE instruction (or discrete equivalent),
4080      so it is profitable in some other cases as well.  Especially when there
4081      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
4082      and there is exactly one register to pop. This heruistic may need some
4083      tuning in future.  */
4084   if ((!sp_valid && frame.nregs <= 1)
4085       || (TARGET_EPILOGUE_USING_MOVE
4086           && use_fast_prologue_epilogue
4087           && (frame.nregs > 1 || frame.to_allocate))
4088       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
4089       || (frame_pointer_needed && TARGET_USE_LEAVE
4090           && use_fast_prologue_epilogue && frame.nregs == 1)
4091       || current_function_calls_eh_return)
4092     {
4093       /* Restore registers.  We can use ebp or esp to address the memory
4094          locations.  If both are available, default to ebp, since offsets
4095          are known to be small.  Only exception is esp pointing directly to the
4096          end of block of saved registers, where we may simplify addressing
4097          mode.  */
4098
4099       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
4100         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
4101                                           frame.to_allocate, style == 2);
4102       else
4103         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
4104                                           offset, style == 2);
4105
4106       /* eh_return epilogues need %ecx added to the stack pointer.  */
4107       if (style == 2)
4108         {
4109           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
4110
4111           if (frame_pointer_needed)
4112             {
4113               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
4114               tmp = plus_constant (tmp, UNITS_PER_WORD);
4115               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
4116
4117               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
4118               emit_move_insn (hard_frame_pointer_rtx, tmp);
4119
4120               emit_insn (gen_pro_epilogue_adjust_stack
4121                          (stack_pointer_rtx, sa, const0_rtx));
4122             }
4123           else
4124             {
4125               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
4126               tmp = plus_constant (tmp, (frame.to_allocate
4127                                          + frame.nregs * UNITS_PER_WORD));
4128               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
4129             }
4130         }
4131       else if (!frame_pointer_needed)
4132         emit_insn (gen_pro_epilogue_adjust_stack
4133                    (stack_pointer_rtx, stack_pointer_rtx,
4134                     GEN_INT (frame.to_allocate
4135                              + frame.nregs * UNITS_PER_WORD)));
4136       /* If not an i386, mov & pop is faster than "leave".  */
4137       else if (TARGET_USE_LEAVE || optimize_size || !use_fast_prologue_epilogue)
4138         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
4139       else
4140         {
4141           emit_insn (gen_pro_epilogue_adjust_stack (stack_pointer_rtx,
4142                                                     hard_frame_pointer_rtx,
4143                                                     const0_rtx));
4144           if (TARGET_64BIT)
4145             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
4146           else
4147             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
4148         }
4149     }
4150   else
4151     {
4152       /* First step is to deallocate the stack frame so that we can
4153          pop the registers.  */
4154       if (!sp_valid)
4155         {
4156           if (!frame_pointer_needed)
4157             abort ();
4158           emit_insn (gen_pro_epilogue_adjust_stack (stack_pointer_rtx,
4159                                                     hard_frame_pointer_rtx,
4160                                                     GEN_INT (offset)));
4161         }
4162       else if (frame.to_allocate)
4163         emit_insn (gen_pro_epilogue_adjust_stack
4164                    (stack_pointer_rtx, stack_pointer_rtx,
4165                     GEN_INT (frame.to_allocate)));
4166
4167       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4168         if (ix86_save_reg (regno, false))
4169           {
4170             if (TARGET_64BIT)
4171               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
4172             else
4173               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
4174           }
4175       if (frame_pointer_needed)
4176         {
4177           /* Leave results in shorter depdendancy chains on CPUs that are
4178              able to grok it fast.  */
4179           if (TARGET_USE_LEAVE)
4180             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
4181           else if (TARGET_64BIT)
4182             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
4183           else
4184             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
4185         }
4186     }
4187
4188   /* Sibcall epilogues don't want a return instruction.  */
4189   if (style == 0)
4190     return;
4191
4192   if (current_function_pops_args && current_function_args_size)
4193     {
4194       rtx popc = GEN_INT (current_function_pops_args);
4195
4196       /* i386 can only pop 64K bytes.  If asked to pop more, pop
4197          return address, do explicit add, and jump indirectly to the
4198          caller.  */
4199
4200       if (current_function_pops_args >= 65536)
4201         {
4202           rtx ecx = gen_rtx_REG (SImode, 2);
4203
4204           /* There are is no "pascal" calling convention in 64bit ABI.  */
4205           if (TARGET_64BIT)
4206             abort();
4207
4208           emit_insn (gen_popsi1 (ecx));
4209           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
4210           emit_jump_insn (gen_return_indirect_internal (ecx));
4211         }
4212       else
4213         emit_jump_insn (gen_return_pop_internal (popc));
4214     }
4215   else
4216     emit_jump_insn (gen_return_internal ());
4217 }
4218 \f
4219 /* Extract the parts of an RTL expression that is a valid memory address
4220    for an instruction.  Return false if the structure of the address is
4221    grossly off.  */
4222
4223 static int
4224 ix86_decompose_address (addr, out)
4225      register rtx addr;
4226      struct ix86_address *out;
4227 {
4228   rtx base = NULL_RTX;
4229   rtx index = NULL_RTX;
4230   rtx disp = NULL_RTX;
4231   HOST_WIDE_INT scale = 1;
4232   rtx scale_rtx = NULL_RTX;
4233
4234   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
4235     base = addr;
4236   else if (GET_CODE (addr) == PLUS)
4237     {
4238       rtx op0 = XEXP (addr, 0);
4239       rtx op1 = XEXP (addr, 1);
4240       enum rtx_code code0 = GET_CODE (op0);
4241       enum rtx_code code1 = GET_CODE (op1);
4242
4243       if (code0 == REG || code0 == SUBREG)
4244         {
4245           if (code1 == REG || code1 == SUBREG)
4246             index = op0, base = op1;    /* index + base */
4247           else
4248             base = op0, disp = op1;     /* base + displacement */
4249         }
4250       else if (code0 == MULT)
4251         {
4252           index = XEXP (op0, 0);
4253           scale_rtx = XEXP (op0, 1);
4254           if (code1 == REG || code1 == SUBREG)
4255             base = op1;                 /* index*scale + base */
4256           else
4257             disp = op1;                 /* index*scale + disp */
4258         }
4259       else if (code0 == PLUS && GET_CODE (XEXP (op0, 0)) == MULT)
4260         {
4261           index = XEXP (XEXP (op0, 0), 0);      /* index*scale + base + disp */
4262           scale_rtx = XEXP (XEXP (op0, 0), 1);
4263           base = XEXP (op0, 1);
4264           disp = op1;
4265         }
4266       else if (code0 == PLUS)
4267         {
4268           index = XEXP (op0, 0);        /* index + base + disp */
4269           base = XEXP (op0, 1);
4270           disp = op1;
4271         }
4272       else
4273         return FALSE;
4274     }
4275   else if (GET_CODE (addr) == MULT)
4276     {
4277       index = XEXP (addr, 0);           /* index*scale */
4278       scale_rtx = XEXP (addr, 1);
4279     }
4280   else if (GET_CODE (addr) == ASHIFT)
4281     {
4282       rtx tmp;
4283
4284       /* We're called for lea too, which implements ashift on occasion.  */
4285       index = XEXP (addr, 0);
4286       tmp = XEXP (addr, 1);
4287       if (GET_CODE (tmp) != CONST_INT)
4288         return FALSE;
4289       scale = INTVAL (tmp);
4290       if ((unsigned HOST_WIDE_INT) scale > 3)
4291         return FALSE;
4292       scale = 1 << scale;
4293     }
4294   else
4295     disp = addr;                        /* displacement */
4296
4297   /* Extract the integral value of scale.  */
4298   if (scale_rtx)
4299     {
4300       if (GET_CODE (scale_rtx) != CONST_INT)
4301         return FALSE;
4302       scale = INTVAL (scale_rtx);
4303     }
4304
4305   /* Allow arg pointer and stack pointer as index if there is not scaling */
4306   if (base && index && scale == 1
4307       && (index == arg_pointer_rtx || index == frame_pointer_rtx
4308           || index == stack_pointer_rtx))
4309     {
4310       rtx tmp = base;
4311       base = index;
4312       index = tmp;
4313     }
4314
4315   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
4316   if ((base == hard_frame_pointer_rtx
4317        || base == frame_pointer_rtx
4318        || base == arg_pointer_rtx) && !disp)
4319     disp = const0_rtx;
4320
4321   /* Special case: on K6, [%esi] makes the instruction vector decoded.
4322      Avoid this by transforming to [%esi+0].  */
4323   if (ix86_cpu == PROCESSOR_K6 && !optimize_size
4324       && base && !index && !disp
4325       && REG_P (base)
4326       && REGNO_REG_CLASS (REGNO (base)) == SIREG)
4327     disp = const0_rtx;
4328
4329   /* Special case: encode reg+reg instead of reg*2.  */
4330   if (!base && index && scale && scale == 2)
4331     base = index, scale = 1;
4332
4333   /* Special case: scaling cannot be encoded without base or displacement.  */
4334   if (!base && !disp && index && scale != 1)
4335     disp = const0_rtx;
4336
4337   out->base = base;
4338   out->index = index;
4339   out->disp = disp;
4340   out->scale = scale;
4341
4342   return TRUE;
4343 }
4344 \f
4345 /* Return cost of the memory address x.
4346    For i386, it is better to use a complex address than let gcc copy
4347    the address into a reg and make a new pseudo.  But not if the address
4348    requires to two regs - that would mean more pseudos with longer
4349    lifetimes.  */
4350 int
4351 ix86_address_cost (x)
4352      rtx x;
4353 {
4354   struct ix86_address parts;
4355   int cost = 1;
4356
4357   if (!ix86_decompose_address (x, &parts))
4358     abort ();
4359
4360   /* More complex memory references are better.  */
4361   if (parts.disp && parts.disp != const0_rtx)
4362     cost--;
4363
4364   /* Attempt to minimize number of registers in the address.  */
4365   if ((parts.base
4366        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
4367       || (parts.index
4368           && (!REG_P (parts.index)
4369               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
4370     cost++;
4371
4372   if (parts.base
4373       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
4374       && parts.index
4375       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
4376       && parts.base != parts.index)
4377     cost++;
4378
4379   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
4380      since it's predecode logic can't detect the length of instructions
4381      and it degenerates to vector decoded.  Increase cost of such
4382      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
4383      to split such addresses or even refuse such addresses at all.
4384
4385      Following addressing modes are affected:
4386       [base+scale*index]
4387       [scale*index+disp]
4388       [base+index]
4389
4390      The first and last case  may be avoidable by explicitly coding the zero in
4391      memory address, but I don't have AMD-K6 machine handy to check this
4392      theory.  */
4393
4394   if (TARGET_K6
4395       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
4396           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
4397           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
4398     cost += 10;
4399
4400   return cost;
4401 }
4402 \f
4403 /* If X is a machine specific address (i.e. a symbol or label being
4404    referenced as a displacement from the GOT implemented using an
4405    UNSPEC), then return the base term.  Otherwise return X.  */
4406
4407 rtx
4408 ix86_find_base_term (x)
4409      rtx x;
4410 {
4411   rtx term;
4412
4413   if (TARGET_64BIT)
4414     {
4415       if (GET_CODE (x) != CONST)
4416         return x;
4417       term = XEXP (x, 0);
4418       if (GET_CODE (term) == PLUS
4419           && (GET_CODE (XEXP (term, 1)) == CONST_INT
4420               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
4421         term = XEXP (term, 0);
4422       if (GET_CODE (term) != UNSPEC
4423           || XVECLEN (term, 0) != 1
4424           || XINT (term, 1) !=  15)
4425         return x;
4426
4427       term = XVECEXP (term, 0, 0);
4428
4429       if (GET_CODE (term) != SYMBOL_REF
4430           && GET_CODE (term) != LABEL_REF)
4431         return x;
4432
4433       return term;
4434     }
4435
4436   if (GET_CODE (x) != PLUS
4437       || XEXP (x, 0) != pic_offset_table_rtx
4438       || GET_CODE (XEXP (x, 1)) != CONST)
4439     return x;
4440
4441   term = XEXP (XEXP (x, 1), 0);
4442
4443   if (GET_CODE (term) == PLUS && GET_CODE (XEXP (term, 1)) == CONST_INT)
4444     term = XEXP (term, 0);
4445
4446   if (GET_CODE (term) != UNSPEC
4447       || XVECLEN (term, 0) != 1
4448       || XINT (term, 1) !=  7)
4449     return x;
4450
4451   term = XVECEXP (term, 0, 0);
4452
4453   if (GET_CODE (term) != SYMBOL_REF
4454       && GET_CODE (term) != LABEL_REF)
4455     return x;
4456
4457   return term;
4458 }
4459 \f
4460 /* Determine if a given CONST RTX is a valid memory displacement
4461    in PIC mode.  */
4462
4463 int
4464 legitimate_pic_address_disp_p (disp)
4465      register rtx disp;
4466 {
4467   /* In 64bit mode we can allow direct addresses of symbols and labels
4468      when they are not dynamic symbols.  */
4469   if (TARGET_64BIT)
4470     {
4471       rtx x = disp;
4472       if (GET_CODE (disp) == CONST)
4473         x = XEXP (disp, 0);
4474       /* ??? Handle PIC code models */
4475       if (GET_CODE (x) == PLUS
4476           && (GET_CODE (XEXP (x, 1)) == CONST_INT
4477               && ix86_cmodel == CM_SMALL_PIC
4478               && INTVAL (XEXP (x, 1)) < 1024*1024*1024
4479               && INTVAL (XEXP (x, 1)) > -1024*1024*1024))
4480         x = XEXP (x, 0);
4481       if (local_symbolic_operand (x, Pmode))
4482         return 1;
4483     }
4484   if (GET_CODE (disp) != CONST)
4485     return 0;
4486   disp = XEXP (disp, 0);
4487
4488   if (TARGET_64BIT)
4489     {
4490       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
4491          of GOT tables.  We should not need these anyway.  */
4492       if (GET_CODE (disp) != UNSPEC
4493           || XVECLEN (disp, 0) != 1
4494           || XINT (disp, 1) != 15)
4495         return 0;
4496
4497       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
4498           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
4499         return 0;
4500       return 1;
4501     }
4502
4503   if (GET_CODE (disp) == PLUS)
4504     {
4505       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
4506         return 0;
4507       disp = XEXP (disp, 0);
4508     }
4509
4510   if (GET_CODE (disp) != UNSPEC
4511       || XVECLEN (disp, 0) != 1)
4512     return 0;
4513
4514   /* Must be @GOT or @GOTOFF.  */
4515   switch (XINT (disp, 1))
4516     {
4517     case 6: /* @GOT */
4518       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
4519
4520     case 7: /* @GOTOFF */
4521       return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
4522     }
4523     
4524   return 0;
4525 }
4526
4527 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
4528    memory address for an instruction.  The MODE argument is the machine mode
4529    for the MEM expression that wants to use this address.
4530
4531    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
4532    convert common non-canonical forms to canonical form so that they will
4533    be recognized.  */
4534
4535 int
4536 legitimate_address_p (mode, addr, strict)
4537      enum machine_mode mode;
4538      register rtx addr;
4539      int strict;
4540 {
4541   struct ix86_address parts;
4542   rtx base, index, disp;
4543   HOST_WIDE_INT scale;
4544   const char *reason = NULL;
4545   rtx reason_rtx = NULL_RTX;
4546
4547   if (TARGET_DEBUG_ADDR)
4548     {
4549       fprintf (stderr,
4550                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
4551                GET_MODE_NAME (mode), strict);
4552       debug_rtx (addr);
4553     }
4554
4555   if (! ix86_decompose_address (addr, &parts))
4556     {
4557       reason = "decomposition failed";
4558       goto report_error;
4559     }
4560
4561   base = parts.base;
4562   index = parts.index;
4563   disp = parts.disp;
4564   scale = parts.scale;
4565
4566   /* Validate base register.
4567
4568      Don't allow SUBREG's here, it can lead to spill failures when the base
4569      is one word out of a two word structure, which is represented internally
4570      as a DImode int.  */
4571
4572   if (base)
4573     {
4574       reason_rtx = base;
4575
4576       if (GET_CODE (base) != REG)
4577         {
4578           reason = "base is not a register";
4579           goto report_error;
4580         }
4581
4582       if (GET_MODE (base) != Pmode)
4583         {
4584           reason = "base is not in Pmode";
4585           goto report_error;
4586         }
4587
4588       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (base))
4589           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (base)))
4590         {
4591           reason = "base is not valid";
4592           goto report_error;
4593         }
4594     }
4595
4596   /* Validate index register.
4597
4598      Don't allow SUBREG's here, it can lead to spill failures when the index
4599      is one word out of a two word structure, which is represented internally
4600      as a DImode int.  */
4601
4602   if (index)
4603     {
4604       reason_rtx = index;
4605
4606       if (GET_CODE (index) != REG)
4607         {
4608           reason = "index is not a register";
4609           goto report_error;
4610         }
4611
4612       if (GET_MODE (index) != Pmode)
4613         {
4614           reason = "index is not in Pmode";
4615           goto report_error;
4616         }
4617
4618       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (index))
4619           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (index)))
4620         {
4621           reason = "index is not valid";
4622           goto report_error;
4623         }
4624     }
4625
4626   /* Validate scale factor.  */
4627   if (scale != 1)
4628     {
4629       reason_rtx = GEN_INT (scale);
4630       if (!index)
4631         {
4632           reason = "scale without index";
4633           goto report_error;
4634         }
4635
4636       if (scale != 2 && scale != 4 && scale != 8)
4637         {
4638           reason = "scale is not a valid multiplier";
4639           goto report_error;
4640         }
4641     }
4642
4643   /* Validate displacement.  */
4644   if (disp)
4645     {
4646       reason_rtx = disp;
4647
4648       if (!CONSTANT_ADDRESS_P (disp))
4649         {
4650           reason = "displacement is not constant";
4651           goto report_error;
4652         }
4653
4654       if (TARGET_64BIT)
4655         {
4656           if (!x86_64_sign_extended_value (disp))
4657             {
4658               reason = "displacement is out of range";
4659               goto report_error;
4660             }
4661         }
4662       else
4663         {
4664           if (GET_CODE (disp) == CONST_DOUBLE)
4665             {
4666               reason = "displacement is a const_double";
4667               goto report_error;
4668             }
4669         }
4670
4671       if (flag_pic && SYMBOLIC_CONST (disp))
4672         {
4673           if (TARGET_64BIT && (index || base))
4674             {
4675               reason = "non-constant pic memory reference";
4676               goto report_error;
4677             }
4678           if (! legitimate_pic_address_disp_p (disp))
4679             {
4680               reason = "displacement is an invalid pic construct";
4681               goto report_error;
4682             }
4683
4684           /* This code used to verify that a symbolic pic displacement
4685              includes the pic_offset_table_rtx register.
4686
4687              While this is good idea, unfortunately these constructs may
4688              be created by "adds using lea" optimization for incorrect
4689              code like:
4690
4691              int a;
4692              int foo(int i)
4693                {
4694                  return *(&a+i);
4695                }
4696
4697              This code is nonsensical, but results in addressing
4698              GOT table with pic_offset_table_rtx base.  We can't
4699              just refuse it easily, since it gets matched by
4700              "addsi3" pattern, that later gets split to lea in the
4701              case output register differs from input.  While this
4702              can be handled by separate addsi pattern for this case
4703              that never results in lea, this seems to be easier and
4704              correct fix for crash to disable this test.  */
4705         }
4706       else if (HALF_PIC_P ())
4707         {
4708           if (! HALF_PIC_ADDRESS_P (disp)
4709               || (base != NULL_RTX || index != NULL_RTX))
4710             {
4711               reason = "displacement is an invalid half-pic reference";
4712               goto report_error;
4713             }
4714         }
4715     }
4716
4717   /* Everything looks valid.  */
4718   if (TARGET_DEBUG_ADDR)
4719     fprintf (stderr, "Success.\n");
4720   return TRUE;
4721
4722 report_error:
4723   if (TARGET_DEBUG_ADDR)
4724     {
4725       fprintf (stderr, "Error: %s\n", reason);
4726       debug_rtx (reason_rtx);
4727     }
4728   return FALSE;
4729 }
4730 \f
4731 /* Return an unique alias set for the GOT.  */
4732
4733 static HOST_WIDE_INT
4734 ix86_GOT_alias_set ()
4735 {
4736     static HOST_WIDE_INT set = -1;
4737     if (set == -1)
4738       set = new_alias_set ();
4739     return set;
4740 }
4741
4742 /* Return a legitimate reference for ORIG (an address) using the
4743    register REG.  If REG is 0, a new pseudo is generated.
4744
4745    There are two types of references that must be handled:
4746
4747    1. Global data references must load the address from the GOT, via
4748       the PIC reg.  An insn is emitted to do this load, and the reg is
4749       returned.
4750
4751    2. Static data references, constant pool addresses, and code labels
4752       compute the address as an offset from the GOT, whose base is in
4753       the PIC reg.  Static data objects have SYMBOL_REF_FLAG set to
4754       differentiate them from global data objects.  The returned
4755       address is the PIC reg + an unspec constant.
4756
4757    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
4758    reg also appears in the address.  */
4759
4760 rtx
4761 legitimize_pic_address (orig, reg)
4762      rtx orig;
4763      rtx reg;
4764 {
4765   rtx addr = orig;
4766   rtx new = orig;
4767   rtx base;
4768
4769   if (local_symbolic_operand (addr, Pmode))
4770     {
4771       /* In 64bit mode we can address such objects directly.  */
4772       if (TARGET_64BIT)
4773         new = addr;
4774       else
4775         {
4776           /* This symbol may be referenced via a displacement from the PIC
4777              base address (@GOTOFF).  */
4778
4779           current_function_uses_pic_offset_table = 1;
4780           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), 7);
4781           new = gen_rtx_CONST (Pmode, new);
4782           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
4783
4784           if (reg != 0)
4785             {
4786               emit_move_insn (reg, new);
4787               new = reg;
4788             }
4789         }
4790     }
4791   else if (GET_CODE (addr) == SYMBOL_REF)
4792     {
4793       if (TARGET_64BIT)
4794         {
4795           current_function_uses_pic_offset_table = 1;
4796           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), 15);
4797           new = gen_rtx_CONST (Pmode, new);
4798           new = gen_rtx_MEM (Pmode, new);
4799           RTX_UNCHANGING_P (new) = 1;
4800           set_mem_alias_set (new, ix86_GOT_alias_set ());
4801
4802           if (reg == 0)
4803             reg = gen_reg_rtx (Pmode);
4804           /* Use directly gen_movsi, otherwise the address is loaded
4805              into register for CSE.  We don't want to CSE this addresses,
4806              instead we CSE addresses from the GOT table, so skip this.  */
4807           emit_insn (gen_movsi (reg, new));
4808           new = reg;
4809         }
4810       else
4811         {
4812           /* This symbol must be referenced via a load from the
4813              Global Offset Table (@GOT).  */
4814
4815           current_function_uses_pic_offset_table = 1;
4816           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), 6);
4817           new = gen_rtx_CONST (Pmode, new);
4818           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
4819           new = gen_rtx_MEM (Pmode, new);
4820           RTX_UNCHANGING_P (new) = 1;
4821           set_mem_alias_set (new, ix86_GOT_alias_set ());
4822
4823           if (reg == 0)
4824             reg = gen_reg_rtx (Pmode);
4825           emit_move_insn (reg, new);
4826           new = reg;
4827         }
4828     }
4829   else
4830     {
4831       if (GET_CODE (addr) == CONST)
4832         {
4833           addr = XEXP (addr, 0);
4834           if (GET_CODE (addr) == UNSPEC)
4835             {
4836               /* Check that the unspec is one of the ones we generate?  */
4837             }
4838           else if (GET_CODE (addr) != PLUS)
4839             abort ();
4840         }
4841       if (GET_CODE (addr) == PLUS)
4842         {
4843           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
4844
4845           /* Check first to see if this is a constant offset from a @GOTOFF
4846              symbol reference.  */
4847           if (local_symbolic_operand (op0, Pmode)
4848               && GET_CODE (op1) == CONST_INT)
4849             {
4850               if (!TARGET_64BIT)
4851                 {
4852                   current_function_uses_pic_offset_table = 1;
4853                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0), 7);
4854                   new = gen_rtx_PLUS (Pmode, new, op1);
4855                   new = gen_rtx_CONST (Pmode, new);
4856                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
4857
4858                   if (reg != 0)
4859                     {
4860                       emit_move_insn (reg, new);
4861                       new = reg;
4862                     }
4863                 }
4864               else
4865                 {
4866                   /* ??? We need to limit offsets here.  */
4867                 }
4868             }
4869           else
4870             {
4871               base = legitimize_pic_address (XEXP (addr, 0), reg);
4872               new  = legitimize_pic_address (XEXP (addr, 1),
4873                                              base == reg ? NULL_RTX : reg);
4874
4875               if (GET_CODE (new) == CONST_INT)
4876                 new = plus_constant (base, INTVAL (new));
4877               else
4878                 {
4879                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
4880                     {
4881                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
4882                       new = XEXP (new, 1);
4883                     }
4884                   new = gen_rtx_PLUS (Pmode, base, new);
4885                 }
4886             }
4887         }
4888     }
4889   return new;
4890 }
4891 \f
4892 /* Try machine-dependent ways of modifying an illegitimate address
4893    to be legitimate.  If we find one, return the new, valid address.
4894    This macro is used in only one place: `memory_address' in explow.c.
4895
4896    OLDX is the address as it was before break_out_memory_refs was called.
4897    In some cases it is useful to look at this to decide what needs to be done.
4898
4899    MODE and WIN are passed so that this macro can use
4900    GO_IF_LEGITIMATE_ADDRESS.
4901
4902    It is always safe for this macro to do nothing.  It exists to recognize
4903    opportunities to optimize the output.
4904
4905    For the 80386, we handle X+REG by loading X into a register R and
4906    using R+REG.  R will go in a general reg and indexing will be used.
4907    However, if REG is a broken-out memory address or multiplication,
4908    nothing needs to be done because REG can certainly go in a general reg.
4909
4910    When -fpic is used, special handling is needed for symbolic references.
4911    See comments by legitimize_pic_address in i386.c for details.  */
4912
4913 rtx
4914 legitimize_address (x, oldx, mode)
4915      register rtx x;
4916      register rtx oldx ATTRIBUTE_UNUSED;
4917      enum machine_mode mode;
4918 {
4919   int changed = 0;
4920   unsigned log;
4921
4922   if (TARGET_DEBUG_ADDR)
4923     {
4924       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
4925                GET_MODE_NAME (mode));
4926       debug_rtx (x);
4927     }
4928
4929   if (flag_pic && SYMBOLIC_CONST (x))
4930     return legitimize_pic_address (x, 0);
4931
4932   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
4933   if (GET_CODE (x) == ASHIFT
4934       && GET_CODE (XEXP (x, 1)) == CONST_INT
4935       && (log = (unsigned)exact_log2 (INTVAL (XEXP (x, 1)))) < 4)
4936     {
4937       changed = 1;
4938       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
4939                         GEN_INT (1 << log));
4940     }
4941
4942   if (GET_CODE (x) == PLUS)
4943     {
4944       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
4945
4946       if (GET_CODE (XEXP (x, 0)) == ASHIFT
4947           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4948           && (log = (unsigned)exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) < 4)
4949         {
4950           changed = 1;
4951           XEXP (x, 0) = gen_rtx_MULT (Pmode,
4952                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
4953                                       GEN_INT (1 << log));
4954         }
4955
4956       if (GET_CODE (XEXP (x, 1)) == ASHIFT
4957           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4958           && (log = (unsigned)exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1)))) < 4)
4959         {
4960           changed = 1;
4961           XEXP (x, 1) = gen_rtx_MULT (Pmode,
4962                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
4963                                       GEN_INT (1 << log));
4964         }
4965
4966       /* Put multiply first if it isn't already.  */
4967       if (GET_CODE (XEXP (x, 1)) == MULT)
4968         {
4969           rtx tmp = XEXP (x, 0);
4970           XEXP (x, 0) = XEXP (x, 1);
4971           XEXP (x, 1) = tmp;
4972           changed = 1;
4973         }
4974
4975       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
4976          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
4977          created by virtual register instantiation, register elimination, and
4978          similar optimizations.  */
4979       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
4980         {
4981           changed = 1;
4982           x = gen_rtx_PLUS (Pmode,
4983                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
4984                                           XEXP (XEXP (x, 1), 0)),
4985                             XEXP (XEXP (x, 1), 1));
4986         }
4987
4988       /* Canonicalize
4989          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
4990          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
4991       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
4992                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4993                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
4994                && CONSTANT_P (XEXP (x, 1)))
4995         {
4996           rtx constant;
4997           rtx other = NULL_RTX;
4998
4999           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5000             {
5001               constant = XEXP (x, 1);
5002               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
5003             }
5004           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
5005             {
5006               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
5007               other = XEXP (x, 1);
5008             }
5009           else
5010             constant = 0;
5011
5012           if (constant)
5013             {
5014               changed = 1;
5015               x = gen_rtx_PLUS (Pmode,
5016                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
5017                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
5018                                 plus_constant (other, INTVAL (constant)));
5019             }
5020         }
5021
5022       if (changed && legitimate_address_p (mode, x, FALSE))
5023         return x;
5024
5025       if (GET_CODE (XEXP (x, 0)) == MULT)
5026         {
5027           changed = 1;
5028           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
5029         }
5030
5031       if (GET_CODE (XEXP (x, 1)) == MULT)
5032         {
5033           changed = 1;
5034           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
5035         }
5036
5037       if (changed
5038           && GET_CODE (XEXP (x, 1)) == REG
5039           && GET_CODE (XEXP (x, 0)) == REG)
5040         return x;
5041
5042       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
5043         {
5044           changed = 1;
5045           x = legitimize_pic_address (x, 0);
5046         }
5047
5048       if (changed && legitimate_address_p (mode, x, FALSE))
5049         return x;
5050
5051       if (GET_CODE (XEXP (x, 0)) == REG)
5052         {
5053           register rtx temp = gen_reg_rtx (Pmode);
5054           register rtx val  = force_operand (XEXP (x, 1), temp);
5055           if (val != temp)
5056             emit_move_insn (temp, val);
5057
5058           XEXP (x, 1) = temp;
5059           return x;
5060         }
5061
5062       else if (GET_CODE (XEXP (x, 1)) == REG)
5063         {
5064           register rtx temp = gen_reg_rtx (Pmode);
5065           register rtx val  = force_operand (XEXP (x, 0), temp);
5066           if (val != temp)
5067             emit_move_insn (temp, val);
5068
5069           XEXP (x, 0) = temp;
5070           return x;
5071         }
5072     }
5073
5074   return x;
5075 }
5076 \f
5077 /* Print an integer constant expression in assembler syntax.  Addition
5078    and subtraction are the only arithmetic that may appear in these
5079    expressions.  FILE is the stdio stream to write to, X is the rtx, and
5080    CODE is the operand print code from the output string.  */
5081
5082 static void
5083 output_pic_addr_const (file, x, code)
5084      FILE *file;
5085      rtx x;
5086      int code;
5087 {
5088   char buf[256];
5089
5090   switch (GET_CODE (x))
5091     {
5092     case PC:
5093       if (flag_pic)
5094         putc ('.', file);
5095       else
5096         abort ();
5097       break;
5098
5099     case SYMBOL_REF:
5100       assemble_name (file, XSTR (x, 0));
5101       if (code == 'P' && ! SYMBOL_REF_FLAG (x))
5102         fputs ("@PLT", file);
5103       break;
5104
5105     case LABEL_REF:
5106       x = XEXP (x, 0);
5107       /* FALLTHRU */
5108     case CODE_LABEL:
5109       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
5110       assemble_name (asm_out_file, buf);
5111       break;
5112
5113     case CONST_INT:
5114       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
5115       break;
5116
5117     case CONST:
5118       /* This used to output parentheses around the expression,
5119          but that does not work on the 386 (either ATT or BSD assembler).  */
5120       output_pic_addr_const (file, XEXP (x, 0), code);
5121       break;
5122
5123     case CONST_DOUBLE:
5124       if (GET_MODE (x) == VOIDmode)
5125         {
5126           /* We can use %d if the number is <32 bits and positive.  */
5127           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
5128             fprintf (file, "0x%lx%08lx",
5129                      (unsigned long) CONST_DOUBLE_HIGH (x),
5130                      (unsigned long) CONST_DOUBLE_LOW (x));
5131           else
5132             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
5133         }
5134       else
5135         /* We can't handle floating point constants;
5136            PRINT_OPERAND must handle them.  */
5137         output_operand_lossage ("floating constant misused");
5138       break;
5139
5140     case PLUS:
5141       /* Some assemblers need integer constants to appear first.  */
5142       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5143         {
5144           output_pic_addr_const (file, XEXP (x, 0), code);
5145           putc ('+', file);
5146           output_pic_addr_const (file, XEXP (x, 1), code);
5147         }
5148       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5149         {
5150           output_pic_addr_const (file, XEXP (x, 1), code);
5151           putc ('+', file);
5152           output_pic_addr_const (file, XEXP (x, 0), code);
5153         }
5154       else
5155         abort ();
5156       break;
5157
5158     case MINUS:
5159       putc (ASSEMBLER_DIALECT ? '(' : '[', file);
5160       output_pic_addr_const (file, XEXP (x, 0), code);
5161       putc ('-', file);
5162       output_pic_addr_const (file, XEXP (x, 1), code);
5163       putc (ASSEMBLER_DIALECT ? ')' : ']', file);
5164       break;
5165
5166      case UNSPEC:
5167        if (XVECLEN (x, 0) != 1)
5168         abort ();
5169        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
5170        switch (XINT (x, 1))
5171         {
5172         case 6:
5173           fputs ("@GOT", file);
5174           break;
5175         case 7:
5176           fputs ("@GOTOFF", file);
5177           break;
5178         case 8:
5179           fputs ("@PLT", file);
5180           break;
5181         case 15:
5182           fputs ("@GOTPCREL(%RIP)", file);
5183           break;
5184         default:
5185           output_operand_lossage ("invalid UNSPEC as operand");
5186           break;
5187         }
5188        break;
5189
5190     default:
5191       output_operand_lossage ("invalid expression as operand");
5192     }
5193 }
5194
5195 /* This is called from dwarfout.c via ASM_OUTPUT_DWARF_ADDR_CONST.
5196    We need to handle our special PIC relocations.  */
5197
5198 void
5199 i386_dwarf_output_addr_const (file, x)
5200      FILE *file;
5201      rtx x;
5202 {
5203 #ifdef ASM_QUAD
5204   fprintf (file, "%s", TARGET_64BIT ? ASM_QUAD : INT_ASM_OP);
5205 #else
5206   if (TARGET_64BIT)
5207     abort ();
5208   fprintf (file, "%s", INT_ASM_OP);
5209 #endif
5210   if (flag_pic)
5211     output_pic_addr_const (file, x, '\0');
5212   else
5213     output_addr_const (file, x);
5214   fputc ('\n', file);
5215 }
5216
5217 /* In the name of slightly smaller debug output, and to cater to
5218    general assembler losage, recognize PIC+GOTOFF and turn it back
5219    into a direct symbol reference.  */
5220
5221 rtx
5222 i386_simplify_dwarf_addr (orig_x)
5223      rtx orig_x;
5224 {
5225   rtx x = orig_x;
5226
5227   if (TARGET_64BIT)
5228     {
5229       if (GET_CODE (x) != CONST
5230           || GET_CODE (XEXP (x, 0)) != UNSPEC
5231           || XINT (XEXP (x, 0), 1) != 15)
5232         return orig_x;
5233       return XVECEXP (XEXP (x, 0), 0, 0);
5234     }
5235
5236   if (GET_CODE (x) != PLUS
5237       || GET_CODE (XEXP (x, 0)) != REG
5238       || GET_CODE (XEXP (x, 1)) != CONST)
5239     return orig_x;
5240
5241   x = XEXP (XEXP (x, 1), 0);
5242   if (GET_CODE (x) == UNSPEC
5243       && (XINT (x, 1) == 6
5244           || XINT (x, 1) == 7))
5245     return XVECEXP (x, 0, 0);
5246
5247   if (GET_CODE (x) == PLUS
5248       && GET_CODE (XEXP (x, 0)) == UNSPEC
5249       && GET_CODE (XEXP (x, 1)) == CONST_INT
5250       && (XINT (XEXP (x, 0), 1) == 6
5251           || XINT (XEXP (x, 0), 1) == 7))
5252     return gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
5253
5254   return orig_x;
5255 }
5256 \f
5257 static void
5258 put_condition_code (code, mode, reverse, fp, file)
5259      enum rtx_code code;
5260      enum machine_mode mode;
5261      int reverse, fp;
5262      FILE *file;
5263 {
5264   const char *suffix;
5265
5266   if (mode == CCFPmode || mode == CCFPUmode)
5267     {
5268       enum rtx_code second_code, bypass_code;
5269       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
5270       if (bypass_code != NIL || second_code != NIL)
5271         abort();
5272       code = ix86_fp_compare_code_to_integer (code);
5273       mode = CCmode;
5274     }
5275   if (reverse)
5276     code = reverse_condition (code);
5277
5278   switch (code)
5279     {
5280     case EQ:
5281       suffix = "e";
5282       break;
5283     case NE:
5284       suffix = "ne";
5285       break;
5286     case GT:
5287       if (mode != CCmode && mode != CCNOmode && mode != CCGCmode)
5288         abort ();
5289       suffix = "g";
5290       break;
5291     case GTU:
5292       /* ??? Use "nbe" instead of "a" for fcmov losage on some assemblers.
5293          Those same assemblers have the same but opposite losage on cmov.  */
5294       if (mode != CCmode)
5295         abort ();
5296       suffix = fp ? "nbe" : "a";
5297       break;
5298     case LT:
5299       if (mode == CCNOmode || mode == CCGOCmode)
5300         suffix = "s";
5301       else if (mode == CCmode || mode == CCGCmode)
5302         suffix = "l";
5303       else
5304         abort ();
5305       break;
5306     case LTU:
5307       if (mode != CCmode)
5308         abort ();
5309       suffix = "b";
5310       break;
5311     case GE:
5312       if (mode == CCNOmode || mode == CCGOCmode)
5313         suffix = "ns";
5314       else if (mode == CCmode || mode == CCGCmode)
5315         suffix = "ge";
5316       else
5317         abort ();
5318       break;
5319     case GEU:
5320       /* ??? As above.  */
5321       if (mode != CCmode)
5322         abort ();
5323       suffix = fp ? "nb" : "ae";
5324       break;
5325     case LE:
5326       if (mode != CCmode && mode != CCGCmode && mode != CCNOmode)
5327         abort ();
5328       suffix = "le";
5329       break;
5330     case LEU:
5331       if (mode != CCmode)
5332         abort ();
5333       suffix = "be";
5334       break;
5335     case UNORDERED:
5336       suffix = fp ? "u" : "p";
5337       break;
5338     case ORDERED:
5339       suffix = fp ? "nu" : "np";
5340       break;
5341     default:
5342       abort ();
5343     }
5344   fputs (suffix, file);
5345 }
5346
5347 void
5348 print_reg (x, code, file)
5349      rtx x;
5350      int code;
5351      FILE *file;
5352 {
5353   if (REGNO (x) == ARG_POINTER_REGNUM
5354       || REGNO (x) == FRAME_POINTER_REGNUM
5355       || REGNO (x) == FLAGS_REG
5356       || REGNO (x) == FPSR_REG)
5357     abort ();
5358
5359   if (ASSEMBLER_DIALECT == 0 || USER_LABEL_PREFIX[0] == 0)
5360     putc ('%', file);
5361
5362   if (code == 'w' || MMX_REG_P (x))
5363     code = 2;
5364   else if (code == 'b')
5365     code = 1;
5366   else if (code == 'k')
5367     code = 4;
5368   else if (code == 'q')
5369     code = 8;
5370   else if (code == 'y')
5371     code = 3;
5372   else if (code == 'h')
5373     code = 0;
5374   else
5375     code = GET_MODE_SIZE (GET_MODE (x));
5376
5377   /* Irritatingly, AMD extended registers use different naming convention
5378      from the normal registers.  */
5379   if (REX_INT_REG_P (x))
5380     {
5381       if (!TARGET_64BIT)
5382         abort ();
5383       switch (code)
5384         {
5385           case 0:
5386             error ("Extended registers have no high halves\n");
5387             break;
5388           case 1:
5389             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
5390             break;
5391           case 2:
5392             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
5393             break;
5394           case 4:
5395             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
5396             break;
5397           case 8:
5398             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
5399             break;
5400           default:
5401             error ("Unsupported operand size for extended register.\n");
5402             break;
5403         }
5404       return;
5405     }
5406   switch (code)
5407     {
5408     case 3:
5409       if (STACK_TOP_P (x))
5410         {
5411           fputs ("st(0)", file);
5412           break;
5413         }
5414       /* FALLTHRU */
5415     case 8:
5416     case 4:
5417     case 12:
5418       if (! ANY_FP_REG_P (x))
5419         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
5420       /* FALLTHRU */
5421     case 16:
5422     case 2:
5423       fputs (hi_reg_name[REGNO (x)], file);
5424       break;
5425     case 1:
5426       fputs (qi_reg_name[REGNO (x)], file);
5427       break;
5428     case 0:
5429       fputs (qi_high_reg_name[REGNO (x)], file);
5430       break;
5431     default:
5432       abort ();
5433     }
5434 }
5435
5436 /* Meaning of CODE:
5437    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
5438    C -- print opcode suffix for set/cmov insn.
5439    c -- like C, but print reversed condition
5440    F,f -- likewise, but for floating-point.
5441    R -- print the prefix for register names.
5442    z -- print the opcode suffix for the size of the current operand.
5443    * -- print a star (in certain assembler syntax)
5444    A -- print an absolute memory reference.
5445    w -- print the operand as if it's a "word" (HImode) even if it isn't.
5446    s -- print a shift double count, followed by the assemblers argument
5447         delimiter.
5448    b -- print the QImode name of the register for the indicated operand.
5449         %b0 would print %al if operands[0] is reg 0.
5450    w --  likewise, print the HImode name of the register.
5451    k --  likewise, print the SImode name of the register.
5452    q --  likewise, print the DImode name of the register.
5453    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
5454    y -- print "st(0)" instead of "st" as a register.
5455    D -- print condition for SSE cmp instruction.
5456    P -- if PIC, print an @PLT suffix.
5457    X -- don't print any sort of PIC '@' suffix for a symbol.
5458  */
5459
5460 void
5461 print_operand (file, x, code)
5462      FILE *file;
5463      rtx x;
5464      int code;
5465 {
5466   if (code)
5467     {
5468       switch (code)
5469         {
5470         case '*':
5471           if (ASSEMBLER_DIALECT == 0)
5472             putc ('*', file);
5473           return;
5474
5475         case 'A':
5476           if (ASSEMBLER_DIALECT == 0)
5477             putc ('*', file);
5478           else if (ASSEMBLER_DIALECT == 1)
5479             {
5480               /* Intel syntax. For absolute addresses, registers should not
5481                  be surrounded by braces.  */
5482               if (GET_CODE (x) != REG)
5483                 {
5484                   putc ('[', file);
5485                   PRINT_OPERAND (file, x, 0);
5486                   putc (']', file);
5487                   return;
5488                 }
5489             }
5490
5491           PRINT_OPERAND (file, x, 0);
5492           return;
5493
5494
5495         case 'L':
5496           if (ASSEMBLER_DIALECT == 0)
5497             putc ('l', file);
5498           return;
5499
5500         case 'W':
5501           if (ASSEMBLER_DIALECT == 0)
5502             putc ('w', file);
5503           return;
5504
5505         case 'B':
5506           if (ASSEMBLER_DIALECT == 0)
5507             putc ('b', file);
5508           return;
5509
5510         case 'Q':
5511           if (ASSEMBLER_DIALECT == 0)
5512             putc ('l', file);
5513           return;
5514
5515         case 'S':
5516           if (ASSEMBLER_DIALECT == 0)
5517             putc ('s', file);
5518           return;
5519
5520         case 'T':
5521           if (ASSEMBLER_DIALECT == 0)
5522             putc ('t', file);
5523           return;
5524
5525         case 'z':
5526           /* 387 opcodes don't get size suffixes if the operands are
5527              registers.  */
5528
5529           if (STACK_REG_P (x))
5530             return;
5531
5532           /* this is the size of op from size of operand */
5533           switch (GET_MODE_SIZE (GET_MODE (x)))
5534             {
5535             case 2:
5536 #ifdef HAVE_GAS_FILDS_FISTS
5537               putc ('s', file);
5538 #endif
5539               return;
5540
5541             case 4:
5542               if (GET_MODE (x) == SFmode)
5543                 {
5544                   putc ('s', file);
5545                   return;
5546                 }
5547               else
5548                 putc ('l', file);
5549               return;
5550
5551             case 12:
5552             case 16:
5553               putc ('t', file);
5554               return;
5555
5556             case 8:
5557               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
5558                 {
5559 #ifdef GAS_MNEMONICS
5560                   putc ('q', file);
5561 #else
5562                   putc ('l', file);
5563                   putc ('l', file);
5564 #endif
5565                 }
5566               else
5567                 putc ('l', file);
5568               return;
5569
5570             default:
5571               abort ();
5572             }
5573
5574         case 'b':
5575         case 'w':
5576         case 'k':
5577         case 'q':
5578         case 'h':
5579         case 'y':
5580         case 'X':
5581         case 'P':
5582           break;
5583
5584         case 's':
5585           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
5586             {
5587               PRINT_OPERAND (file, x, 0);
5588               putc (',', file);
5589             }
5590           return;
5591
5592         case 'D':
5593           /* Little bit of braindamage here.  The SSE compare instructions
5594              does use completely different names for the comparisons that the
5595              fp conditional moves.  */
5596           switch (GET_CODE (x))
5597             {
5598             case EQ:
5599             case UNEQ:
5600               fputs ("eq", file);
5601               break;
5602             case LT:
5603             case UNLT:
5604               fputs ("lt", file);
5605               break;
5606             case LE:
5607             case UNLE:
5608               fputs ("le", file);
5609               break;
5610             case UNORDERED:
5611               fputs ("unord", file);
5612               break;
5613             case NE:
5614             case LTGT:
5615               fputs ("neq", file);
5616               break;
5617             case UNGE:
5618             case GE:
5619               fputs ("nlt", file);
5620               break;
5621             case UNGT:
5622             case GT:
5623               fputs ("nle", file);
5624               break;
5625             case ORDERED:
5626               fputs ("ord", file);
5627               break;
5628             default:
5629               abort ();
5630               break;
5631             }
5632           return;
5633         case 'C':
5634           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
5635           return;
5636         case 'F':
5637           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
5638           return;
5639
5640           /* Like above, but reverse condition */
5641         case 'c':
5642           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
5643           return;
5644         case 'f':
5645           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
5646           return;
5647         case '+':
5648           {
5649             rtx x;
5650
5651             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
5652               return;
5653
5654             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
5655             if (x)
5656               {
5657                 int pred_val = INTVAL (XEXP (x, 0));
5658
5659                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
5660                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
5661                   {
5662                     int taken = pred_val > REG_BR_PROB_BASE / 2;
5663                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
5664
5665                     /* Emit hints only in the case default branch prediction
5666                        heruistics would fail.  */
5667                     if (taken != cputaken)
5668                       {
5669                         /* We use 3e (DS) prefix for taken branches and
5670                            2e (CS) prefix for not taken branches.  */
5671                         if (taken)
5672                           fputs ("ds ; ", file);
5673                         else
5674                           fputs ("cs ; ", file);
5675                       }
5676                   }
5677               }
5678             return;
5679           }
5680         default:
5681           {
5682             char str[50];
5683             sprintf (str, "invalid operand code `%c'", code);
5684             output_operand_lossage (str);
5685           }
5686         }
5687     }
5688
5689   if (GET_CODE (x) == REG)
5690     {
5691       PRINT_REG (x, code, file);
5692     }
5693
5694   else if (GET_CODE (x) == MEM)
5695     {
5696       /* No `byte ptr' prefix for call instructions.  */
5697       if (ASSEMBLER_DIALECT != 0 && code != 'X' && code != 'P')
5698         {
5699           const char * size;
5700           switch (GET_MODE_SIZE (GET_MODE (x)))
5701             {
5702             case 1: size = "BYTE"; break;
5703             case 2: size = "WORD"; break;
5704             case 4: size = "DWORD"; break;
5705             case 8: size = "QWORD"; break;
5706             case 12: size = "XWORD"; break;
5707             case 16: size = "XMMWORD"; break;
5708             default:
5709               abort ();
5710             }
5711
5712           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
5713           if (code == 'b')
5714             size = "BYTE";
5715           else if (code == 'w')
5716             size = "WORD";
5717           else if (code == 'k')
5718             size = "DWORD";
5719
5720           fputs (size, file);
5721           fputs (" PTR ", file);
5722         }
5723
5724       x = XEXP (x, 0);
5725       if (flag_pic && CONSTANT_ADDRESS_P (x))
5726         output_pic_addr_const (file, x, code);
5727       /* Avoid (%rip) for call operands.  */
5728       else if (CONSTANT_ADDRESS_P (x) && code =='P'
5729                && GET_CODE (x) != CONST_INT)
5730         output_addr_const (file, x);
5731       else
5732         output_address (x);
5733     }
5734
5735   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
5736     {
5737       REAL_VALUE_TYPE r;
5738       long l;
5739
5740       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5741       REAL_VALUE_TO_TARGET_SINGLE (r, l);
5742
5743       if (ASSEMBLER_DIALECT == 0)
5744         putc ('$', file);
5745       fprintf (file, "0x%lx", l);
5746     }
5747
5748  /* These float cases don't actually occur as immediate operands.  */
5749  else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
5750     {
5751       REAL_VALUE_TYPE r;
5752       char dstr[30];
5753
5754       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5755       REAL_VALUE_TO_DECIMAL (r, "%.22e", dstr);
5756       fprintf (file, "%s", dstr);
5757     }
5758
5759   else if (GET_CODE (x) == CONST_DOUBLE
5760            && (GET_MODE (x) == XFmode || GET_MODE (x) == TFmode))
5761     {
5762       REAL_VALUE_TYPE r;
5763       char dstr[30];
5764
5765       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5766       REAL_VALUE_TO_DECIMAL (r, "%.22e", dstr);
5767       fprintf (file, "%s", dstr);
5768     }
5769   else
5770     {
5771       if (code != 'P')
5772         {
5773           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
5774             {
5775               if (ASSEMBLER_DIALECT == 0)
5776                 putc ('$', file);
5777             }
5778           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
5779                    || GET_CODE (x) == LABEL_REF)
5780             {
5781               if (ASSEMBLER_DIALECT == 0)
5782                 putc ('$', file);
5783               else
5784                 fputs ("OFFSET FLAT:", file);
5785             }
5786         }
5787       if (GET_CODE (x) == CONST_INT)
5788         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
5789       else if (flag_pic)
5790         output_pic_addr_const (file, x, code);
5791       else
5792         output_addr_const (file, x);
5793     }
5794 }
5795 \f
5796 /* Print a memory operand whose address is ADDR.  */
5797
5798 void
5799 print_operand_address (file, addr)
5800      FILE *file;
5801      register rtx addr;
5802 {
5803   struct ix86_address parts;
5804   rtx base, index, disp;
5805   int scale;
5806
5807   if (! ix86_decompose_address (addr, &parts))
5808     abort ();
5809
5810   base = parts.base;
5811   index = parts.index;
5812   disp = parts.disp;
5813   scale = parts.scale;
5814
5815   if (!base && !index)
5816     {
5817       /* Displacement only requires special attention.  */
5818
5819       if (GET_CODE (disp) == CONST_INT)
5820         {
5821           if (ASSEMBLER_DIALECT != 0)
5822             {
5823               if (USER_LABEL_PREFIX[0] == 0)
5824                 putc ('%', file);
5825               fputs ("ds:", file);
5826             }
5827           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
5828         }
5829       else if (flag_pic)
5830         output_pic_addr_const (file, addr, 0);
5831       else
5832         output_addr_const (file, addr);
5833
5834       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
5835       if (GET_CODE (disp) != CONST_INT && TARGET_64BIT)
5836         fputs ("(%rip)", file);
5837     }
5838   else
5839     {
5840       if (ASSEMBLER_DIALECT == 0)
5841         {
5842           if (disp)
5843             {
5844               if (flag_pic)
5845                 output_pic_addr_const (file, disp, 0);
5846               else if (GET_CODE (disp) == LABEL_REF)
5847                 output_asm_label (disp);
5848               else
5849                 output_addr_const (file, disp);
5850             }
5851
5852           putc ('(', file);
5853           if (base)
5854             PRINT_REG (base, 0, file);
5855           if (index)
5856             {
5857               putc (',', file);
5858               PRINT_REG (index, 0, file);
5859               if (scale != 1)
5860                 fprintf (file, ",%d", scale);
5861             }
5862           putc (')', file);
5863         }
5864       else
5865         {
5866           rtx offset = NULL_RTX;
5867
5868           if (disp)
5869             {
5870               /* Pull out the offset of a symbol; print any symbol itself.  */
5871               if (GET_CODE (disp) == CONST
5872                   && GET_CODE (XEXP (disp, 0)) == PLUS
5873                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
5874                 {
5875                   offset = XEXP (XEXP (disp, 0), 1);
5876                   disp = gen_rtx_CONST (VOIDmode,
5877                                         XEXP (XEXP (disp, 0), 0));
5878                 }
5879
5880               if (flag_pic)
5881                 output_pic_addr_const (file, disp, 0);
5882               else if (GET_CODE (disp) == LABEL_REF)
5883                 output_asm_label (disp);
5884               else if (GET_CODE (disp) == CONST_INT)
5885                 offset = disp;
5886               else
5887                 output_addr_const (file, disp);
5888             }
5889
5890           putc ('[', file);
5891           if (base)
5892             {
5893               PRINT_REG (base, 0, file);
5894               if (offset)
5895                 {
5896                   if (INTVAL (offset) >= 0)
5897                     putc ('+', file);
5898                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
5899                 }
5900             }
5901           else if (offset)
5902             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
5903           else
5904             putc ('0', file);
5905
5906           if (index)
5907             {
5908               putc ('+', file);
5909               PRINT_REG (index, 0, file);
5910               if (scale != 1)
5911                 fprintf (file, "*%d", scale);
5912             }
5913           putc (']', file);
5914         }
5915     }
5916 }
5917 \f
5918 /* Split one or more DImode RTL references into pairs of SImode
5919    references.  The RTL can be REG, offsettable MEM, integer constant, or
5920    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
5921    split and "num" is its length.  lo_half and hi_half are output arrays
5922    that parallel "operands".  */
5923
5924 void
5925 split_di (operands, num, lo_half, hi_half)
5926      rtx operands[];
5927      int num;
5928      rtx lo_half[], hi_half[];
5929 {
5930   while (num--)
5931     {
5932       rtx op = operands[num];
5933
5934       /* simplify_subreg refuse to split volatile memory addresses,
5935          but we still have to handle it.  */
5936       if (GET_CODE (op) == MEM)
5937         {
5938           lo_half[num] = adjust_address (op, SImode, 0);
5939           hi_half[num] = adjust_address (op, SImode, 4);
5940         }
5941       else
5942         {
5943           lo_half[num] = simplify_gen_subreg (SImode, op,
5944                                               GET_MODE (op) == VOIDmode
5945                                               ? DImode : GET_MODE (op), 0);
5946           hi_half[num] = simplify_gen_subreg (SImode, op,
5947                                               GET_MODE (op) == VOIDmode
5948                                               ? DImode : GET_MODE (op), 4);
5949         }
5950     }
5951 }
5952 /* Split one or more TImode RTL references into pairs of SImode
5953    references.  The RTL can be REG, offsettable MEM, integer constant, or
5954    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
5955    split and "num" is its length.  lo_half and hi_half are output arrays
5956    that parallel "operands".  */
5957
5958 void
5959 split_ti (operands, num, lo_half, hi_half)
5960      rtx operands[];
5961      int num;
5962      rtx lo_half[], hi_half[];
5963 {
5964   while (num--)
5965     {
5966       rtx op = operands[num];
5967
5968       /* simplify_subreg refuse to split volatile memory addresses, but we
5969          still have to handle it.  */
5970       if (GET_CODE (op) == MEM)
5971         {
5972           lo_half[num] = adjust_address (op, DImode, 0);
5973           hi_half[num] = adjust_address (op, DImode, 8);
5974         }
5975       else
5976         {
5977           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
5978           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
5979         }
5980     }
5981 }
5982 \f
5983 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
5984    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
5985    is the expression of the binary operation.  The output may either be
5986    emitted here, or returned to the caller, like all output_* functions.
5987
5988    There is no guarantee that the operands are the same mode, as they
5989    might be within FLOAT or FLOAT_EXTEND expressions.  */
5990
5991 #ifndef SYSV386_COMPAT
5992 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
5993    wants to fix the assemblers because that causes incompatibility
5994    with gcc.  No-one wants to fix gcc because that causes
5995    incompatibility with assemblers...  You can use the option of
5996    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
5997 #define SYSV386_COMPAT 1
5998 #endif
5999
6000 const char *
6001 output_387_binary_op (insn, operands)
6002      rtx insn;
6003      rtx *operands;
6004 {
6005   static char buf[30];
6006   const char *p;
6007   const char *ssep;
6008   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]) | SSE_REG_P (operands[2]);
6009
6010 #ifdef ENABLE_CHECKING
6011   /* Even if we do not want to check the inputs, this documents input
6012      constraints.  Which helps in understanding the following code.  */
6013   if (STACK_REG_P (operands[0])
6014       && ((REG_P (operands[1])
6015            && REGNO (operands[0]) == REGNO (operands[1])
6016            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
6017           || (REG_P (operands[2])
6018               && REGNO (operands[0]) == REGNO (operands[2])
6019               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
6020       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
6021     ; /* ok */
6022   else if (!is_sse)
6023     abort ();
6024 #endif
6025
6026   switch (GET_CODE (operands[3]))
6027     {
6028     case PLUS:
6029       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6030           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6031         p = "fiadd";
6032       else
6033         p = "fadd";
6034       ssep = "add";
6035       break;
6036
6037     case MINUS:
6038       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6039           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6040         p = "fisub";
6041       else
6042         p = "fsub";
6043       ssep = "sub";
6044       break;
6045
6046     case MULT:
6047       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6048           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6049         p = "fimul";
6050       else
6051         p = "fmul";
6052       ssep = "mul";
6053       break;
6054
6055     case DIV:
6056       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
6057           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
6058         p = "fidiv";
6059       else
6060         p = "fdiv";
6061       ssep = "div";
6062       break;
6063
6064     default:
6065       abort ();
6066     }
6067
6068   if (is_sse)
6069    {
6070       strcpy (buf, ssep);
6071       if (GET_MODE (operands[0]) == SFmode)
6072         strcat (buf, "ss\t{%2, %0|%0, %2}");
6073       else
6074         strcat (buf, "sd\t{%2, %0|%0, %2}");
6075       return buf;
6076    }
6077   strcpy (buf, p);
6078
6079   switch (GET_CODE (operands[3]))
6080     {
6081     case MULT:
6082     case PLUS:
6083       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
6084         {
6085           rtx temp = operands[2];
6086           operands[2] = operands[1];
6087           operands[1] = temp;
6088         }
6089
6090       /* know operands[0] == operands[1].  */
6091
6092       if (GET_CODE (operands[2]) == MEM)
6093         {
6094           p = "%z2\t%2";
6095           break;
6096         }
6097
6098       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
6099         {
6100           if (STACK_TOP_P (operands[0]))
6101             /* How is it that we are storing to a dead operand[2]?
6102                Well, presumably operands[1] is dead too.  We can't
6103                store the result to st(0) as st(0) gets popped on this
6104                instruction.  Instead store to operands[2] (which I
6105                think has to be st(1)).  st(1) will be popped later.
6106                gcc <= 2.8.1 didn't have this check and generated
6107                assembly code that the Unixware assembler rejected.  */
6108             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
6109           else
6110             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
6111           break;
6112         }
6113
6114       if (STACK_TOP_P (operands[0]))
6115         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
6116       else
6117         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
6118       break;
6119
6120     case MINUS:
6121     case DIV:
6122       if (GET_CODE (operands[1]) == MEM)
6123         {
6124           p = "r%z1\t%1";
6125           break;
6126         }
6127
6128       if (GET_CODE (operands[2]) == MEM)
6129         {
6130           p = "%z2\t%2";
6131           break;
6132         }
6133
6134       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
6135         {
6136 #if SYSV386_COMPAT
6137           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
6138              derived assemblers, confusingly reverse the direction of
6139              the operation for fsub{r} and fdiv{r} when the
6140              destination register is not st(0).  The Intel assembler
6141              doesn't have this brain damage.  Read !SYSV386_COMPAT to
6142              figure out what the hardware really does.  */
6143           if (STACK_TOP_P (operands[0]))
6144             p = "{p\t%0, %2|rp\t%2, %0}";
6145           else
6146             p = "{rp\t%2, %0|p\t%0, %2}";
6147 #else
6148           if (STACK_TOP_P (operands[0]))
6149             /* As above for fmul/fadd, we can't store to st(0).  */
6150             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
6151           else
6152             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
6153 #endif
6154           break;
6155         }
6156
6157       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
6158         {
6159 #if SYSV386_COMPAT
6160           if (STACK_TOP_P (operands[0]))
6161             p = "{rp\t%0, %1|p\t%1, %0}";
6162           else
6163             p = "{p\t%1, %0|rp\t%0, %1}";
6164 #else
6165           if (STACK_TOP_P (operands[0]))
6166             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
6167           else
6168             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
6169 #endif
6170           break;
6171         }
6172
6173       if (STACK_TOP_P (operands[0]))
6174         {
6175           if (STACK_TOP_P (operands[1]))
6176             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
6177           else
6178             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
6179           break;
6180         }
6181       else if (STACK_TOP_P (operands[1]))
6182         {
6183 #if SYSV386_COMPAT
6184           p = "{\t%1, %0|r\t%0, %1}";
6185 #else
6186           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
6187 #endif
6188         }
6189       else
6190         {
6191 #if SYSV386_COMPAT
6192           p = "{r\t%2, %0|\t%0, %2}";
6193 #else
6194           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
6195 #endif
6196         }
6197       break;
6198
6199     default:
6200       abort ();
6201     }
6202
6203   strcat (buf, p);
6204   return buf;
6205 }
6206
6207 /* Output code to initialize control word copies used by
6208    trunc?f?i patterns.  NORMAL is set to current control word, while ROUND_DOWN
6209    is set to control word rounding downwards.  */
6210 void
6211 emit_i387_cw_initialization (normal, round_down)
6212      rtx normal, round_down;
6213 {
6214   rtx reg = gen_reg_rtx (HImode);
6215
6216   emit_insn (gen_x86_fnstcw_1 (normal));
6217   emit_move_insn (reg, normal);
6218   if (!TARGET_PARTIAL_REG_STALL && !optimize_size
6219       && !TARGET_64BIT)
6220     emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
6221   else
6222     emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0xc00)));
6223   emit_move_insn (round_down, reg);
6224 }
6225
6226 /* Output code for INSN to convert a float to a signed int.  OPERANDS
6227    are the insn operands.  The output may be [HSD]Imode and the input
6228    operand may be [SDX]Fmode.  */
6229
6230 const char *
6231 output_fix_trunc (insn, operands)
6232      rtx insn;
6233      rtx *operands;
6234 {
6235   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
6236   int dimode_p = GET_MODE (operands[0]) == DImode;
6237
6238   /* Jump through a hoop or two for DImode, since the hardware has no
6239      non-popping instruction.  We used to do this a different way, but
6240      that was somewhat fragile and broke with post-reload splitters.  */
6241   if (dimode_p && !stack_top_dies)
6242     output_asm_insn ("fld\t%y1", operands);
6243
6244   if (!STACK_TOP_P (operands[1]))
6245     abort ();
6246
6247   if (GET_CODE (operands[0]) != MEM)
6248     abort ();
6249
6250   output_asm_insn ("fldcw\t%3", operands);
6251   if (stack_top_dies || dimode_p)
6252     output_asm_insn ("fistp%z0\t%0", operands);
6253   else
6254     output_asm_insn ("fist%z0\t%0", operands);
6255   output_asm_insn ("fldcw\t%2", operands);
6256
6257   return "";
6258 }
6259
6260 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
6261    should be used and 2 when fnstsw should be used.  UNORDERED_P is true
6262    when fucom should be used.  */
6263
6264 const char *
6265 output_fp_compare (insn, operands, eflags_p, unordered_p)
6266      rtx insn;
6267      rtx *operands;
6268      int eflags_p, unordered_p;
6269 {
6270   int stack_top_dies;
6271   rtx cmp_op0 = operands[0];
6272   rtx cmp_op1 = operands[1];
6273   int is_sse = SSE_REG_P (operands[0]) | SSE_REG_P (operands[1]);
6274
6275   if (eflags_p == 2)
6276     {
6277       cmp_op0 = cmp_op1;
6278       cmp_op1 = operands[2];
6279     }
6280   if (is_sse)
6281     {
6282       if (GET_MODE (operands[0]) == SFmode)
6283         if (unordered_p)
6284           return "ucomiss\t{%1, %0|%0, %1}";
6285         else
6286           return "comiss\t{%1, %0|%0, %y}";
6287       else
6288         if (unordered_p)
6289           return "ucomisd\t{%1, %0|%0, %1}";
6290         else
6291           return "comisd\t{%1, %0|%0, %y}";
6292     }
6293
6294   if (! STACK_TOP_P (cmp_op0))
6295     abort ();
6296
6297   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
6298
6299   if (STACK_REG_P (cmp_op1)
6300       && stack_top_dies
6301       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
6302       && REGNO (cmp_op1) != FIRST_STACK_REG)
6303     {
6304       /* If both the top of the 387 stack dies, and the other operand
6305          is also a stack register that dies, then this must be a
6306          `fcompp' float compare */
6307
6308       if (eflags_p == 1)
6309         {
6310           /* There is no double popping fcomi variant.  Fortunately,
6311              eflags is immune from the fstp's cc clobbering.  */
6312           if (unordered_p)
6313             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
6314           else
6315             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
6316           return "fstp\t%y0";
6317         }
6318       else
6319         {
6320           if (eflags_p == 2)
6321             {
6322               if (unordered_p)
6323                 return "fucompp\n\tfnstsw\t%0";
6324               else
6325                 return "fcompp\n\tfnstsw\t%0";
6326             }
6327           else
6328             {
6329               if (unordered_p)
6330                 return "fucompp";
6331               else
6332                 return "fcompp";
6333             }
6334         }
6335     }
6336   else
6337     {
6338       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
6339
6340       static const char * const alt[24] =
6341       {
6342         "fcom%z1\t%y1",
6343         "fcomp%z1\t%y1",
6344         "fucom%z1\t%y1",
6345         "fucomp%z1\t%y1",
6346
6347         "ficom%z1\t%y1",
6348         "ficomp%z1\t%y1",
6349         NULL,
6350         NULL,
6351
6352         "fcomi\t{%y1, %0|%0, %y1}",
6353         "fcomip\t{%y1, %0|%0, %y1}",
6354         "fucomi\t{%y1, %0|%0, %y1}",
6355         "fucomip\t{%y1, %0|%0, %y1}",
6356
6357         NULL,
6358         NULL,
6359         NULL,
6360         NULL,
6361
6362         "fcom%z2\t%y2\n\tfnstsw\t%0",
6363         "fcomp%z2\t%y2\n\tfnstsw\t%0",
6364         "fucom%z2\t%y2\n\tfnstsw\t%0",
6365         "fucomp%z2\t%y2\n\tfnstsw\t%0",
6366
6367         "ficom%z2\t%y2\n\tfnstsw\t%0",
6368         "ficomp%z2\t%y2\n\tfnstsw\t%0",
6369         NULL,
6370         NULL
6371       };
6372
6373       int mask;
6374       const char *ret;
6375
6376       mask  = eflags_p << 3;
6377       mask |= (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT) << 2;
6378       mask |= unordered_p << 1;
6379       mask |= stack_top_dies;
6380
6381       if (mask >= 24)
6382         abort ();
6383       ret = alt[mask];
6384       if (ret == NULL)
6385         abort ();
6386
6387       return ret;
6388     }
6389 }
6390
6391 /* Output assembler code to FILE to initialize basic-block profiling.
6392
6393    If profile_block_flag == 2
6394
6395         Output code to call the subroutine `__bb_init_trace_func'
6396         and pass two parameters to it. The first parameter is
6397         the address of a block allocated in the object module.
6398         The second parameter is the number of the first basic block
6399         of the function.
6400
6401         The name of the block is a local symbol made with this statement:
6402
6403             ASM_GENERATE_INTERNAL_LABEL (BUFFER, "LPBX", 0);
6404
6405         Of course, since you are writing the definition of
6406         `ASM_GENERATE_INTERNAL_LABEL' as well as that of this macro, you
6407         can take a short cut in the definition of this macro and use the
6408         name that you know will result.
6409
6410         The number of the first basic block of the function is
6411         passed to the macro in BLOCK_OR_LABEL.
6412
6413         If described in a virtual assembler language the code to be
6414         output looks like:
6415
6416                 parameter1 <- LPBX0
6417                 parameter2 <- BLOCK_OR_LABEL
6418                 call __bb_init_trace_func
6419
6420     else if profile_block_flag != 0
6421
6422         Output code to call the subroutine `__bb_init_func'
6423         and pass one single parameter to it, which is the same
6424         as the first parameter to `__bb_init_trace_func'.
6425
6426         The first word of this parameter is a flag which will be nonzero if
6427         the object module has already been initialized.  So test this word
6428         first, and do not call `__bb_init_func' if the flag is nonzero.
6429         Note: When profile_block_flag == 2 the test need not be done
6430         but `__bb_init_trace_func' *must* be called.
6431
6432         BLOCK_OR_LABEL may be used to generate a label number as a
6433         branch destination in case `__bb_init_func' will not be called.
6434
6435         If described in a virtual assembler language the code to be
6436         output looks like:
6437
6438                 cmp (LPBX0),0
6439                 jne local_label
6440                 parameter1 <- LPBX0
6441                 call __bb_init_func
6442               local_label:
6443 */
6444
6445 void
6446 ix86_output_function_block_profiler (file, block_or_label)
6447      FILE *file;
6448      int block_or_label;
6449 {
6450   static int num_func = 0;
6451   rtx xops[8];
6452   char block_table[80], false_label[80];
6453
6454   ASM_GENERATE_INTERNAL_LABEL (block_table, "LPBX", 0);
6455
6456   xops[1] = gen_rtx_SYMBOL_REF (VOIDmode, block_table);
6457   xops[5] = stack_pointer_rtx;
6458   xops[7] = gen_rtx_REG (Pmode, 0); /* eax */
6459
6460   CONSTANT_POOL_ADDRESS_P (xops[1]) = TRUE;
6461
6462   switch (profile_block_flag)
6463     {
6464     case 2:
6465       xops[2] = GEN_INT (block_or_label);
6466       xops[3] = gen_rtx_MEM (Pmode,
6467                      gen_rtx_SYMBOL_REF (VOIDmode, "__bb_init_trace_func"));
6468       xops[6] = GEN_INT (8);
6469
6470       output_asm_insn ("push{l}\t%2", xops);
6471       if (!flag_pic)
6472         output_asm_insn ("push{l}\t%1", xops);
6473       else
6474         {
6475           output_asm_insn ("lea{l}\t{%a1, %7|%7, %a1}", xops);
6476           output_asm_insn ("push{l}\t%7", xops);
6477         }
6478       output_asm_insn ("call\t%P3", xops);
6479       output_asm_insn ("add{l}\t{%6, %5|%5, %6}", xops);
6480       break;
6481
6482     default:
6483       ASM_GENERATE_INTERNAL_LABEL (false_label, "LPBZ", num_func);
6484
6485       xops[0] = const0_rtx;
6486       xops[2] = gen_rtx_MEM (Pmode,
6487                              gen_rtx_SYMBOL_REF (VOIDmode, false_label));
6488       xops[3] = gen_rtx_MEM (Pmode,
6489                              gen_rtx_SYMBOL_REF (VOIDmode, "__bb_init_func"));
6490       xops[4] = gen_rtx_MEM (Pmode, xops[1]);
6491       xops[6] = GEN_INT (4);
6492
6493       CONSTANT_POOL_ADDRESS_P (xops[2]) = TRUE;
6494
6495       output_asm_insn ("cmp{l}\t{%0, %4|%4, %0}", xops);
6496       output_asm_insn ("jne\t%2", xops);
6497
6498       if (!flag_pic)
6499         output_asm_insn ("push{l}\t%1", xops);
6500       else
6501         {
6502           output_asm_insn ("lea{l}\t{%a1, %7|%7, %a2}", xops);
6503           output_asm_insn ("push{l}\t%7", xops);
6504         }
6505       output_asm_insn ("call\t%P3", xops);
6506       output_asm_insn ("add{l}\t{%6, %5|%5, %6}", xops);
6507       ASM_OUTPUT_INTERNAL_LABEL (file, "LPBZ", num_func);
6508       num_func++;
6509       break;
6510     }
6511 }
6512
6513 /* Output assembler code to FILE to increment a counter associated
6514    with basic block number BLOCKNO.
6515
6516    If profile_block_flag == 2
6517
6518         Output code to initialize the global structure `__bb' and
6519         call the function `__bb_trace_func' which will increment the
6520         counter.
6521
6522         `__bb' consists of two words. In the first word the number
6523         of the basic block has to be stored. In the second word
6524         the address of a block allocated in the object module
6525         has to be stored.
6526
6527         The basic block number is given by BLOCKNO.
6528
6529         The address of the block is given by the label created with
6530
6531             ASM_GENERATE_INTERNAL_LABEL (BUFFER, "LPBX", 0);
6532
6533         by FUNCTION_BLOCK_PROFILER.
6534
6535         Of course, since you are writing the definition of
6536         `ASM_GENERATE_INTERNAL_LABEL' as well as that of this macro, you
6537         can take a short cut in the definition of this macro and use the
6538         name that you know will result.
6539
6540         If described in a virtual assembler language the code to be
6541         output looks like:
6542
6543                 move BLOCKNO -> (__bb)
6544                 move LPBX0 -> (__bb+4)
6545                 call __bb_trace_func
6546
6547         Note that function `__bb_trace_func' must not change the
6548         machine state, especially the flag register. To grant
6549         this, you must output code to save and restore registers
6550         either in this macro or in the macros MACHINE_STATE_SAVE
6551         and MACHINE_STATE_RESTORE. The last two macros will be
6552         used in the function `__bb_trace_func', so you must make
6553         sure that the function prologue does not change any
6554         register prior to saving it with MACHINE_STATE_SAVE.
6555
6556    else if profile_block_flag != 0
6557
6558         Output code to increment the counter directly.
6559         Basic blocks are numbered separately from zero within each
6560         compiled object module. The count associated with block number
6561         BLOCKNO is at index BLOCKNO in an array of words; the name of
6562         this array is a local symbol made with this statement:
6563
6564             ASM_GENERATE_INTERNAL_LABEL (BUFFER, "LPBX", 2);
6565
6566         Of course, since you are writing the definition of
6567         `ASM_GENERATE_INTERNAL_LABEL' as well as that of this macro, you
6568         can take a short cut in the definition of this macro and use the
6569         name that you know will result.
6570
6571         If described in a virtual assembler language the code to be
6572         output looks like:
6573
6574                 inc (LPBX2+4*BLOCKNO)
6575 */
6576
6577 void
6578 ix86_output_block_profiler (file, blockno)
6579      FILE *file ATTRIBUTE_UNUSED;
6580      int blockno;
6581 {
6582   rtx xops[8], cnt_rtx;
6583   char counts[80];
6584   char *block_table = counts;
6585
6586   switch (profile_block_flag)
6587     {
6588     case 2:
6589       ASM_GENERATE_INTERNAL_LABEL (block_table, "LPBX", 0);
6590
6591       xops[1] = gen_rtx_SYMBOL_REF (VOIDmode, block_table);
6592       xops[2] = GEN_INT (blockno);
6593       xops[3] = gen_rtx_MEM (Pmode,
6594                              gen_rtx_SYMBOL_REF (VOIDmode, "__bb_trace_func"));
6595       xops[4] = gen_rtx_SYMBOL_REF (VOIDmode, "__bb");
6596       xops[5] = plus_constant (xops[4], 4);
6597       xops[0] = gen_rtx_MEM (SImode, xops[4]);
6598       xops[6] = gen_rtx_MEM (SImode, xops[5]);
6599
6600       CONSTANT_POOL_ADDRESS_P (xops[1]) = TRUE;
6601
6602       output_asm_insn ("pushf", xops);
6603       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
6604       if (flag_pic)
6605         {
6606           xops[7] = gen_rtx_REG (Pmode, 0); /* eax */
6607           output_asm_insn ("push{l}\t%7", xops);
6608           output_asm_insn ("lea{l}\t{%a1, %7|%7, %a1}", xops);
6609           output_asm_insn ("mov{l}\t{%7, %6|%6, %7}", xops);
6610           output_asm_insn ("pop{l}\t%7", xops);
6611         }
6612       else
6613         output_asm_insn ("mov{l}\t{%1, %6|%6, %1}", xops);
6614       output_asm_insn ("call\t%P3", xops);
6615       output_asm_insn ("popf", xops);
6616
6617       break;
6618
6619     default:
6620       ASM_GENERATE_INTERNAL_LABEL (counts, "LPBX", 2);
6621       cnt_rtx = gen_rtx_SYMBOL_REF (VOIDmode, counts);
6622       SYMBOL_REF_FLAG (cnt_rtx) = TRUE;
6623
6624       if (blockno)
6625         cnt_rtx = plus_constant (cnt_rtx, blockno*4);
6626
6627       if (flag_pic)
6628         cnt_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, cnt_rtx);
6629
6630       xops[0] = gen_rtx_MEM (SImode, cnt_rtx);
6631       output_asm_insn ("inc{l}\t%0", xops);
6632
6633       break;
6634     }
6635 }
6636
6637 void
6638 ix86_output_addr_vec_elt (file, value)
6639      FILE *file;
6640      int value;
6641 {
6642   const char *directive = ASM_LONG;
6643
6644   if (TARGET_64BIT)
6645     {
6646 #ifdef ASM_QUAD
6647       directive = ASM_QUAD;
6648 #else
6649       abort ();
6650 #endif
6651     }
6652
6653   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
6654 }
6655
6656 void
6657 ix86_output_addr_diff_elt (file, value, rel)
6658      FILE *file;
6659      int value, rel;
6660 {
6661   if (TARGET_64BIT)
6662     fprintf (file, "%s%s%d-.+4+(.-%s%d)\n",
6663              ASM_LONG, LPREFIX, value, LPREFIX, rel);
6664   else if (HAVE_AS_GOTOFF_IN_DATA)
6665     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
6666   else
6667     asm_fprintf (file, "%s%U_GLOBAL_OFFSET_TABLE_+[.-%s%d]\n",
6668                  ASM_LONG, LPREFIX, value);
6669 }
6670 \f
6671 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
6672    for the target.  */
6673
6674 void
6675 ix86_expand_clear (dest)
6676      rtx dest;
6677 {
6678   rtx tmp;
6679
6680   /* We play register width games, which are only valid after reload.  */
6681   if (!reload_completed)
6682     abort ();
6683
6684   /* Avoid HImode and its attendant prefix byte.  */
6685   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
6686     dest = gen_rtx_REG (SImode, REGNO (dest));
6687
6688   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
6689
6690   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
6691   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
6692     {
6693       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
6694       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
6695     }
6696
6697   emit_insn (tmp);
6698 }
6699
6700 void
6701 ix86_expand_move (mode, operands)
6702      enum machine_mode mode;
6703      rtx operands[];
6704 {
6705   int strict = (reload_in_progress || reload_completed);
6706   rtx insn;
6707
6708   if (flag_pic && mode == Pmode && symbolic_operand (operands[1], Pmode))
6709     {
6710       /* Emit insns to move operands[1] into operands[0].  */
6711
6712       if (GET_CODE (operands[0]) == MEM)
6713         operands[1] = force_reg (Pmode, operands[1]);
6714       else
6715         {
6716           rtx temp = operands[0];
6717           if (GET_CODE (temp) != REG)
6718             temp = gen_reg_rtx (Pmode);
6719           temp = legitimize_pic_address (operands[1], temp);
6720           if (temp == operands[0])
6721             return;
6722           operands[1] = temp;
6723         }
6724     }
6725   else
6726     {
6727       if (GET_CODE (operands[0]) == MEM
6728           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
6729               || !push_operand (operands[0], mode))
6730           && GET_CODE (operands[1]) == MEM)
6731         operands[1] = force_reg (mode, operands[1]);
6732
6733       if (push_operand (operands[0], mode)
6734           && ! general_no_elim_operand (operands[1], mode))
6735         operands[1] = copy_to_mode_reg (mode, operands[1]);
6736
6737       /* Force large constants in 64bit compilation into register
6738          to get them CSEed.  */
6739       if (TARGET_64BIT && mode == DImode
6740           && immediate_operand (operands[1], mode)
6741           && !x86_64_zero_extended_value (operands[1])
6742           && !register_operand (operands[0], mode)
6743           && optimize && !reload_completed && !reload_in_progress)
6744         operands[1] = copy_to_mode_reg (mode, operands[1]);
6745
6746       if (FLOAT_MODE_P (mode))
6747         {
6748           /* If we are loading a floating point constant to a register,
6749              force the value to memory now, since we'll get better code
6750              out the back end.  */
6751
6752           if (strict)
6753             ;
6754           else if (GET_CODE (operands[1]) == CONST_DOUBLE
6755                    && register_operand (operands[0], mode))
6756             operands[1] = validize_mem (force_const_mem (mode, operands[1]));
6757         }
6758     }
6759
6760   insn = gen_rtx_SET (VOIDmode, operands[0], operands[1]);
6761
6762   emit_insn (insn);
6763 }
6764
6765 /* Attempt to expand a binary operator.  Make the expansion closer to the
6766    actual machine, then just general_operand, which will allow 3 separate
6767    memory references (one output, two input) in a single insn.  */
6768
6769 void
6770 ix86_expand_binary_operator (code, mode, operands)
6771      enum rtx_code code;
6772      enum machine_mode mode;
6773      rtx operands[];
6774 {
6775   int matching_memory;
6776   rtx src1, src2, dst, op, clob;
6777
6778   dst = operands[0];
6779   src1 = operands[1];
6780   src2 = operands[2];
6781
6782   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
6783   if (GET_RTX_CLASS (code) == 'c'
6784       && (rtx_equal_p (dst, src2)
6785           || immediate_operand (src1, mode)))
6786     {
6787       rtx temp = src1;
6788       src1 = src2;
6789       src2 = temp;
6790     }
6791
6792   /* If the destination is memory, and we do not have matching source
6793      operands, do things in registers.  */
6794   matching_memory = 0;
6795   if (GET_CODE (dst) == MEM)
6796     {
6797       if (rtx_equal_p (dst, src1))
6798         matching_memory = 1;
6799       else if (GET_RTX_CLASS (code) == 'c'
6800                && rtx_equal_p (dst, src2))
6801         matching_memory = 2;
6802       else
6803         dst = gen_reg_rtx (mode);
6804     }
6805
6806   /* Both source operands cannot be in memory.  */
6807   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
6808     {
6809       if (matching_memory != 2)
6810         src2 = force_reg (mode, src2);
6811       else
6812         src1 = force_reg (mode, src1);
6813     }
6814
6815   /* If the operation is not commutable, source 1 cannot be a constant
6816      or non-matching memory.  */
6817   if ((CONSTANT_P (src1)
6818        || (!matching_memory && GET_CODE (src1) == MEM))
6819       && GET_RTX_CLASS (code) != 'c')
6820     src1 = force_reg (mode, src1);
6821
6822   /* If optimizing, copy to regs to improve CSE */
6823   if (optimize && ! no_new_pseudos)
6824     {
6825       if (GET_CODE (dst) == MEM)
6826         dst = gen_reg_rtx (mode);
6827       if (GET_CODE (src1) == MEM)
6828         src1 = force_reg (mode, src1);
6829       if (GET_CODE (src2) == MEM)
6830         src2 = force_reg (mode, src2);
6831     }
6832
6833   /* Emit the instruction.  */
6834
6835   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
6836   if (reload_in_progress)
6837     {
6838       /* Reload doesn't know about the flags register, and doesn't know that
6839          it doesn't want to clobber it.  We can only do this with PLUS.  */
6840       if (code != PLUS)
6841         abort ();
6842       emit_insn (op);
6843     }
6844   else
6845     {
6846       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
6847       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
6848     }
6849
6850   /* Fix up the destination if needed.  */
6851   if (dst != operands[0])
6852     emit_move_insn (operands[0], dst);
6853 }
6854
6855 /* Return TRUE or FALSE depending on whether the binary operator meets the
6856    appropriate constraints.  */
6857
6858 int
6859 ix86_binary_operator_ok (code, mode, operands)
6860      enum rtx_code code;
6861      enum machine_mode mode ATTRIBUTE_UNUSED;
6862      rtx operands[3];
6863 {
6864   /* Both source operands cannot be in memory.  */
6865   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
6866     return 0;
6867   /* If the operation is not commutable, source 1 cannot be a constant.  */
6868   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != 'c')
6869     return 0;
6870   /* If the destination is memory, we must have a matching source operand.  */
6871   if (GET_CODE (operands[0]) == MEM
6872       && ! (rtx_equal_p (operands[0], operands[1])
6873             || (GET_RTX_CLASS (code) == 'c'
6874                 && rtx_equal_p (operands[0], operands[2]))))
6875     return 0;
6876   /* If the operation is not commutable and the source 1 is memory, we must
6877      have a matching destionation.  */
6878   if (GET_CODE (operands[1]) == MEM
6879       && GET_RTX_CLASS (code) != 'c'
6880       && ! rtx_equal_p (operands[0], operands[1]))
6881     return 0;
6882   return 1;
6883 }
6884
6885 /* Attempt to expand a unary operator.  Make the expansion closer to the
6886    actual machine, then just general_operand, which will allow 2 separate
6887    memory references (one output, one input) in a single insn.  */
6888
6889 void
6890 ix86_expand_unary_operator (code, mode, operands)
6891      enum rtx_code code;
6892      enum machine_mode mode;
6893      rtx operands[];
6894 {
6895   int matching_memory;
6896   rtx src, dst, op, clob;
6897
6898   dst = operands[0];
6899   src = operands[1];
6900
6901   /* If the destination is memory, and we do not have matching source
6902      operands, do things in registers.  */
6903   matching_memory = 0;
6904   if (GET_CODE (dst) == MEM)
6905     {
6906       if (rtx_equal_p (dst, src))
6907         matching_memory = 1;
6908       else
6909         dst = gen_reg_rtx (mode);
6910     }
6911
6912   /* When source operand is memory, destination must match.  */
6913   if (!matching_memory && GET_CODE (src) == MEM)
6914     src = force_reg (mode, src);
6915
6916   /* If optimizing, copy to regs to improve CSE */
6917   if (optimize && ! no_new_pseudos)
6918     {
6919       if (GET_CODE (dst) == MEM)
6920         dst = gen_reg_rtx (mode);
6921       if (GET_CODE (src) == MEM)
6922         src = force_reg (mode, src);
6923     }
6924
6925   /* Emit the instruction.  */
6926
6927   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
6928   if (reload_in_progress || code == NOT)
6929     {
6930       /* Reload doesn't know about the flags register, and doesn't know that
6931          it doesn't want to clobber it.  */
6932       if (code != NOT)
6933         abort ();
6934       emit_insn (op);
6935     }
6936   else
6937     {
6938       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
6939       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
6940     }
6941
6942   /* Fix up the destination if needed.  */
6943   if (dst != operands[0])
6944     emit_move_insn (operands[0], dst);
6945 }
6946
6947 /* Return TRUE or FALSE depending on whether the unary operator meets the
6948    appropriate constraints.  */
6949
6950 int
6951 ix86_unary_operator_ok (code, mode, operands)
6952      enum rtx_code code ATTRIBUTE_UNUSED;
6953      enum machine_mode mode ATTRIBUTE_UNUSED;
6954      rtx operands[2] ATTRIBUTE_UNUSED;
6955 {
6956   /* If one of operands is memory, source and destination must match.  */
6957   if ((GET_CODE (operands[0]) == MEM
6958        || GET_CODE (operands[1]) == MEM)
6959       && ! rtx_equal_p (operands[0], operands[1]))
6960     return FALSE;
6961   return TRUE;
6962 }
6963
6964 /* Return TRUE or FALSE depending on whether the first SET in INSN
6965    has source and destination with matching CC modes, and that the
6966    CC mode is at least as constrained as REQ_MODE.  */
6967
6968 int
6969 ix86_match_ccmode (insn, req_mode)
6970      rtx insn;
6971      enum machine_mode req_mode;
6972 {
6973   rtx set;
6974   enum machine_mode set_mode;
6975
6976   set = PATTERN (insn);
6977   if (GET_CODE (set) == PARALLEL)
6978     set = XVECEXP (set, 0, 0);
6979   if (GET_CODE (set) != SET)
6980     abort ();
6981   if (GET_CODE (SET_SRC (set)) != COMPARE)
6982     abort ();
6983
6984   set_mode = GET_MODE (SET_DEST (set));
6985   switch (set_mode)
6986     {
6987     case CCNOmode:
6988       if (req_mode != CCNOmode
6989           && (req_mode != CCmode
6990               || XEXP (SET_SRC (set), 1) != const0_rtx))
6991         return 0;
6992       break;
6993     case CCmode:
6994       if (req_mode == CCGCmode)
6995         return 0;
6996       /* FALLTHRU */
6997     case CCGCmode:
6998       if (req_mode == CCGOCmode || req_mode == CCNOmode)
6999         return 0;
7000       /* FALLTHRU */
7001     case CCGOCmode:
7002       if (req_mode == CCZmode)
7003         return 0;
7004       /* FALLTHRU */
7005     case CCZmode:
7006       break;
7007
7008     default:
7009       abort ();
7010     }
7011
7012   return (GET_MODE (SET_SRC (set)) == set_mode);
7013 }
7014
7015 /* Generate insn patterns to do an integer compare of OPERANDS.  */
7016
7017 static rtx
7018 ix86_expand_int_compare (code, op0, op1)
7019      enum rtx_code code;
7020      rtx op0, op1;
7021 {
7022   enum machine_mode cmpmode;
7023   rtx tmp, flags;
7024
7025   cmpmode = SELECT_CC_MODE (code, op0, op1);
7026   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
7027
7028   /* This is very simple, but making the interface the same as in the
7029      FP case makes the rest of the code easier.  */
7030   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
7031   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
7032
7033   /* Return the test that should be put into the flags user, i.e.
7034      the bcc, scc, or cmov instruction.  */
7035   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
7036 }
7037
7038 /* Figure out whether to use ordered or unordered fp comparisons.
7039    Return the appropriate mode to use.  */
7040
7041 enum machine_mode
7042 ix86_fp_compare_mode (code)
7043      enum rtx_code code ATTRIBUTE_UNUSED;
7044 {
7045   /* ??? In order to make all comparisons reversible, we do all comparisons
7046      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
7047      all forms trapping and nontrapping comparisons, we can make inequality
7048      comparisons trapping again, since it results in better code when using
7049      FCOM based compares.  */
7050   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
7051 }
7052
7053 enum machine_mode
7054 ix86_cc_mode (code, op0, op1)
7055      enum rtx_code code;
7056      rtx op0, op1;
7057 {
7058   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7059     return ix86_fp_compare_mode (code);
7060   switch (code)
7061     {
7062       /* Only zero flag is needed.  */
7063     case EQ:                    /* ZF=0 */
7064     case NE:                    /* ZF!=0 */
7065       return CCZmode;
7066       /* Codes needing carry flag.  */
7067     case GEU:                   /* CF=0 */
7068     case GTU:                   /* CF=0 & ZF=0 */
7069     case LTU:                   /* CF=1 */
7070     case LEU:                   /* CF=1 | ZF=1 */
7071       return CCmode;
7072       /* Codes possibly doable only with sign flag when
7073          comparing against zero.  */
7074     case GE:                    /* SF=OF   or   SF=0 */
7075     case LT:                    /* SF<>OF  or   SF=1 */
7076       if (op1 == const0_rtx)
7077         return CCGOCmode;
7078       else
7079         /* For other cases Carry flag is not required.  */
7080         return CCGCmode;
7081       /* Codes doable only with sign flag when comparing
7082          against zero, but we miss jump instruction for it
7083          so we need to use relational tests agains overflow
7084          that thus needs to be zero.  */
7085     case GT:                    /* ZF=0 & SF=OF */
7086     case LE:                    /* ZF=1 | SF<>OF */
7087       if (op1 == const0_rtx)
7088         return CCNOmode;
7089       else
7090         return CCGCmode;
7091     default:
7092       abort ();
7093     }
7094 }
7095
7096 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
7097
7098 int
7099 ix86_use_fcomi_compare (code)
7100      enum rtx_code code ATTRIBUTE_UNUSED;
7101 {
7102   enum rtx_code swapped_code = swap_condition (code);
7103   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
7104           || (ix86_fp_comparison_cost (swapped_code)
7105               == ix86_fp_comparison_fcomi_cost (swapped_code)));
7106 }
7107
7108 /* Swap, force into registers, or otherwise massage the two operands
7109    to a fp comparison.  The operands are updated in place; the new
7110    comparsion code is returned.  */
7111
7112 static enum rtx_code
7113 ix86_prepare_fp_compare_args (code, pop0, pop1)
7114      enum rtx_code code;
7115      rtx *pop0, *pop1;
7116 {
7117   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
7118   rtx op0 = *pop0, op1 = *pop1;
7119   enum machine_mode op_mode = GET_MODE (op0);
7120   int is_sse = SSE_REG_P (op0) | SSE_REG_P (op1);
7121
7122   /* All of the unordered compare instructions only work on registers.
7123      The same is true of the XFmode compare instructions.  The same is
7124      true of the fcomi compare instructions.  */
7125
7126   if (!is_sse
7127       && (fpcmp_mode == CCFPUmode
7128           || op_mode == XFmode
7129           || op_mode == TFmode
7130           || ix86_use_fcomi_compare (code)))
7131     {
7132       op0 = force_reg (op_mode, op0);
7133       op1 = force_reg (op_mode, op1);
7134     }
7135   else
7136     {
7137       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
7138          things around if they appear profitable, otherwise force op0
7139          into a register.  */
7140
7141       if (standard_80387_constant_p (op0) == 0
7142           || (GET_CODE (op0) == MEM
7143               && ! (standard_80387_constant_p (op1) == 0
7144                     || GET_CODE (op1) == MEM)))
7145         {
7146           rtx tmp;
7147           tmp = op0, op0 = op1, op1 = tmp;
7148           code = swap_condition (code);
7149         }
7150
7151       if (GET_CODE (op0) != REG)
7152         op0 = force_reg (op_mode, op0);
7153
7154       if (CONSTANT_P (op1))
7155         {
7156           if (standard_80387_constant_p (op1))
7157             op1 = force_reg (op_mode, op1);
7158           else
7159             op1 = validize_mem (force_const_mem (op_mode, op1));
7160         }
7161     }
7162
7163   /* Try to rearrange the comparison to make it cheaper.  */
7164   if (ix86_fp_comparison_cost (code)
7165       > ix86_fp_comparison_cost (swap_condition (code))
7166       && (GET_CODE (op0) == REG || !reload_completed))
7167     {
7168       rtx tmp;
7169       tmp = op0, op0 = op1, op1 = tmp;
7170       code = swap_condition (code);
7171       if (GET_CODE (op0) != REG)
7172         op0 = force_reg (op_mode, op0);
7173     }
7174
7175   *pop0 = op0;
7176   *pop1 = op1;
7177   return code;
7178 }
7179
7180 /* Convert comparison codes we use to represent FP comparison to integer
7181    code that will result in proper branch.  Return UNKNOWN if no such code
7182    is available.  */
7183 static enum rtx_code
7184 ix86_fp_compare_code_to_integer (code)
7185      enum rtx_code code;
7186 {
7187   switch (code)
7188     {
7189     case GT:
7190       return GTU;
7191     case GE:
7192       return GEU;
7193     case ORDERED:
7194     case UNORDERED:
7195       return code;
7196       break;
7197     case UNEQ:
7198       return EQ;
7199       break;
7200     case UNLT:
7201       return LTU;
7202       break;
7203     case UNLE:
7204       return LEU;
7205       break;
7206     case LTGT:
7207       return NE;
7208       break;
7209     default:
7210       return UNKNOWN;
7211     }
7212 }
7213
7214 /* Split comparison code CODE into comparisons we can do using branch
7215    instructions.  BYPASS_CODE is comparison code for branch that will
7216    branch around FIRST_CODE and SECOND_CODE.  If some of branches
7217    is not required, set value to NIL.
7218    We never require more than two branches.  */
7219 static void
7220 ix86_fp_comparison_codes (code, bypass_code, first_code, second_code)
7221      enum rtx_code code, *bypass_code, *first_code, *second_code;
7222 {
7223   *first_code = code;
7224   *bypass_code = NIL;
7225   *second_code = NIL;
7226
7227   /* The fcomi comparison sets flags as follows:
7228
7229      cmp    ZF PF CF
7230      >      0  0  0
7231      <      0  0  1
7232      =      1  0  0
7233      un     1  1  1 */
7234
7235   switch (code)
7236     {
7237     case GT:                    /* GTU - CF=0 & ZF=0 */
7238     case GE:                    /* GEU - CF=0 */
7239     case ORDERED:               /* PF=0 */
7240     case UNORDERED:             /* PF=1 */
7241     case UNEQ:                  /* EQ - ZF=1 */
7242     case UNLT:                  /* LTU - CF=1 */
7243     case UNLE:                  /* LEU - CF=1 | ZF=1 */
7244     case LTGT:                  /* EQ - ZF=0 */
7245       break;
7246     case LT:                    /* LTU - CF=1 - fails on unordered */
7247       *first_code = UNLT;
7248       *bypass_code = UNORDERED;
7249       break;
7250     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
7251       *first_code = UNLE;
7252       *bypass_code = UNORDERED;
7253       break;
7254     case EQ:                    /* EQ - ZF=1 - fails on unordered */
7255       *first_code = UNEQ;
7256       *bypass_code = UNORDERED;
7257       break;
7258     case NE:                    /* NE - ZF=0 - fails on unordered */
7259       *first_code = LTGT;
7260       *second_code = UNORDERED;
7261       break;
7262     case UNGE:                  /* GEU - CF=0 - fails on unordered */
7263       *first_code = GE;
7264       *second_code = UNORDERED;
7265       break;
7266     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
7267       *first_code = GT;
7268       *second_code = UNORDERED;
7269       break;
7270     default:
7271       abort ();
7272     }
7273   if (!TARGET_IEEE_FP)
7274     {
7275       *second_code = NIL;
7276       *bypass_code = NIL;
7277     }
7278 }
7279
7280 /* Return cost of comparison done fcom + arithmetics operations on AX.
7281    All following functions do use number of instructions as an cost metrics.
7282    In future this should be tweaked to compute bytes for optimize_size and
7283    take into account performance of various instructions on various CPUs.  */
7284 static int
7285 ix86_fp_comparison_arithmetics_cost (code)
7286      enum rtx_code code;
7287 {
7288   if (!TARGET_IEEE_FP)
7289     return 4;
7290   /* The cost of code output by ix86_expand_fp_compare.  */
7291   switch (code)
7292     {
7293     case UNLE:
7294     case UNLT:
7295     case LTGT:
7296     case GT:
7297     case GE:
7298     case UNORDERED:
7299     case ORDERED:
7300     case UNEQ:
7301       return 4;
7302       break;
7303     case LT:
7304     case NE:
7305     case EQ:
7306     case UNGE:
7307       return 5;
7308       break;
7309     case LE:
7310     case UNGT:
7311       return 6;
7312       break;
7313     default:
7314       abort ();
7315     }
7316 }
7317
7318 /* Return cost of comparison done using fcomi operation.
7319    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
7320 static int
7321 ix86_fp_comparison_fcomi_cost (code)
7322      enum rtx_code code;
7323 {
7324   enum rtx_code bypass_code, first_code, second_code;
7325   /* Return arbitarily high cost when instruction is not supported - this
7326      prevents gcc from using it.  */
7327   if (!TARGET_CMOVE)
7328     return 1024;
7329   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
7330   return (bypass_code != NIL || second_code != NIL) + 2;
7331 }
7332
7333 /* Return cost of comparison done using sahf operation.
7334    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
7335 static int
7336 ix86_fp_comparison_sahf_cost (code)
7337      enum rtx_code code;
7338 {
7339   enum rtx_code bypass_code, first_code, second_code;
7340   /* Return arbitarily high cost when instruction is not preferred - this
7341      avoids gcc from using it.  */
7342   if (!TARGET_USE_SAHF && !optimize_size)
7343     return 1024;
7344   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
7345   return (bypass_code != NIL || second_code != NIL) + 3;
7346 }
7347
7348 /* Compute cost of the comparison done using any method.
7349    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
7350 static int
7351 ix86_fp_comparison_cost (code)
7352      enum rtx_code code;
7353 {
7354   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
7355   int min;
7356
7357   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
7358   sahf_cost = ix86_fp_comparison_sahf_cost (code);
7359
7360   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
7361   if (min > sahf_cost)
7362     min = sahf_cost;
7363   if (min > fcomi_cost)
7364     min = fcomi_cost;
7365   return min;
7366 }
7367
7368 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
7369
7370 static rtx
7371 ix86_expand_fp_compare (code, op0, op1, scratch, second_test, bypass_test)
7372      enum rtx_code code;
7373      rtx op0, op1, scratch;
7374      rtx *second_test;
7375      rtx *bypass_test;
7376 {
7377   enum machine_mode fpcmp_mode, intcmp_mode;
7378   rtx tmp, tmp2;
7379   int cost = ix86_fp_comparison_cost (code);
7380   enum rtx_code bypass_code, first_code, second_code;
7381
7382   fpcmp_mode = ix86_fp_compare_mode (code);
7383   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
7384
7385   if (second_test)
7386     *second_test = NULL_RTX;
7387   if (bypass_test)
7388     *bypass_test = NULL_RTX;
7389
7390   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
7391
7392   /* Do fcomi/sahf based test when profitable.  */
7393   if ((bypass_code == NIL || bypass_test)
7394       && (second_code == NIL || second_test)
7395       && ix86_fp_comparison_arithmetics_cost (code) > cost)
7396     {
7397       if (TARGET_CMOVE)
7398         {
7399           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
7400           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
7401                              tmp);
7402           emit_insn (tmp);
7403         }
7404       else
7405         {
7406           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
7407           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), 9);
7408           if (!scratch)
7409             scratch = gen_reg_rtx (HImode);
7410           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
7411           emit_insn (gen_x86_sahf_1 (scratch));
7412         }
7413
7414       /* The FP codes work out to act like unsigned.  */
7415       intcmp_mode = fpcmp_mode;
7416       code = first_code;
7417       if (bypass_code != NIL)
7418         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
7419                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
7420                                        const0_rtx);
7421       if (second_code != NIL)
7422         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
7423                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
7424                                        const0_rtx);
7425     }
7426   else
7427     {
7428       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
7429       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
7430       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), 9);
7431       if (!scratch)
7432         scratch = gen_reg_rtx (HImode);
7433       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
7434
7435       /* In the unordered case, we have to check C2 for NaN's, which
7436          doesn't happen to work out to anything nice combination-wise.
7437          So do some bit twiddling on the value we've got in AH to come
7438          up with an appropriate set of condition codes.  */
7439
7440       intcmp_mode = CCNOmode;
7441       switch (code)
7442         {
7443         case GT:
7444         case UNGT:
7445           if (code == GT || !TARGET_IEEE_FP)
7446             {
7447               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
7448               code = EQ;
7449             }
7450           else
7451             {
7452               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
7453               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
7454               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
7455               intcmp_mode = CCmode;
7456               code = GEU;
7457             }
7458           break;
7459         case LT:
7460         case UNLT:
7461           if (code == LT && TARGET_IEEE_FP)
7462             {
7463               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
7464               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
7465               intcmp_mode = CCmode;
7466               code = EQ;
7467             }
7468           else
7469             {
7470               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
7471               code = NE;
7472             }
7473           break;
7474         case GE:
7475         case UNGE:
7476           if (code == GE || !TARGET_IEEE_FP)
7477             {
7478               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
7479               code = EQ;
7480             }
7481           else
7482             {
7483               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
7484               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
7485                                              GEN_INT (0x01)));
7486               code = NE;
7487             }
7488           break;
7489         case LE:
7490         case UNLE:
7491           if (code == LE && TARGET_IEEE_FP)
7492             {
7493               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
7494               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
7495               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
7496               intcmp_mode = CCmode;
7497               code = LTU;
7498             }
7499           else
7500             {
7501               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
7502               code = NE;
7503             }
7504           break;
7505         case EQ:
7506         case UNEQ:
7507           if (code == EQ && TARGET_IEEE_FP)
7508             {
7509               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
7510               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
7511               intcmp_mode = CCmode;
7512               code = EQ;
7513             }
7514           else
7515             {
7516               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
7517               code = NE;
7518               break;
7519             }
7520           break;
7521         case NE:
7522         case LTGT:
7523           if (code == NE && TARGET_IEEE_FP)
7524             {
7525               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
7526               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
7527                                              GEN_INT (0x40)));
7528               code = NE;
7529             }
7530           else
7531             {
7532               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
7533               code = EQ;
7534             }
7535           break;
7536
7537         case UNORDERED:
7538           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
7539           code = NE;
7540           break;
7541         case ORDERED:
7542           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
7543           code = EQ;
7544           break;
7545
7546         default:
7547           abort ();
7548         }
7549     }
7550
7551   /* Return the test that should be put into the flags user, i.e.
7552      the bcc, scc, or cmov instruction.  */
7553   return gen_rtx_fmt_ee (code, VOIDmode,
7554                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
7555                          const0_rtx);
7556 }
7557
7558 rtx
7559 ix86_expand_compare (code, second_test, bypass_test)
7560      enum rtx_code code;
7561      rtx *second_test, *bypass_test;
7562 {
7563   rtx op0, op1, ret;
7564   op0 = ix86_compare_op0;
7565   op1 = ix86_compare_op1;
7566
7567   if (second_test)
7568     *second_test = NULL_RTX;
7569   if (bypass_test)
7570     *bypass_test = NULL_RTX;
7571
7572   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7573     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
7574                                   second_test, bypass_test);
7575   else
7576     ret = ix86_expand_int_compare (code, op0, op1);
7577
7578   return ret;
7579 }
7580
7581 /* Return true if the CODE will result in nontrivial jump sequence.  */
7582 bool
7583 ix86_fp_jump_nontrivial_p (code)
7584     enum rtx_code code;
7585 {
7586   enum rtx_code bypass_code, first_code, second_code;
7587   if (!TARGET_CMOVE)
7588     return true;
7589   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
7590   return bypass_code != NIL || second_code != NIL;
7591 }
7592
7593 void
7594 ix86_expand_branch (code, label)
7595      enum rtx_code code;
7596      rtx label;
7597 {
7598   rtx tmp;
7599
7600   switch (GET_MODE (ix86_compare_op0))
7601     {
7602     case QImode:
7603     case HImode:
7604     case SImode:
7605       simple:
7606       tmp = ix86_expand_compare (code, NULL, NULL);
7607       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
7608                                   gen_rtx_LABEL_REF (VOIDmode, label),
7609                                   pc_rtx);
7610       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
7611       return;
7612
7613     case SFmode:
7614     case DFmode:
7615     case XFmode:
7616     case TFmode:
7617       {
7618         rtvec vec;
7619         int use_fcomi;
7620         enum rtx_code bypass_code, first_code, second_code;
7621
7622         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
7623                                              &ix86_compare_op1);
7624         
7625         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
7626
7627         /* Check whether we will use the natural sequence with one jump.  If
7628            so, we can expand jump early.  Otherwise delay expansion by
7629            creating compound insn to not confuse optimizers.  */
7630         if (bypass_code == NIL && second_code == NIL
7631             && TARGET_CMOVE)
7632           {
7633             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
7634                                   gen_rtx_LABEL_REF (VOIDmode, label),
7635                                   pc_rtx, NULL_RTX);
7636           }
7637         else
7638           {
7639             tmp = gen_rtx_fmt_ee (code, VOIDmode,
7640                                   ix86_compare_op0, ix86_compare_op1);
7641             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
7642                                         gen_rtx_LABEL_REF (VOIDmode, label),
7643                                         pc_rtx);
7644             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
7645
7646             use_fcomi = ix86_use_fcomi_compare (code);
7647             vec = rtvec_alloc (3 + !use_fcomi);
7648             RTVEC_ELT (vec, 0) = tmp;
7649             RTVEC_ELT (vec, 1)
7650               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
7651             RTVEC_ELT (vec, 2)
7652               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
7653             if (! use_fcomi)
7654               RTVEC_ELT (vec, 3)
7655                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
7656
7657             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
7658           }
7659         return;
7660       }
7661
7662     case DImode:
7663       if (TARGET_64BIT)
7664         goto simple;
7665       /* Expand DImode branch into multiple compare+branch.  */
7666       {
7667         rtx lo[2], hi[2], label2;
7668         enum rtx_code code1, code2, code3;
7669
7670         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
7671           {
7672             tmp = ix86_compare_op0;
7673             ix86_compare_op0 = ix86_compare_op1;
7674             ix86_compare_op1 = tmp;
7675             code = swap_condition (code);
7676           }
7677         split_di (&ix86_compare_op0, 1, lo+0, hi+0);
7678         split_di (&ix86_compare_op1, 1, lo+1, hi+1);
7679
7680         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
7681            avoid two branches.  This costs one extra insn, so disable when
7682            optimizing for size.  */
7683
7684         if ((code == EQ || code == NE)
7685             && (!optimize_size
7686                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
7687           {
7688             rtx xor0, xor1;
7689
7690             xor1 = hi[0];
7691             if (hi[1] != const0_rtx)
7692               xor1 = expand_binop (SImode, xor_optab, xor1, hi[1],
7693                                    NULL_RTX, 0, OPTAB_WIDEN);
7694
7695             xor0 = lo[0];
7696             if (lo[1] != const0_rtx)
7697               xor0 = expand_binop (SImode, xor_optab, xor0, lo[1],
7698                                    NULL_RTX, 0, OPTAB_WIDEN);
7699
7700             tmp = expand_binop (SImode, ior_optab, xor1, xor0,
7701                                 NULL_RTX, 0, OPTAB_WIDEN);
7702
7703             ix86_compare_op0 = tmp;
7704             ix86_compare_op1 = const0_rtx;
7705             ix86_expand_branch (code, label);
7706             return;
7707           }
7708
7709         /* Otherwise, if we are doing less-than or greater-or-equal-than,
7710            op1 is a constant and the low word is zero, then we can just
7711            examine the high word.  */
7712
7713         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
7714           switch (code)
7715             {
7716             case LT: case LTU: case GE: case GEU:
7717               ix86_compare_op0 = hi[0];
7718               ix86_compare_op1 = hi[1];
7719               ix86_expand_branch (code, label);
7720               return;
7721             default:
7722               break;
7723             }
7724
7725         /* Otherwise, we need two or three jumps.  */
7726
7727         label2 = gen_label_rtx ();
7728
7729         code1 = code;
7730         code2 = swap_condition (code);
7731         code3 = unsigned_condition (code);
7732
7733         switch (code)
7734           {
7735           case LT: case GT: case LTU: case GTU:
7736             break;
7737
7738           case LE:   code1 = LT;  code2 = GT;  break;
7739           case GE:   code1 = GT;  code2 = LT;  break;
7740           case LEU:  code1 = LTU; code2 = GTU; break;
7741           case GEU:  code1 = GTU; code2 = LTU; break;
7742
7743           case EQ:   code1 = NIL; code2 = NE;  break;
7744           case NE:   code2 = NIL; break;
7745
7746           default:
7747             abort ();
7748           }
7749
7750         /*
7751          * a < b =>
7752          *    if (hi(a) < hi(b)) goto true;
7753          *    if (hi(a) > hi(b)) goto false;
7754          *    if (lo(a) < lo(b)) goto true;
7755          *  false:
7756          */
7757
7758         ix86_compare_op0 = hi[0];
7759         ix86_compare_op1 = hi[1];
7760
7761         if (code1 != NIL)
7762           ix86_expand_branch (code1, label);
7763         if (code2 != NIL)
7764           ix86_expand_branch (code2, label2);
7765
7766         ix86_compare_op0 = lo[0];
7767         ix86_compare_op1 = lo[1];
7768         ix86_expand_branch (code3, label);
7769
7770         if (code2 != NIL)
7771           emit_label (label2);
7772         return;
7773       }
7774
7775     default:
7776       abort ();
7777     }
7778 }
7779
7780 /* Split branch based on floating point condition.  */
7781 void
7782 ix86_split_fp_branch (code, op1, op2, target1, target2, tmp)
7783      enum rtx_code code;
7784      rtx op1, op2, target1, target2, tmp;
7785 {
7786   rtx second, bypass;
7787   rtx label = NULL_RTX;
7788   rtx condition;
7789   int bypass_probability = -1, second_probability = -1, probability = -1;
7790   rtx i;
7791
7792   if (target2 != pc_rtx)
7793     {
7794       rtx tmp = target2;
7795       code = reverse_condition_maybe_unordered (code);
7796       target2 = target1;
7797       target1 = tmp;
7798     }
7799
7800   condition = ix86_expand_fp_compare (code, op1, op2,
7801                                       tmp, &second, &bypass);
7802
7803   if (split_branch_probability >= 0)
7804     {
7805       /* Distribute the probabilities across the jumps.
7806          Assume the BYPASS and SECOND to be always test
7807          for UNORDERED.  */
7808       probability = split_branch_probability;
7809
7810       /* Value of 1 is low enought to make no need for probability
7811          to be updated.  Later we may run some experiments and see
7812          if unordered values are more frequent in practice.  */
7813       if (bypass)
7814         bypass_probability = 1;
7815       if (second)
7816         second_probability = 1;
7817     }
7818   if (bypass != NULL_RTX)
7819     {
7820       label = gen_label_rtx ();
7821       i = emit_jump_insn (gen_rtx_SET
7822                           (VOIDmode, pc_rtx,
7823                            gen_rtx_IF_THEN_ELSE (VOIDmode,
7824                                                  bypass,
7825                                                  gen_rtx_LABEL_REF (VOIDmode,
7826                                                                     label),
7827                                                  pc_rtx)));
7828       if (bypass_probability >= 0)
7829         REG_NOTES (i)
7830           = gen_rtx_EXPR_LIST (REG_BR_PROB,
7831                                GEN_INT (bypass_probability),
7832                                REG_NOTES (i));
7833     }
7834   i = emit_jump_insn (gen_rtx_SET
7835                       (VOIDmode, pc_rtx,
7836                        gen_rtx_IF_THEN_ELSE (VOIDmode,
7837                                              condition, target1, target2)));
7838   if (probability >= 0)
7839     REG_NOTES (i)
7840       = gen_rtx_EXPR_LIST (REG_BR_PROB,
7841                            GEN_INT (probability),
7842                            REG_NOTES (i));
7843   if (second != NULL_RTX)
7844     {
7845       i = emit_jump_insn (gen_rtx_SET
7846                           (VOIDmode, pc_rtx,
7847                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
7848                                                  target2)));
7849       if (second_probability >= 0)
7850         REG_NOTES (i)
7851           = gen_rtx_EXPR_LIST (REG_BR_PROB,
7852                                GEN_INT (second_probability),
7853                                REG_NOTES (i));
7854     }
7855   if (label != NULL_RTX)
7856     emit_label (label);
7857 }
7858
7859 int
7860 ix86_expand_setcc (code, dest)
7861      enum rtx_code code;
7862      rtx dest;
7863 {
7864   rtx ret, tmp, tmpreg;
7865   rtx second_test, bypass_test;
7866
7867   if (GET_MODE (ix86_compare_op0) == DImode
7868       && !TARGET_64BIT)
7869     return 0; /* FAIL */
7870
7871   if (GET_MODE (dest) != QImode)
7872     abort ();
7873
7874   ret = ix86_expand_compare (code, &second_test, &bypass_test);
7875   PUT_MODE (ret, QImode);
7876
7877   tmp = dest;
7878   tmpreg = dest;
7879
7880   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
7881   if (bypass_test || second_test)
7882     {
7883       rtx test = second_test;
7884       int bypass = 0;
7885       rtx tmp2 = gen_reg_rtx (QImode);
7886       if (bypass_test)
7887         {
7888           if (second_test)
7889             abort();
7890           test = bypass_test;
7891           bypass = 1;
7892           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
7893         }
7894       PUT_MODE (test, QImode);
7895       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
7896
7897       if (bypass)
7898         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
7899       else
7900         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
7901     }
7902
7903   return 1; /* DONE */
7904 }
7905
7906 int
7907 ix86_expand_int_movcc (operands)
7908      rtx operands[];
7909 {
7910   enum rtx_code code = GET_CODE (operands[1]), compare_code;
7911   rtx compare_seq, compare_op;
7912   rtx second_test, bypass_test;
7913   enum machine_mode mode = GET_MODE (operands[0]);
7914
7915   /* When the compare code is not LTU or GEU, we can not use sbbl case.
7916      In case comparsion is done with immediate, we can convert it to LTU or
7917      GEU by altering the integer.  */
7918
7919   if ((code == LEU || code == GTU)
7920       && GET_CODE (ix86_compare_op1) == CONST_INT
7921       && mode != HImode
7922       && (unsigned int)INTVAL (ix86_compare_op1) != 0xffffffff
7923       && GET_CODE (operands[2]) == CONST_INT
7924       && GET_CODE (operands[3]) == CONST_INT)
7925     {
7926       if (code == LEU)
7927         code = LTU;
7928       else
7929         code = GEU;
7930       ix86_compare_op1 = GEN_INT (INTVAL (ix86_compare_op1) + 1);
7931     }
7932
7933   start_sequence ();
7934   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
7935   compare_seq = gen_sequence ();
7936   end_sequence ();
7937
7938   compare_code = GET_CODE (compare_op);
7939
7940   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
7941      HImode insns, we'd be swallowed in word prefix ops.  */
7942
7943   if (mode != HImode
7944       && (mode != DImode || TARGET_64BIT)
7945       && GET_CODE (operands[2]) == CONST_INT
7946       && GET_CODE (operands[3]) == CONST_INT)
7947     {
7948       rtx out = operands[0];
7949       HOST_WIDE_INT ct = INTVAL (operands[2]);
7950       HOST_WIDE_INT cf = INTVAL (operands[3]);
7951       HOST_WIDE_INT diff;
7952
7953       if ((compare_code == LTU || compare_code == GEU)
7954           && !second_test && !bypass_test)
7955         {
7956
7957           /* Detect overlap between destination and compare sources.  */
7958           rtx tmp = out;
7959
7960           /* To simplify rest of code, restrict to the GEU case.  */
7961           if (compare_code == LTU)
7962             {
7963               int tmp = ct;
7964               ct = cf;
7965               cf = tmp;
7966               compare_code = reverse_condition (compare_code);
7967               code = reverse_condition (code);
7968             }
7969           diff = ct - cf;
7970
7971           if (reg_overlap_mentioned_p (out, ix86_compare_op0)
7972               || reg_overlap_mentioned_p (out, ix86_compare_op1))
7973             tmp = gen_reg_rtx (mode);
7974
7975           emit_insn (compare_seq);
7976           if (mode == DImode)
7977             emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp));
7978           else
7979             emit_insn (gen_x86_movsicc_0_m1 (tmp));
7980
7981           if (diff == 1)
7982             {
7983               /*
7984                * cmpl op0,op1
7985                * sbbl dest,dest
7986                * [addl dest, ct]
7987                *
7988                * Size 5 - 8.
7989                */
7990               if (ct)
7991                 tmp = expand_simple_binop (mode, PLUS,
7992                                            tmp, GEN_INT (ct),
7993                                            tmp, 1, OPTAB_DIRECT);
7994             }
7995           else if (cf == -1)
7996             {
7997               /*
7998                * cmpl op0,op1
7999                * sbbl dest,dest
8000                * orl $ct, dest
8001                *
8002                * Size 8.
8003                */
8004               tmp = expand_simple_binop (mode, IOR,
8005                                          tmp, GEN_INT (ct),
8006                                          tmp, 1, OPTAB_DIRECT);
8007             }
8008           else if (diff == -1 && ct)
8009             {
8010               /*
8011                * cmpl op0,op1
8012                * sbbl dest,dest
8013                * xorl $-1, dest
8014                * [addl dest, cf]
8015                *
8016                * Size 8 - 11.
8017                */
8018               tmp = expand_simple_unop (mode, NOT, tmp, tmp, 1);
8019               if (cf)
8020                 tmp = expand_simple_binop (mode, PLUS,
8021                                            tmp, GEN_INT (cf),
8022                                            tmp, 1, OPTAB_DIRECT);
8023             }
8024           else
8025             {
8026               /*
8027                * cmpl op0,op1
8028                * sbbl dest,dest
8029                * andl cf - ct, dest
8030                * [addl dest, ct]
8031                *
8032                * Size 8 - 11.
8033                */
8034               tmp = expand_simple_binop (mode, AND,
8035                                          tmp,
8036                                          GEN_INT (trunc_int_for_mode
8037                                                   (cf - ct, mode)),
8038                                          tmp, 1, OPTAB_DIRECT);
8039               if (ct)
8040                 tmp = expand_simple_binop (mode, PLUS,
8041                                            tmp, GEN_INT (ct),
8042                                            tmp, 1, OPTAB_DIRECT);
8043             }
8044
8045           if (tmp != out)
8046             emit_move_insn (out, tmp);
8047
8048           return 1; /* DONE */
8049         }
8050
8051       diff = ct - cf;
8052       if (diff < 0)
8053         {
8054           HOST_WIDE_INT tmp;
8055           tmp = ct, ct = cf, cf = tmp;
8056           diff = -diff;
8057           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
8058             {
8059               /* We may be reversing unordered compare to normal compare, that
8060                  is not valid in general (we may convert non-trapping condition
8061                  to trapping one), however on i386 we currently emit all
8062                  comparisons unordered.  */
8063               compare_code = reverse_condition_maybe_unordered (compare_code);
8064               code = reverse_condition_maybe_unordered (code);
8065             }
8066           else
8067             {
8068               compare_code = reverse_condition (compare_code);
8069               code = reverse_condition (code);
8070             }
8071         }
8072       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
8073            || diff == 3 || diff == 5 || diff == 9)
8074           && (mode != DImode || x86_64_sign_extended_value (GEN_INT (cf))))
8075         {
8076           /*
8077            * xorl dest,dest
8078            * cmpl op1,op2
8079            * setcc dest
8080            * lea cf(dest*(ct-cf)),dest
8081            *
8082            * Size 14.
8083            *
8084            * This also catches the degenerate setcc-only case.
8085            */
8086
8087           rtx tmp;
8088           int nops;
8089
8090           out = emit_store_flag (out, code, ix86_compare_op0,
8091                                  ix86_compare_op1, VOIDmode, 0, 1);
8092
8093           nops = 0;
8094           /* On x86_64 the lea instruction operates on Pmode, so we need to get arithmetics
8095              done in proper mode to match.  */
8096           if (diff == 1)
8097             tmp = out;
8098           else
8099             {
8100               rtx out1;
8101               out1 = out;
8102               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
8103               nops++;
8104               if (diff & 1)
8105                 {
8106                   tmp = gen_rtx_PLUS (mode, tmp, out1);
8107                   nops++;
8108                 }
8109             }
8110           if (cf != 0)
8111             {
8112               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
8113               nops++;
8114             }
8115           if (tmp != out
8116               && (GET_CODE (tmp) != SUBREG || SUBREG_REG (tmp) != out))
8117             {
8118               if (nops == 1)
8119                 {
8120                   rtx clob;
8121
8122                   clob = gen_rtx_REG (CCmode, FLAGS_REG);
8123                   clob = gen_rtx_CLOBBER (VOIDmode, clob);
8124
8125                   tmp = gen_rtx_SET (VOIDmode, out, tmp);
8126                   tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
8127                   emit_insn (tmp);
8128                 }
8129               else
8130                 emit_insn (gen_rtx_SET (VOIDmode, out, tmp));
8131             }
8132           if (out != operands[0])
8133             emit_move_insn (operands[0], out);
8134
8135           return 1; /* DONE */
8136         }
8137
8138       /*
8139        * General case:                  Jumpful:
8140        *   xorl dest,dest               cmpl op1, op2
8141        *   cmpl op1, op2                movl ct, dest
8142        *   setcc dest                   jcc 1f
8143        *   decl dest                    movl cf, dest
8144        *   andl (cf-ct),dest            1:
8145        *   addl ct,dest
8146        *
8147        * Size 20.                       Size 14.
8148        *
8149        * This is reasonably steep, but branch mispredict costs are
8150        * high on modern cpus, so consider failing only if optimizing
8151        * for space.
8152        *
8153        * %%% Parameterize branch_cost on the tuning architecture, then
8154        * use that.  The 80386 couldn't care less about mispredicts.
8155        */
8156
8157       if (!optimize_size && !TARGET_CMOVE)
8158         {
8159           if (ct == 0)
8160             {
8161               ct = cf;
8162               cf = 0;
8163               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
8164                 {
8165                   /* We may be reversing unordered compare to normal compare,
8166                      that is not valid in general (we may convert non-trapping
8167                      condition to trapping one), however on i386 we currently
8168                      emit all comparisons unordered.  */
8169                   compare_code = reverse_condition_maybe_unordered (compare_code);
8170                   code = reverse_condition_maybe_unordered (code);
8171                 }
8172               else
8173                 {
8174                   compare_code = reverse_condition (compare_code);
8175                   code = reverse_condition (code);
8176                 }
8177             }
8178
8179           out = emit_store_flag (out, code, ix86_compare_op0,
8180                                  ix86_compare_op1, VOIDmode, 0, 1);
8181
8182           out = expand_simple_binop (mode, PLUS,
8183                                      out, constm1_rtx,
8184                                      out, 1, OPTAB_DIRECT);
8185           out = expand_simple_binop (mode, AND,
8186                                      out,
8187                                      GEN_INT (trunc_int_for_mode
8188                                               (cf - ct, mode)),
8189                                      out, 1, OPTAB_DIRECT);
8190           out = expand_simple_binop (mode, PLUS,
8191                                      out, GEN_INT (ct),
8192                                      out, 1, OPTAB_DIRECT);
8193           if (out != operands[0])
8194             emit_move_insn (operands[0], out);
8195
8196           return 1; /* DONE */
8197         }
8198     }
8199
8200   if (!TARGET_CMOVE)
8201     {
8202       /* Try a few things more with specific constants and a variable.  */
8203
8204       optab op;
8205       rtx var, orig_out, out, tmp;
8206
8207       if (optimize_size)
8208         return 0; /* FAIL */
8209
8210       /* If one of the two operands is an interesting constant, load a
8211          constant with the above and mask it in with a logical operation.  */
8212
8213       if (GET_CODE (operands[2]) == CONST_INT)
8214         {
8215           var = operands[3];
8216           if (INTVAL (operands[2]) == 0)
8217             operands[3] = constm1_rtx, op = and_optab;
8218           else if (INTVAL (operands[2]) == -1)
8219             operands[3] = const0_rtx, op = ior_optab;
8220           else
8221             return 0; /* FAIL */
8222         }
8223       else if (GET_CODE (operands[3]) == CONST_INT)
8224         {
8225           var = operands[2];
8226           if (INTVAL (operands[3]) == 0)
8227             operands[2] = constm1_rtx, op = and_optab;
8228           else if (INTVAL (operands[3]) == -1)
8229             operands[2] = const0_rtx, op = ior_optab;
8230           else
8231             return 0; /* FAIL */
8232         }
8233       else
8234         return 0; /* FAIL */
8235
8236       orig_out = operands[0];
8237       tmp = gen_reg_rtx (mode);
8238       operands[0] = tmp;
8239
8240       /* Recurse to get the constant loaded.  */
8241       if (ix86_expand_int_movcc (operands) == 0)
8242         return 0; /* FAIL */
8243
8244       /* Mask in the interesting variable.  */
8245       out = expand_binop (mode, op, var, tmp, orig_out, 0,
8246                           OPTAB_WIDEN);
8247       if (out != orig_out)
8248         emit_move_insn (orig_out, out);
8249
8250       return 1; /* DONE */
8251     }
8252
8253   /*
8254    * For comparison with above,
8255    *
8256    * movl cf,dest
8257    * movl ct,tmp
8258    * cmpl op1,op2
8259    * cmovcc tmp,dest
8260    *
8261    * Size 15.
8262    */
8263
8264   if (! nonimmediate_operand (operands[2], mode))
8265     operands[2] = force_reg (mode, operands[2]);
8266   if (! nonimmediate_operand (operands[3], mode))
8267     operands[3] = force_reg (mode, operands[3]);
8268
8269   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
8270     {
8271       rtx tmp = gen_reg_rtx (mode);
8272       emit_move_insn (tmp, operands[3]);
8273       operands[3] = tmp;
8274     }
8275   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
8276     {
8277       rtx tmp = gen_reg_rtx (mode);
8278       emit_move_insn (tmp, operands[2]);
8279       operands[2] = tmp;
8280     }
8281   if (! register_operand (operands[2], VOIDmode)
8282       && ! register_operand (operands[3], VOIDmode))
8283     operands[2] = force_reg (mode, operands[2]);
8284
8285   emit_insn (compare_seq);
8286   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
8287                           gen_rtx_IF_THEN_ELSE (mode,
8288                                                 compare_op, operands[2],
8289                                                 operands[3])));
8290   if (bypass_test)
8291     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
8292                             gen_rtx_IF_THEN_ELSE (mode,
8293                                   bypass_test,
8294                                   operands[3],
8295                                   operands[0])));
8296   if (second_test)
8297     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
8298                             gen_rtx_IF_THEN_ELSE (mode,
8299                                   second_test,
8300                                   operands[2],
8301                                   operands[0])));
8302
8303   return 1; /* DONE */
8304 }
8305
8306 int
8307 ix86_expand_fp_movcc (operands)
8308      rtx operands[];
8309 {
8310   enum rtx_code code;
8311   rtx tmp;
8312   rtx compare_op, second_test, bypass_test;
8313
8314   /* For SF/DFmode conditional moves based on comparisons
8315      in same mode, we may want to use SSE min/max instructions.  */
8316   if (((TARGET_SSE && GET_MODE (operands[0]) == SFmode)
8317        || (TARGET_SSE2 && GET_MODE (operands[0]) == DFmode))
8318       && GET_MODE (ix86_compare_op0) == GET_MODE (operands[0])
8319       /* The SSE comparisons does not support the LTGT/UNEQ pair.  */
8320       && (!TARGET_IEEE_FP
8321           || (GET_CODE (operands[1]) != LTGT && GET_CODE (operands[1]) != UNEQ))
8322       /* We may be called from the post-reload splitter.  */
8323       && (!REG_P (operands[0])
8324           || SSE_REG_P (operands[0])
8325           || REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER))
8326     {
8327       rtx op0 = ix86_compare_op0, op1 = ix86_compare_op1;
8328       code = GET_CODE (operands[1]);
8329
8330       /* See if we have (cross) match between comparison operands and
8331          conditional move operands.  */
8332       if (rtx_equal_p (operands[2], op1))
8333         {
8334           rtx tmp = op0;
8335           op0 = op1;
8336           op1 = tmp;
8337           code = reverse_condition_maybe_unordered (code);
8338         }
8339       if (rtx_equal_p (operands[2], op0) && rtx_equal_p (operands[3], op1))
8340         {
8341           /* Check for min operation.  */
8342           if (code == LT)
8343             {
8344                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
8345                if (memory_operand (op0, VOIDmode))
8346                  op0 = force_reg (GET_MODE (operands[0]), op0);
8347                if (GET_MODE (operands[0]) == SFmode)
8348                  emit_insn (gen_minsf3 (operands[0], op0, op1));
8349                else
8350                  emit_insn (gen_mindf3 (operands[0], op0, op1));
8351                return 1;
8352             }
8353           /* Check for max operation.  */
8354           if (code == GT)
8355             {
8356                operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
8357                if (memory_operand (op0, VOIDmode))
8358                  op0 = force_reg (GET_MODE (operands[0]), op0);
8359                if (GET_MODE (operands[0]) == SFmode)
8360                  emit_insn (gen_maxsf3 (operands[0], op0, op1));
8361                else
8362                  emit_insn (gen_maxdf3 (operands[0], op0, op1));
8363                return 1;
8364             }
8365         }
8366       /* Manage condition to be sse_comparison_operator.  In case we are
8367          in non-ieee mode, try to canonicalize the destination operand
8368          to be first in the comparison - this helps reload to avoid extra
8369          moves.  */
8370       if (!sse_comparison_operator (operands[1], VOIDmode)
8371           || (rtx_equal_p (operands[0], ix86_compare_op1) && !TARGET_IEEE_FP))
8372         {
8373           rtx tmp = ix86_compare_op0;
8374           ix86_compare_op0 = ix86_compare_op1;
8375           ix86_compare_op1 = tmp;
8376           operands[1] = gen_rtx_fmt_ee (swap_condition (GET_CODE (operands[1])),
8377                                         VOIDmode, ix86_compare_op0,
8378                                         ix86_compare_op1);
8379         }
8380       /* Similary try to manage result to be first operand of conditional
8381          move. We also don't support the NE comparison on SSE, so try to
8382          avoid it.  */
8383       if ((rtx_equal_p (operands[0], operands[3])
8384            && (!TARGET_IEEE_FP || GET_CODE (operands[1]) != EQ))
8385           || (GET_CODE (operands[1]) == NE && TARGET_IEEE_FP))
8386         {
8387           rtx tmp = operands[2];
8388           operands[2] = operands[3];
8389           operands[3] = tmp;
8390           operands[1] = gen_rtx_fmt_ee (reverse_condition_maybe_unordered
8391                                           (GET_CODE (operands[1])),
8392                                         VOIDmode, ix86_compare_op0,
8393                                         ix86_compare_op1);
8394         }
8395       if (GET_MODE (operands[0]) == SFmode)
8396         emit_insn (gen_sse_movsfcc (operands[0], operands[1],
8397                                     operands[2], operands[3],
8398                                     ix86_compare_op0, ix86_compare_op1));
8399       else
8400         emit_insn (gen_sse_movdfcc (operands[0], operands[1],
8401                                     operands[2], operands[3],
8402                                     ix86_compare_op0, ix86_compare_op1));
8403       return 1;
8404     }
8405
8406   /* The floating point conditional move instructions don't directly
8407      support conditions resulting from a signed integer comparison.  */
8408
8409   code = GET_CODE (operands[1]);
8410   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
8411
8412   /* The floating point conditional move instructions don't directly
8413      support signed integer comparisons.  */
8414
8415   if (!fcmov_comparison_operator (compare_op, VOIDmode))
8416     {
8417       if (second_test != NULL || bypass_test != NULL)
8418         abort();
8419       tmp = gen_reg_rtx (QImode);
8420       ix86_expand_setcc (code, tmp);
8421       code = NE;
8422       ix86_compare_op0 = tmp;
8423       ix86_compare_op1 = const0_rtx;
8424       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
8425     }
8426   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
8427     {
8428       tmp = gen_reg_rtx (GET_MODE (operands[0]));
8429       emit_move_insn (tmp, operands[3]);
8430       operands[3] = tmp;
8431     }
8432   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
8433     {
8434       tmp = gen_reg_rtx (GET_MODE (operands[0]));
8435       emit_move_insn (tmp, operands[2]);
8436       operands[2] = tmp;
8437     }
8438
8439   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
8440                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
8441                                 compare_op,
8442                                 operands[2],
8443                                 operands[3])));
8444   if (bypass_test)
8445     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
8446                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
8447                                   bypass_test,
8448                                   operands[3],
8449                                   operands[0])));
8450   if (second_test)
8451     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
8452                             gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
8453                                   second_test,
8454                                   operands[2],
8455                                   operands[0])));
8456
8457   return 1;
8458 }
8459
8460 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
8461    works for floating pointer parameters and nonoffsetable memories.
8462    For pushes, it returns just stack offsets; the values will be saved
8463    in the right order.  Maximally three parts are generated.  */
8464
8465 static int
8466 ix86_split_to_parts (operand, parts, mode)
8467      rtx operand;
8468      rtx *parts;
8469      enum machine_mode mode;
8470 {
8471   int size;
8472
8473   if (!TARGET_64BIT)
8474     size = mode == TFmode ? 3 : (GET_MODE_SIZE (mode) / 4);
8475   else
8476     size = (GET_MODE_SIZE (mode) + 4) / 8;
8477
8478   if (GET_CODE (operand) == REG && MMX_REGNO_P (REGNO (operand)))
8479     abort ();
8480   if (size < 2 || size > 3)
8481     abort ();
8482
8483   /* Optimize constant pool reference to immediates.  This is used by fp moves,
8484      that force all constants to memory to allow combining.  */
8485
8486   if (GET_CODE (operand) == MEM
8487       && GET_CODE (XEXP (operand, 0)) == SYMBOL_REF
8488       && CONSTANT_POOL_ADDRESS_P (XEXP (operand, 0)))
8489     operand = get_pool_constant (XEXP (operand, 0));
8490
8491   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
8492     {
8493       /* The only non-offsetable memories we handle are pushes.  */
8494       if (! push_operand (operand, VOIDmode))
8495         abort ();
8496
8497       operand = copy_rtx (operand);
8498       PUT_MODE (operand, Pmode);
8499       parts[0] = parts[1] = parts[2] = operand;
8500     }
8501   else if (!TARGET_64BIT)
8502     {
8503       if (mode == DImode)
8504         split_di (&operand, 1, &parts[0], &parts[1]);
8505       else
8506         {
8507           if (REG_P (operand))
8508             {
8509               if (!reload_completed)
8510                 abort ();
8511               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
8512               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
8513               if (size == 3)
8514                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
8515             }
8516           else if (offsettable_memref_p (operand))
8517             {
8518               operand = adjust_address (operand, SImode, 0);
8519               parts[0] = operand;
8520               parts[1] = adjust_address (operand, SImode, 4);
8521               if (size == 3)
8522                 parts[2] = adjust_address (operand, SImode, 8);
8523             }
8524           else if (GET_CODE (operand) == CONST_DOUBLE)
8525             {
8526               REAL_VALUE_TYPE r;
8527               long l[4];
8528
8529               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
8530               switch (mode)
8531                 {
8532                 case XFmode:
8533                 case TFmode:
8534                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
8535                   parts[2] = GEN_INT (trunc_int_for_mode (l[2], SImode));
8536                   break;
8537                 case DFmode:
8538                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8539                   break;
8540                 default:
8541                   abort ();
8542                 }
8543               parts[1] = GEN_INT (trunc_int_for_mode (l[1], SImode));
8544               parts[0] = GEN_INT (trunc_int_for_mode (l[0], SImode));
8545             }
8546           else
8547             abort ();
8548         }
8549     }
8550   else
8551     {
8552       if (mode == TImode)
8553         split_ti (&operand, 1, &parts[0], &parts[1]);
8554       if (mode == XFmode || mode == TFmode)
8555         {
8556           if (REG_P (operand))
8557             {
8558               if (!reload_completed)
8559                 abort ();
8560               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
8561               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
8562             }
8563           else if (offsettable_memref_p (operand))
8564             {
8565               operand = adjust_address (operand, DImode, 0);
8566               parts[0] = operand;
8567               parts[1] = adjust_address (operand, SImode, 8);
8568             }
8569           else if (GET_CODE (operand) == CONST_DOUBLE)
8570             {
8571               REAL_VALUE_TYPE r;
8572               long l[3];
8573
8574               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
8575               REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
8576               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
8577               if (HOST_BITS_PER_WIDE_INT >= 64)
8578                 parts[0]
8579                   = GEN_INT (trunc_int_for_mode
8580                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
8581                        + ((((HOST_WIDE_INT)l[1]) << 31) << 1),
8582                        DImode));
8583               else
8584                 parts[0] = immed_double_const (l[0], l[1], DImode);
8585               parts[1] = GEN_INT (trunc_int_for_mode (l[2], SImode));
8586             }
8587           else
8588             abort ();
8589         }
8590     }
8591
8592   return size;
8593 }
8594
8595 /* Emit insns to perform a move or push of DI, DF, and XF values.
8596    Return false when normal moves are needed; true when all required
8597    insns have been emitted.  Operands 2-4 contain the input values
8598    int the correct order; operands 5-7 contain the output values.  */
8599
8600 void
8601 ix86_split_long_move (operands)
8602      rtx operands[];
8603 {
8604   rtx part[2][3];
8605   int nparts;
8606   int push = 0;
8607   int collisions = 0;
8608   enum machine_mode mode = GET_MODE (operands[0]);
8609
8610   /* The DFmode expanders may ask us to move double.
8611      For 64bit target this is single move.  By hiding the fact
8612      here we simplify i386.md splitters.  */
8613   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
8614     {
8615       /* Optimize constant pool reference to immediates.  This is used by fp moves,
8616          that force all constants to memory to allow combining.  */
8617
8618       if (GET_CODE (operands[1]) == MEM
8619           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
8620           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
8621         operands[1] = get_pool_constant (XEXP (operands[1], 0));
8622       if (push_operand (operands[0], VOIDmode))
8623         {
8624           operands[0] = copy_rtx (operands[0]);
8625           PUT_MODE (operands[0], Pmode);
8626         }
8627       else
8628         operands[0] = gen_lowpart (DImode, operands[0]);
8629       operands[1] = gen_lowpart (DImode, operands[1]);
8630       emit_move_insn (operands[0], operands[1]);
8631       return;
8632     }
8633
8634   /* The only non-offsettable memory we handle is push.  */
8635   if (push_operand (operands[0], VOIDmode))
8636     push = 1;
8637   else if (GET_CODE (operands[0]) == MEM
8638            && ! offsettable_memref_p (operands[0]))
8639     abort ();
8640
8641   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
8642   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
8643
8644   /* When emitting push, take care for source operands on the stack.  */
8645   if (push && GET_CODE (operands[1]) == MEM
8646       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
8647     {
8648       if (nparts == 3)
8649         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
8650                                      XEXP (part[1][2], 0));
8651       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
8652                                    XEXP (part[1][1], 0));
8653     }
8654
8655   /* We need to do copy in the right order in case an address register
8656      of the source overlaps the destination.  */
8657   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
8658     {
8659       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
8660         collisions++;
8661       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
8662         collisions++;
8663       if (nparts == 3
8664           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
8665         collisions++;
8666
8667       /* Collision in the middle part can be handled by reordering.  */
8668       if (collisions == 1 && nparts == 3
8669           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
8670         {
8671           rtx tmp;
8672           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
8673           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
8674         }
8675
8676       /* If there are more collisions, we can't handle it by reordering.
8677          Do an lea to the last part and use only one colliding move.  */
8678       else if (collisions > 1)
8679         {
8680           collisions = 1;
8681           emit_insn (gen_rtx_SET (VOIDmode, part[0][nparts - 1],
8682                                   XEXP (part[1][0], 0)));
8683           part[1][0] = change_address (part[1][0],
8684                                        TARGET_64BIT ? DImode : SImode,
8685                                        part[0][nparts - 1]);
8686           part[1][1] = adjust_address (part[1][0], VOIDmode, UNITS_PER_WORD);
8687           if (nparts == 3)
8688             part[1][2] = adjust_address (part[1][0], VOIDmode, 8);
8689         }
8690     }
8691
8692   if (push)
8693     {
8694       if (!TARGET_64BIT)
8695         {
8696           if (nparts == 3)
8697             {
8698               /* We use only first 12 bytes of TFmode value, but for pushing we
8699                  are required to adjust stack as if we were pushing real 16byte
8700                  value.  */
8701               if (mode == TFmode && !TARGET_64BIT)
8702                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
8703                                        GEN_INT (-4)));
8704               emit_move_insn (part[0][2], part[1][2]);
8705             }
8706         }
8707       else
8708         {
8709           /* In 64bit mode we don't have 32bit push available.  In case this is
8710              register, it is OK - we will just use larger counterpart.  We also
8711              retype memory - these comes from attempt to avoid REX prefix on
8712              moving of second half of TFmode value.  */
8713           if (GET_MODE (part[1][1]) == SImode)
8714             {
8715               if (GET_CODE (part[1][1]) == MEM)
8716                 part[1][1] = adjust_address (part[1][1], DImode, 0);
8717               else if (REG_P (part[1][1]))
8718                 part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
8719               else
8720                 abort();
8721               if (GET_MODE (part[1][0]) == SImode)
8722                 part[1][0] = part[1][1];
8723             }
8724         }
8725       emit_move_insn (part[0][1], part[1][1]);
8726       emit_move_insn (part[0][0], part[1][0]);
8727       return;
8728     }
8729
8730   /* Choose correct order to not overwrite the source before it is copied.  */
8731   if ((REG_P (part[0][0])
8732        && REG_P (part[1][1])
8733        && (REGNO (part[0][0]) == REGNO (part[1][1])
8734            || (nparts == 3
8735                && REGNO (part[0][0]) == REGNO (part[1][2]))))
8736       || (collisions > 0
8737           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
8738     {
8739       if (nparts == 3)
8740         {
8741           operands[2] = part[0][2];
8742           operands[3] = part[0][1];
8743           operands[4] = part[0][0];
8744           operands[5] = part[1][2];
8745           operands[6] = part[1][1];
8746           operands[7] = part[1][0];
8747         }
8748       else
8749         {
8750           operands[2] = part[0][1];
8751           operands[3] = part[0][0];
8752           operands[5] = part[1][1];
8753           operands[6] = part[1][0];
8754         }
8755     }
8756   else
8757     {
8758       if (nparts == 3)
8759         {
8760           operands[2] = part[0][0];
8761           operands[3] = part[0][1];
8762           operands[4] = part[0][2];
8763           operands[5] = part[1][0];
8764           operands[6] = part[1][1];
8765           operands[7] = part[1][2];
8766         }
8767       else
8768         {
8769           operands[2] = part[0][0];
8770           operands[3] = part[0][1];
8771           operands[5] = part[1][0];
8772           operands[6] = part[1][1];
8773         }
8774     }
8775   emit_move_insn (operands[2], operands[5]);
8776   emit_move_insn (operands[3], operands[6]);
8777   if (nparts == 3)
8778     emit_move_insn (operands[4], operands[7]);
8779
8780   return;
8781 }
8782
8783 void
8784 ix86_split_ashldi (operands, scratch)
8785      rtx *operands, scratch;
8786 {
8787   rtx low[2], high[2];
8788   int count;
8789
8790   if (GET_CODE (operands[2]) == CONST_INT)
8791     {
8792       split_di (operands, 2, low, high);
8793       count = INTVAL (operands[2]) & 63;
8794
8795       if (count >= 32)
8796         {
8797           emit_move_insn (high[0], low[1]);
8798           emit_move_insn (low[0], const0_rtx);
8799
8800           if (count > 32)
8801             emit_insn (gen_ashlsi3 (high[0], high[0], GEN_INT (count - 32)));
8802         }
8803       else
8804         {
8805           if (!rtx_equal_p (operands[0], operands[1]))
8806             emit_move_insn (operands[0], operands[1]);
8807           emit_insn (gen_x86_shld_1 (high[0], low[0], GEN_INT (count)));
8808           emit_insn (gen_ashlsi3 (low[0], low[0], GEN_INT (count)));
8809         }
8810     }
8811   else
8812     {
8813       if (!rtx_equal_p (operands[0], operands[1]))
8814         emit_move_insn (operands[0], operands[1]);
8815
8816       split_di (operands, 1, low, high);
8817
8818       emit_insn (gen_x86_shld_1 (high[0], low[0], operands[2]));
8819       emit_insn (gen_ashlsi3 (low[0], low[0], operands[2]));
8820
8821       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
8822         {
8823           if (! no_new_pseudos)
8824             scratch = force_reg (SImode, const0_rtx);
8825           else
8826             emit_move_insn (scratch, const0_rtx);
8827
8828           emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2],
8829                                           scratch));
8830         }
8831       else
8832         emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
8833     }
8834 }
8835
8836 void
8837 ix86_split_ashrdi (operands, scratch)
8838      rtx *operands, scratch;
8839 {
8840   rtx low[2], high[2];
8841   int count;
8842
8843   if (GET_CODE (operands[2]) == CONST_INT)
8844     {
8845       split_di (operands, 2, low, high);
8846       count = INTVAL (operands[2]) & 63;
8847
8848       if (count >= 32)
8849         {
8850           emit_move_insn (low[0], high[1]);
8851
8852           if (! reload_completed)
8853             emit_insn (gen_ashrsi3 (high[0], low[0], GEN_INT (31)));
8854           else
8855             {
8856               emit_move_insn (high[0], low[0]);
8857               emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
8858             }
8859
8860           if (count > 32)
8861             emit_insn (gen_ashrsi3 (low[0], low[0], GEN_INT (count - 32)));
8862         }
8863       else
8864         {
8865           if (!rtx_equal_p (operands[0], operands[1]))
8866             emit_move_insn (operands[0], operands[1]);
8867           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
8868           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (count)));
8869         }
8870     }
8871   else
8872     {
8873       if (!rtx_equal_p (operands[0], operands[1]))
8874         emit_move_insn (operands[0], operands[1]);
8875
8876       split_di (operands, 1, low, high);
8877
8878       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
8879       emit_insn (gen_ashrsi3 (high[0], high[0], operands[2]));
8880
8881       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
8882         {
8883           if (! no_new_pseudos)
8884             scratch = gen_reg_rtx (SImode);
8885           emit_move_insn (scratch, high[0]);
8886           emit_insn (gen_ashrsi3 (scratch, scratch, GEN_INT (31)));
8887           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
8888                                           scratch));
8889         }
8890       else
8891         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
8892     }
8893 }
8894
8895 void
8896 ix86_split_lshrdi (operands, scratch)
8897      rtx *operands, scratch;
8898 {
8899   rtx low[2], high[2];
8900   int count;
8901
8902   if (GET_CODE (operands[2]) == CONST_INT)
8903     {
8904       split_di (operands, 2, low, high);
8905       count = INTVAL (operands[2]) & 63;
8906
8907       if (count >= 32)
8908         {
8909           emit_move_insn (low[0], high[1]);
8910           emit_move_insn (high[0], const0_rtx);
8911
8912           if (count > 32)
8913             emit_insn (gen_lshrsi3 (low[0], low[0], GEN_INT (count - 32)));
8914         }
8915       else
8916         {
8917           if (!rtx_equal_p (operands[0], operands[1]))
8918             emit_move_insn (operands[0], operands[1]);
8919           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
8920           emit_insn (gen_lshrsi3 (high[0], high[0], GEN_INT (count)));
8921         }
8922     }
8923   else
8924     {
8925       if (!rtx_equal_p (operands[0], operands[1]))
8926         emit_move_insn (operands[0], operands[1]);
8927
8928       split_di (operands, 1, low, high);
8929
8930       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
8931       emit_insn (gen_lshrsi3 (high[0], high[0], operands[2]));
8932
8933       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
8934       if (TARGET_CMOVE && (! no_new_pseudos || scratch))
8935         {
8936           if (! no_new_pseudos)
8937             scratch = force_reg (SImode, const0_rtx);
8938           else
8939             emit_move_insn (scratch, const0_rtx);
8940
8941           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
8942                                           scratch));
8943         }
8944       else
8945         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
8946     }
8947 }
8948
8949 /* Helper function for the string operations below.  Dest VARIABLE whether
8950    it is aligned to VALUE bytes.  If true, jump to the label.  */
8951 static rtx
8952 ix86_expand_aligntest (variable, value)
8953      rtx variable;
8954      int value;
8955 {
8956   rtx label = gen_label_rtx ();
8957   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
8958   if (GET_MODE (variable) == DImode)
8959     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
8960   else
8961     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
8962   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
8963                            1, 0, label);
8964   return label;
8965 }
8966
8967 /* Adjust COUNTER by the VALUE.  */
8968 static void
8969 ix86_adjust_counter (countreg, value)
8970      rtx countreg;
8971      HOST_WIDE_INT value;
8972 {
8973   if (GET_MODE (countreg) == DImode)
8974     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
8975   else
8976     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
8977 }
8978
8979 /* Zero extend possibly SImode EXP to Pmode register.  */
8980 rtx
8981 ix86_zero_extend_to_Pmode (exp)
8982    rtx exp;
8983 {
8984   rtx r;
8985   if (GET_MODE (exp) == VOIDmode)
8986     return force_reg (Pmode, exp);
8987   if (GET_MODE (exp) == Pmode)
8988     return copy_to_mode_reg (Pmode, exp);
8989   r = gen_reg_rtx (Pmode);
8990   emit_insn (gen_zero_extendsidi2 (r, exp));
8991   return r;
8992 }
8993
8994 /* Expand string move (memcpy) operation.  Use i386 string operations when
8995    profitable.  expand_clrstr contains similar code.  */
8996 int
8997 ix86_expand_movstr (dst, src, count_exp, align_exp)
8998      rtx dst, src, count_exp, align_exp;
8999 {
9000   rtx srcreg, destreg, countreg;
9001   enum machine_mode counter_mode;
9002   HOST_WIDE_INT align = 0;
9003   unsigned HOST_WIDE_INT count = 0;
9004   rtx insns;
9005
9006   start_sequence ();
9007
9008   if (GET_CODE (align_exp) == CONST_INT)
9009     align = INTVAL (align_exp);
9010
9011   /* This simple hack avoids all inlining code and simplifies code bellow.  */
9012   if (!TARGET_ALIGN_STRINGOPS)
9013     align = 64;
9014
9015   if (GET_CODE (count_exp) == CONST_INT)
9016     count = INTVAL (count_exp);
9017
9018   /* Figure out proper mode for counter.  For 32bits it is always SImode,
9019      for 64bits use SImode when possible, otherwise DImode.
9020      Set count to number of bytes copied when known at compile time.  */
9021   if (!TARGET_64BIT || GET_MODE (count_exp) == SImode
9022       || x86_64_zero_extended_value (count_exp))
9023     counter_mode = SImode;
9024   else
9025     counter_mode = DImode;
9026
9027   if (counter_mode != SImode && counter_mode != DImode)
9028     abort ();
9029
9030   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
9031   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
9032
9033   emit_insn (gen_cld ());
9034
9035   /* When optimizing for size emit simple rep ; movsb instruction for
9036      counts not divisible by 4.  */
9037
9038   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
9039     {
9040       countreg = ix86_zero_extend_to_Pmode (count_exp);
9041       if (TARGET_64BIT)
9042         emit_insn (gen_rep_movqi_rex64 (destreg, srcreg, countreg,
9043                                         destreg, srcreg, countreg));
9044       else
9045         emit_insn (gen_rep_movqi (destreg, srcreg, countreg,
9046                                   destreg, srcreg, countreg));
9047     }
9048
9049   /* For constant aligned (or small unaligned) copies use rep movsl
9050      followed by code copying the rest.  For PentiumPro ensure 8 byte
9051      alignment to allow rep movsl acceleration.  */
9052
9053   else if (count != 0
9054            && (align >= 8
9055                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
9056                || optimize_size || count < (unsigned int)64))
9057     {
9058       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
9059       if (count & ~(size - 1))
9060         {
9061           countreg = copy_to_mode_reg (counter_mode,
9062                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
9063                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
9064           countreg = ix86_zero_extend_to_Pmode (countreg);
9065           if (size == 4)
9066             {
9067               if (TARGET_64BIT)
9068                 emit_insn (gen_rep_movsi_rex64 (destreg, srcreg, countreg,
9069                                                 destreg, srcreg, countreg));
9070               else
9071                 emit_insn (gen_rep_movsi (destreg, srcreg, countreg,
9072                                           destreg, srcreg, countreg));
9073             }
9074           else
9075             emit_insn (gen_rep_movdi_rex64 (destreg, srcreg, countreg,
9076                                             destreg, srcreg, countreg));
9077         }
9078       if (size == 8 && (count & 0x04))
9079         emit_insn (gen_strmovsi (destreg, srcreg));
9080       if (count & 0x02)
9081         emit_insn (gen_strmovhi (destreg, srcreg));
9082       if (count & 0x01)
9083         emit_insn (gen_strmovqi (destreg, srcreg));
9084     }
9085   /* The generic code based on the glibc implementation:
9086      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
9087      allowing accelerated copying there)
9088      - copy the data using rep movsl
9089      - copy the rest.  */
9090   else
9091     {
9092       rtx countreg2;
9093       rtx label = NULL;
9094
9095       /* In case we don't know anything about the alignment, default to
9096          library version, since it is usually equally fast and result in
9097          shorter code.  */
9098       if (!TARGET_INLINE_ALL_STRINGOPS && align < UNITS_PER_WORD)
9099         {
9100           end_sequence ();
9101           return 0;
9102         }
9103
9104       if (TARGET_SINGLE_STRINGOP)
9105         emit_insn (gen_cld ());
9106
9107       countreg2 = gen_reg_rtx (Pmode);
9108       countreg = copy_to_mode_reg (counter_mode, count_exp);
9109
9110       /* We don't use loops to align destination and to copy parts smaller
9111          than 4 bytes, because gcc is able to optimize such code better (in
9112          the case the destination or the count really is aligned, gcc is often
9113          able to predict the branches) and also it is friendlier to the
9114          hardware branch prediction.
9115
9116          Using loops is benefical for generic case, because we can
9117          handle small counts using the loops.  Many CPUs (such as Athlon)
9118          have large REP prefix setup costs.
9119
9120          This is quite costy.  Maybe we can revisit this decision later or
9121          add some customizability to this code.  */
9122
9123       if (count == 0
9124           && align < (TARGET_PENTIUMPRO && (count == 0
9125                                             || count >= (unsigned int)260)
9126                       ? 8 : UNITS_PER_WORD))
9127         {
9128           label = gen_label_rtx ();
9129           emit_cmp_and_jump_insns (countreg, GEN_INT (UNITS_PER_WORD - 1),
9130                                    LEU, 0, counter_mode, 1, 0, label);
9131         }
9132       if (align <= 1)
9133         {
9134           rtx label = ix86_expand_aligntest (destreg, 1);
9135           emit_insn (gen_strmovqi (destreg, srcreg));
9136           ix86_adjust_counter (countreg, 1);
9137           emit_label (label);
9138           LABEL_NUSES (label) = 1;
9139         }
9140       if (align <= 2)
9141         {
9142           rtx label = ix86_expand_aligntest (destreg, 2);
9143           emit_insn (gen_strmovhi (destreg, srcreg));
9144           ix86_adjust_counter (countreg, 2);
9145           emit_label (label);
9146           LABEL_NUSES (label) = 1;
9147         }
9148       if (align <= 4
9149           && ((TARGET_PENTIUMPRO && (count == 0
9150                                      || count >= (unsigned int)260))
9151               || TARGET_64BIT))
9152         {
9153           rtx label = ix86_expand_aligntest (destreg, 4);
9154           emit_insn (gen_strmovsi (destreg, srcreg));
9155           ix86_adjust_counter (countreg, 4);
9156           emit_label (label);
9157           LABEL_NUSES (label) = 1;
9158         }
9159
9160       if (!TARGET_SINGLE_STRINGOP)
9161         emit_insn (gen_cld ());
9162       if (TARGET_64BIT)
9163         {
9164           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
9165                                   GEN_INT (3)));
9166           emit_insn (gen_rep_movdi_rex64 (destreg, srcreg, countreg2,
9167                                           destreg, srcreg, countreg2));
9168         }
9169       else
9170         {
9171           emit_insn (gen_lshrsi3 (countreg2, countreg, GEN_INT (2)));
9172           emit_insn (gen_rep_movsi (destreg, srcreg, countreg2,
9173                                     destreg, srcreg, countreg2));
9174         }
9175
9176       if (label)
9177         {
9178           emit_label (label);
9179           LABEL_NUSES (label) = 1;
9180         }
9181       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
9182         emit_insn (gen_strmovsi (destreg, srcreg));
9183       if ((align <= 4 || count == 0) && TARGET_64BIT)
9184         {
9185           rtx label = ix86_expand_aligntest (countreg, 4);
9186           emit_insn (gen_strmovsi (destreg, srcreg));
9187           emit_label (label);
9188           LABEL_NUSES (label) = 1;
9189         }
9190       if (align > 2 && count != 0 && (count & 2))
9191         emit_insn (gen_strmovhi (destreg, srcreg));
9192       if (align <= 2 || count == 0)
9193         {
9194           rtx label = ix86_expand_aligntest (countreg, 2);
9195           emit_insn (gen_strmovhi (destreg, srcreg));
9196           emit_label (label);
9197           LABEL_NUSES (label) = 1;
9198         }
9199       if (align > 1 && count != 0 && (count & 1))
9200         emit_insn (gen_strmovqi (destreg, srcreg));
9201       if (align <= 1 || count == 0)
9202         {
9203           rtx label = ix86_expand_aligntest (countreg, 1);
9204           emit_insn (gen_strmovqi (destreg, srcreg));
9205           emit_label (label);
9206           LABEL_NUSES (label) = 1;
9207         }
9208     }
9209
9210   insns = get_insns ();
9211   end_sequence ();
9212
9213   ix86_set_move_mem_attrs (insns, dst, src, destreg, srcreg);
9214   emit_insns (insns);
9215   return 1;
9216 }
9217
9218 /* Expand string clear operation (bzero).  Use i386 string operations when
9219    profitable.  expand_movstr contains similar code.  */
9220 int
9221 ix86_expand_clrstr (src, count_exp, align_exp)
9222      rtx src, count_exp, align_exp;
9223 {
9224   rtx destreg, zeroreg, countreg;
9225   enum machine_mode counter_mode;
9226   HOST_WIDE_INT align = 0;
9227   unsigned HOST_WIDE_INT count = 0;
9228
9229   if (GET_CODE (align_exp) == CONST_INT)
9230     align = INTVAL (align_exp);
9231
9232   /* This simple hack avoids all inlining code and simplifies code bellow.  */
9233   if (!TARGET_ALIGN_STRINGOPS)
9234     align = 32;
9235
9236   if (GET_CODE (count_exp) == CONST_INT)
9237     count = INTVAL (count_exp);
9238   /* Figure out proper mode for counter.  For 32bits it is always SImode,
9239      for 64bits use SImode when possible, otherwise DImode.
9240      Set count to number of bytes copied when known at compile time.  */
9241   if (!TARGET_64BIT || GET_MODE (count_exp) == SImode
9242       || x86_64_zero_extended_value (count_exp))
9243     counter_mode = SImode;
9244   else
9245     counter_mode = DImode;
9246
9247   destreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
9248
9249   emit_insn (gen_cld ());
9250
9251   /* When optimizing for size emit simple rep ; movsb instruction for
9252      counts not divisible by 4.  */
9253
9254   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
9255     {
9256       countreg = ix86_zero_extend_to_Pmode (count_exp);
9257       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
9258       if (TARGET_64BIT)
9259         emit_insn (gen_rep_stosqi_rex64 (destreg, countreg, zeroreg,
9260                                          destreg, countreg));
9261       else
9262         emit_insn (gen_rep_stosqi (destreg, countreg, zeroreg,
9263                                    destreg, countreg));
9264     }
9265   else if (count != 0
9266            && (align >= 8
9267                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
9268                || optimize_size || count < (unsigned int)64))
9269     {
9270       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
9271       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
9272       if (count & ~(size - 1))
9273         {
9274           countreg = copy_to_mode_reg (counter_mode,
9275                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
9276                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
9277           countreg = ix86_zero_extend_to_Pmode (countreg);
9278           if (size == 4)
9279             {
9280               if (TARGET_64BIT)
9281                 emit_insn (gen_rep_stossi_rex64 (destreg, countreg, zeroreg,
9282                                                  destreg, countreg));
9283               else
9284                 emit_insn (gen_rep_stossi (destreg, countreg, zeroreg,
9285                                            destreg, countreg));
9286             }
9287           else
9288             emit_insn (gen_rep_stosdi_rex64 (destreg, countreg, zeroreg,
9289                                              destreg, countreg));
9290         }
9291       if (size == 8 && (count & 0x04))
9292         emit_insn (gen_strsetsi (destreg,
9293                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
9294       if (count & 0x02)
9295         emit_insn (gen_strsethi (destreg,
9296                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
9297       if (count & 0x01)
9298         emit_insn (gen_strsetqi (destreg,
9299                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
9300     }
9301   else
9302     {
9303       rtx countreg2;
9304       rtx label = NULL;
9305
9306       /* In case we don't know anything about the alignment, default to
9307          library version, since it is usually equally fast and result in
9308          shorter code.  */
9309       if (!TARGET_INLINE_ALL_STRINGOPS && align < UNITS_PER_WORD)
9310         return 0;
9311
9312       if (TARGET_SINGLE_STRINGOP)
9313         emit_insn (gen_cld ());
9314
9315       countreg2 = gen_reg_rtx (Pmode);
9316       countreg = copy_to_mode_reg (counter_mode, count_exp);
9317       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
9318
9319       if (count == 0
9320           && align < (TARGET_PENTIUMPRO && (count == 0
9321                                             || count >= (unsigned int)260)
9322                       ? 8 : UNITS_PER_WORD))
9323         {
9324           label = gen_label_rtx ();
9325           emit_cmp_and_jump_insns (countreg, GEN_INT (UNITS_PER_WORD - 1),
9326                                    LEU, 0, counter_mode, 1, 0, label);
9327         }
9328       if (align <= 1)
9329         {
9330           rtx label = ix86_expand_aligntest (destreg, 1);
9331           emit_insn (gen_strsetqi (destreg,
9332                                    gen_rtx_SUBREG (QImode, zeroreg, 0)));
9333           ix86_adjust_counter (countreg, 1);
9334           emit_label (label);
9335           LABEL_NUSES (label) = 1;
9336         }
9337       if (align <= 2)
9338         {
9339           rtx label = ix86_expand_aligntest (destreg, 2);
9340           emit_insn (gen_strsethi (destreg,
9341                                    gen_rtx_SUBREG (HImode, zeroreg, 0)));
9342           ix86_adjust_counter (countreg, 2);
9343           emit_label (label);
9344           LABEL_NUSES (label) = 1;
9345         }
9346       if (align <= 4 && TARGET_PENTIUMPRO && (count == 0
9347                                               || count >= (unsigned int)260))
9348         {
9349           rtx label = ix86_expand_aligntest (destreg, 4);
9350           emit_insn (gen_strsetsi (destreg, (TARGET_64BIT
9351                                              ? gen_rtx_SUBREG (SImode, zeroreg, 0)
9352                                              : zeroreg)));
9353           ix86_adjust_counter (countreg, 4);
9354           emit_label (label);
9355           LABEL_NUSES (label) = 1;
9356         }
9357
9358       if (!TARGET_SINGLE_STRINGOP)
9359         emit_insn (gen_cld ());
9360       if (TARGET_64BIT)
9361         {
9362           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
9363                                   GEN_INT (3)));
9364           emit_insn (gen_rep_stosdi_rex64 (destreg, countreg2, zeroreg,
9365                                            destreg, countreg2));
9366         }
9367       else
9368         {
9369           emit_insn (gen_lshrsi3 (countreg2, countreg, GEN_INT (2)));
9370           emit_insn (gen_rep_stossi (destreg, countreg2, zeroreg,
9371                                      destreg, countreg2));
9372         }
9373
9374       if (label)
9375         {
9376           emit_label (label);
9377           LABEL_NUSES (label) = 1;
9378         }
9379       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
9380         emit_insn (gen_strsetsi (destreg,
9381                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
9382       if (TARGET_64BIT && (align <= 4 || count == 0))
9383         {
9384           rtx label = ix86_expand_aligntest (destreg, 2);
9385           emit_insn (gen_strsetsi (destreg,
9386                                    gen_rtx_SUBREG (SImode, zeroreg, 0)));
9387           emit_label (label);
9388           LABEL_NUSES (label) = 1;
9389         }
9390       if (align > 2 && count != 0 && (count & 2))
9391         emit_insn (gen_strsethi (destreg,
9392                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
9393       if (align <= 2 || count == 0)
9394         {
9395           rtx label = ix86_expand_aligntest (destreg, 2);
9396           emit_insn (gen_strsethi (destreg,
9397                                    gen_rtx_SUBREG (HImode, zeroreg, 0)));
9398           emit_label (label);
9399           LABEL_NUSES (label) = 1;
9400         }
9401       if (align > 1 && count != 0 && (count & 1))
9402         emit_insn (gen_strsetqi (destreg,
9403                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
9404       if (align <= 1 || count == 0)
9405         {
9406           rtx label = ix86_expand_aligntest (destreg, 1);
9407           emit_insn (gen_strsetqi (destreg,
9408                                    gen_rtx_SUBREG (QImode, zeroreg, 0)));
9409           emit_label (label);
9410           LABEL_NUSES (label) = 1;
9411         }
9412     }
9413   return 1;
9414 }
9415 /* Expand strlen.  */
9416 int
9417 ix86_expand_strlen (out, src, eoschar, align)
9418      rtx out, src, eoschar, align;
9419 {
9420   rtx addr, scratch1, scratch2, scratch3, scratch4;
9421
9422   /* The generic case of strlen expander is long.  Avoid it's
9423      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
9424
9425   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
9426       && !TARGET_INLINE_ALL_STRINGOPS
9427       && !optimize_size
9428       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
9429     return 0;
9430
9431   addr = force_reg (Pmode, XEXP (src, 0));
9432   scratch1 = gen_reg_rtx (Pmode);
9433
9434   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
9435       && !optimize_size)
9436     {
9437       /* Well it seems that some optimizer does not combine a call like
9438          foo(strlen(bar), strlen(bar));
9439          when the move and the subtraction is done here.  It does calculate
9440          the length just once when these instructions are done inside of
9441          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
9442          often used and I use one fewer register for the lifetime of
9443          output_strlen_unroll() this is better.  */
9444
9445       emit_move_insn (out, addr);
9446
9447       ix86_expand_strlensi_unroll_1 (out, align);
9448
9449       /* strlensi_unroll_1 returns the address of the zero at the end of
9450          the string, like memchr(), so compute the length by subtracting
9451          the start address.  */
9452       if (TARGET_64BIT)
9453         emit_insn (gen_subdi3 (out, out, addr));
9454       else
9455         emit_insn (gen_subsi3 (out, out, addr));
9456     }
9457   else
9458     {
9459       scratch2 = gen_reg_rtx (Pmode);
9460       scratch3 = gen_reg_rtx (Pmode);
9461       scratch4 = force_reg (Pmode, constm1_rtx);
9462
9463       emit_move_insn (scratch3, addr);
9464       eoschar = force_reg (QImode, eoschar);
9465
9466       emit_insn (gen_cld ());
9467       if (TARGET_64BIT)
9468         {
9469           emit_insn (gen_strlenqi_rex_1 (scratch1, scratch3, eoschar,
9470                                          align, scratch4, scratch3));
9471           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
9472           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
9473         }
9474       else
9475         {
9476           emit_insn (gen_strlenqi_1 (scratch1, scratch3, eoschar,
9477                                      align, scratch4, scratch3));
9478           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
9479           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
9480         }
9481     }
9482   return 1;
9483 }
9484
9485 /* Expand the appropriate insns for doing strlen if not just doing
9486    repnz; scasb
9487
9488    out = result, initialized with the start address
9489    align_rtx = alignment of the address.
9490    scratch = scratch register, initialized with the startaddress when
9491         not aligned, otherwise undefined
9492
9493    This is just the body. It needs the initialisations mentioned above and
9494    some address computing at the end.  These things are done in i386.md.  */
9495
9496 static void
9497 ix86_expand_strlensi_unroll_1 (out, align_rtx)
9498      rtx out, align_rtx;
9499 {
9500   int align;
9501   rtx tmp;
9502   rtx align_2_label = NULL_RTX;
9503   rtx align_3_label = NULL_RTX;
9504   rtx align_4_label = gen_label_rtx ();
9505   rtx end_0_label = gen_label_rtx ();
9506   rtx mem;
9507   rtx tmpreg = gen_reg_rtx (SImode);
9508   rtx scratch = gen_reg_rtx (SImode);
9509
9510   align = 0;
9511   if (GET_CODE (align_rtx) == CONST_INT)
9512     align = INTVAL (align_rtx);
9513
9514   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
9515
9516   /* Is there a known alignment and is it less than 4?  */
9517   if (align < 4)
9518     {
9519       rtx scratch1 = gen_reg_rtx (Pmode);
9520       emit_move_insn (scratch1, out);
9521       /* Is there a known alignment and is it not 2? */
9522       if (align != 2)
9523         {
9524           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
9525           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
9526
9527           /* Leave just the 3 lower bits.  */
9528           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
9529                                     NULL_RTX, 0, OPTAB_WIDEN);
9530
9531           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
9532                                    Pmode, 1, 0, align_4_label);
9533           emit_cmp_and_jump_insns (align_rtx, GEN_INT (2), EQ, NULL,
9534                                    Pmode, 1, 0, align_2_label);
9535           emit_cmp_and_jump_insns (align_rtx, GEN_INT (2), GTU, NULL,
9536                                    Pmode, 1, 0, align_3_label);
9537         }
9538       else
9539         {
9540           /* Since the alignment is 2, we have to check 2 or 0 bytes;
9541              check if is aligned to 4 - byte.  */
9542
9543           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (2),
9544                                     NULL_RTX, 0, OPTAB_WIDEN);
9545
9546           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
9547                                    Pmode, 1, 0, align_4_label);
9548         }
9549
9550       mem = gen_rtx_MEM (QImode, out);
9551
9552       /* Now compare the bytes.  */
9553
9554       /* Compare the first n unaligned byte on a byte per byte basis.  */
9555       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
9556                                QImode, 1, 0, end_0_label);
9557
9558       /* Increment the address.  */
9559       if (TARGET_64BIT)
9560         emit_insn (gen_adddi3 (out, out, const1_rtx));
9561       else
9562         emit_insn (gen_addsi3 (out, out, const1_rtx));
9563
9564       /* Not needed with an alignment of 2 */
9565       if (align != 2)
9566         {
9567           emit_label (align_2_label);
9568
9569           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
9570                                    QImode, 1, 0, end_0_label);
9571
9572           if (TARGET_64BIT)
9573             emit_insn (gen_adddi3 (out, out, const1_rtx));
9574           else
9575             emit_insn (gen_addsi3 (out, out, const1_rtx));
9576
9577           emit_label (align_3_label);
9578         }
9579
9580       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
9581                                QImode, 1, 0, end_0_label);
9582
9583       if (TARGET_64BIT)
9584         emit_insn (gen_adddi3 (out, out, const1_rtx));
9585       else
9586         emit_insn (gen_addsi3 (out, out, const1_rtx));
9587     }
9588
9589   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
9590      align this loop.  It gives only huge programs, but does not help to
9591      speed up.  */
9592   emit_label (align_4_label);
9593
9594   mem = gen_rtx_MEM (SImode, out);
9595   emit_move_insn (scratch, mem);
9596   if (TARGET_64BIT)
9597     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
9598   else
9599     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
9600
9601   /* This formula yields a nonzero result iff one of the bytes is zero.
9602      This saves three branches inside loop and many cycles.  */
9603
9604   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
9605   emit_insn (gen_one_cmplsi2 (scratch, scratch));
9606   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
9607   emit_insn (gen_andsi3 (tmpreg, tmpreg,
9608                          GEN_INT (trunc_int_for_mode
9609                                   (0x80808080, SImode))));
9610   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0,
9611                            SImode, 1, 0, align_4_label);
9612
9613   if (TARGET_CMOVE)
9614     {
9615        rtx reg = gen_reg_rtx (SImode);
9616        rtx reg2 = gen_reg_rtx (Pmode);
9617        emit_move_insn (reg, tmpreg);
9618        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
9619
9620        /* If zero is not in the first two bytes, move two bytes forward.  */
9621        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
9622        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
9623        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
9624        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
9625                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
9626                                                      reg,
9627                                                      tmpreg)));
9628        /* Emit lea manually to avoid clobbering of flags.  */
9629        emit_insn (gen_rtx_SET (SImode, reg2,
9630                                gen_rtx_PLUS (Pmode, out, GEN_INT (2))));
9631
9632        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
9633        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
9634        emit_insn (gen_rtx_SET (VOIDmode, out,
9635                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
9636                                                      reg2,
9637                                                      out)));
9638
9639     }
9640   else
9641     {
9642        rtx end_2_label = gen_label_rtx ();
9643        /* Is zero in the first two bytes? */
9644
9645        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
9646        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
9647        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
9648        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9649                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
9650                             pc_rtx);
9651        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
9652        JUMP_LABEL (tmp) = end_2_label;
9653
9654        /* Not in the first two.  Move two bytes forward.  */
9655        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
9656        if (TARGET_64BIT)
9657          emit_insn (gen_adddi3 (out, out, GEN_INT (2)));
9658        else
9659          emit_insn (gen_addsi3 (out, out, GEN_INT (2)));
9660
9661        emit_label (end_2_label);
9662
9663     }
9664
9665   /* Avoid branch in fixing the byte.  */
9666   tmpreg = gen_lowpart (QImode, tmpreg);
9667   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
9668   if (TARGET_64BIT)
9669     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3)));
9670   else
9671     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3)));
9672
9673   emit_label (end_0_label);
9674 }
9675 \f
9676 /* Clear stack slot assignments remembered from previous functions.
9677    This is called from INIT_EXPANDERS once before RTL is emitted for each
9678    function.  */
9679
9680 static void
9681 ix86_init_machine_status (p)
9682      struct function *p;
9683 {
9684   p->machine = (struct machine_function *)
9685     xcalloc (1, sizeof (struct machine_function));
9686 }
9687
9688 /* Mark machine specific bits of P for GC.  */
9689 static void
9690 ix86_mark_machine_status (p)
9691      struct function *p;
9692 {
9693   struct machine_function *machine = p->machine;
9694   enum machine_mode mode;
9695   int n;
9696
9697   if (! machine)
9698     return;
9699
9700   for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
9701        mode = (enum machine_mode) ((int) mode + 1))
9702     for (n = 0; n < MAX_386_STACK_LOCALS; n++)
9703       ggc_mark_rtx (machine->stack_locals[(int) mode][n]);
9704 }
9705
9706 static void
9707 ix86_free_machine_status (p)
9708      struct function *p;
9709 {
9710   free (p->machine);
9711   p->machine = NULL;
9712 }
9713
9714 /* Return a MEM corresponding to a stack slot with mode MODE.
9715    Allocate a new slot if necessary.
9716
9717    The RTL for a function can have several slots available: N is
9718    which slot to use.  */
9719
9720 rtx
9721 assign_386_stack_local (mode, n)
9722      enum machine_mode mode;
9723      int n;
9724 {
9725   if (n < 0 || n >= MAX_386_STACK_LOCALS)
9726     abort ();
9727
9728   if (ix86_stack_locals[(int) mode][n] == NULL_RTX)
9729     ix86_stack_locals[(int) mode][n]
9730       = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
9731
9732   return ix86_stack_locals[(int) mode][n];
9733 }
9734 \f
9735 /* Calculate the length of the memory address in the instruction
9736    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
9737
9738 static int
9739 memory_address_length (addr)
9740      rtx addr;
9741 {
9742   struct ix86_address parts;
9743   rtx base, index, disp;
9744   int len;
9745
9746   if (GET_CODE (addr) == PRE_DEC
9747       || GET_CODE (addr) == POST_INC
9748       || GET_CODE (addr) == PRE_MODIFY
9749       || GET_CODE (addr) == POST_MODIFY)
9750     return 0;
9751
9752   if (! ix86_decompose_address (addr, &parts))
9753     abort ();
9754
9755   base = parts.base;
9756   index = parts.index;
9757   disp = parts.disp;
9758   len = 0;
9759
9760   /* Register Indirect.  */
9761   if (base && !index && !disp)
9762     {
9763       /* Special cases: ebp and esp need the two-byte modrm form.  */
9764       if (addr == stack_pointer_rtx
9765           || addr == arg_pointer_rtx
9766           || addr == frame_pointer_rtx
9767           || addr == hard_frame_pointer_rtx)
9768         len = 1;
9769     }
9770
9771   /* Direct Addressing.  */
9772   else if (disp && !base && !index)
9773     len = 4;
9774
9775   else
9776     {
9777       /* Find the length of the displacement constant.  */
9778       if (disp)
9779         {
9780           if (GET_CODE (disp) == CONST_INT
9781               && CONST_OK_FOR_LETTER_P (INTVAL (disp), 'K'))
9782             len = 1;
9783           else
9784             len = 4;
9785         }
9786
9787       /* An index requires the two-byte modrm form.  */
9788       if (index)
9789         len += 1;
9790     }
9791
9792   return len;
9793 }
9794
9795 /* Compute default value for "length_immediate" attribute.  When SHORTFORM is set
9796    expect that insn have 8bit immediate alternative.  */
9797 int
9798 ix86_attr_length_immediate_default (insn, shortform)
9799      rtx insn;
9800      int shortform;
9801 {
9802   int len = 0;
9803   int i;
9804   extract_insn_cached (insn);
9805   for (i = recog_data.n_operands - 1; i >= 0; --i)
9806     if (CONSTANT_P (recog_data.operand[i]))
9807       {
9808         if (len)
9809           abort ();
9810         if (shortform
9811             && GET_CODE (recog_data.operand[i]) == CONST_INT
9812             && CONST_OK_FOR_LETTER_P (INTVAL (recog_data.operand[i]), 'K'))
9813           len = 1;
9814         else
9815           {
9816             switch (get_attr_mode (insn))
9817               {
9818                 case MODE_QI:
9819                   len+=1;
9820                   break;
9821                 case MODE_HI:
9822                   len+=2;
9823                   break;
9824                 case MODE_SI:
9825                   len+=4;
9826                   break;
9827                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
9828                 case MODE_DI:
9829                   len+=4;
9830                   break;
9831                 default:
9832                   fatal_insn ("Unknown insn mode", insn);
9833               }
9834           }
9835       }
9836   return len;
9837 }
9838 /* Compute default value for "length_address" attribute.  */
9839 int
9840 ix86_attr_length_address_default (insn)
9841      rtx insn;
9842 {
9843   int i;
9844   extract_insn_cached (insn);
9845   for (i = recog_data.n_operands - 1; i >= 0; --i)
9846     if (GET_CODE (recog_data.operand[i]) == MEM)
9847       {
9848         return memory_address_length (XEXP (recog_data.operand[i], 0));
9849         break;
9850       }
9851   return 0;
9852 }
9853 \f
9854 /* Return the maximum number of instructions a cpu can issue.  */
9855
9856 static int
9857 ix86_issue_rate ()
9858 {
9859   switch (ix86_cpu)
9860     {
9861     case PROCESSOR_PENTIUM:
9862     case PROCESSOR_K6:
9863       return 2;
9864
9865     case PROCESSOR_PENTIUMPRO:
9866     case PROCESSOR_PENTIUM4:
9867     case PROCESSOR_ATHLON:
9868       return 3;
9869
9870     default:
9871       return 1;
9872     }
9873 }
9874
9875 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
9876    by DEP_INSN and nothing set by DEP_INSN.  */
9877
9878 static int
9879 ix86_flags_dependant (insn, dep_insn, insn_type)
9880      rtx insn, dep_insn;
9881      enum attr_type insn_type;
9882 {
9883   rtx set, set2;
9884
9885   /* Simplify the test for uninteresting insns.  */
9886   if (insn_type != TYPE_SETCC
9887       && insn_type != TYPE_ICMOV
9888       && insn_type != TYPE_FCMOV
9889       && insn_type != TYPE_IBR)
9890     return 0;
9891
9892   if ((set = single_set (dep_insn)) != 0)
9893     {
9894       set = SET_DEST (set);
9895       set2 = NULL_RTX;
9896     }
9897   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
9898            && XVECLEN (PATTERN (dep_insn), 0) == 2
9899            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
9900            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
9901     {
9902       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
9903       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
9904     }
9905   else
9906     return 0;
9907
9908   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
9909     return 0;
9910
9911   /* This test is true if the dependant insn reads the flags but
9912      not any other potentially set register.  */
9913   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
9914     return 0;
9915
9916   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
9917     return 0;
9918
9919   return 1;
9920 }
9921
9922 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
9923    address with operands set by DEP_INSN.  */
9924
9925 static int
9926 ix86_agi_dependant (insn, dep_insn, insn_type)
9927      rtx insn, dep_insn;
9928      enum attr_type insn_type;
9929 {
9930   rtx addr;
9931
9932   if (insn_type == TYPE_LEA
9933       && TARGET_PENTIUM)
9934     {
9935       addr = PATTERN (insn);
9936       if (GET_CODE (addr) == SET)
9937         ;
9938       else if (GET_CODE (addr) == PARALLEL
9939                && GET_CODE (XVECEXP (addr, 0, 0)) == SET)
9940         addr = XVECEXP (addr, 0, 0);
9941       else
9942         abort ();
9943       addr = SET_SRC (addr);
9944     }
9945   else
9946     {
9947       int i;
9948       extract_insn_cached (insn);
9949       for (i = recog_data.n_operands - 1; i >= 0; --i)
9950         if (GET_CODE (recog_data.operand[i]) == MEM)
9951           {
9952             addr = XEXP (recog_data.operand[i], 0);
9953             goto found;
9954           }
9955       return 0;
9956     found:;
9957     }
9958
9959   return modified_in_p (addr, dep_insn);
9960 }
9961
9962 static int
9963 ix86_adjust_cost (insn, link, dep_insn, cost)
9964      rtx insn, link, dep_insn;
9965      int cost;
9966 {
9967   enum attr_type insn_type, dep_insn_type;
9968   enum attr_memory memory, dep_memory;
9969   rtx set, set2;
9970   int dep_insn_code_number;
9971
9972   /* Anti and output depenancies have zero cost on all CPUs.  */
9973   if (REG_NOTE_KIND (link) != 0)
9974     return 0;
9975
9976   dep_insn_code_number = recog_memoized (dep_insn);
9977
9978   /* If we can't recognize the insns, we can't really do anything.  */
9979   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
9980     return cost;
9981
9982   insn_type = get_attr_type (insn);
9983   dep_insn_type = get_attr_type (dep_insn);
9984
9985   switch (ix86_cpu)
9986     {
9987     case PROCESSOR_PENTIUM:
9988       /* Address Generation Interlock adds a cycle of latency.  */
9989       if (ix86_agi_dependant (insn, dep_insn, insn_type))
9990         cost += 1;
9991
9992       /* ??? Compares pair with jump/setcc.  */
9993       if (ix86_flags_dependant (insn, dep_insn, insn_type))
9994         cost = 0;
9995
9996       /* Floating point stores require value to be ready one cycle ealier.  */
9997       if (insn_type == TYPE_FMOV
9998           && get_attr_memory (insn) == MEMORY_STORE
9999           && !ix86_agi_dependant (insn, dep_insn, insn_type))
10000         cost += 1;
10001       break;
10002
10003     case PROCESSOR_PENTIUMPRO:
10004       memory = get_attr_memory (insn);
10005       dep_memory = get_attr_memory (dep_insn);
10006
10007       /* Since we can't represent delayed latencies of load+operation,
10008          increase the cost here for non-imov insns.  */
10009       if (dep_insn_type != TYPE_IMOV
10010           && dep_insn_type != TYPE_FMOV
10011           && (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH))
10012         cost += 1;
10013
10014       /* INT->FP conversion is expensive.  */
10015       if (get_attr_fp_int_src (dep_insn))
10016         cost += 5;
10017
10018       /* There is one cycle extra latency between an FP op and a store.  */
10019       if (insn_type == TYPE_FMOV
10020           && (set = single_set (dep_insn)) != NULL_RTX
10021           && (set2 = single_set (insn)) != NULL_RTX
10022           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
10023           && GET_CODE (SET_DEST (set2)) == MEM)
10024         cost += 1;
10025
10026       /* Show ability of reorder buffer to hide latency of load by executing
10027          in parallel with previous instruction in case
10028          previous instruction is not needed to compute the address.  */
10029       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
10030           && !ix86_agi_dependant (insn, dep_insn, insn_type))
10031         {
10032           /* Claim moves to take one cycle, as core can issue one load
10033              at time and the next load can start cycle later.  */
10034           if (dep_insn_type == TYPE_IMOV
10035               || dep_insn_type == TYPE_FMOV)
10036             cost = 1;
10037           else if (cost > 1)
10038             cost--;
10039         }
10040       break;
10041
10042     case PROCESSOR_K6:
10043       memory = get_attr_memory (insn);
10044       dep_memory = get_attr_memory (dep_insn);
10045       /* The esp dependency is resolved before the instruction is really
10046          finished.  */
10047       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
10048           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
10049         return 1;
10050
10051       /* Since we can't represent delayed latencies of load+operation,
10052          increase the cost here for non-imov insns.  */
10053       if (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH)
10054         cost += (dep_insn_type != TYPE_IMOV) ? 2 : 1;
10055
10056       /* INT->FP conversion is expensive.  */
10057       if (get_attr_fp_int_src (dep_insn))
10058         cost += 5;
10059
10060       /* Show ability of reorder buffer to hide latency of load by executing
10061          in parallel with previous instruction in case
10062          previous instruction is not needed to compute the address.  */
10063       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
10064           && !ix86_agi_dependant (insn, dep_insn, insn_type))
10065         {
10066           /* Claim moves to take one cycle, as core can issue one load
10067              at time and the next load can start cycle later.  */
10068           if (dep_insn_type == TYPE_IMOV
10069               || dep_insn_type == TYPE_FMOV)
10070             cost = 1;
10071           else if (cost > 2)
10072             cost -= 2;
10073           else
10074             cost = 1;
10075         }
10076       break;
10077
10078     case PROCESSOR_ATHLON:
10079       memory = get_attr_memory (insn);
10080       dep_memory = get_attr_memory (dep_insn);
10081
10082       if (dep_memory == MEMORY_LOAD || dep_memory == MEMORY_BOTH)
10083         {
10084           if (dep_insn_type == TYPE_IMOV || dep_insn_type == TYPE_FMOV)
10085             cost += 2;
10086           else
10087             cost += 3;
10088         }
10089       /* Show ability of reorder buffer to hide latency of load by executing
10090          in parallel with previous instruction in case
10091          previous instruction is not needed to compute the address.  */
10092       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
10093           && !ix86_agi_dependant (insn, dep_insn, insn_type))
10094         {
10095           /* Claim moves to take one cycle, as core can issue one load
10096              at time and the next load can start cycle later.  */
10097           if (dep_insn_type == TYPE_IMOV
10098               || dep_insn_type == TYPE_FMOV)
10099             cost = 0;
10100           else if (cost >= 3)
10101             cost -= 3;
10102           else
10103             cost = 0;
10104         }
10105
10106     default:
10107       break;
10108     }
10109
10110   return cost;
10111 }
10112
10113 static union
10114 {
10115   struct ppro_sched_data
10116   {
10117     rtx decode[3];
10118     int issued_this_cycle;
10119   } ppro;
10120 } ix86_sched_data;
10121
10122 static int
10123 ix86_safe_length (insn)
10124      rtx insn;
10125 {
10126   if (recog_memoized (insn) >= 0)
10127     return get_attr_length(insn);
10128   else
10129     return 128;
10130 }
10131
10132 static int
10133 ix86_safe_length_prefix (insn)
10134      rtx insn;
10135 {
10136   if (recog_memoized (insn) >= 0)
10137     return get_attr_length(insn);
10138   else
10139     return 0;
10140 }
10141
10142 static enum attr_memory
10143 ix86_safe_memory (insn)
10144      rtx insn;
10145 {
10146   if (recog_memoized (insn) >= 0)
10147     return get_attr_memory(insn);
10148   else
10149     return MEMORY_UNKNOWN;
10150 }
10151
10152 static enum attr_pent_pair
10153 ix86_safe_pent_pair (insn)
10154      rtx insn;
10155 {
10156   if (recog_memoized (insn) >= 0)
10157     return get_attr_pent_pair(insn);
10158   else
10159     return PENT_PAIR_NP;
10160 }
10161
10162 static enum attr_ppro_uops
10163 ix86_safe_ppro_uops (insn)
10164      rtx insn;
10165 {
10166   if (recog_memoized (insn) >= 0)
10167     return get_attr_ppro_uops (insn);
10168   else
10169     return PPRO_UOPS_MANY;
10170 }
10171
10172 static void
10173 ix86_dump_ppro_packet (dump)
10174      FILE *dump;
10175 {
10176   if (ix86_sched_data.ppro.decode[0])
10177     {
10178       fprintf (dump, "PPRO packet: %d",
10179                INSN_UID (ix86_sched_data.ppro.decode[0]));
10180       if (ix86_sched_data.ppro.decode[1])
10181         fprintf (dump, " %d", INSN_UID (ix86_sched_data.ppro.decode[1]));
10182       if (ix86_sched_data.ppro.decode[2])
10183         fprintf (dump, " %d", INSN_UID (ix86_sched_data.ppro.decode[2]));
10184       fputc ('\n', dump);
10185     }
10186 }
10187
10188 /* We're beginning a new block.  Initialize data structures as necessary.  */
10189
10190 static void
10191 ix86_sched_init (dump, sched_verbose, veclen)
10192      FILE *dump ATTRIBUTE_UNUSED;
10193      int sched_verbose ATTRIBUTE_UNUSED;
10194      int veclen ATTRIBUTE_UNUSED;
10195 {
10196   memset (&ix86_sched_data, 0, sizeof (ix86_sched_data));
10197 }
10198
10199 /* Shift INSN to SLOT, and shift everything else down.  */
10200
10201 static void
10202 ix86_reorder_insn (insnp, slot)
10203      rtx *insnp, *slot;
10204 {
10205   if (insnp != slot)
10206     {
10207       rtx insn = *insnp;
10208       do
10209         insnp[0] = insnp[1];
10210       while (++insnp != slot);
10211       *insnp = insn;
10212     }
10213 }
10214
10215 /* Find an instruction with given pairability and minimal amount of cycles
10216    lost by the fact that the CPU waits for both pipelines to finish before
10217    reading next instructions.  Also take care that both instructions together
10218    can not exceed 7 bytes.  */
10219
10220 static rtx *
10221 ix86_pent_find_pair (e_ready, ready, type, first)
10222      rtx *e_ready;
10223      rtx *ready;
10224      enum attr_pent_pair type;
10225      rtx first;
10226 {
10227   int mincycles, cycles;
10228   enum attr_pent_pair tmp;
10229   enum attr_memory memory;
10230   rtx *insnp, *bestinsnp = NULL;
10231
10232   if (ix86_safe_length (first) > 7 + ix86_safe_length_prefix (first))
10233     return NULL;
10234
10235   memory = ix86_safe_memory (first);
10236   cycles = result_ready_cost (first);
10237   mincycles = INT_MAX;
10238
10239   for (insnp = e_ready; insnp >= ready && mincycles; --insnp)
10240     if ((tmp = ix86_safe_pent_pair (*insnp)) == type
10241         && ix86_safe_length (*insnp) <= 7 + ix86_safe_length_prefix (*insnp))
10242       {
10243         enum attr_memory second_memory;
10244         int secondcycles, currentcycles;
10245
10246         second_memory = ix86_safe_memory (*insnp);
10247         secondcycles = result_ready_cost (*insnp);
10248         currentcycles = abs (cycles - secondcycles);
10249
10250         if (secondcycles >= 1 && cycles >= 1)
10251           {
10252             /* Two read/modify/write instructions together takes two
10253                cycles longer.  */
10254             if (memory == MEMORY_BOTH && second_memory == MEMORY_BOTH)
10255               currentcycles += 2;
10256
10257             /* Read modify/write instruction followed by read/modify
10258                takes one cycle longer.  */
10259             if (memory == MEMORY_BOTH && second_memory == MEMORY_LOAD
10260                 && tmp != PENT_PAIR_UV
10261                 && ix86_safe_pent_pair (first) != PENT_PAIR_UV)
10262               currentcycles += 1;
10263           }
10264         if (currentcycles < mincycles)
10265           bestinsnp = insnp, mincycles = currentcycles;
10266       }
10267
10268   return bestinsnp;
10269 }
10270
10271 /* Subroutines of ix86_sched_reorder.  */
10272
10273 static void
10274 ix86_sched_reorder_pentium (ready, e_ready)
10275      rtx *ready;
10276      rtx *e_ready;
10277 {
10278   enum attr_pent_pair pair1, pair2;
10279   rtx *insnp;
10280
10281   /* This wouldn't be necessary if Haifa knew that static insn ordering
10282      is important to which pipe an insn is issued to.  So we have to make
10283      some minor rearrangements.  */
10284
10285   pair1 = ix86_safe_pent_pair (*e_ready);
10286
10287   /* If the first insn is non-pairable, let it be.  */
10288   if (pair1 == PENT_PAIR_NP)
10289     return;
10290
10291   pair2 = PENT_PAIR_NP;
10292   insnp = 0;
10293
10294   /* If the first insn is UV or PV pairable, search for a PU
10295      insn to go with.  */
10296   if (pair1 == PENT_PAIR_UV || pair1 == PENT_PAIR_PV)
10297     {
10298       insnp = ix86_pent_find_pair (e_ready-1, ready,
10299                                    PENT_PAIR_PU, *e_ready);
10300       if (insnp)
10301         pair2 = PENT_PAIR_PU;
10302     }
10303
10304   /* If the first insn is PU or UV pairable, search for a PV
10305      insn to go with.  */
10306   if (pair2 == PENT_PAIR_NP
10307       && (pair1 == PENT_PAIR_PU || pair1 == PENT_PAIR_UV))
10308     {
10309       insnp = ix86_pent_find_pair (e_ready-1, ready,
10310                                    PENT_PAIR_PV, *e_ready);
10311       if (insnp)
10312         pair2 = PENT_PAIR_PV;
10313     }
10314
10315   /* If the first insn is pairable, search for a UV
10316      insn to go with.  */
10317   if (pair2 == PENT_PAIR_NP)
10318     {
10319       insnp = ix86_pent_find_pair (e_ready-1, ready,
10320                                    PENT_PAIR_UV, *e_ready);
10321       if (insnp)
10322         pair2 = PENT_PAIR_UV;
10323     }
10324
10325   if (pair2 == PENT_PAIR_NP)
10326     return;
10327
10328   /* Found something!  Decide if we need to swap the order.  */
10329   if (pair1 == PENT_PAIR_PV || pair2 == PENT_PAIR_PU
10330       || (pair1 == PENT_PAIR_UV && pair2 == PENT_PAIR_UV
10331           && ix86_safe_memory (*e_ready) == MEMORY_BOTH
10332           && ix86_safe_memory (*insnp) == MEMORY_LOAD))
10333     ix86_reorder_insn (insnp, e_ready);
10334   else
10335     ix86_reorder_insn (insnp, e_ready - 1);
10336 }
10337
10338 static void
10339 ix86_sched_reorder_ppro (ready, e_ready)
10340      rtx *ready;
10341      rtx *e_ready;
10342 {
10343   rtx decode[3];
10344   enum attr_ppro_uops cur_uops;
10345   int issued_this_cycle;
10346   rtx *insnp;
10347   int i;
10348
10349   /* At this point .ppro.decode contains the state of the three
10350      decoders from last "cycle".  That is, those insns that were
10351      actually independent.  But here we're scheduling for the
10352      decoder, and we may find things that are decodable in the
10353      same cycle.  */
10354
10355   memcpy (decode, ix86_sched_data.ppro.decode, sizeof (decode));
10356   issued_this_cycle = 0;
10357
10358   insnp = e_ready;
10359   cur_uops = ix86_safe_ppro_uops (*insnp);
10360
10361   /* If the decoders are empty, and we've a complex insn at the
10362      head of the priority queue, let it issue without complaint.  */
10363   if (decode[0] == NULL)
10364     {
10365       if (cur_uops == PPRO_UOPS_MANY)
10366         {
10367           decode[0] = *insnp;
10368           goto ppro_done;
10369         }
10370
10371       /* Otherwise, search for a 2-4 uop unsn to issue.  */
10372       while (cur_uops != PPRO_UOPS_FEW)
10373         {
10374           if (insnp == ready)
10375             break;
10376           cur_uops = ix86_safe_ppro_uops (*--insnp);
10377         }
10378
10379       /* If so, move it to the head of the line.  */
10380       if (cur_uops == PPRO_UOPS_FEW)
10381         ix86_reorder_insn (insnp, e_ready);
10382
10383       /* Issue the head of the queue.  */
10384       issued_this_cycle = 1;
10385       decode[0] = *e_ready--;
10386     }
10387
10388   /* Look for simple insns to fill in the other two slots.  */
10389   for (i = 1; i < 3; ++i)
10390     if (decode[i] == NULL)
10391       {
10392         if (ready >= e_ready)
10393           goto ppro_done;
10394
10395         insnp = e_ready;
10396         cur_uops = ix86_safe_ppro_uops (*insnp);
10397         while (cur_uops != PPRO_UOPS_ONE)
10398           {
10399             if (insnp == ready)
10400               break;
10401             cur_uops = ix86_safe_ppro_uops (*--insnp);
10402           }
10403
10404         /* Found one.  Move it to the head of the queue and issue it.  */
10405         if (cur_uops == PPRO_UOPS_ONE)
10406           {
10407             ix86_reorder_insn (insnp, e_ready);
10408             decode[i] = *e_ready--;
10409             issued_this_cycle++;
10410             continue;
10411           }
10412
10413         /* ??? Didn't find one.  Ideally, here we would do a lazy split
10414            of 2-uop insns, issue one and queue the other.  */
10415       }
10416
10417  ppro_done:
10418   if (issued_this_cycle == 0)
10419     issued_this_cycle = 1;
10420   ix86_sched_data.ppro.issued_this_cycle = issued_this_cycle;
10421 }
10422
10423 /* We are about to being issuing insns for this clock cycle.
10424    Override the default sort algorithm to better slot instructions.  */
10425 static int
10426 ix86_sched_reorder (dump, sched_verbose, ready, n_readyp, clock_var)
10427      FILE *dump ATTRIBUTE_UNUSED;
10428      int sched_verbose ATTRIBUTE_UNUSED;
10429      rtx *ready;
10430      int *n_readyp;
10431      int clock_var ATTRIBUTE_UNUSED;
10432 {
10433   int n_ready = *n_readyp;
10434   rtx *e_ready = ready + n_ready - 1;
10435
10436   if (n_ready < 2)
10437     goto out;
10438
10439   switch (ix86_cpu)
10440     {
10441     default:
10442       break;
10443
10444     case PROCESSOR_PENTIUM:
10445       ix86_sched_reorder_pentium (ready, e_ready);
10446       break;
10447
10448     case PROCESSOR_PENTIUMPRO:
10449       ix86_sched_reorder_ppro (ready, e_ready);
10450       break;
10451     }
10452
10453 out:
10454   return ix86_issue_rate ();
10455 }
10456
10457 /* We are about to issue INSN.  Return the number of insns left on the
10458    ready queue that can be issued this cycle.  */
10459
10460 static int
10461 ix86_variable_issue (dump, sched_verbose, insn, can_issue_more)
10462      FILE *dump;
10463      int sched_verbose;
10464      rtx insn;
10465      int can_issue_more;
10466 {
10467   int i;
10468   switch (ix86_cpu)
10469     {
10470     default:
10471       return can_issue_more - 1;
10472
10473     case PROCESSOR_PENTIUMPRO:
10474       {
10475         enum attr_ppro_uops uops = ix86_safe_ppro_uops (insn);
10476
10477         if (uops == PPRO_UOPS_MANY)
10478           {
10479             if (sched_verbose)
10480               ix86_dump_ppro_packet (dump);
10481             ix86_sched_data.ppro.decode[0] = insn;
10482             ix86_sched_data.ppro.decode[1] = NULL;
10483             ix86_sched_data.ppro.decode[2] = NULL;
10484             if (sched_verbose)
10485               ix86_dump_ppro_packet (dump);
10486             ix86_sched_data.ppro.decode[0] = NULL;
10487           }
10488         else if (uops == PPRO_UOPS_FEW)
10489           {
10490             if (sched_verbose)
10491               ix86_dump_ppro_packet (dump);
10492             ix86_sched_data.ppro.decode[0] = insn;
10493             ix86_sched_data.ppro.decode[1] = NULL;
10494             ix86_sched_data.ppro.decode[2] = NULL;
10495           }
10496         else
10497           {
10498             for (i = 0; i < 3; ++i)
10499               if (ix86_sched_data.ppro.decode[i] == NULL)
10500                 {
10501                   ix86_sched_data.ppro.decode[i] = insn;
10502                   break;
10503                 }
10504             if (i == 3)
10505               abort ();
10506             if (i == 2)
10507               {
10508                 if (sched_verbose)
10509                   ix86_dump_ppro_packet (dump);
10510                 ix86_sched_data.ppro.decode[0] = NULL;
10511                 ix86_sched_data.ppro.decode[1] = NULL;
10512                 ix86_sched_data.ppro.decode[2] = NULL;
10513               }
10514           }
10515       }
10516       return --ix86_sched_data.ppro.issued_this_cycle;
10517     }
10518 }
10519 \f
10520 /* Walk through INSNS and look for MEM references whose address is DSTREG or
10521    SRCREG and set the memory attribute to those of DSTREF and SRCREF, as
10522    appropriate.  */
10523
10524 void
10525 ix86_set_move_mem_attrs (insns, dstref, srcref, dstreg, srcreg)
10526      rtx insns;
10527      rtx dstref, srcref, dstreg, srcreg;
10528 {
10529   rtx insn;
10530
10531   for (insn = insns; insn != 0 ; insn = NEXT_INSN (insn))
10532     if (INSN_P (insn))
10533       ix86_set_move_mem_attrs_1 (PATTERN (insn), dstref, srcref,
10534                                  dstreg, srcreg);
10535 }
10536
10537 /* Subroutine of above to actually do the updating by recursively walking
10538    the rtx.  */
10539
10540 static void
10541 ix86_set_move_mem_attrs_1 (x, dstref, srcref, dstreg, srcreg)
10542      rtx x;
10543      rtx dstref, srcref, dstreg, srcreg;
10544 {
10545   enum rtx_code code = GET_CODE (x);
10546   const char *format_ptr = GET_RTX_FORMAT (code);
10547   int i, j;
10548
10549   if (code == MEM && XEXP (x, 0) == dstreg)
10550     MEM_COPY_ATTRIBUTES (x, dstref);
10551   else if (code == MEM && XEXP (x, 0) == srcreg)
10552     MEM_COPY_ATTRIBUTES (x, srcref);
10553
10554   for (i = 0; i < GET_RTX_LENGTH (code); i++, format_ptr++)
10555     {
10556       if (*format_ptr == 'e')
10557         ix86_set_move_mem_attrs_1 (XEXP (x, i), dstref, srcref,
10558                                    dstreg, srcreg);
10559       else if (*format_ptr == 'E')
10560         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10561           ix86_set_move_mem_attrs_1 (XVECEXP (x, i, j), dstref, srcref,
10562                                      dstreg, srcreg);
10563     }
10564 }
10565 \f
10566 /* Compute the alignment given to a constant that is being placed in memory.
10567    EXP is the constant and ALIGN is the alignment that the object would
10568    ordinarily have.
10569    The value of this function is used instead of that alignment to align
10570    the object.  */
10571
10572 int
10573 ix86_constant_alignment (exp, align)
10574      tree exp;
10575      int align;
10576 {
10577   if (TREE_CODE (exp) == REAL_CST)
10578     {
10579       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
10580         return 64;
10581       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
10582         return 128;
10583     }
10584   else if (TREE_CODE (exp) == STRING_CST && TREE_STRING_LENGTH (exp) >= 31
10585            && align < 256)
10586     return 256;
10587
10588   return align;
10589 }
10590
10591 /* Compute the alignment for a static variable.
10592    TYPE is the data type, and ALIGN is the alignment that
10593    the object would ordinarily have.  The value of this function is used
10594    instead of that alignment to align the object.  */
10595
10596 int
10597 ix86_data_alignment (type, align)
10598      tree type;
10599      int align;
10600 {
10601   if (AGGREGATE_TYPE_P (type)
10602        && TYPE_SIZE (type)
10603        && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10604        && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
10605            || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
10606     return 256;
10607
10608   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
10609      to 16byte boundary.  */
10610   if (TARGET_64BIT)
10611     {
10612       if (AGGREGATE_TYPE_P (type)
10613            && TYPE_SIZE (type)
10614            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10615            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
10616                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
10617         return 128;
10618     }
10619
10620   if (TREE_CODE (type) == ARRAY_TYPE)
10621     {
10622       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
10623         return 64;
10624       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
10625         return 128;
10626     }
10627   else if (TREE_CODE (type) == COMPLEX_TYPE)
10628     {
10629
10630       if (TYPE_MODE (type) == DCmode && align < 64)
10631         return 64;
10632       if (TYPE_MODE (type) == XCmode && align < 128)
10633         return 128;
10634     }
10635   else if ((TREE_CODE (type) == RECORD_TYPE
10636             || TREE_CODE (type) == UNION_TYPE
10637             || TREE_CODE (type) == QUAL_UNION_TYPE)
10638            && TYPE_FIELDS (type))
10639     {
10640       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
10641         return 64;
10642       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
10643         return 128;
10644     }
10645   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
10646            || TREE_CODE (type) == INTEGER_TYPE)
10647     {
10648       if (TYPE_MODE (type) == DFmode && align < 64)
10649         return 64;
10650       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
10651         return 128;
10652     }
10653
10654   return align;
10655 }
10656
10657 /* Compute the alignment for a local variable.
10658    TYPE is the data type, and ALIGN is the alignment that
10659    the object would ordinarily have.  The value of this macro is used
10660    instead of that alignment to align the object.  */
10661
10662 int
10663 ix86_local_alignment (type, align)
10664      tree type;
10665      int align;
10666 {
10667   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
10668      to 16byte boundary.  */
10669   if (TARGET_64BIT)
10670     {
10671       if (AGGREGATE_TYPE_P (type)
10672            && TYPE_SIZE (type)
10673            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10674            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
10675                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
10676         return 128;
10677     }
10678   if (TREE_CODE (type) == ARRAY_TYPE)
10679     {
10680       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
10681         return 64;
10682       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
10683         return 128;
10684     }
10685   else if (TREE_CODE (type) == COMPLEX_TYPE)
10686     {
10687       if (TYPE_MODE (type) == DCmode && align < 64)
10688         return 64;
10689       if (TYPE_MODE (type) == XCmode && align < 128)
10690         return 128;
10691     }
10692   else if ((TREE_CODE (type) == RECORD_TYPE
10693             || TREE_CODE (type) == UNION_TYPE
10694             || TREE_CODE (type) == QUAL_UNION_TYPE)
10695            && TYPE_FIELDS (type))
10696     {
10697       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
10698         return 64;
10699       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
10700         return 128;
10701     }
10702   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
10703            || TREE_CODE (type) == INTEGER_TYPE)
10704     {
10705
10706       if (TYPE_MODE (type) == DFmode && align < 64)
10707         return 64;
10708       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
10709         return 128;
10710     }
10711   return align;
10712 }
10713 \f
10714 /* Emit RTL insns to initialize the variable parts of a trampoline.
10715    FNADDR is an RTX for the address of the function's pure code.
10716    CXT is an RTX for the static chain value for the function.  */
10717 void
10718 x86_initialize_trampoline (tramp, fnaddr, cxt)
10719      rtx tramp, fnaddr, cxt;
10720 {
10721   if (!TARGET_64BIT)
10722     {
10723       /* Compute offset from the end of the jmp to the target function.  */
10724       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
10725                                plus_constant (tramp, 10),
10726                                NULL_RTX, 1, OPTAB_DIRECT);
10727       emit_move_insn (gen_rtx_MEM (QImode, tramp),
10728                       GEN_INT (trunc_int_for_mode (0xb9, QImode)));
10729       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
10730       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
10731                       GEN_INT (trunc_int_for_mode (0xe9, QImode)));
10732       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
10733     }
10734   else
10735     {
10736       int offset = 0;
10737       /* Try to load address using shorter movl instead of movabs.
10738          We may want to support movq for kernel mode, but kernel does not use
10739          trampolines at the moment.  */
10740       if (x86_64_zero_extended_value (fnaddr))
10741         {
10742           fnaddr = copy_to_mode_reg (DImode, fnaddr);
10743           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
10744                           GEN_INT (trunc_int_for_mode (0xbb41, HImode)));
10745           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
10746                           gen_lowpart (SImode, fnaddr));
10747           offset += 6;
10748         }
10749       else
10750         {
10751           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
10752                           GEN_INT (trunc_int_for_mode (0xbb49, HImode)));
10753           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
10754                           fnaddr);
10755           offset += 10;
10756         }
10757       /* Load static chain using movabs to r10.  */
10758       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
10759                       GEN_INT (trunc_int_for_mode (0xba49, HImode)));
10760       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
10761                       cxt);
10762       offset += 10;
10763       /* Jump to the r11 */
10764       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
10765                       GEN_INT (trunc_int_for_mode (0xff49, HImode)));
10766       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
10767                       GEN_INT (trunc_int_for_mode (0xe3, QImode)));
10768       offset += 3;
10769       if (offset > TRAMPOLINE_SIZE)
10770         abort();
10771     }
10772 }
10773 \f
10774 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
10775 do {                                                                    \
10776   if ((MASK) & target_flags)                                            \
10777     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL);       \
10778 } while (0)
10779
10780 struct builtin_description
10781 {
10782   const unsigned int mask;
10783   const enum insn_code icode;
10784   const char *const name;
10785   const enum ix86_builtins code;
10786   const enum rtx_code comparison;
10787   const unsigned int flag;
10788 };
10789
10790 static const struct builtin_description bdesc_comi[] =
10791 {
10792   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, EQ, 0 },
10793   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, LT, 0 },
10794   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, LE, 0 },
10795   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, LT, 1 },
10796   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, LE, 1 },
10797   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, NE, 0 },
10798   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, EQ, 0 },
10799   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, LT, 0 },
10800   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, LE, 0 },
10801   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, LT, 1 },
10802   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, LE, 1 },
10803   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, NE, 0 }
10804 };
10805
10806 static const struct builtin_description bdesc_2arg[] =
10807 {
10808   /* SSE */
10809   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
10810   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
10811   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
10812   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
10813   { MASK_SSE, CODE_FOR_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
10814   { MASK_SSE, CODE_FOR_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
10815   { MASK_SSE, CODE_FOR_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
10816   { MASK_SSE, CODE_FOR_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
10817
10818   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
10819   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
10820   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
10821   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, 1 },
10822   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, 1 },
10823   { MASK_SSE, CODE_FOR_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
10824   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, EQ, 0 },
10825   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, LT, 0 },
10826   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, LE, 0 },
10827   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, LT, 1 },
10828   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, LE, 1 },
10829   { MASK_SSE, CODE_FOR_maskncmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, UNORDERED, 0 },
10830   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
10831   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
10832   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
10833   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpgtss", IX86_BUILTIN_CMPGTSS, LT, 1 },
10834   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpgess", IX86_BUILTIN_CMPGESS, LE, 1 },
10835   { MASK_SSE, CODE_FOR_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
10836   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, EQ, 0 },
10837   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, LT, 0 },
10838   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, LE, 0 },
10839   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, LT, 1 },
10840   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, LE, 1 },
10841   { MASK_SSE, CODE_FOR_vmmaskncmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
10842
10843   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
10844   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
10845   { MASK_SSE, CODE_FOR_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
10846   { MASK_SSE, CODE_FOR_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
10847
10848   { MASK_SSE, CODE_FOR_sse_andti3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
10849   { MASK_SSE, CODE_FOR_sse_nandti3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
10850   { MASK_SSE, CODE_FOR_sse_iorti3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
10851   { MASK_SSE, CODE_FOR_sse_xorti3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
10852
10853   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
10854   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
10855   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
10856   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
10857   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
10858
10859   /* MMX */
10860   { MASK_MMX, CODE_FOR_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
10861   { MASK_MMX, CODE_FOR_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
10862   { MASK_MMX, CODE_FOR_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
10863   { MASK_MMX, CODE_FOR_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
10864   { MASK_MMX, CODE_FOR_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
10865   { MASK_MMX, CODE_FOR_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
10866
10867   { MASK_MMX, CODE_FOR_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
10868   { MASK_MMX, CODE_FOR_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
10869   { MASK_MMX, CODE_FOR_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
10870   { MASK_MMX, CODE_FOR_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
10871   { MASK_MMX, CODE_FOR_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
10872   { MASK_MMX, CODE_FOR_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
10873   { MASK_MMX, CODE_FOR_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
10874   { MASK_MMX, CODE_FOR_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
10875
10876   { MASK_MMX, CODE_FOR_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
10877   { MASK_MMX, CODE_FOR_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
10878   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
10879
10880   { MASK_MMX, CODE_FOR_mmx_anddi3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
10881   { MASK_MMX, CODE_FOR_mmx_nanddi3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
10882   { MASK_MMX, CODE_FOR_mmx_iordi3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
10883   { MASK_MMX, CODE_FOR_mmx_xordi3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
10884
10885   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
10886   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
10887
10888   { MASK_MMX, CODE_FOR_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
10889   { MASK_MMX, CODE_FOR_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
10890   { MASK_MMX, CODE_FOR_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
10891   { MASK_MMX, CODE_FOR_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
10892   { MASK_MMX, CODE_FOR_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
10893   { MASK_MMX, CODE_FOR_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
10894
10895   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
10896   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
10897   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
10898   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
10899
10900   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
10901   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
10902   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
10903   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
10904   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
10905   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
10906
10907   /* Special.  */
10908   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
10909   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
10910   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
10911
10912   { MASK_SSE, CODE_FOR_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
10913   { MASK_SSE, CODE_FOR_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
10914
10915   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
10916   { MASK_MMX, CODE_FOR_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
10917   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
10918   { MASK_MMX, CODE_FOR_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
10919   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
10920   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
10921
10922   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
10923   { MASK_MMX, CODE_FOR_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
10924   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
10925   { MASK_MMX, CODE_FOR_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
10926   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
10927   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
10928
10929   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
10930   { MASK_MMX, CODE_FOR_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
10931   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
10932   { MASK_MMX, CODE_FOR_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
10933
10934   { MASK_SSE, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
10935   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 }
10936
10937 };
10938
10939 static const struct builtin_description bdesc_1arg[] =
10940 {
10941   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
10942   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
10943
10944   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
10945   { MASK_SSE, CODE_FOR_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
10946   { MASK_SSE, CODE_FOR_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
10947
10948   { MASK_SSE, CODE_FOR_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
10949   { MASK_SSE, CODE_FOR_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
10950   { MASK_SSE, CODE_FOR_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
10951   { MASK_SSE, CODE_FOR_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 }
10952
10953 };
10954
10955 void
10956 ix86_init_builtins ()
10957 {
10958   if (TARGET_MMX)
10959     ix86_init_mmx_sse_builtins ();
10960 }
10961
10962 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
10963    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
10964    builtins.  */
10965 void
10966 ix86_init_mmx_sse_builtins ()
10967 {
10968   const struct builtin_description * d;
10969   size_t i;
10970   tree endlink = void_list_node;
10971
10972   tree pchar_type_node = build_pointer_type (char_type_node);
10973   tree pfloat_type_node = build_pointer_type (float_type_node);
10974   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
10975   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
10976
10977   /* Comparisons.  */
10978   tree int_ftype_v4sf_v4sf
10979     = build_function_type (integer_type_node,
10980                            tree_cons (NULL_TREE, V4SF_type_node,
10981                                       tree_cons (NULL_TREE,
10982                                                  V4SF_type_node,
10983                                                  endlink)));
10984   tree v4si_ftype_v4sf_v4sf
10985     = build_function_type (V4SI_type_node,
10986                            tree_cons (NULL_TREE, V4SF_type_node,
10987                                       tree_cons (NULL_TREE,
10988                                                  V4SF_type_node,
10989                                                  endlink)));
10990   /* MMX/SSE/integer conversions.  */
10991   tree int_ftype_v4sf
10992     = build_function_type (integer_type_node,
10993                            tree_cons (NULL_TREE, V4SF_type_node,
10994                                       endlink));
10995   tree int_ftype_v8qi
10996     = build_function_type (integer_type_node,
10997                            tree_cons (NULL_TREE, V8QI_type_node,
10998                                       endlink));
10999   tree int_ftype_v2si
11000     = build_function_type (integer_type_node,
11001                            tree_cons (NULL_TREE, V2SI_type_node,
11002                                       endlink));
11003   tree v2si_ftype_int
11004     = build_function_type (V2SI_type_node,
11005                            tree_cons (NULL_TREE, integer_type_node,
11006                                       endlink));
11007   tree v4sf_ftype_v4sf_int
11008     = build_function_type (V4SF_type_node,
11009                            tree_cons (NULL_TREE, V4SF_type_node,
11010                                       tree_cons (NULL_TREE, integer_type_node,
11011                                                  endlink)));
11012   tree v4sf_ftype_v4sf_v2si
11013     = build_function_type (V4SF_type_node,
11014                            tree_cons (NULL_TREE, V4SF_type_node,
11015                                       tree_cons (NULL_TREE, V2SI_type_node,
11016                                                  endlink)));
11017   tree int_ftype_v4hi_int
11018     = build_function_type (integer_type_node,
11019                            tree_cons (NULL_TREE, V4HI_type_node,
11020                                       tree_cons (NULL_TREE, integer_type_node,
11021                                                  endlink)));
11022   tree v4hi_ftype_v4hi_int_int
11023     = build_function_type (V4HI_type_node,
11024                            tree_cons (NULL_TREE, V4HI_type_node,
11025                                       tree_cons (NULL_TREE, integer_type_node,
11026                                                  tree_cons (NULL_TREE,
11027                                                             integer_type_node,
11028                                                             endlink))));
11029   /* Miscellaneous.  */
11030   tree v8qi_ftype_v4hi_v4hi
11031     = build_function_type (V8QI_type_node,
11032                            tree_cons (NULL_TREE, V4HI_type_node,
11033                                       tree_cons (NULL_TREE, V4HI_type_node,
11034                                                  endlink)));
11035   tree v4hi_ftype_v2si_v2si
11036     = build_function_type (V4HI_type_node,
11037                            tree_cons (NULL_TREE, V2SI_type_node,
11038                                       tree_cons (NULL_TREE, V2SI_type_node,
11039                                                  endlink)));
11040   tree v4sf_ftype_v4sf_v4sf_int
11041     = build_function_type (V4SF_type_node,
11042                            tree_cons (NULL_TREE, V4SF_type_node,
11043                                       tree_cons (NULL_TREE, V4SF_type_node,
11044                                                  tree_cons (NULL_TREE,
11045                                                             integer_type_node,
11046                                                             endlink))));
11047   tree v4hi_ftype_v8qi_v8qi
11048     = build_function_type (V4HI_type_node,
11049                            tree_cons (NULL_TREE, V8QI_type_node,
11050                                       tree_cons (NULL_TREE, V8QI_type_node,
11051                                                  endlink)));
11052   tree v2si_ftype_v4hi_v4hi
11053     = build_function_type (V2SI_type_node,
11054                            tree_cons (NULL_TREE, V4HI_type_node,
11055                                       tree_cons (NULL_TREE, V4HI_type_node,
11056                                                  endlink)));
11057   tree v4hi_ftype_v4hi_int
11058     = build_function_type (V4HI_type_node,
11059                            tree_cons (NULL_TREE, V4HI_type_node,
11060                                       tree_cons (NULL_TREE, integer_type_node,
11061                                                  endlink)));
11062   tree v4hi_ftype_v4hi_di
11063     = build_function_type (V4HI_type_node,
11064                            tree_cons (NULL_TREE, V4HI_type_node,
11065                                       tree_cons (NULL_TREE,
11066                                                  long_long_integer_type_node,
11067                                                  endlink)));
11068   tree v2si_ftype_v2si_di
11069     = build_function_type (V2SI_type_node,
11070                            tree_cons (NULL_TREE, V2SI_type_node,
11071                                       tree_cons (NULL_TREE,
11072                                                  long_long_integer_type_node,
11073                                                  endlink)));
11074   tree void_ftype_void
11075     = build_function_type (void_type_node, endlink);
11076   tree void_ftype_pchar_int
11077     = build_function_type (void_type_node,
11078                            tree_cons (NULL_TREE, pchar_type_node,
11079                                       tree_cons (NULL_TREE, integer_type_node,
11080                                                  endlink)));
11081   tree void_ftype_unsigned
11082     = build_function_type (void_type_node,
11083                            tree_cons (NULL_TREE, unsigned_type_node,
11084                                       endlink));
11085   tree unsigned_ftype_void
11086     = build_function_type (unsigned_type_node, endlink);
11087   tree di_ftype_void
11088     = build_function_type (long_long_unsigned_type_node, endlink);
11089   tree ti_ftype_void
11090     = build_function_type (intTI_type_node, endlink);
11091   tree v2si_ftype_v4sf
11092     = build_function_type (V2SI_type_node,
11093                            tree_cons (NULL_TREE, V4SF_type_node,
11094                                       endlink));
11095   /* Loads/stores.  */
11096   tree maskmovq_args = tree_cons (NULL_TREE, V8QI_type_node,
11097                                   tree_cons (NULL_TREE, V8QI_type_node,
11098                                              tree_cons (NULL_TREE,
11099                                                         pchar_type_node,
11100                                                         endlink)));
11101   tree void_ftype_v8qi_v8qi_pchar
11102     = build_function_type (void_type_node, maskmovq_args);
11103   tree v4sf_ftype_pfloat
11104     = build_function_type (V4SF_type_node,
11105                            tree_cons (NULL_TREE, pfloat_type_node,
11106                                       endlink));
11107   tree v4sf_ftype_float
11108     = build_function_type (V4SF_type_node,
11109                            tree_cons (NULL_TREE, float_type_node,
11110                                       endlink));
11111   tree v4sf_ftype_float_float_float_float
11112     = build_function_type (V4SF_type_node,
11113                            tree_cons (NULL_TREE, float_type_node,
11114                                       tree_cons (NULL_TREE, float_type_node,
11115                                                  tree_cons (NULL_TREE,
11116                                                             float_type_node,
11117                                                             tree_cons (NULL_TREE,
11118                                                                        float_type_node,
11119                                                                        endlink)))));
11120   /* @@@ the type is bogus */
11121   tree v4sf_ftype_v4sf_pv2si
11122     = build_function_type (V4SF_type_node,
11123                            tree_cons (NULL_TREE, V4SF_type_node,
11124                                       tree_cons (NULL_TREE, pv2si_type_node,
11125                                                  endlink)));
11126   tree void_ftype_pv2si_v4sf
11127     = build_function_type (void_type_node,
11128                            tree_cons (NULL_TREE, pv2si_type_node,
11129                                       tree_cons (NULL_TREE, V4SF_type_node,
11130                                                  endlink)));
11131   tree void_ftype_pfloat_v4sf
11132     = build_function_type (void_type_node,
11133                            tree_cons (NULL_TREE, pfloat_type_node,
11134                                       tree_cons (NULL_TREE, V4SF_type_node,
11135                                                  endlink)));
11136   tree void_ftype_pdi_di
11137     = build_function_type (void_type_node,
11138                            tree_cons (NULL_TREE, pdi_type_node,
11139                                       tree_cons (NULL_TREE,
11140                                                  long_long_unsigned_type_node,
11141                                                  endlink)));
11142   /* Normal vector unops.  */
11143   tree v4sf_ftype_v4sf
11144     = build_function_type (V4SF_type_node,
11145                            tree_cons (NULL_TREE, V4SF_type_node,
11146                                       endlink));
11147
11148   /* Normal vector binops.  */
11149   tree v4sf_ftype_v4sf_v4sf
11150     = build_function_type (V4SF_type_node,
11151                            tree_cons (NULL_TREE, V4SF_type_node,
11152                                       tree_cons (NULL_TREE, V4SF_type_node,
11153                                                  endlink)));
11154   tree v8qi_ftype_v8qi_v8qi
11155     = build_function_type (V8QI_type_node,
11156                            tree_cons (NULL_TREE, V8QI_type_node,
11157                                       tree_cons (NULL_TREE, V8QI_type_node,
11158                                                  endlink)));
11159   tree v4hi_ftype_v4hi_v4hi
11160     = build_function_type (V4HI_type_node,
11161                            tree_cons (NULL_TREE, V4HI_type_node,
11162                                       tree_cons (NULL_TREE, V4HI_type_node,
11163                                                  endlink)));
11164   tree v2si_ftype_v2si_v2si
11165     = build_function_type (V2SI_type_node,
11166                            tree_cons (NULL_TREE, V2SI_type_node,
11167                                       tree_cons (NULL_TREE, V2SI_type_node,
11168                                                  endlink)));
11169   tree ti_ftype_ti_ti
11170     = build_function_type (intTI_type_node,
11171                            tree_cons (NULL_TREE, intTI_type_node,
11172                                       tree_cons (NULL_TREE, intTI_type_node,
11173                                                  endlink)));
11174   tree di_ftype_di_di
11175     = build_function_type (long_long_unsigned_type_node,
11176                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11177                                       tree_cons (NULL_TREE,
11178                                                  long_long_unsigned_type_node,
11179                                                  endlink)));
11180
11181   tree v2si_ftype_v2sf
11182     = build_function_type (V2SI_type_node,
11183                            tree_cons (NULL_TREE, V2SF_type_node,
11184                                       endlink));
11185   tree v2sf_ftype_v2si
11186     = build_function_type (V2SF_type_node,
11187                            tree_cons (NULL_TREE, V2SI_type_node,
11188                                       endlink));
11189   tree v2si_ftype_v2si
11190     = build_function_type (V2SI_type_node,
11191                            tree_cons (NULL_TREE, V2SI_type_node,
11192                                       endlink));
11193   tree v2sf_ftype_v2sf
11194     = build_function_type (V2SF_type_node,
11195                            tree_cons (NULL_TREE, V2SF_type_node,
11196                                       endlink));
11197   tree v2sf_ftype_v2sf_v2sf
11198     = build_function_type (V2SF_type_node,
11199                            tree_cons (NULL_TREE, V2SF_type_node,
11200                                       tree_cons (NULL_TREE,
11201                                                  V2SF_type_node,
11202                                                  endlink)));
11203   tree v2si_ftype_v2sf_v2sf
11204     = build_function_type (V2SI_type_node,
11205                            tree_cons (NULL_TREE, V2SF_type_node,
11206                                       tree_cons (NULL_TREE,
11207                                                  V2SF_type_node,
11208                                                  endlink)));
11209
11210   tree void_ftype_pchar
11211     = build_function_type (void_type_node,
11212                            tree_cons (NULL_TREE, pchar_type_node,
11213                                       endlink));
11214
11215   /* Add all builtins that are more or less simple operations on two
11216      operands.  */
11217   for (i = 0, d = bdesc_2arg; i < sizeof (bdesc_2arg) / sizeof *d; i++, d++)
11218     {
11219       /* Use one of the operands; the target can have a different mode for
11220          mask-generating compares.  */
11221       enum machine_mode mode;
11222       tree type;
11223
11224       if (d->name == 0)
11225         continue;
11226       mode = insn_data[d->icode].operand[1].mode;
11227
11228       switch (mode)
11229         {
11230         case V4SFmode:
11231           type = v4sf_ftype_v4sf_v4sf;
11232           break;
11233         case V8QImode:
11234           type = v8qi_ftype_v8qi_v8qi;
11235           break;
11236         case V4HImode:
11237           type = v4hi_ftype_v4hi_v4hi;
11238           break;
11239         case V2SImode:
11240           type = v2si_ftype_v2si_v2si;
11241           break;
11242         case TImode:
11243           type = ti_ftype_ti_ti;
11244           break;
11245         case DImode:
11246           type = di_ftype_di_di;
11247           break;
11248
11249         default:
11250           abort ();
11251         }
11252
11253       /* Override for comparisons.  */
11254       if (d->icode == CODE_FOR_maskcmpv4sf3
11255           || d->icode == CODE_FOR_maskncmpv4sf3
11256           || d->icode == CODE_FOR_vmmaskcmpv4sf3
11257           || d->icode == CODE_FOR_vmmaskncmpv4sf3)
11258         type = v4si_ftype_v4sf_v4sf;
11259
11260       def_builtin (d->mask, d->name, type, d->code);
11261     }
11262
11263   /* Add the remaining MMX insns with somewhat more complicated types.  */
11264   def_builtin (MASK_MMX, "__builtin_ia32_m_from_int", v2si_ftype_int, IX86_BUILTIN_M_FROM_INT);
11265   def_builtin (MASK_MMX, "__builtin_ia32_m_to_int", int_ftype_v2si, IX86_BUILTIN_M_TO_INT);
11266   def_builtin (MASK_MMX, "__builtin_ia32_mmx_zero", di_ftype_void, IX86_BUILTIN_MMX_ZERO);
11267   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
11268   def_builtin (MASK_MMX, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
11269   def_builtin (MASK_MMX, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
11270   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
11271   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
11272   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
11273
11274   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
11275   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
11276   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
11277
11278   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
11279   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
11280
11281   def_builtin (MASK_MMX, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
11282   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
11283
11284   /* comi/ucomi insns.  */
11285   for (i = 0, d = bdesc_comi; i < sizeof (bdesc_comi) / sizeof *d; i++, d++)
11286     def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
11287
11288   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
11289   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
11290   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
11291
11292   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
11293   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
11294   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
11295   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
11296   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
11297   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
11298
11299   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pextrw", int_ftype_v4hi_int, IX86_BUILTIN_PEXTRW);
11300   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pinsrw", v4hi_ftype_v4hi_int_int, IX86_BUILTIN_PINSRW);
11301
11302   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
11303
11304   def_builtin (MASK_SSE, "__builtin_ia32_loadaps", v4sf_ftype_pfloat, IX86_BUILTIN_LOADAPS);
11305   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pfloat, IX86_BUILTIN_LOADUPS);
11306   def_builtin (MASK_SSE, "__builtin_ia32_loadss", v4sf_ftype_pfloat, IX86_BUILTIN_LOADSS);
11307   def_builtin (MASK_SSE, "__builtin_ia32_storeaps", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREAPS);
11308   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
11309   def_builtin (MASK_SSE, "__builtin_ia32_storess", void_ftype_pfloat_v4sf, IX86_BUILTIN_STORESS);
11310
11311   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
11312   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
11313   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
11314   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
11315
11316   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
11317   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
11318   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
11319   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
11320
11321   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
11322   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_prefetch", void_ftype_pchar_int, IX86_BUILTIN_PREFETCH);
11323
11324   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", v4hi_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
11325
11326   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
11327   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
11328   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
11329   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
11330   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
11331   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
11332
11333   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
11334
11335   /* Original 3DNow!  */
11336   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
11337   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
11338   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
11339   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
11340   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
11341   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
11342   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
11343   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
11344   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
11345   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
11346   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
11347   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
11348   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
11349   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
11350   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
11351   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
11352   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
11353   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
11354   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
11355   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
11356   def_builtin (MASK_3DNOW, "__builtin_ia32_prefetch_3dnow", void_ftype_pchar, IX86_BUILTIN_PREFETCH_3DNOW);
11357   def_builtin (MASK_3DNOW, "__builtin_ia32_prefetchw", void_ftype_pchar, IX86_BUILTIN_PREFETCHW);
11358
11359   /* 3DNow! extension as used in the Athlon CPU.  */
11360   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
11361   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
11362   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
11363   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
11364   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
11365   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
11366
11367   /* Composite intrinsics.  */
11368   def_builtin (MASK_SSE, "__builtin_ia32_setps1", v4sf_ftype_float, IX86_BUILTIN_SETPS1);
11369   def_builtin (MASK_SSE, "__builtin_ia32_setps", v4sf_ftype_float_float_float_float, IX86_BUILTIN_SETPS);
11370   def_builtin (MASK_SSE, "__builtin_ia32_setzerops", ti_ftype_void, IX86_BUILTIN_CLRPS);
11371   def_builtin (MASK_SSE, "__builtin_ia32_loadps1", v4sf_ftype_pfloat, IX86_BUILTIN_LOADPS1);
11372   def_builtin (MASK_SSE, "__builtin_ia32_loadrps", v4sf_ftype_pfloat, IX86_BUILTIN_LOADRPS);
11373   def_builtin (MASK_SSE, "__builtin_ia32_storeps1", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREPS1);
11374   def_builtin (MASK_SSE, "__builtin_ia32_storerps", void_ftype_pfloat_v4sf, IX86_BUILTIN_STORERPS);
11375 }
11376
11377 /* Errors in the source file can cause expand_expr to return const0_rtx
11378    where we expect a vector.  To avoid crashing, use one of the vector
11379    clear instructions.  */
11380 static rtx
11381 safe_vector_operand (x, mode)
11382      rtx x;
11383      enum machine_mode mode;
11384 {
11385   if (x != const0_rtx)
11386     return x;
11387   x = gen_reg_rtx (mode);
11388
11389   if (VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode))
11390     emit_insn (gen_mmx_clrdi (mode == DImode ? x
11391                               : gen_rtx_SUBREG (DImode, x, 0)));
11392   else
11393     emit_insn (gen_sse_clrti (mode == TImode ? x
11394                               : gen_rtx_SUBREG (TImode, x, 0)));
11395   return x;
11396 }
11397
11398 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
11399
11400 static rtx
11401 ix86_expand_binop_builtin (icode, arglist, target)
11402      enum insn_code icode;
11403      tree arglist;
11404      rtx target;
11405 {
11406   rtx pat;
11407   tree arg0 = TREE_VALUE (arglist);
11408   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11409   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11410   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11411   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11412   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11413   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11414
11415   if (VECTOR_MODE_P (mode0))
11416     op0 = safe_vector_operand (op0, mode0);
11417   if (VECTOR_MODE_P (mode1))
11418     op1 = safe_vector_operand (op1, mode1);
11419
11420   if (! target
11421       || GET_MODE (target) != tmode
11422       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11423     target = gen_reg_rtx (tmode);
11424
11425   /* In case the insn wants input operands in modes different from
11426      the result, abort.  */
11427   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
11428     abort ();
11429
11430   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11431     op0 = copy_to_mode_reg (mode0, op0);
11432   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11433     op1 = copy_to_mode_reg (mode1, op1);
11434
11435   pat = GEN_FCN (icode) (target, op0, op1);
11436   if (! pat)
11437     return 0;
11438   emit_insn (pat);
11439   return target;
11440 }
11441
11442 /* Subroutine of ix86_expand_builtin to take care of stores.  */
11443
11444 static rtx
11445 ix86_expand_store_builtin (icode, arglist, shuffle)
11446      enum insn_code icode;
11447      tree arglist;
11448      int shuffle;
11449 {
11450   rtx pat;
11451   tree arg0 = TREE_VALUE (arglist);
11452   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11453   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11454   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11455   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
11456   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
11457
11458   if (VECTOR_MODE_P (mode1))
11459     op1 = safe_vector_operand (op1, mode1);
11460
11461   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11462   if (shuffle >= 0 || ! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11463     op1 = copy_to_mode_reg (mode1, op1);
11464   if (shuffle >= 0)
11465     emit_insn (gen_sse_shufps (op1, op1, op1, GEN_INT (shuffle)));
11466   pat = GEN_FCN (icode) (op0, op1);
11467   if (pat)
11468     emit_insn (pat);
11469   return 0;
11470 }
11471
11472 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
11473
11474 static rtx
11475 ix86_expand_unop_builtin (icode, arglist, target, do_load)
11476      enum insn_code icode;
11477      tree arglist;
11478      rtx target;
11479      int do_load;
11480 {
11481   rtx pat;
11482   tree arg0 = TREE_VALUE (arglist);
11483   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11484   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11485   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11486
11487   if (! target
11488       || GET_MODE (target) != tmode
11489       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11490     target = gen_reg_rtx (tmode);
11491   if (do_load)
11492     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11493   else
11494     {
11495       if (VECTOR_MODE_P (mode0))
11496         op0 = safe_vector_operand (op0, mode0);
11497
11498       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11499         op0 = copy_to_mode_reg (mode0, op0);
11500     }
11501
11502   pat = GEN_FCN (icode) (target, op0);
11503   if (! pat)
11504     return 0;
11505   emit_insn (pat);
11506   return target;
11507 }
11508
11509 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
11510    sqrtss, rsqrtss, rcpss.  */
11511
11512 static rtx
11513 ix86_expand_unop1_builtin (icode, arglist, target)
11514      enum insn_code icode;
11515      tree arglist;
11516      rtx target;
11517 {
11518   rtx pat;
11519   tree arg0 = TREE_VALUE (arglist);
11520   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11521   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11522   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11523
11524   if (! target
11525       || GET_MODE (target) != tmode
11526       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11527     target = gen_reg_rtx (tmode);
11528
11529   if (VECTOR_MODE_P (mode0))
11530     op0 = safe_vector_operand (op0, mode0);
11531
11532   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11533     op0 = copy_to_mode_reg (mode0, op0);
11534
11535   pat = GEN_FCN (icode) (target, op0, op0);
11536   if (! pat)
11537     return 0;
11538   emit_insn (pat);
11539   return target;
11540 }
11541
11542 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
11543
11544 static rtx
11545 ix86_expand_sse_compare (d, arglist, target)
11546      const struct builtin_description *d;
11547      tree arglist;
11548      rtx target;
11549 {
11550   rtx pat;
11551   tree arg0 = TREE_VALUE (arglist);
11552   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11553   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11554   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11555   rtx op2;
11556   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
11557   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
11558   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
11559   enum rtx_code comparison = d->comparison;
11560
11561   if (VECTOR_MODE_P (mode0))
11562     op0 = safe_vector_operand (op0, mode0);
11563   if (VECTOR_MODE_P (mode1))
11564     op1 = safe_vector_operand (op1, mode1);
11565
11566   /* Swap operands if we have a comparison that isn't available in
11567      hardware.  */
11568   if (d->flag)
11569     {
11570       rtx tmp = gen_reg_rtx (mode1);
11571       emit_move_insn (tmp, op1);
11572       op1 = op0;
11573       op0 = tmp;
11574     }
11575
11576   if (! target
11577       || GET_MODE (target) != tmode
11578       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
11579     target = gen_reg_rtx (tmode);
11580
11581   if (! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
11582     op0 = copy_to_mode_reg (mode0, op0);
11583   if (! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
11584     op1 = copy_to_mode_reg (mode1, op1);
11585
11586   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
11587   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
11588   if (! pat)
11589     return 0;
11590   emit_insn (pat);
11591   return target;
11592 }
11593
11594 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
11595
11596 static rtx
11597 ix86_expand_sse_comi (d, arglist, target)
11598      const struct builtin_description *d;
11599      tree arglist;
11600      rtx target;
11601 {
11602   rtx pat;
11603   tree arg0 = TREE_VALUE (arglist);
11604   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11605   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11606   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11607   rtx op2;
11608   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
11609   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
11610   enum rtx_code comparison = d->comparison;
11611
11612   if (VECTOR_MODE_P (mode0))
11613     op0 = safe_vector_operand (op0, mode0);
11614   if (VECTOR_MODE_P (mode1))
11615     op1 = safe_vector_operand (op1, mode1);
11616
11617   /* Swap operands if we have a comparison that isn't available in
11618      hardware.  */
11619   if (d->flag)
11620     {
11621       rtx tmp = op1;
11622       op1 = op0;
11623       op0 = tmp;
11624     }
11625
11626   target = gen_reg_rtx (SImode);
11627   emit_move_insn (target, const0_rtx);
11628   target = gen_rtx_SUBREG (QImode, target, 0);
11629
11630   if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
11631     op0 = copy_to_mode_reg (mode0, op0);
11632   if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
11633     op1 = copy_to_mode_reg (mode1, op1);
11634
11635   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
11636   pat = GEN_FCN (d->icode) (op0, op1, op2);
11637   if (! pat)
11638     return 0;
11639   emit_insn (pat);
11640   emit_insn (gen_setcc_2 (target, op2));
11641
11642   return target;
11643 }
11644
11645 /* Expand an expression EXP that calls a built-in function,
11646    with result going to TARGET if that's convenient
11647    (and in mode MODE if that's convenient).
11648    SUBTARGET may be used as the target for computing one of EXP's operands.
11649    IGNORE is nonzero if the value is to be ignored.  */
11650
11651 rtx
11652 ix86_expand_builtin (exp, target, subtarget, mode, ignore)
11653      tree exp;
11654      rtx target;
11655      rtx subtarget ATTRIBUTE_UNUSED;
11656      enum machine_mode mode ATTRIBUTE_UNUSED;
11657      int ignore ATTRIBUTE_UNUSED;
11658 {
11659   const struct builtin_description *d;
11660   size_t i;
11661   enum insn_code icode;
11662   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
11663   tree arglist = TREE_OPERAND (exp, 1);
11664   tree arg0, arg1, arg2, arg3;
11665   rtx op0, op1, op2, pat;
11666   enum machine_mode tmode, mode0, mode1, mode2;
11667   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11668
11669   switch (fcode)
11670     {
11671     case IX86_BUILTIN_EMMS:
11672       emit_insn (gen_emms ());
11673       return 0;
11674
11675     case IX86_BUILTIN_SFENCE:
11676       emit_insn (gen_sfence ());
11677       return 0;
11678
11679     case IX86_BUILTIN_M_FROM_INT:
11680       target = gen_reg_rtx (DImode);
11681       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
11682       emit_move_insn (gen_rtx_SUBREG (SImode, target, 0), op0);
11683       return target;
11684
11685     case IX86_BUILTIN_M_TO_INT:
11686       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
11687       op0 = copy_to_mode_reg (DImode, op0);
11688       target = gen_reg_rtx (SImode);
11689       emit_move_insn (target, gen_rtx_SUBREG (SImode, op0, 0));
11690       return target;
11691
11692     case IX86_BUILTIN_PEXTRW:
11693       icode = CODE_FOR_mmx_pextrw;
11694       arg0 = TREE_VALUE (arglist);
11695       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11696       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11697       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11698       tmode = insn_data[icode].operand[0].mode;
11699       mode0 = insn_data[icode].operand[1].mode;
11700       mode1 = insn_data[icode].operand[2].mode;
11701
11702       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11703         op0 = copy_to_mode_reg (mode0, op0);
11704       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11705         {
11706           /* @@@ better error message */
11707           error ("selector must be an immediate");
11708           return const0_rtx;
11709         }
11710       if (target == 0
11711           || GET_MODE (target) != tmode
11712           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11713         target = gen_reg_rtx (tmode);
11714       pat = GEN_FCN (icode) (target, op0, op1);
11715       if (! pat)
11716         return 0;
11717       emit_insn (pat);
11718       return target;
11719
11720     case IX86_BUILTIN_PINSRW:
11721       icode = CODE_FOR_mmx_pinsrw;
11722       arg0 = TREE_VALUE (arglist);
11723       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11724       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11725       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11726       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11727       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
11728       tmode = insn_data[icode].operand[0].mode;
11729       mode0 = insn_data[icode].operand[1].mode;
11730       mode1 = insn_data[icode].operand[2].mode;
11731       mode2 = insn_data[icode].operand[3].mode;
11732
11733       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11734         op0 = copy_to_mode_reg (mode0, op0);
11735       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11736         op1 = copy_to_mode_reg (mode1, op1);
11737       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
11738         {
11739           /* @@@ better error message */
11740           error ("selector must be an immediate");
11741           return const0_rtx;
11742         }
11743       if (target == 0
11744           || GET_MODE (target) != tmode
11745           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11746         target = gen_reg_rtx (tmode);
11747       pat = GEN_FCN (icode) (target, op0, op1, op2);
11748       if (! pat)
11749         return 0;
11750       emit_insn (pat);
11751       return target;
11752
11753     case IX86_BUILTIN_MASKMOVQ:
11754       icode = CODE_FOR_mmx_maskmovq;
11755       /* Note the arg order is different from the operand order.  */
11756       arg1 = TREE_VALUE (arglist);
11757       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
11758       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11759       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11760       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11761       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
11762       mode0 = insn_data[icode].operand[0].mode;
11763       mode1 = insn_data[icode].operand[1].mode;
11764       mode2 = insn_data[icode].operand[2].mode;
11765
11766       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11767         op0 = copy_to_mode_reg (mode0, op0);
11768       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11769         op1 = copy_to_mode_reg (mode1, op1);
11770       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
11771         op2 = copy_to_mode_reg (mode2, op2);
11772       pat = GEN_FCN (icode) (op0, op1, op2);
11773       if (! pat)
11774         return 0;
11775       emit_insn (pat);
11776       return 0;
11777
11778     case IX86_BUILTIN_SQRTSS:
11779       return ix86_expand_unop1_builtin (CODE_FOR_vmsqrtv4sf2, arglist, target);
11780     case IX86_BUILTIN_RSQRTSS:
11781       return ix86_expand_unop1_builtin (CODE_FOR_vmrsqrtv4sf2, arglist, target);
11782     case IX86_BUILTIN_RCPSS:
11783       return ix86_expand_unop1_builtin (CODE_FOR_vmrcpv4sf2, arglist, target);
11784
11785     case IX86_BUILTIN_LOADAPS:
11786       return ix86_expand_unop_builtin (CODE_FOR_sse_movaps, arglist, target, 1);
11787
11788     case IX86_BUILTIN_LOADUPS:
11789       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
11790
11791     case IX86_BUILTIN_STOREAPS:
11792       return ix86_expand_store_builtin (CODE_FOR_sse_movaps, arglist, -1);
11793     case IX86_BUILTIN_STOREUPS:
11794       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist, -1);
11795
11796     case IX86_BUILTIN_LOADSS:
11797       return ix86_expand_unop_builtin (CODE_FOR_sse_loadss, arglist, target, 1);
11798
11799     case IX86_BUILTIN_STORESS:
11800       return ix86_expand_store_builtin (CODE_FOR_sse_storess, arglist, -1);
11801
11802     case IX86_BUILTIN_LOADHPS:
11803     case IX86_BUILTIN_LOADLPS:
11804       icode = (fcode == IX86_BUILTIN_LOADHPS
11805                ? CODE_FOR_sse_movhps : CODE_FOR_sse_movlps);
11806       arg0 = TREE_VALUE (arglist);
11807       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11808       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11809       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11810       tmode = insn_data[icode].operand[0].mode;
11811       mode0 = insn_data[icode].operand[1].mode;
11812       mode1 = insn_data[icode].operand[2].mode;
11813
11814       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11815         op0 = copy_to_mode_reg (mode0, op0);
11816       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
11817       if (target == 0
11818           || GET_MODE (target) != tmode
11819           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11820         target = gen_reg_rtx (tmode);
11821       pat = GEN_FCN (icode) (target, op0, op1);
11822       if (! pat)
11823         return 0;
11824       emit_insn (pat);
11825       return target;
11826
11827     case IX86_BUILTIN_STOREHPS:
11828     case IX86_BUILTIN_STORELPS:
11829       icode = (fcode == IX86_BUILTIN_STOREHPS
11830                ? CODE_FOR_sse_movhps : CODE_FOR_sse_movlps);
11831       arg0 = TREE_VALUE (arglist);
11832       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11833       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11834       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11835       mode0 = insn_data[icode].operand[1].mode;
11836       mode1 = insn_data[icode].operand[2].mode;
11837
11838       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11839       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11840         op1 = copy_to_mode_reg (mode1, op1);
11841
11842       pat = GEN_FCN (icode) (op0, op0, op1);
11843       if (! pat)
11844         return 0;
11845       emit_insn (pat);
11846       return 0;
11847
11848     case IX86_BUILTIN_MOVNTPS:
11849       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist, -1);
11850     case IX86_BUILTIN_MOVNTQ:
11851       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist, -1);
11852
11853     case IX86_BUILTIN_LDMXCSR:
11854       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
11855       target = assign_386_stack_local (SImode, 0);
11856       emit_move_insn (target, op0);
11857       emit_insn (gen_ldmxcsr (target));
11858       return 0;
11859
11860     case IX86_BUILTIN_STMXCSR:
11861       target = assign_386_stack_local (SImode, 0);
11862       emit_insn (gen_stmxcsr (target));
11863       return copy_to_mode_reg (SImode, target);
11864
11865     case IX86_BUILTIN_PREFETCH:
11866       icode = CODE_FOR_prefetch;
11867       arg0 = TREE_VALUE (arglist);
11868       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11869       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11870       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11871       mode0 = insn_data[icode].operand[0].mode;
11872       mode1 = insn_data[icode].operand[1].mode;
11873
11874       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11875         {
11876           /* @@@ better error message */
11877           error ("selector must be an immediate");
11878           return const0_rtx;
11879         }
11880
11881       op0 = copy_to_mode_reg (Pmode, op0);
11882       pat = GEN_FCN (icode) (op0, op1);
11883       if (! pat)
11884         return 0;
11885       emit_insn (pat);
11886       return target;
11887
11888     case IX86_BUILTIN_SHUFPS:
11889       icode = CODE_FOR_sse_shufps;
11890       arg0 = TREE_VALUE (arglist);
11891       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11892       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11893       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11894       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11895       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
11896       tmode = insn_data[icode].operand[0].mode;
11897       mode0 = insn_data[icode].operand[1].mode;
11898       mode1 = insn_data[icode].operand[2].mode;
11899       mode2 = insn_data[icode].operand[3].mode;
11900
11901       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11902         op0 = copy_to_mode_reg (mode0, op0);
11903       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11904         op1 = copy_to_mode_reg (mode1, op1);
11905       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
11906         {
11907           /* @@@ better error message */
11908           error ("mask must be an immediate");
11909           return const0_rtx;
11910         }
11911       if (target == 0
11912           || GET_MODE (target) != tmode
11913           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11914         target = gen_reg_rtx (tmode);
11915       pat = GEN_FCN (icode) (target, op0, op1, op2);
11916       if (! pat)
11917         return 0;
11918       emit_insn (pat);
11919       return target;
11920
11921     case IX86_BUILTIN_PSHUFW:
11922       icode = CODE_FOR_mmx_pshufw;
11923       arg0 = TREE_VALUE (arglist);
11924       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11925       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11926       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11927       tmode = insn_data[icode].operand[0].mode;
11928       mode0 = insn_data[icode].operand[2].mode;
11929       mode1 = insn_data[icode].operand[3].mode;
11930
11931       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11932         op0 = copy_to_mode_reg (mode0, op0);
11933       if (! (*insn_data[icode].operand[3].predicate) (op1, mode1))
11934         {
11935           /* @@@ better error message */
11936           error ("mask must be an immediate");
11937           return const0_rtx;
11938         }
11939       if (target == 0
11940           || GET_MODE (target) != tmode
11941           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11942         target = gen_reg_rtx (tmode);
11943       pat = GEN_FCN (icode) (target, target, op0, op1);
11944       if (! pat)
11945         return 0;
11946       emit_insn (pat);
11947       return target;
11948
11949     case IX86_BUILTIN_FEMMS:
11950       emit_insn (gen_femms ());
11951       return NULL_RTX;
11952
11953     case IX86_BUILTIN_PAVGUSB:
11954       return ix86_expand_binop_builtin (CODE_FOR_pavgusb, arglist, target);
11955
11956     case IX86_BUILTIN_PF2ID:
11957       return ix86_expand_unop_builtin (CODE_FOR_pf2id, arglist, target, 0);
11958
11959     case IX86_BUILTIN_PFACC:
11960       return ix86_expand_binop_builtin (CODE_FOR_pfacc, arglist, target);
11961
11962     case IX86_BUILTIN_PFADD:
11963      return ix86_expand_binop_builtin (CODE_FOR_addv2sf3, arglist, target);
11964
11965     case IX86_BUILTIN_PFCMPEQ:
11966       return ix86_expand_binop_builtin (CODE_FOR_eqv2sf3, arglist, target);
11967
11968     case IX86_BUILTIN_PFCMPGE:
11969       return ix86_expand_binop_builtin (CODE_FOR_gev2sf3, arglist, target);
11970
11971     case IX86_BUILTIN_PFCMPGT:
11972       return ix86_expand_binop_builtin (CODE_FOR_gtv2sf3, arglist, target);
11973
11974     case IX86_BUILTIN_PFMAX:
11975       return ix86_expand_binop_builtin (CODE_FOR_pfmaxv2sf3, arglist, target);
11976
11977     case IX86_BUILTIN_PFMIN:
11978       return ix86_expand_binop_builtin (CODE_FOR_pfminv2sf3, arglist, target);
11979
11980     case IX86_BUILTIN_PFMUL:
11981       return ix86_expand_binop_builtin (CODE_FOR_mulv2sf3, arglist, target);
11982
11983     case IX86_BUILTIN_PFRCP:
11984       return ix86_expand_unop_builtin (CODE_FOR_pfrcpv2sf2, arglist, target, 0);
11985
11986     case IX86_BUILTIN_PFRCPIT1:
11987       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit1v2sf3, arglist, target);
11988
11989     case IX86_BUILTIN_PFRCPIT2:
11990       return ix86_expand_binop_builtin (CODE_FOR_pfrcpit2v2sf3, arglist, target);
11991
11992     case IX86_BUILTIN_PFRSQIT1:
11993       return ix86_expand_binop_builtin (CODE_FOR_pfrsqit1v2sf3, arglist, target);
11994
11995     case IX86_BUILTIN_PFRSQRT:
11996       return ix86_expand_unop_builtin (CODE_FOR_pfrsqrtv2sf2, arglist, target, 0);
11997
11998     case IX86_BUILTIN_PFSUB:
11999       return ix86_expand_binop_builtin (CODE_FOR_subv2sf3, arglist, target);
12000
12001     case IX86_BUILTIN_PFSUBR:
12002       return ix86_expand_binop_builtin (CODE_FOR_subrv2sf3, arglist, target);
12003
12004     case IX86_BUILTIN_PI2FD:
12005       return ix86_expand_unop_builtin (CODE_FOR_floatv2si2, arglist, target, 0);
12006
12007     case IX86_BUILTIN_PMULHRW:
12008       return ix86_expand_binop_builtin (CODE_FOR_pmulhrwv4hi3, arglist, target);
12009
12010     case IX86_BUILTIN_PREFETCH_3DNOW:
12011       icode = CODE_FOR_prefetch_3dnow;
12012       arg0 = TREE_VALUE (arglist);
12013       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12014       mode0 = insn_data[icode].operand[0].mode;
12015       pat = GEN_FCN (icode) (copy_to_mode_reg (Pmode, op0));
12016       if (! pat)
12017         return NULL_RTX;
12018       emit_insn (pat);
12019       return NULL_RTX;
12020
12021     case IX86_BUILTIN_PREFETCHW:
12022       icode = CODE_FOR_prefetchw;
12023       arg0 = TREE_VALUE (arglist);
12024       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12025       mode0 = insn_data[icode].operand[0].mode;
12026       pat = GEN_FCN (icode) (copy_to_mode_reg (Pmode, op0));
12027       if (! pat)
12028         return NULL_RTX;
12029       emit_insn (pat);
12030       return NULL_RTX;
12031
12032     case IX86_BUILTIN_PF2IW:
12033       return ix86_expand_unop_builtin (CODE_FOR_pf2iw, arglist, target, 0);
12034
12035     case IX86_BUILTIN_PFNACC:
12036       return ix86_expand_binop_builtin (CODE_FOR_pfnacc, arglist, target);
12037
12038     case IX86_BUILTIN_PFPNACC:
12039       return ix86_expand_binop_builtin (CODE_FOR_pfpnacc, arglist, target);
12040
12041     case IX86_BUILTIN_PI2FW:
12042       return ix86_expand_unop_builtin (CODE_FOR_pi2fw, arglist, target, 0);
12043
12044     case IX86_BUILTIN_PSWAPDSI:
12045       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2si2, arglist, target, 0);
12046
12047     case IX86_BUILTIN_PSWAPDSF:
12048       return ix86_expand_unop_builtin (CODE_FOR_pswapdv2sf2, arglist, target, 0);
12049
12050       /* Composite intrinsics.  */
12051     case IX86_BUILTIN_SETPS1:
12052       target = assign_386_stack_local (SFmode, 0);
12053       arg0 = TREE_VALUE (arglist);
12054       emit_move_insn (adjust_address (target, SFmode, 0),
12055                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12056       op0 = gen_reg_rtx (V4SFmode);
12057       emit_insn (gen_sse_loadss (op0, adjust_address (target, V4SFmode, 0)));
12058       emit_insn (gen_sse_shufps (op0, op0, op0, GEN_INT (0)));
12059       return op0;
12060
12061     case IX86_BUILTIN_SETPS:
12062       target = assign_386_stack_local (V4SFmode, 0);
12063       arg0 = TREE_VALUE (arglist);
12064       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12065       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12066       arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
12067       emit_move_insn (adjust_address (target, SFmode, 0),
12068                       expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12069       emit_move_insn (adjust_address (target, SFmode, 4),
12070                       expand_expr (arg1, NULL_RTX, VOIDmode, 0));
12071       emit_move_insn (adjust_address (target, SFmode, 8),
12072                       expand_expr (arg2, NULL_RTX, VOIDmode, 0));
12073       emit_move_insn (adjust_address (target, SFmode, 12),
12074                       expand_expr (arg3, NULL_RTX, VOIDmode, 0));
12075       op0 = gen_reg_rtx (V4SFmode);
12076       emit_insn (gen_sse_movaps (op0, target));
12077       return op0;
12078
12079     case IX86_BUILTIN_CLRPS:
12080       target = gen_reg_rtx (TImode);
12081       emit_insn (gen_sse_clrti (target));
12082       return target;
12083
12084     case IX86_BUILTIN_LOADRPS:
12085       target = ix86_expand_unop_builtin (CODE_FOR_sse_movaps, arglist,
12086                                          gen_reg_rtx (V4SFmode), 1);
12087       emit_insn (gen_sse_shufps (target, target, target, GEN_INT (0x1b)));
12088       return target;
12089
12090     case IX86_BUILTIN_LOADPS1:
12091       target = ix86_expand_unop_builtin (CODE_FOR_sse_loadss, arglist,
12092                                          gen_reg_rtx (V4SFmode), 1);
12093       emit_insn (gen_sse_shufps (target, target, target, const0_rtx));
12094       return target;
12095
12096     case IX86_BUILTIN_STOREPS1:
12097       return ix86_expand_store_builtin (CODE_FOR_sse_movaps, arglist, 0);
12098     case IX86_BUILTIN_STORERPS:
12099       return ix86_expand_store_builtin (CODE_FOR_sse_movaps, arglist, 0x1B);
12100
12101     case IX86_BUILTIN_MMX_ZERO:
12102       target = gen_reg_rtx (DImode);
12103       emit_insn (gen_mmx_clrdi (target));
12104       return target;
12105
12106     default:
12107       break;
12108     }
12109
12110   for (i = 0, d = bdesc_2arg; i < sizeof (bdesc_2arg) / sizeof *d; i++, d++)
12111     if (d->code == fcode)
12112       {
12113         /* Compares are treated specially.  */
12114         if (d->icode == CODE_FOR_maskcmpv4sf3
12115             || d->icode == CODE_FOR_vmmaskcmpv4sf3
12116             || d->icode == CODE_FOR_maskncmpv4sf3
12117             || d->icode == CODE_FOR_vmmaskncmpv4sf3)
12118           return ix86_expand_sse_compare (d, arglist, target);
12119
12120         return ix86_expand_binop_builtin (d->icode, arglist, target);
12121       }
12122
12123   for (i = 0, d = bdesc_1arg; i < sizeof (bdesc_1arg) / sizeof *d; i++, d++)
12124     if (d->code == fcode)
12125       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
12126
12127   for (i = 0, d = bdesc_comi; i < sizeof (bdesc_comi) / sizeof *d; i++, d++)
12128     if (d->code == fcode)
12129       return ix86_expand_sse_comi (d, arglist, target);
12130
12131   /* @@@ Should really do something sensible here.  */
12132   return 0;
12133 }
12134
12135 /* Store OPERAND to the memory after reload is completed.  This means
12136    that we can't easily use assign_stack_local.  */
12137 rtx
12138 ix86_force_to_memory (mode, operand)
12139      enum machine_mode mode;
12140      rtx operand;
12141 {
12142   rtx result;
12143   if (!reload_completed)
12144     abort ();
12145   if (TARGET_64BIT && TARGET_RED_ZONE)
12146     {
12147       result = gen_rtx_MEM (mode,
12148                             gen_rtx_PLUS (Pmode,
12149                                           stack_pointer_rtx,
12150                                           GEN_INT (-RED_ZONE_SIZE)));
12151       emit_move_insn (result, operand);
12152     }
12153   else if (TARGET_64BIT && !TARGET_RED_ZONE)
12154     {
12155       switch (mode)
12156         {
12157         case HImode:
12158         case SImode:
12159           operand = gen_lowpart (DImode, operand);
12160           /* FALLTHRU */
12161         case DImode:
12162           emit_insn (
12163                       gen_rtx_SET (VOIDmode,
12164                                    gen_rtx_MEM (DImode,
12165                                                 gen_rtx_PRE_DEC (DImode,
12166                                                         stack_pointer_rtx)),
12167                                    operand));
12168           break;
12169         default:
12170           abort ();
12171         }
12172       result = gen_rtx_MEM (mode, stack_pointer_rtx);
12173     }
12174   else
12175     {
12176       switch (mode)
12177         {
12178         case DImode:
12179           {
12180             rtx operands[2];
12181             split_di (&operand, 1, operands, operands + 1);
12182             emit_insn (
12183                         gen_rtx_SET (VOIDmode,
12184                                      gen_rtx_MEM (SImode,
12185                                                   gen_rtx_PRE_DEC (Pmode,
12186                                                         stack_pointer_rtx)),
12187                                      operands[1]));
12188             emit_insn (
12189                         gen_rtx_SET (VOIDmode,
12190                                      gen_rtx_MEM (SImode,
12191                                                   gen_rtx_PRE_DEC (Pmode,
12192                                                         stack_pointer_rtx)),
12193                                      operands[0]));
12194           }
12195           break;
12196         case HImode:
12197           /* It is better to store HImodes as SImodes.  */
12198           if (!TARGET_PARTIAL_REG_STALL)
12199             operand = gen_lowpart (SImode, operand);
12200           /* FALLTHRU */
12201         case SImode:
12202           emit_insn (
12203                       gen_rtx_SET (VOIDmode,
12204                                    gen_rtx_MEM (GET_MODE (operand),
12205                                                 gen_rtx_PRE_DEC (SImode,
12206                                                         stack_pointer_rtx)),
12207                                    operand));
12208           break;
12209         default:
12210           abort ();
12211         }
12212       result = gen_rtx_MEM (mode, stack_pointer_rtx);
12213     }
12214   return result;
12215 }
12216
12217 /* Free operand from the memory.  */
12218 void
12219 ix86_free_from_memory (mode)
12220      enum machine_mode mode;
12221 {
12222   if (!TARGET_64BIT || !TARGET_RED_ZONE)
12223     {
12224       int size;
12225
12226       if (mode == DImode || TARGET_64BIT)
12227         size = 8;
12228       else if (mode == HImode && TARGET_PARTIAL_REG_STALL)
12229         size = 2;
12230       else
12231         size = 4;
12232       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
12233          to pop or add instruction if registers are available.  */
12234       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12235                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
12236                                             GEN_INT (size))));
12237     }
12238 }
12239
12240 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
12241    QImode must go into class Q_REGS.
12242    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
12243    movdf to do mem-to-mem moves through integer regs. */
12244 enum reg_class
12245 ix86_preferred_reload_class (x, class)
12246      rtx x;
12247      enum reg_class class;
12248 {
12249   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
12250     {
12251       /* SSE can't load any constant directly yet.  */
12252       if (SSE_CLASS_P (class))
12253         return NO_REGS;
12254       /* Floats can load 0 and 1.  */
12255       if (MAYBE_FLOAT_CLASS_P (class) && standard_80387_constant_p (x))
12256         {
12257           /* Limit class to non-SSE.  Use GENERAL_REGS if possible.  */
12258           if (MAYBE_SSE_CLASS_P (class))
12259             return (reg_class_subset_p (class, GENERAL_REGS)
12260                     ? GENERAL_REGS : FLOAT_REGS);
12261           else
12262             return class;
12263         }
12264       /* General regs can load everything.  */
12265       if (reg_class_subset_p (class, GENERAL_REGS))
12266         return GENERAL_REGS;
12267       /* In case we haven't resolved FLOAT or SSE yet, give up.  */
12268       if (MAYBE_FLOAT_CLASS_P (class) || MAYBE_SSE_CLASS_P (class))
12269         return NO_REGS;
12270     }
12271   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
12272     return NO_REGS;
12273   if (GET_MODE (x) == QImode && ! reg_class_subset_p (class, Q_REGS))
12274     return Q_REGS;
12275   return class;
12276 }
12277
12278 /* If we are copying between general and FP registers, we need a memory
12279    location. The same is true for SSE and MMX registers.
12280
12281    The macro can't work reliably when one of the CLASSES is class containing
12282    registers from multiple units (SSE, MMX, integer).  We avoid this by never
12283    combining those units in single alternative in the machine description.
12284    Ensure that this constraint holds to avoid unexpected surprises.
12285
12286    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
12287    enforce these sanity checks.  */
12288 int
12289 ix86_secondary_memory_needed (class1, class2, mode, strict)
12290      enum reg_class class1, class2;
12291      enum machine_mode mode;
12292      int strict;
12293 {
12294   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
12295       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
12296       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
12297       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
12298       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
12299       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
12300     {
12301       if (strict)
12302         abort ();
12303       else
12304         return 1;
12305     }
12306   return (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2)
12307           || (SSE_CLASS_P (class1) != SSE_CLASS_P (class2)
12308               && (mode) != SImode)
12309           || (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
12310               && (mode) != SImode));
12311 }
12312 /* Return the cost of moving data from a register in class CLASS1 to
12313    one in class CLASS2.
12314
12315    It is not required that the cost always equal 2 when FROM is the same as TO;
12316    on some machines it is expensive to move between registers if they are not
12317    general registers.  */
12318 int
12319 ix86_register_move_cost (mode, class1, class2)
12320      enum machine_mode mode;
12321      enum reg_class class1, class2;
12322 {
12323   /* In case we require secondary memory, compute cost of the store followed
12324      by load.  In case of copying from general_purpose_register we may emit
12325      multiple stores followed by single load causing memory size mismatch
12326      stall.  Count this as arbitarily high cost of 20.  */
12327   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
12328     {
12329       int add_cost = 0;
12330       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
12331           add_cost = 20;
12332       return (MEMORY_MOVE_COST (mode, class1, 0)
12333               + MEMORY_MOVE_COST (mode, class2, 1) + add_cost);
12334     }
12335   /* Moves between SSE/MMX and integer unit are expensive.  */
12336   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
12337       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
12338     return ix86_cost->mmxsse_to_integer;
12339   if (MAYBE_FLOAT_CLASS_P (class1))
12340     return ix86_cost->fp_move;
12341   if (MAYBE_SSE_CLASS_P (class1))
12342     return ix86_cost->sse_move;
12343   if (MAYBE_MMX_CLASS_P (class1))
12344     return ix86_cost->mmx_move;
12345   return 2;
12346 }
12347
12348 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
12349 int
12350 ix86_hard_regno_mode_ok (regno, mode)
12351      int regno;
12352      enum machine_mode mode;
12353 {
12354   /* Flags and only flags can only hold CCmode values.  */
12355   if (CC_REGNO_P (regno))
12356     return GET_MODE_CLASS (mode) == MODE_CC;
12357   if (GET_MODE_CLASS (mode) == MODE_CC
12358       || GET_MODE_CLASS (mode) == MODE_RANDOM
12359       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
12360     return 0;
12361   if (FP_REGNO_P (regno))
12362     return VALID_FP_MODE_P (mode);
12363   if (SSE_REGNO_P (regno))
12364     return VALID_SSE_REG_MODE (mode);
12365   if (MMX_REGNO_P (regno))
12366     return VALID_MMX_REG_MODE (mode) || VALID_MMX_REG_MODE_3DNOW (mode);
12367   /* We handle both integer and floats in the general purpose registers.
12368      In future we should be able to handle vector modes as well.  */
12369   if (!VALID_INT_MODE_P (mode) && !VALID_FP_MODE_P (mode))
12370     return 0;
12371   /* Take care for QImode values - they can be in non-QI regs, but then
12372      they do cause partial register stalls.  */
12373   if (regno < 4 || mode != QImode || TARGET_64BIT)
12374     return 1;
12375   return reload_in_progress || reload_completed || !TARGET_PARTIAL_REG_STALL;
12376 }
12377
12378 /* Return the cost of moving data of mode M between a
12379    register and memory.  A value of 2 is the default; this cost is
12380    relative to those in `REGISTER_MOVE_COST'.
12381
12382    If moving between registers and memory is more expensive than
12383    between two registers, you should define this macro to express the
12384    relative cost.
12385
12386    Model also increased moving costs of QImode registers in non
12387    Q_REGS classes.
12388  */
12389 int
12390 ix86_memory_move_cost (mode, class, in)
12391      enum machine_mode mode;
12392      enum reg_class class;
12393      int in;
12394 {
12395   if (FLOAT_CLASS_P (class))
12396     {
12397       int index;
12398       switch (mode)
12399         {
12400           case SFmode:
12401             index = 0;
12402             break;
12403           case DFmode:
12404             index = 1;
12405             break;
12406           case XFmode:
12407           case TFmode:
12408             index = 2;
12409             break;
12410           default:
12411             return 100;
12412         }
12413       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
12414     }
12415   if (SSE_CLASS_P (class))
12416     {
12417       int index;
12418       switch (GET_MODE_SIZE (mode))
12419         {
12420           case 4:
12421             index = 0;
12422             break;
12423           case 8:
12424             index = 1;
12425             break;
12426           case 16:
12427             index = 2;
12428             break;
12429           default:
12430             return 100;
12431         }
12432       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
12433     }
12434   if (MMX_CLASS_P (class))
12435     {
12436       int index;
12437       switch (GET_MODE_SIZE (mode))
12438         {
12439           case 4:
12440             index = 0;
12441             break;
12442           case 8:
12443             index = 1;
12444             break;
12445           default:
12446             return 100;
12447         }
12448       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
12449     }
12450   switch (GET_MODE_SIZE (mode))
12451     {
12452       case 1:
12453         if (in)
12454           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
12455                   : ix86_cost->movzbl_load);
12456         else
12457           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
12458                   : ix86_cost->int_store[0] + 4);
12459         break;
12460       case 2:
12461         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
12462       default:
12463         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
12464         if (mode == TFmode)
12465           mode = XFmode;
12466         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
12467                 * (int) GET_MODE_SIZE (mode) / 4);
12468     }
12469 }
12470
12471 #ifdef DO_GLOBAL_CTORS_BODY
12472 static void
12473 ix86_svr3_asm_out_constructor (symbol, priority)
12474      rtx symbol;
12475      int priority ATTRIBUTE_UNUSED;
12476 {
12477   init_section ();
12478   fputs ("\tpushl $", asm_out_file);
12479   assemble_name (asm_out_file, XSTR (symbol, 0));
12480   fputc ('\n', asm_out_file);
12481 }
12482 #endif