OSDN Git Service

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