OSDN Git Service

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