OSDN Git Service

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