OSDN Git Service

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