Update gcc-50 to SVN version 221261
[dragonfly.git] / contrib / gcc-5.0 / gcc / config / i386 / i386.md
1 ;; GCC machine description for IA-32 and x86-64.
2 ;; Copyright (C) 1988-2015 Free Software Foundation, Inc.
3 ;; Mostly by William Schelter.
4 ;; x86_64 support added by Jan Hubicka
5 ;;
6 ;; This file is part of GCC.
7 ;;
8 ;; GCC is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
12 ;;
13 ;; GCC is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GCC; see the file COPYING3.  If not see
20 ;; <http://www.gnu.org/licenses/>.  */
21 ;;
22 ;; The original PO technology requires these to be ordered by speed,
23 ;; so that assigner will pick the fastest.
24 ;;
25 ;; See file "rtl.def" for documentation on define_insn, match_*, et. al.
26 ;;
27 ;; The special asm out single letter directives following a '%' are:
28 ;; L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
29 ;; C -- print opcode suffix for set/cmov insn.
30 ;; c -- like C, but print reversed condition
31 ;; F,f -- likewise, but for floating-point.
32 ;; O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
33 ;;      otherwise nothing
34 ;; R -- print the prefix for register names.
35 ;; z -- print the opcode suffix for the size of the current operand.
36 ;; Z -- likewise, with special suffixes for x87 instructions.
37 ;; * -- print a star (in certain assembler syntax)
38 ;; A -- print an absolute memory reference.
39 ;; E -- print address with DImode register names if TARGET_64BIT.
40 ;; w -- print the operand as if it's a "word" (HImode) even if it isn't.
41 ;; s -- print a shift double count, followed by the assemblers argument
42 ;;      delimiter.
43 ;; b -- print the QImode name of the register for the indicated operand.
44 ;;      %b0 would print %al if operands[0] is reg 0.
45 ;; w --  likewise, print the HImode name of the register.
46 ;; k --  likewise, print the SImode name of the register.
47 ;; q --  likewise, print the DImode name of the register.
48 ;; x --  likewise, print the V4SFmode name of the register.
49 ;; t --  likewise, print the V8SFmode name of the register.
50 ;; h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
51 ;; y -- print "st(0)" instead of "st" as a register.
52 ;; d -- print duplicated register operand for AVX instruction.
53 ;; D -- print condition for SSE cmp instruction.
54 ;; P -- if PIC, print an @PLT suffix.
55 ;; p -- print raw symbol name.
56 ;; X -- don't print any sort of PIC '@' suffix for a symbol.
57 ;; & -- print some in-use local-dynamic symbol name.
58 ;; H -- print a memory address offset by 8; used for sse high-parts
59 ;; K -- print HLE lock prefix
60 ;; Y -- print condition for XOP pcom* instruction.
61 ;; + -- print a branch hint as 'cs' or 'ds' prefix
62 ;; ; -- print a semicolon (after prefixes due to bug in older gas).
63 ;; ~ -- print "i" if TARGET_AVX2, "f" otherwise.
64 ;; @ -- print a segment register of thread base pointer load
65 ;; ^ -- print addr32 prefix if TARGET_64BIT and Pmode != word_mode
66 ;; ! -- print MPX prefix for jxx/call/ret instructions if required.
67
68 (define_c_enum "unspec" [
69   ;; Relocation specifiers
70   UNSPEC_GOT
71   UNSPEC_GOTOFF
72   UNSPEC_GOTPCREL
73   UNSPEC_GOTTPOFF
74   UNSPEC_TPOFF
75   UNSPEC_NTPOFF
76   UNSPEC_DTPOFF
77   UNSPEC_GOTNTPOFF
78   UNSPEC_INDNTPOFF
79   UNSPEC_PLTOFF
80   UNSPEC_MACHOPIC_OFFSET
81   UNSPEC_PCREL
82   UNSPEC_SIZEOF
83
84   ;; Prologue support
85   UNSPEC_STACK_ALLOC
86   UNSPEC_SET_GOT
87   UNSPEC_SET_RIP
88   UNSPEC_SET_GOT_OFFSET
89   UNSPEC_MEMORY_BLOCKAGE
90   UNSPEC_STACK_CHECK
91
92   ;; TLS support
93   UNSPEC_TP
94   UNSPEC_TLS_GD
95   UNSPEC_TLS_LD_BASE
96   UNSPEC_TLSDESC
97   UNSPEC_TLS_IE_SUN
98
99   ;; Other random patterns
100   UNSPEC_SCAS
101   UNSPEC_FNSTSW
102   UNSPEC_SAHF
103   UNSPEC_PARITY
104   UNSPEC_FSTCW
105   UNSPEC_ADD_CARRY
106   UNSPEC_FLDCW
107   UNSPEC_REP
108   UNSPEC_LD_MPIC        ; load_macho_picbase
109   UNSPEC_TRUNC_NOOP
110   UNSPEC_DIV_ALREADY_SPLIT
111   UNSPEC_PAUSE
112   UNSPEC_LEA_ADDR
113   UNSPEC_XBEGIN_ABORT
114   UNSPEC_STOS
115   UNSPEC_PEEPSIB
116   UNSPEC_INSN_FALSE_DEP
117
118   ;; For SSE/MMX support:
119   UNSPEC_FIX_NOTRUNC
120   UNSPEC_MASKMOV
121   UNSPEC_MOVMSK
122   UNSPEC_RCP
123   UNSPEC_RSQRT
124   UNSPEC_PSADBW
125
126   ;; Generic math support
127   UNSPEC_COPYSIGN
128   UNSPEC_IEEE_MIN       ; not commutative
129   UNSPEC_IEEE_MAX       ; not commutative
130
131   ;; x87 Floating point
132   UNSPEC_SIN
133   UNSPEC_COS
134   UNSPEC_FPATAN
135   UNSPEC_FYL2X
136   UNSPEC_FYL2XP1
137   UNSPEC_FRNDINT
138   UNSPEC_FIST
139   UNSPEC_F2XM1
140   UNSPEC_TAN
141   UNSPEC_FXAM
142
143   ;; x87 Rounding
144   UNSPEC_FRNDINT_FLOOR
145   UNSPEC_FRNDINT_CEIL
146   UNSPEC_FRNDINT_TRUNC
147   UNSPEC_FRNDINT_MASK_PM
148   UNSPEC_FIST_FLOOR
149   UNSPEC_FIST_CEIL
150
151   ;; x87 Double output FP
152   UNSPEC_SINCOS_COS
153   UNSPEC_SINCOS_SIN
154   UNSPEC_XTRACT_FRACT
155   UNSPEC_XTRACT_EXP
156   UNSPEC_FSCALE_FRACT
157   UNSPEC_FSCALE_EXP
158   UNSPEC_FPREM_F
159   UNSPEC_FPREM_U
160   UNSPEC_FPREM1_F
161   UNSPEC_FPREM1_U
162
163   UNSPEC_C2_FLAG
164   UNSPEC_FXAM_MEM
165
166   ;; SSP patterns
167   UNSPEC_SP_SET
168   UNSPEC_SP_TEST
169   UNSPEC_SP_TLS_SET
170   UNSPEC_SP_TLS_TEST
171
172   ;; For ROUND support
173   UNSPEC_ROUND
174
175   ;; For CRC32 support
176   UNSPEC_CRC32
177
178   ;; For BMI support
179   UNSPEC_BEXTR
180
181   ;; For BMI2 support
182   UNSPEC_PDEP
183   UNSPEC_PEXT
184
185   ;; For AVX512F support
186   UNSPEC_KMOV
187
188   UNSPEC_BNDMK
189   UNSPEC_BNDMK_ADDR
190   UNSPEC_BNDSTX
191   UNSPEC_BNDLDX
192   UNSPEC_BNDLDX_ADDR
193   UNSPEC_BNDCL
194   UNSPEC_BNDCU
195   UNSPEC_BNDCN
196   UNSPEC_MPX_FENCE
197 ])
198
199 (define_c_enum "unspecv" [
200   UNSPECV_BLOCKAGE
201   UNSPECV_STACK_PROBE
202   UNSPECV_PROBE_STACK_RANGE
203   UNSPECV_ALIGN
204   UNSPECV_PROLOGUE_USE
205   UNSPECV_SPLIT_STACK_RETURN
206   UNSPECV_CLD
207   UNSPECV_NOPS
208   UNSPECV_RDTSC
209   UNSPECV_RDTSCP
210   UNSPECV_RDPMC
211   UNSPECV_LLWP_INTRINSIC
212   UNSPECV_SLWP_INTRINSIC
213   UNSPECV_LWPVAL_INTRINSIC
214   UNSPECV_LWPINS_INTRINSIC
215   UNSPECV_RDFSBASE
216   UNSPECV_RDGSBASE
217   UNSPECV_WRFSBASE
218   UNSPECV_WRGSBASE
219   UNSPECV_FXSAVE
220   UNSPECV_FXRSTOR
221   UNSPECV_FXSAVE64
222   UNSPECV_FXRSTOR64
223   UNSPECV_XSAVE
224   UNSPECV_XRSTOR
225   UNSPECV_XSAVE64
226   UNSPECV_XRSTOR64
227   UNSPECV_XSAVEOPT
228   UNSPECV_XSAVEOPT64
229   UNSPECV_XSAVES
230   UNSPECV_XRSTORS
231   UNSPECV_XSAVES64
232   UNSPECV_XRSTORS64
233   UNSPECV_XSAVEC
234   UNSPECV_XSAVEC64
235
236   ;; For atomic compound assignments.
237   UNSPECV_FNSTENV
238   UNSPECV_FLDENV
239   UNSPECV_FNSTSW
240   UNSPECV_FNCLEX
241
242   ;; For RDRAND support
243   UNSPECV_RDRAND
244
245   ;; For RDSEED support
246   UNSPECV_RDSEED
247
248   ;; For RTM support
249   UNSPECV_XBEGIN
250   UNSPECV_XEND
251   UNSPECV_XABORT
252   UNSPECV_XTEST
253
254   UNSPECV_NLGR
255
256   ;; For CLWB support
257   UNSPECV_CLWB
258
259   ;; For PCOMMIT support
260   UNSPECV_PCOMMIT
261
262   ;; For CLFLUSHOPT support
263   UNSPECV_CLFLUSHOPT
264 ])
265
266 ;; Constants to represent rounding modes in the ROUND instruction
267 (define_constants
268   [(ROUND_FLOOR                 0x1)
269    (ROUND_CEIL                  0x2)
270    (ROUND_TRUNC                 0x3)
271    (ROUND_MXCSR                 0x4)
272    (ROUND_NO_EXC                0x8)
273   ])
274
275 ;; Constants to represent AVX512F embeded rounding
276 (define_constants
277   [(ROUND_NEAREST_INT                   0)
278    (ROUND_NEG_INF                       1)
279    (ROUND_POS_INF                       2)
280    (ROUND_ZERO                          3)
281    (NO_ROUND                            4)
282    (ROUND_SAE                           8)
283   ])
284
285 ;; Constants to represent pcomtrue/pcomfalse variants
286 (define_constants
287   [(PCOM_FALSE                  0)
288    (PCOM_TRUE                   1)
289    (COM_FALSE_S                 2)
290    (COM_FALSE_P                 3)
291    (COM_TRUE_S                  4)
292    (COM_TRUE_P                  5)
293   ])
294
295 ;; Constants used in the XOP pperm instruction
296 (define_constants
297   [(PPERM_SRC                   0x00)   /* copy source */
298    (PPERM_INVERT                0x20)   /* invert source */
299    (PPERM_REVERSE               0x40)   /* bit reverse source */
300    (PPERM_REV_INV               0x60)   /* bit reverse & invert src */
301    (PPERM_ZERO                  0x80)   /* all 0's */
302    (PPERM_ONES                  0xa0)   /* all 1's */
303    (PPERM_SIGN                  0xc0)   /* propagate sign bit */
304    (PPERM_INV_SIGN              0xe0)   /* invert & propagate sign */
305    (PPERM_SRC1                  0x00)   /* use first source byte */
306    (PPERM_SRC2                  0x10)   /* use second source byte */
307    ])
308
309 ;; Registers by name.
310 (define_constants
311   [(AX_REG                       0)
312    (DX_REG                       1)
313    (CX_REG                       2)
314    (BX_REG                       3)
315    (SI_REG                       4)
316    (DI_REG                       5)
317    (BP_REG                       6)
318    (SP_REG                       7)
319    (ST0_REG                      8)
320    (ST1_REG                      9)
321    (ST2_REG                     10)
322    (ST3_REG                     11)
323    (ST4_REG                     12)
324    (ST5_REG                     13)
325    (ST6_REG                     14)
326    (ST7_REG                     15)
327    (FLAGS_REG                   17)
328    (FPSR_REG                    18)
329    (FPCR_REG                    19)
330    (XMM0_REG                    21)
331    (XMM1_REG                    22)
332    (XMM2_REG                    23)
333    (XMM3_REG                    24)
334    (XMM4_REG                    25)
335    (XMM5_REG                    26)
336    (XMM6_REG                    27)
337    (XMM7_REG                    28)
338    (MM0_REG                     29)
339    (MM1_REG                     30)
340    (MM2_REG                     31)
341    (MM3_REG                     32)
342    (MM4_REG                     33)
343    (MM5_REG                     34)
344    (MM6_REG                     35)
345    (MM7_REG                     36)
346    (R8_REG                      37)
347    (R9_REG                      38)
348    (R10_REG                     39)
349    (R11_REG                     40)
350    (R12_REG                     41)
351    (R13_REG                     42)
352    (R14_REG                     43)
353    (R15_REG                     44)
354    (XMM8_REG                    45)
355    (XMM9_REG                    46)
356    (XMM10_REG                   47)
357    (XMM11_REG                   48)
358    (XMM12_REG                   49)
359    (XMM13_REG                   50)
360    (XMM14_REG                   51)
361    (XMM15_REG                   52)
362    (XMM16_REG                   53)
363    (XMM17_REG                   54)
364    (XMM18_REG                   55)
365    (XMM19_REG                   56)
366    (XMM20_REG                   57)
367    (XMM21_REG                   58)
368    (XMM22_REG                   59)
369    (XMM23_REG                   60)
370    (XMM24_REG                   61)
371    (XMM25_REG                   62)
372    (XMM26_REG                   63)
373    (XMM27_REG                   64)
374    (XMM28_REG                   65)
375    (XMM29_REG                   66)
376    (XMM30_REG                   67)
377    (XMM31_REG                   68)
378    (MASK0_REG                   69)
379    (MASK1_REG                   70)
380    (MASK2_REG                   71)
381    (MASK3_REG                   72)
382    (MASK4_REG                   73)
383    (MASK5_REG                   74)
384    (MASK6_REG                   75)
385    (MASK7_REG                   76)
386    (BND0_REG                    77)
387    (BND1_REG                    78)
388   ])
389
390 ;; Insns whose names begin with "x86_" are emitted by gen_FOO calls
391 ;; from i386.c.
392
393 ;; In C guard expressions, put expressions which may be compile-time
394 ;; constants first.  This allows for better optimization.  For
395 ;; example, write "TARGET_64BIT && reload_completed", not
396 ;; "reload_completed && TARGET_64BIT".
397
398 \f
399 ;; Processor type.
400 (define_attr "cpu" "none,pentium,pentiumpro,geode,k6,athlon,k8,core2,nehalem,
401                     atom,slm,generic,amdfam10,bdver1,bdver2,bdver3,bdver4,
402                     btver2,knl"
403   (const (symbol_ref "ix86_schedule")))
404
405 ;; A basic instruction type.  Refinements due to arguments to be
406 ;; provided in other attributes.
407 (define_attr "type"
408   "other,multi,
409    alu,alu1,negnot,imov,imovx,lea,
410    incdec,ishift,ishiftx,ishift1,rotate,rotatex,rotate1,
411    imul,imulx,idiv,icmp,test,ibr,setcc,icmov,
412    push,pop,call,callv,leave,
413    str,bitmanip,
414    fmov,fop,fsgn,fmul,fdiv,fpspc,fcmov,fcmp,
415    fxch,fistp,fisttp,frndint,
416    sse,ssemov,sseadd,sseadd1,sseiadd,sseiadd1,
417    ssemul,sseimul,ssediv,sselog,sselog1,
418    sseishft,sseishft1,ssecmp,ssecomi,
419    ssecvt,ssecvt1,sseicvt,sseins,
420    sseshuf,sseshuf1,ssemuladd,sse4arg,
421    lwp,mskmov,msklog,
422    mmx,mmxmov,mmxadd,mmxmul,mmxcmp,mmxcvt,mmxshft,
423    mpxmov,mpxmk,mpxchk,mpxld,mpxst"
424   (const_string "other"))
425
426 ;; Main data type used by the insn
427 (define_attr "mode"
428   "unknown,none,QI,HI,SI,DI,TI,OI,XI,SF,DF,XF,TF,V16SF,V8SF,V4DF,V4SF,
429   V2DF,V2SF,V1DF,V8DF"
430   (const_string "unknown"))
431
432 ;; The CPU unit operations uses.
433 (define_attr "unit" "integer,i387,sse,mmx,unknown"
434   (cond [(eq_attr "type" "fmov,fop,fsgn,fmul,fdiv,fpspc,fcmov,fcmp,
435                           fxch,fistp,fisttp,frndint")
436            (const_string "i387")
437          (eq_attr "type" "sse,ssemov,sseadd,sseadd1,sseiadd,sseiadd1,
438                           ssemul,sseimul,ssediv,sselog,sselog1,
439                           sseishft,sseishft1,ssecmp,ssecomi,
440                           ssecvt,ssecvt1,sseicvt,sseins,
441                           sseshuf,sseshuf1,ssemuladd,sse4arg,mskmov")
442            (const_string "sse")
443          (eq_attr "type" "mmx,mmxmov,mmxadd,mmxmul,mmxcmp,mmxcvt,mmxshft")
444            (const_string "mmx")
445          (eq_attr "type" "other")
446            (const_string "unknown")]
447          (const_string "integer")))
448
449 ;; The minimum required alignment of vector mode memory operands of the SSE
450 ;; (non-VEX/EVEX) instruction in bits, if it is different from
451 ;; GET_MODE_ALIGNMENT of the operand, otherwise 0.  If an instruction has
452 ;; multiple alternatives, this should be conservative maximum of those minimum
453 ;; required alignments.
454 (define_attr "ssememalign" "" (const_int 0))
455
456 ;; The (bounding maximum) length of an instruction immediate.
457 (define_attr "length_immediate" ""
458   (cond [(eq_attr "type" "incdec,setcc,icmov,str,lea,other,multi,idiv,leave,
459                           bitmanip,imulx,msklog,mskmov,mpxmk,mpxmov,mpxchk,
460                           mpxld,mpxst")
461            (const_int 0)
462          (eq_attr "unit" "i387,sse,mmx")
463            (const_int 0)
464          (eq_attr "type" "alu,alu1,negnot,imovx,ishift,ishiftx,ishift1,
465                           rotate,rotatex,rotate1,imul,icmp,push,pop")
466            (symbol_ref "ix86_attr_length_immediate_default (insn, true)")
467          (eq_attr "type" "imov,test")
468            (symbol_ref "ix86_attr_length_immediate_default (insn, false)")
469          (eq_attr "type" "call")
470            (if_then_else (match_operand 0 "constant_call_address_operand")
471              (const_int 4)
472              (const_int 0))
473          (eq_attr "type" "callv")
474            (if_then_else (match_operand 1 "constant_call_address_operand")
475              (const_int 4)
476              (const_int 0))
477          ;; We don't know the size before shorten_branches.  Expect
478          ;; the instruction to fit for better scheduling.
479          (eq_attr "type" "ibr")
480            (const_int 1)
481          ]
482          (symbol_ref "/* Update immediate_length and other attributes! */
483                       gcc_unreachable (),1")))
484
485 ;; The (bounding maximum) length of an instruction address.
486 (define_attr "length_address" ""
487   (cond [(eq_attr "type" "str,other,multi,fxch")
488            (const_int 0)
489          (and (eq_attr "type" "call")
490               (match_operand 0 "constant_call_address_operand"))
491              (const_int 0)
492          (and (eq_attr "type" "callv")
493               (match_operand 1 "constant_call_address_operand"))
494              (const_int 0)
495          ]
496          (symbol_ref "ix86_attr_length_address_default (insn)")))
497
498 ;; Set when length prefix is used.
499 (define_attr "prefix_data16" ""
500   (cond [(eq_attr "type" "ssemuladd,sse4arg,sseiadd1,ssecvt1")
501            (const_int 0)
502          (eq_attr "mode" "HI")
503            (const_int 1)
504          (and (eq_attr "unit" "sse") (eq_attr "mode" "V2DF,TI"))
505            (const_int 1)
506         ]
507         (const_int 0)))
508
509 ;; Set when string REP prefix is used.
510 (define_attr "prefix_rep" ""
511   (cond [(eq_attr "type" "ssemuladd,sse4arg,sseiadd1,ssecvt1")
512            (const_int 0)
513          (and (eq_attr "unit" "sse") (eq_attr "mode" "SF,DF"))
514            (const_int 1)
515          (and (eq_attr "type" "ibr,call,callv")
516               (match_test "ix86_bnd_prefixed_insn_p (insn)"))
517            (const_int 1)
518         ]
519         (const_int 0)))
520
521 ;; Set when 0f opcode prefix is used.
522 (define_attr "prefix_0f" ""
523   (if_then_else
524     (ior (eq_attr "type" "imovx,setcc,icmov,bitmanip,msklog,mskmov,
525                           mpxmk,mpxmov,mpxchk,mpxld,mpxst")
526          (eq_attr "unit" "sse,mmx"))
527     (const_int 1)
528     (const_int 0)))
529
530 ;; Set when REX opcode prefix is used.
531 (define_attr "prefix_rex" ""
532   (cond [(not (match_test "TARGET_64BIT"))
533            (const_int 0)
534          (and (eq_attr "mode" "DI")
535               (and (eq_attr "type" "!push,pop,call,callv,leave,ibr")
536                    (eq_attr "unit" "!mmx")))
537            (const_int 1)
538          (and (eq_attr "mode" "QI")
539               (match_test "x86_extended_QIreg_mentioned_p (insn)"))
540            (const_int 1)
541          (match_test "x86_extended_reg_mentioned_p (insn)")
542            (const_int 1)
543          (and (eq_attr "type" "imovx")
544               (match_operand:QI 1 "ext_QIreg_operand"))
545            (const_int 1)
546         ]
547         (const_int 0)))
548
549 ;; There are also additional prefixes in 3DNOW, SSSE3.
550 ;; ssemuladd,sse4arg default to 0f24/0f25 and DREX byte,
551 ;; sseiadd1,ssecvt1 to 0f7a with no DREX byte.
552 ;; 3DNOW has 0f0f prefix, SSSE3 and SSE4_{1,2} 0f38/0f3a.
553 (define_attr "prefix_extra" ""
554   (cond [(eq_attr "type" "ssemuladd,sse4arg")
555            (const_int 2)
556          (eq_attr "type" "sseiadd1,ssecvt1")
557            (const_int 1)
558         ]
559         (const_int 0)))
560
561 ;; Prefix used: original, VEX or maybe VEX.
562 (define_attr "prefix" "orig,vex,maybe_vex,evex,maybe_evex"
563   (cond [(eq_attr "mode" "OI,V8SF,V4DF")
564            (const_string "vex")
565          (eq_attr "mode" "XI,V16SF,V8DF")
566            (const_string "evex")
567         ]
568         (const_string "orig")))
569
570 ;; VEX W bit is used.
571 (define_attr "prefix_vex_w" "" (const_int 0))
572
573 ;; The length of VEX prefix
574 ;; Only instructions with 0f prefix can have 2 byte VEX prefix,
575 ;; 0f38/0f3a prefixes can't.  In i386.md 0f3[8a] is
576 ;; still prefix_0f 1, with prefix_extra 1.
577 (define_attr "length_vex" ""
578   (if_then_else (and (eq_attr "prefix_0f" "1")
579                      (eq_attr "prefix_extra" "0"))
580     (if_then_else (eq_attr "prefix_vex_w" "1")
581       (symbol_ref "ix86_attr_length_vex_default (insn, true, true)")
582       (symbol_ref "ix86_attr_length_vex_default (insn, true, false)"))
583     (if_then_else (eq_attr "prefix_vex_w" "1")
584       (symbol_ref "ix86_attr_length_vex_default (insn, false, true)")
585       (symbol_ref "ix86_attr_length_vex_default (insn, false, false)"))))
586
587 ;; 4-bytes evex prefix and 1 byte opcode.
588 (define_attr "length_evex" "" (const_int 5))
589
590 ;; Set when modrm byte is used.
591 (define_attr "modrm" ""
592   (cond [(eq_attr "type" "str,leave")
593            (const_int 0)
594          (eq_attr "unit" "i387")
595            (const_int 0)
596          (and (eq_attr "type" "incdec")
597               (and (not (match_test "TARGET_64BIT"))
598                    (ior (match_operand:SI 1 "register_operand")
599                         (match_operand:HI 1 "register_operand"))))
600            (const_int 0)
601          (and (eq_attr "type" "push")
602               (not (match_operand 1 "memory_operand")))
603            (const_int 0)
604          (and (eq_attr "type" "pop")
605               (not (match_operand 0 "memory_operand")))
606            (const_int 0)
607          (and (eq_attr "type" "imov")
608               (and (not (eq_attr "mode" "DI"))
609                    (ior (and (match_operand 0 "register_operand")
610                              (match_operand 1 "immediate_operand"))
611                         (ior (and (match_operand 0 "ax_reg_operand")
612                                   (match_operand 1 "memory_displacement_only_operand"))
613                              (and (match_operand 0 "memory_displacement_only_operand")
614                                   (match_operand 1 "ax_reg_operand"))))))
615            (const_int 0)
616          (and (eq_attr "type" "call")
617               (match_operand 0 "constant_call_address_operand"))
618              (const_int 0)
619          (and (eq_attr "type" "callv")
620               (match_operand 1 "constant_call_address_operand"))
621              (const_int 0)
622          (and (eq_attr "type" "alu,alu1,icmp,test")
623               (match_operand 0 "ax_reg_operand"))
624              (symbol_ref "(get_attr_length_immediate (insn) <= (get_attr_mode (insn) != MODE_QI))")
625          ]
626          (const_int 1)))
627
628 ;; When this attribute is set, calculate total insn length from
629 ;; length_nobnd attribute, prefixed with eventual bnd prefix byte
630 (define_attr "length_nobnd" "" (const_int 0))
631
632 ;; The (bounding maximum) length of an instruction in bytes.
633 ;; ??? fistp and frndint are in fact fldcw/{fistp,frndint}/fldcw sequences.
634 ;; Later we may want to split them and compute proper length as for
635 ;; other insns.
636 (define_attr "length" ""
637   (cond [(eq_attr "length_nobnd" "!0")
638            (plus (symbol_ref ("ix86_bnd_prefixed_insn_p (insn)"))
639                  (attr "length_nobnd"))
640          (eq_attr "type" "other,multi,fistp,frndint")
641            (const_int 16)
642          (eq_attr "type" "fcmp")
643            (const_int 4)
644          (eq_attr "unit" "i387")
645            (plus (const_int 2)
646                  (plus (attr "prefix_data16")
647                        (attr "length_address")))
648          (ior (eq_attr "prefix" "evex")
649               (and (ior (eq_attr "prefix" "maybe_evex")
650                         (eq_attr "prefix" "maybe_vex"))
651                    (match_test "TARGET_AVX512F")))
652            (plus (attr "length_evex")
653                  (plus (attr "length_immediate")
654                        (plus (attr "modrm")
655                              (attr "length_address"))))
656          (ior (eq_attr "prefix" "vex")
657               (and (ior (eq_attr "prefix" "maybe_vex")
658                         (eq_attr "prefix" "maybe_evex"))
659                    (match_test "TARGET_AVX")))
660            (plus (attr "length_vex")
661                  (plus (attr "length_immediate")
662                        (plus (attr "modrm")
663                              (attr "length_address"))))]
664          (plus (plus (attr "modrm")
665                      (plus (attr "prefix_0f")
666                            (plus (attr "prefix_rex")
667                                  (plus (attr "prefix_extra")
668                                        (const_int 1)))))
669                (plus (attr "prefix_rep")
670                      (plus (attr "prefix_data16")
671                            (plus (attr "length_immediate")
672                                  (attr "length_address")))))))
673
674 ;; The `memory' attribute is `none' if no memory is referenced, `load' or
675 ;; `store' if there is a simple memory reference therein, or `unknown'
676 ;; if the instruction is complex.
677
678 (define_attr "memory" "none,load,store,both,unknown"
679   (cond [(eq_attr "type" "other,multi,str,lwp")
680            (const_string "unknown")
681          (eq_attr "type" "lea,fcmov,fpspc,mpxmk,mpxchk")
682            (const_string "none")
683          (eq_attr "type" "fistp,leave")
684            (const_string "both")
685          (eq_attr "type" "frndint")
686            (const_string "load")
687          (eq_attr "type" "mpxld")
688            (const_string "load")
689          (eq_attr "type" "mpxst")
690            (const_string "store")
691          (eq_attr "type" "push")
692            (if_then_else (match_operand 1 "memory_operand")
693              (const_string "both")
694              (const_string "store"))
695          (eq_attr "type" "pop")
696            (if_then_else (match_operand 0 "memory_operand")
697              (const_string "both")
698              (const_string "load"))
699          (eq_attr "type" "setcc")
700            (if_then_else (match_operand 0 "memory_operand")
701              (const_string "store")
702              (const_string "none"))
703          (eq_attr "type" "icmp,test,ssecmp,ssecomi,mmxcmp,fcmp")
704            (if_then_else (ior (match_operand 0 "memory_operand")
705                               (match_operand 1 "memory_operand"))
706              (const_string "load")
707              (const_string "none"))
708          (eq_attr "type" "ibr")
709            (if_then_else (match_operand 0 "memory_operand")
710              (const_string "load")
711              (const_string "none"))
712          (eq_attr "type" "call")
713            (if_then_else (match_operand 0 "constant_call_address_operand")
714              (const_string "none")
715              (const_string "load"))
716          (eq_attr "type" "callv")
717            (if_then_else (match_operand 1 "constant_call_address_operand")
718              (const_string "none")
719              (const_string "load"))
720          (and (eq_attr "type" "alu1,negnot,ishift1,sselog1,sseshuf1")
721               (match_operand 1 "memory_operand"))
722            (const_string "both")
723          (and (match_operand 0 "memory_operand")
724               (match_operand 1 "memory_operand"))
725            (const_string "both")
726          (match_operand 0 "memory_operand")
727            (const_string "store")
728          (match_operand 1 "memory_operand")
729            (const_string "load")
730          (and (eq_attr "type"
731                  "!alu1,negnot,ishift1,
732                    imov,imovx,icmp,test,bitmanip,
733                    fmov,fcmp,fsgn,
734                    sse,ssemov,ssecmp,ssecomi,ssecvt,ssecvt1,sseicvt,
735                    sselog1,sseshuf1,sseadd1,sseiadd1,sseishft1,
736                    mmx,mmxmov,mmxcmp,mmxcvt,mskmov,msklog,mpxmov")
737               (match_operand 2 "memory_operand"))
738            (const_string "load")
739          (and (eq_attr "type" "icmov,ssemuladd,sse4arg")
740               (match_operand 3 "memory_operand"))
741            (const_string "load")
742         ]
743         (const_string "none")))
744
745 ;; Indicates if an instruction has both an immediate and a displacement.
746
747 (define_attr "imm_disp" "false,true,unknown"
748   (cond [(eq_attr "type" "other,multi")
749            (const_string "unknown")
750          (and (eq_attr "type" "icmp,test,imov,alu1,ishift1,rotate1")
751               (and (match_operand 0 "memory_displacement_operand")
752                    (match_operand 1 "immediate_operand")))
753            (const_string "true")
754          (and (eq_attr "type" "alu,ishift,ishiftx,rotate,rotatex,imul,idiv")
755               (and (match_operand 0 "memory_displacement_operand")
756                    (match_operand 2 "immediate_operand")))
757            (const_string "true")
758         ]
759         (const_string "false")))
760
761 ;; Indicates if an FP operation has an integer source.
762
763 (define_attr "fp_int_src" "false,true"
764   (const_string "false"))
765
766 ;; Defines rounding mode of an FP operation.
767
768 (define_attr "i387_cw" "trunc,floor,ceil,mask_pm,uninitialized,any"
769   (const_string "any"))
770
771 ;; Define attribute to classify add/sub insns that consumes carry flag (CF)
772 (define_attr "use_carry" "0,1" (const_string "0"))
773
774 ;; Define attribute to indicate unaligned ssemov insns
775 (define_attr "movu" "0,1" (const_string "0"))
776
777 ;; Used to control the "enabled" attribute on a per-instruction basis.
778 (define_attr "isa" "base,x64,x64_sse4,x64_sse4_noavx,x64_avx,nox64,
779                     sse2,sse2_noavx,sse3,sse4,sse4_noavx,avx,noavx,
780                     avx2,noavx2,bmi,bmi2,fma4,fma,avx512f,noavx512f,
781                     fma_avx512f,avx512bw,noavx512bw,avx512dq,noavx512dq"
782   (const_string "base"))
783
784 (define_attr "enabled" ""
785   (cond [(eq_attr "isa" "x64") (symbol_ref "TARGET_64BIT")
786          (eq_attr "isa" "x64_sse4")
787            (symbol_ref "TARGET_64BIT && TARGET_SSE4_1")
788          (eq_attr "isa" "x64_sse4_noavx")
789            (symbol_ref "TARGET_64BIT && TARGET_SSE4_1 && !TARGET_AVX")
790          (eq_attr "isa" "x64_avx")
791            (symbol_ref "TARGET_64BIT && TARGET_AVX")
792          (eq_attr "isa" "nox64") (symbol_ref "!TARGET_64BIT")
793          (eq_attr "isa" "sse2") (symbol_ref "TARGET_SSE2")
794          (eq_attr "isa" "sse2_noavx")
795            (symbol_ref "TARGET_SSE2 && !TARGET_AVX")
796          (eq_attr "isa" "sse3") (symbol_ref "TARGET_SSE3")
797          (eq_attr "isa" "sse4") (symbol_ref "TARGET_SSE4_1")
798          (eq_attr "isa" "sse4_noavx")
799            (symbol_ref "TARGET_SSE4_1 && !TARGET_AVX")
800          (eq_attr "isa" "avx") (symbol_ref "TARGET_AVX")
801          (eq_attr "isa" "noavx") (symbol_ref "!TARGET_AVX")
802          (eq_attr "isa" "avx2") (symbol_ref "TARGET_AVX2")
803          (eq_attr "isa" "noavx2") (symbol_ref "!TARGET_AVX2")
804          (eq_attr "isa" "bmi") (symbol_ref "TARGET_BMI")
805          (eq_attr "isa" "bmi2") (symbol_ref "TARGET_BMI2")
806          (eq_attr "isa" "fma4") (symbol_ref "TARGET_FMA4")
807          (eq_attr "isa" "fma") (symbol_ref "TARGET_FMA")
808          (eq_attr "isa" "avx512f") (symbol_ref "TARGET_AVX512F")
809          (eq_attr "isa" "noavx512f") (symbol_ref "!TARGET_AVX512F")
810          (eq_attr "isa" "fma_avx512f")
811            (symbol_ref "TARGET_FMA || TARGET_AVX512F")
812          (eq_attr "isa" "avx512bw") (symbol_ref "TARGET_AVX512BW")
813          (eq_attr "isa" "noavx512bw") (symbol_ref "!TARGET_AVX512BW")
814          (eq_attr "isa" "avx512dq") (symbol_ref "TARGET_AVX512DQ")
815          (eq_attr "isa" "noavx512dq") (symbol_ref "!TARGET_AVX512DQ")
816         ]
817         (const_int 1)))
818
819 (define_attr "preferred_for_size" "" (const_int 1))
820 (define_attr "preferred_for_speed" "" (const_int 1))
821
822 ;; Describe a user's asm statement.
823 (define_asm_attributes
824   [(set_attr "length" "128")
825    (set_attr "type" "multi")])
826
827 (define_code_iterator plusminus [plus minus])
828
829 (define_code_iterator sat_plusminus [ss_plus us_plus ss_minus us_minus])
830
831 (define_code_iterator multdiv [mult div])
832
833 ;; Base name for define_insn
834 (define_code_attr plusminus_insn
835   [(plus "add") (ss_plus "ssadd") (us_plus "usadd")
836    (minus "sub") (ss_minus "sssub") (us_minus "ussub")])
837
838 ;; Base name for insn mnemonic.
839 (define_code_attr plusminus_mnemonic
840   [(plus "add") (ss_plus "adds") (us_plus "addus")
841    (minus "sub") (ss_minus "subs") (us_minus "subus")])
842 (define_code_attr plusminus_carry_mnemonic
843   [(plus "adc") (minus "sbb")])
844 (define_code_attr multdiv_mnemonic
845   [(mult "mul") (div "div")])
846
847 ;; Mark commutative operators as such in constraints.
848 (define_code_attr comm [(plus "%") (ss_plus "%") (us_plus "%")
849                         (minus "") (ss_minus "") (us_minus "")])
850
851 ;; Mapping of max and min
852 (define_code_iterator maxmin [smax smin umax umin])
853
854 ;; Mapping of signed max and min
855 (define_code_iterator smaxmin [smax smin])
856
857 ;; Mapping of unsigned max and min
858 (define_code_iterator umaxmin [umax umin])
859
860 ;; Base name for integer and FP insn mnemonic
861 (define_code_attr maxmin_int [(smax "maxs") (smin "mins")
862                               (umax "maxu") (umin "minu")])
863 (define_code_attr maxmin_float [(smax "max") (smin "min")])
864
865 ;; Mapping of logic operators
866 (define_code_iterator any_logic [and ior xor])
867 (define_code_iterator any_or [ior xor])
868 (define_code_iterator fpint_logic [and xor])
869
870 ;; Base name for insn mnemonic.
871 (define_code_attr logic [(and "and") (ior "or") (xor "xor")])
872
873 ;; Mapping of logic-shift operators
874 (define_code_iterator any_lshift [ashift lshiftrt])
875
876 ;; Mapping of shift-right operators
877 (define_code_iterator any_shiftrt [lshiftrt ashiftrt])
878
879 ;; Mapping of all shift operators
880 (define_code_iterator any_shift [ashift lshiftrt ashiftrt])
881
882 ;; Base name for define_insn
883 (define_code_attr shift_insn
884   [(ashift "ashl") (lshiftrt "lshr") (ashiftrt "ashr")])
885
886 ;; Base name for insn mnemonic.
887 (define_code_attr shift [(ashift "sll") (lshiftrt "shr") (ashiftrt "sar")])
888 (define_code_attr vshift [(ashift "sll") (lshiftrt "srl") (ashiftrt "sra")])
889
890 ;; Mapping of rotate operators
891 (define_code_iterator any_rotate [rotate rotatert])
892
893 ;; Base name for define_insn
894 (define_code_attr rotate_insn [(rotate "rotl") (rotatert "rotr")])
895
896 ;; Base name for insn mnemonic.
897 (define_code_attr rotate [(rotate "rol") (rotatert "ror")])
898
899 ;; Mapping of abs neg operators
900 (define_code_iterator absneg [abs neg])
901
902 ;; Base name for x87 insn mnemonic.
903 (define_code_attr absneg_mnemonic [(abs "abs") (neg "chs")])
904
905 ;; Used in signed and unsigned widening multiplications.
906 (define_code_iterator any_extend [sign_extend zero_extend])
907
908 ;; Prefix for insn menmonic.
909 (define_code_attr sgnprefix [(sign_extend "i") (zero_extend "")])
910
911 ;; Prefix for define_insn
912 (define_code_attr u [(sign_extend "") (zero_extend "u")])
913 (define_code_attr s [(sign_extend "s") (zero_extend "u")])
914 (define_code_attr u_bool [(sign_extend "false") (zero_extend "true")])
915
916 ;; Used in signed and unsigned truncations.
917 (define_code_iterator any_truncate [ss_truncate truncate us_truncate])
918 ;; Instruction suffix for truncations.
919 (define_code_attr trunsuffix [(ss_truncate "s") (truncate "") (us_truncate "us")])
920
921 ;; Used in signed and unsigned fix.
922 (define_code_iterator any_fix [fix unsigned_fix])
923 (define_code_attr fixsuffix [(fix "") (unsigned_fix "u")])
924
925 ;; Used in signed and unsigned float.
926 (define_code_iterator any_float [float unsigned_float])
927 (define_code_attr floatsuffix [(float "") (unsigned_float "u")])
928
929 ;; All integer modes.
930 (define_mode_iterator SWI1248x [QI HI SI DI])
931
932 ;; All integer modes with AVX512BW.
933 (define_mode_iterator SWI1248_AVX512BW
934   [QI HI (SI "TARGET_AVX512BW") (DI "TARGET_AVX512BW")])
935
936 ;; All integer modes without QImode.
937 (define_mode_iterator SWI248x [HI SI DI])
938
939 ;; All integer modes without QImode and HImode.
940 (define_mode_iterator SWI48x [SI DI])
941
942 ;; All integer modes without SImode and DImode.
943 (define_mode_iterator SWI12 [QI HI])
944
945 ;; All integer modes without DImode.
946 (define_mode_iterator SWI124 [QI HI SI])
947
948 ;; All integer modes without QImode and DImode.
949 (define_mode_iterator SWI24 [HI SI])
950
951 ;; Single word integer modes.
952 (define_mode_iterator SWI [QI HI SI (DI "TARGET_64BIT")])
953
954 ;; Single word integer modes without QImode.
955 (define_mode_iterator SWI248 [HI SI (DI "TARGET_64BIT")])
956
957 ;; Single word integer modes without QImode and HImode.
958 (define_mode_iterator SWI48 [SI (DI "TARGET_64BIT")])
959
960 ;; All math-dependant single and double word integer modes.
961 (define_mode_iterator SDWIM [(QI "TARGET_QIMODE_MATH")
962                              (HI "TARGET_HIMODE_MATH")
963                              SI DI (TI "TARGET_64BIT")])
964
965 ;; Math-dependant single word integer modes.
966 (define_mode_iterator SWIM [(QI "TARGET_QIMODE_MATH")
967                             (HI "TARGET_HIMODE_MATH")
968                             SI (DI "TARGET_64BIT")])
969
970 ;; Math-dependant integer modes without DImode.
971 (define_mode_iterator SWIM124 [(QI "TARGET_QIMODE_MATH")
972                                (HI "TARGET_HIMODE_MATH")
973                                SI])
974
975 ;; Math-dependant single word integer modes without QImode.
976 (define_mode_iterator SWIM248 [(HI "TARGET_HIMODE_MATH")
977                                SI (DI "TARGET_64BIT")])
978
979 ;; Double word integer modes.
980 (define_mode_iterator DWI [(DI "!TARGET_64BIT")
981                            (TI "TARGET_64BIT")])
982
983 ;; GET_MODE_SIZE for selected modes.  As GET_MODE_SIZE is not
984 ;; compile time constant, it is faster to use <MODE_SIZE> than
985 ;; GET_MODE_SIZE (<MODE>mode).  For XFmode which depends on
986 ;; command line options just use GET_MODE_SIZE macro.
987 (define_mode_attr MODE_SIZE [(QI "1") (HI "2") (SI "4") (DI "8") (TI "16")
988                              (SF "4") (DF "8") (XF "GET_MODE_SIZE (XFmode)")
989                              (V16QI "16") (V32QI "32") (V64QI "64")
990                              (V8HI "16") (V16HI "32") (V32HI "64")
991                              (V4SI "16") (V8SI "32") (V16SI "64")
992                              (V2DI "16") (V4DI "32") (V8DI "64")
993                              (V1TI "16") (V2TI "32") (V4TI "64")
994                              (V2DF "16") (V4DF "32") (V8DF "64")
995                              (V4SF "16") (V8SF "32") (V16SF "64")])
996
997 ;; Double word integer modes as mode attribute.
998 (define_mode_attr DWI [(QI "HI") (HI "SI") (SI "DI") (DI "TI")])
999 (define_mode_attr dwi [(QI "hi") (HI "si") (SI "di") (DI "ti")])
1000
1001 ;; Half mode for double word integer modes.
1002 (define_mode_iterator DWIH [(SI "!TARGET_64BIT")
1003                             (DI "TARGET_64BIT")])
1004
1005 ;; Bound modes.
1006 (define_mode_iterator BND [(BND32 "!TARGET_LP64")
1007                            (BND64 "TARGET_LP64")])
1008
1009 ;; Pointer mode corresponding to bound mode.
1010 (define_mode_attr bnd_ptr [(BND32 "SI") (BND64 "DI")])
1011
1012 ;; MPX check types
1013 (define_int_iterator BNDCHECK [UNSPEC_BNDCL UNSPEC_BNDCU UNSPEC_BNDCN])
1014
1015 ;; Check name
1016 (define_int_attr bndcheck [(UNSPEC_BNDCL "cl")
1017                            (UNSPEC_BNDCU "cu")
1018                            (UNSPEC_BNDCN "cn")])
1019
1020 ;; Instruction suffix for integer modes.
1021 (define_mode_attr imodesuffix [(QI "b") (HI "w") (SI "l") (DI "q")])
1022
1023 ;; Instruction suffix for masks.
1024 (define_mode_attr mskmodesuffix [(QI "b") (HI "w") (SI "d") (DI "q")])
1025
1026 ;; Pointer size prefix for integer modes (Intel asm dialect)
1027 (define_mode_attr iptrsize [(QI "BYTE")
1028                             (HI "WORD")
1029                             (SI "DWORD")
1030                             (DI "QWORD")])
1031
1032 ;; Register class for integer modes.
1033 (define_mode_attr r [(QI "q") (HI "r") (SI "r") (DI "r")])
1034
1035 ;; Immediate operand constraint for integer modes.
1036 (define_mode_attr i [(QI "n") (HI "n") (SI "e") (DI "e")])
1037
1038 ;; General operand constraint for word modes.
1039 (define_mode_attr g [(QI "qmn") (HI "rmn") (SI "rme") (DI "rme")])
1040
1041 ;; Immediate operand constraint for double integer modes.
1042 (define_mode_attr di [(SI "nF") (DI "e")])
1043
1044 ;; Immediate operand constraint for shifts.
1045 (define_mode_attr S [(QI "I") (HI "I") (SI "I") (DI "J") (TI "O")])
1046
1047 ;; General operand predicate for integer modes.
1048 (define_mode_attr general_operand
1049         [(QI "general_operand")
1050          (HI "general_operand")
1051          (SI "x86_64_general_operand")
1052          (DI "x86_64_general_operand")
1053          (TI "x86_64_general_operand")])
1054
1055 ;; General sign extend operand predicate for integer modes,
1056 ;; which disallows VOIDmode operands and thus it is suitable
1057 ;; for use inside sign_extend.
1058 (define_mode_attr general_sext_operand
1059         [(QI "sext_operand")
1060          (HI "sext_operand")
1061          (SI "x86_64_sext_operand")
1062          (DI "x86_64_sext_operand")])
1063
1064 ;; General sign/zero extend operand predicate for integer modes.
1065 (define_mode_attr general_szext_operand
1066         [(QI "general_operand")
1067          (HI "general_operand")
1068          (SI "x86_64_szext_general_operand")
1069          (DI "x86_64_szext_general_operand")])
1070
1071 ;; Immediate operand predicate for integer modes.
1072 (define_mode_attr immediate_operand
1073         [(QI "immediate_operand")
1074          (HI "immediate_operand")
1075          (SI "x86_64_immediate_operand")
1076          (DI "x86_64_immediate_operand")])
1077
1078 ;; Nonmemory operand predicate for integer modes.
1079 (define_mode_attr nonmemory_operand
1080         [(QI "nonmemory_operand")
1081          (HI "nonmemory_operand")
1082          (SI "x86_64_nonmemory_operand")
1083          (DI "x86_64_nonmemory_operand")])
1084
1085 ;; Operand predicate for shifts.
1086 (define_mode_attr shift_operand
1087         [(QI "nonimmediate_operand")
1088          (HI "nonimmediate_operand")
1089          (SI "nonimmediate_operand")
1090          (DI "shiftdi_operand")
1091          (TI "register_operand")])
1092
1093 ;; Operand predicate for shift argument.
1094 (define_mode_attr shift_immediate_operand
1095         [(QI "const_1_to_31_operand")
1096          (HI "const_1_to_31_operand")
1097          (SI "const_1_to_31_operand")
1098          (DI "const_1_to_63_operand")])
1099
1100 ;; Input operand predicate for arithmetic left shifts.
1101 (define_mode_attr ashl_input_operand
1102         [(QI "nonimmediate_operand")
1103          (HI "nonimmediate_operand")
1104          (SI "nonimmediate_operand")
1105          (DI "ashldi_input_operand")
1106          (TI "reg_or_pm1_operand")])
1107
1108 ;; SSE and x87 SFmode and DFmode floating point modes
1109 (define_mode_iterator MODEF [SF DF])
1110
1111 ;; All x87 floating point modes
1112 (define_mode_iterator X87MODEF [SF DF XF])
1113
1114 ;; SSE instruction suffix for various modes
1115 (define_mode_attr ssemodesuffix
1116   [(SF "ss") (DF "sd")
1117    (V16SF "ps") (V8DF "pd")
1118    (V8SF "ps") (V4DF "pd")
1119    (V4SF "ps") (V2DF "pd")
1120    (V16QI "b") (V8HI "w") (V4SI "d") (V2DI "q")
1121    (V32QI "b") (V16HI "w") (V8SI "d") (V4DI "q")
1122    (V64QI "b") (V32HI "w") (V16SI "d") (V8DI "q")])
1123
1124 ;; SSE vector suffix for floating point modes
1125 (define_mode_attr ssevecmodesuffix [(SF "ps") (DF "pd")])
1126
1127 ;; SSE vector mode corresponding to a scalar mode
1128 (define_mode_attr ssevecmode
1129   [(QI "V16QI") (HI "V8HI") (SI "V4SI") (DI "V2DI") (SF "V4SF") (DF "V2DF")])
1130 (define_mode_attr ssevecmodelower
1131   [(QI "v16qi") (HI "v8hi") (SI "v4si") (DI "v2di") (SF "v4sf") (DF "v2df")])
1132
1133 ;; Instruction suffix for REX 64bit operators.
1134 (define_mode_attr rex64suffix [(SI "") (DI "{q}")])
1135
1136 ;; This mode iterator allows :P to be used for patterns that operate on
1137 ;; pointer-sized quantities.  Exactly one of the two alternatives will match.
1138 (define_mode_iterator P [(SI "Pmode == SImode") (DI "Pmode == DImode")])
1139
1140 ;; This mode iterator allows :W to be used for patterns that operate on
1141 ;; word_mode sized quantities.
1142 (define_mode_iterator W
1143   [(SI "word_mode == SImode") (DI "word_mode == DImode")])
1144
1145 ;; This mode iterator allows :PTR to be used for patterns that operate on
1146 ;; ptr_mode sized quantities.
1147 (define_mode_iterator PTR
1148   [(SI "ptr_mode == SImode") (DI "ptr_mode == DImode")])
1149 \f
1150 ;; Scheduling descriptions
1151
1152 (include "pentium.md")
1153 (include "ppro.md")
1154 (include "k6.md")
1155 (include "athlon.md")
1156 (include "bdver1.md")
1157 (include "bdver3.md")
1158 (include "btver2.md")
1159 (include "geode.md")
1160 (include "atom.md")
1161 (include "slm.md")
1162 (include "core2.md")
1163
1164 \f
1165 ;; Operand and operator predicates and constraints
1166
1167 (include "predicates.md")
1168 (include "constraints.md")
1169
1170 \f
1171 ;; Compare and branch/compare and store instructions.
1172
1173 (define_expand "cbranch<mode>4"
1174   [(set (reg:CC FLAGS_REG)
1175         (compare:CC (match_operand:SDWIM 1 "nonimmediate_operand")
1176                     (match_operand:SDWIM 2 "<general_operand>")))
1177    (set (pc) (if_then_else
1178                (match_operator 0 "ordered_comparison_operator"
1179                 [(reg:CC FLAGS_REG) (const_int 0)])
1180                (label_ref (match_operand 3))
1181                (pc)))]
1182   ""
1183 {
1184   if (MEM_P (operands[1]) && MEM_P (operands[2]))
1185     operands[1] = force_reg (<MODE>mode, operands[1]);
1186   ix86_expand_branch (GET_CODE (operands[0]),
1187                       operands[1], operands[2], operands[3]);
1188   DONE;
1189 })
1190
1191 (define_expand "cstore<mode>4"
1192   [(set (reg:CC FLAGS_REG)
1193         (compare:CC (match_operand:SWIM 2 "nonimmediate_operand")
1194                     (match_operand:SWIM 3 "<general_operand>")))
1195    (set (match_operand:QI 0 "register_operand")
1196         (match_operator 1 "ordered_comparison_operator"
1197           [(reg:CC FLAGS_REG) (const_int 0)]))]
1198   ""
1199 {
1200   if (MEM_P (operands[2]) && MEM_P (operands[3]))
1201     operands[2] = force_reg (<MODE>mode, operands[2]);
1202   ix86_expand_setcc (operands[0], GET_CODE (operands[1]),
1203                      operands[2], operands[3]);
1204   DONE;
1205 })
1206
1207 (define_expand "cmp<mode>_1"
1208   [(set (reg:CC FLAGS_REG)
1209         (compare:CC (match_operand:SWI48 0 "nonimmediate_operand")
1210                     (match_operand:SWI48 1 "<general_operand>")))])
1211
1212 (define_insn "*cmp<mode>_ccno_1"
1213   [(set (reg FLAGS_REG)
1214         (compare (match_operand:SWI 0 "nonimmediate_operand" "<r>,?m<r>")
1215                  (match_operand:SWI 1 "const0_operand")))]
1216   "ix86_match_ccmode (insn, CCNOmode)"
1217   "@
1218    test{<imodesuffix>}\t%0, %0
1219    cmp{<imodesuffix>}\t{%1, %0|%0, %1}"
1220   [(set_attr "type" "test,icmp")
1221    (set_attr "length_immediate" "0,1")
1222    (set_attr "mode" "<MODE>")])
1223
1224 (define_insn "*cmp<mode>_1"
1225   [(set (reg FLAGS_REG)
1226         (compare (match_operand:SWI 0 "nonimmediate_operand" "<r>m,<r>")
1227                  (match_operand:SWI 1 "<general_operand>" "<r><i>,<r>m")))]
1228   "ix86_match_ccmode (insn, CCmode)"
1229   "cmp{<imodesuffix>}\t{%1, %0|%0, %1}"
1230   [(set_attr "type" "icmp")
1231    (set_attr "mode" "<MODE>")])
1232
1233 (define_insn "*cmp<mode>_minus_1"
1234   [(set (reg FLAGS_REG)
1235         (compare
1236           (minus:SWI (match_operand:SWI 0 "nonimmediate_operand" "<r>m,<r>")
1237                      (match_operand:SWI 1 "<general_operand>" "<r><i>,<r>m"))
1238           (const_int 0)))]
1239   "ix86_match_ccmode (insn, CCGOCmode)"
1240   "cmp{<imodesuffix>}\t{%1, %0|%0, %1}"
1241   [(set_attr "type" "icmp")
1242    (set_attr "mode" "<MODE>")])
1243
1244 (define_insn "*cmpqi_ext_1"
1245   [(set (reg FLAGS_REG)
1246         (compare
1247           (match_operand:QI 0 "nonimmediate_x64nomem_operand" "Q,m")
1248           (subreg:QI
1249             (zero_extract:SI
1250               (match_operand 1 "ext_register_operand" "Q,Q")
1251               (const_int 8)
1252               (const_int 8)) 0)))]
1253   "ix86_match_ccmode (insn, CCmode)"
1254   "cmp{b}\t{%h1, %0|%0, %h1}"
1255   [(set_attr "isa" "*,nox64")
1256    (set_attr "type" "icmp")
1257    (set_attr "mode" "QI")])
1258
1259 (define_insn "*cmpqi_ext_2"
1260   [(set (reg FLAGS_REG)
1261         (compare
1262           (subreg:QI
1263             (zero_extract:SI
1264               (match_operand 0 "ext_register_operand" "Q")
1265               (const_int 8)
1266               (const_int 8)) 0)
1267           (match_operand:QI 1 "const0_operand")))]
1268   "ix86_match_ccmode (insn, CCNOmode)"
1269   "test{b}\t%h0, %h0"
1270   [(set_attr "type" "test")
1271    (set_attr "length_immediate" "0")
1272    (set_attr "mode" "QI")])
1273
1274 (define_expand "cmpqi_ext_3"
1275   [(set (reg:CC FLAGS_REG)
1276         (compare:CC
1277           (subreg:QI
1278             (zero_extract:SI
1279               (match_operand 0 "ext_register_operand")
1280               (const_int 8)
1281               (const_int 8)) 0)
1282           (match_operand:QI 1 "const_int_operand")))])
1283
1284 (define_insn "*cmpqi_ext_3"
1285   [(set (reg FLAGS_REG)
1286         (compare
1287           (subreg:QI
1288             (zero_extract:SI
1289               (match_operand 0 "ext_register_operand" "Q,Q")
1290               (const_int 8)
1291               (const_int 8)) 0)
1292           (match_operand:QI 1 "general_x64nomem_operand" "Qn,m")))]
1293   "ix86_match_ccmode (insn, CCmode)"
1294   "cmp{b}\t{%1, %h0|%h0, %1}"
1295   [(set_attr "isa" "*,nox64")
1296    (set_attr "type" "icmp")
1297    (set_attr "modrm" "1")
1298    (set_attr "mode" "QI")])
1299
1300 (define_insn "*cmpqi_ext_4"
1301   [(set (reg FLAGS_REG)
1302         (compare
1303           (subreg:QI
1304             (zero_extract:SI
1305               (match_operand 0 "ext_register_operand" "Q")
1306               (const_int 8)
1307               (const_int 8)) 0)
1308           (subreg:QI
1309             (zero_extract:SI
1310               (match_operand 1 "ext_register_operand" "Q")
1311               (const_int 8)
1312               (const_int 8)) 0)))]
1313   "ix86_match_ccmode (insn, CCmode)"
1314   "cmp{b}\t{%h1, %h0|%h0, %h1}"
1315   [(set_attr "type" "icmp")
1316    (set_attr "mode" "QI")])
1317
1318 ;; These implement float point compares.
1319 ;; %%% See if we can get away with VOIDmode operands on the actual insns,
1320 ;; which would allow mix and match FP modes on the compares.  Which is what
1321 ;; the old patterns did, but with many more of them.
1322
1323 (define_expand "cbranchxf4"
1324   [(set (reg:CC FLAGS_REG)
1325         (compare:CC (match_operand:XF 1 "nonmemory_operand")
1326                     (match_operand:XF 2 "nonmemory_operand")))
1327    (set (pc) (if_then_else
1328               (match_operator 0 "ix86_fp_comparison_operator"
1329                [(reg:CC FLAGS_REG)
1330                 (const_int 0)])
1331               (label_ref (match_operand 3))
1332               (pc)))]
1333   "TARGET_80387"
1334 {
1335   ix86_expand_branch (GET_CODE (operands[0]),
1336                       operands[1], operands[2], operands[3]);
1337   DONE;
1338 })
1339
1340 (define_expand "cstorexf4"
1341   [(set (reg:CC FLAGS_REG)
1342         (compare:CC (match_operand:XF 2 "nonmemory_operand")
1343                     (match_operand:XF 3 "nonmemory_operand")))
1344    (set (match_operand:QI 0 "register_operand")
1345               (match_operator 1 "ix86_fp_comparison_operator"
1346                [(reg:CC FLAGS_REG)
1347                 (const_int 0)]))]
1348   "TARGET_80387"
1349 {
1350   ix86_expand_setcc (operands[0], GET_CODE (operands[1]),
1351                      operands[2], operands[3]);
1352   DONE;
1353 })
1354
1355 (define_expand "cbranch<mode>4"
1356   [(set (reg:CC FLAGS_REG)
1357         (compare:CC (match_operand:MODEF 1 "cmp_fp_expander_operand")
1358                     (match_operand:MODEF 2 "cmp_fp_expander_operand")))
1359    (set (pc) (if_then_else
1360               (match_operator 0 "ix86_fp_comparison_operator"
1361                [(reg:CC FLAGS_REG)
1362                 (const_int 0)])
1363               (label_ref (match_operand 3))
1364               (pc)))]
1365   "TARGET_80387 || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
1366 {
1367   ix86_expand_branch (GET_CODE (operands[0]),
1368                       operands[1], operands[2], operands[3]);
1369   DONE;
1370 })
1371
1372 (define_expand "cstore<mode>4"
1373   [(set (reg:CC FLAGS_REG)
1374         (compare:CC (match_operand:MODEF 2 "cmp_fp_expander_operand")
1375                     (match_operand:MODEF 3 "cmp_fp_expander_operand")))
1376    (set (match_operand:QI 0 "register_operand")
1377               (match_operator 1 "ix86_fp_comparison_operator"
1378                [(reg:CC FLAGS_REG)
1379                 (const_int 0)]))]
1380   "TARGET_80387 || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
1381 {
1382   ix86_expand_setcc (operands[0], GET_CODE (operands[1]),
1383                      operands[2], operands[3]);
1384   DONE;
1385 })
1386
1387 (define_expand "cbranchcc4"
1388   [(set (pc) (if_then_else
1389               (match_operator 0 "comparison_operator"
1390                [(match_operand 1 "flags_reg_operand")
1391                 (match_operand 2 "const0_operand")])
1392               (label_ref (match_operand 3))
1393               (pc)))]
1394   ""
1395 {
1396   ix86_expand_branch (GET_CODE (operands[0]),
1397                       operands[1], operands[2], operands[3]);
1398   DONE;
1399 })
1400
1401 (define_expand "cstorecc4"
1402   [(set (match_operand:QI 0 "register_operand")
1403               (match_operator 1 "comparison_operator"
1404                [(match_operand 2 "flags_reg_operand")
1405                 (match_operand 3 "const0_operand")]))]
1406   ""
1407 {
1408   ix86_expand_setcc (operands[0], GET_CODE (operands[1]),
1409                      operands[2], operands[3]);
1410   DONE;
1411 })
1412
1413
1414 ;; FP compares, step 1:
1415 ;; Set the FP condition codes.
1416 ;;
1417 ;; CCFPmode     compare with exceptions
1418 ;; CCFPUmode    compare with no exceptions
1419
1420 ;; We may not use "#" to split and emit these, since the REG_DEAD notes
1421 ;; used to manage the reg stack popping would not be preserved.
1422
1423 (define_insn "*cmp<mode>_0_i387"
1424   [(set (match_operand:HI 0 "register_operand" "=a")
1425         (unspec:HI
1426           [(compare:CCFP
1427              (match_operand:X87MODEF 1 "register_operand" "f")
1428              (match_operand:X87MODEF 2 "const0_operand"))]
1429         UNSPEC_FNSTSW))]
1430   "TARGET_80387"
1431   "* return output_fp_compare (insn, operands, false, false);"
1432   [(set_attr "type" "multi")
1433    (set_attr "unit" "i387")
1434    (set_attr "mode" "<MODE>")])
1435
1436 (define_insn_and_split "*cmp<mode>_0_cc_i387"
1437   [(set (reg:CCFP FLAGS_REG)
1438         (compare:CCFP
1439           (match_operand:X87MODEF 1 "register_operand" "f")
1440           (match_operand:X87MODEF 2 "const0_operand")))
1441    (clobber (match_operand:HI 0 "register_operand" "=a"))]
1442   "TARGET_80387 && TARGET_SAHF && !TARGET_CMOVE"
1443   "#"
1444   "&& reload_completed"
1445   [(set (match_dup 0)
1446         (unspec:HI
1447           [(compare:CCFP (match_dup 1)(match_dup 2))]
1448         UNSPEC_FNSTSW))
1449    (set (reg:CC FLAGS_REG)
1450         (unspec:CC [(match_dup 0)] UNSPEC_SAHF))]
1451   ""
1452   [(set_attr "type" "multi")
1453    (set_attr "unit" "i387")
1454    (set_attr "mode" "<MODE>")])
1455
1456 (define_insn "*cmpxf_i387"
1457   [(set (match_operand:HI 0 "register_operand" "=a")
1458         (unspec:HI
1459           [(compare:CCFP
1460              (match_operand:XF 1 "register_operand" "f")
1461              (match_operand:XF 2 "register_operand" "f"))]
1462           UNSPEC_FNSTSW))]
1463   "TARGET_80387"
1464   "* return output_fp_compare (insn, operands, false, false);"
1465   [(set_attr "type" "multi")
1466    (set_attr "unit" "i387")
1467    (set_attr "mode" "XF")])
1468
1469 (define_insn_and_split "*cmpxf_cc_i387"
1470   [(set (reg:CCFP FLAGS_REG)
1471         (compare:CCFP
1472           (match_operand:XF 1 "register_operand" "f")
1473           (match_operand:XF 2 "register_operand" "f")))
1474    (clobber (match_operand:HI 0 "register_operand" "=a"))]
1475   "TARGET_80387 && TARGET_SAHF && !TARGET_CMOVE"
1476   "#"
1477   "&& reload_completed"
1478   [(set (match_dup 0)
1479         (unspec:HI
1480           [(compare:CCFP (match_dup 1)(match_dup 2))]
1481         UNSPEC_FNSTSW))
1482    (set (reg:CC FLAGS_REG)
1483         (unspec:CC [(match_dup 0)] UNSPEC_SAHF))]
1484   ""
1485   [(set_attr "type" "multi")
1486    (set_attr "unit" "i387")
1487    (set_attr "mode" "XF")])
1488
1489 (define_insn "*cmp<mode>_i387"
1490   [(set (match_operand:HI 0 "register_operand" "=a")
1491         (unspec:HI
1492           [(compare:CCFP
1493              (match_operand:MODEF 1 "register_operand" "f")
1494              (match_operand:MODEF 2 "nonimmediate_operand" "fm"))]
1495           UNSPEC_FNSTSW))]
1496   "TARGET_80387"
1497   "* return output_fp_compare (insn, operands, false, false);"
1498   [(set_attr "type" "multi")
1499    (set_attr "unit" "i387")
1500    (set_attr "mode" "<MODE>")])
1501
1502 (define_insn_and_split "*cmp<mode>_cc_i387"
1503   [(set (reg:CCFP FLAGS_REG)
1504         (compare:CCFP
1505           (match_operand:MODEF 1 "register_operand" "f")
1506           (match_operand:MODEF 2 "nonimmediate_operand" "fm")))
1507    (clobber (match_operand:HI 0 "register_operand" "=a"))]
1508   "TARGET_80387 && TARGET_SAHF && !TARGET_CMOVE"
1509   "#"
1510   "&& reload_completed"
1511   [(set (match_dup 0)
1512         (unspec:HI
1513           [(compare:CCFP (match_dup 1)(match_dup 2))]
1514         UNSPEC_FNSTSW))
1515    (set (reg:CC FLAGS_REG)
1516         (unspec:CC [(match_dup 0)] UNSPEC_SAHF))]
1517   ""
1518   [(set_attr "type" "multi")
1519    (set_attr "unit" "i387")
1520    (set_attr "mode" "<MODE>")])
1521
1522 (define_insn "*cmpu<mode>_i387"
1523   [(set (match_operand:HI 0 "register_operand" "=a")
1524         (unspec:HI
1525           [(compare:CCFPU
1526              (match_operand:X87MODEF 1 "register_operand" "f")
1527              (match_operand:X87MODEF 2 "register_operand" "f"))]
1528           UNSPEC_FNSTSW))]
1529   "TARGET_80387"
1530   "* return output_fp_compare (insn, operands, false, true);"
1531   [(set_attr "type" "multi")
1532    (set_attr "unit" "i387")
1533    (set_attr "mode" "<MODE>")])
1534
1535 (define_insn_and_split "*cmpu<mode>_cc_i387"
1536   [(set (reg:CCFPU FLAGS_REG)
1537         (compare:CCFPU
1538           (match_operand:X87MODEF 1 "register_operand" "f")
1539           (match_operand:X87MODEF 2 "register_operand" "f")))
1540    (clobber (match_operand:HI 0 "register_operand" "=a"))]
1541   "TARGET_80387 && TARGET_SAHF && !TARGET_CMOVE"
1542   "#"
1543   "&& reload_completed"
1544   [(set (match_dup 0)
1545         (unspec:HI
1546           [(compare:CCFPU (match_dup 1)(match_dup 2))]
1547         UNSPEC_FNSTSW))
1548    (set (reg:CC FLAGS_REG)
1549         (unspec:CC [(match_dup 0)] UNSPEC_SAHF))]
1550   ""
1551   [(set_attr "type" "multi")
1552    (set_attr "unit" "i387")
1553    (set_attr "mode" "<MODE>")])
1554
1555 (define_insn "*cmp<X87MODEF:mode>_<SWI24:mode>_i387"
1556   [(set (match_operand:HI 0 "register_operand" "=a")
1557         (unspec:HI
1558           [(compare:CCFP
1559              (match_operand:X87MODEF 1 "register_operand" "f")
1560              (match_operator:X87MODEF 3 "float_operator"
1561                [(match_operand:SWI24 2 "memory_operand" "m")]))]
1562           UNSPEC_FNSTSW))]
1563   "TARGET_80387
1564    && (TARGET_USE_<SWI24:MODE>MODE_FIOP
1565        || optimize_function_for_size_p (cfun))"
1566   "* return output_fp_compare (insn, operands, false, false);"
1567   [(set_attr "type" "multi")
1568    (set_attr "unit" "i387")
1569    (set_attr "fp_int_src" "true")
1570    (set_attr "mode" "<SWI24:MODE>")])
1571
1572 (define_insn_and_split "*cmp<X87MODEF:mode>_<SWI24:mode>_cc_i387"
1573   [(set (reg:CCFP FLAGS_REG)
1574         (compare:CCFP
1575           (match_operand:X87MODEF 1 "register_operand" "f")
1576           (match_operator:X87MODEF 3 "float_operator"
1577             [(match_operand:SWI24 2 "memory_operand" "m")])))
1578    (clobber (match_operand:HI 0 "register_operand" "=a"))]
1579   "TARGET_80387 && TARGET_SAHF && !TARGET_CMOVE
1580    && (TARGET_USE_<SWI24:MODE>MODE_FIOP
1581        || optimize_function_for_size_p (cfun))"
1582   "#"
1583   "&& reload_completed"
1584   [(set (match_dup 0)
1585         (unspec:HI
1586           [(compare:CCFP
1587              (match_dup 1)
1588              (match_op_dup 3 [(match_dup 2)]))]
1589         UNSPEC_FNSTSW))
1590    (set (reg:CC FLAGS_REG)
1591         (unspec:CC [(match_dup 0)] UNSPEC_SAHF))]
1592   ""
1593   [(set_attr "type" "multi")
1594    (set_attr "unit" "i387")
1595    (set_attr "fp_int_src" "true")
1596    (set_attr "mode" "<SWI24:MODE>")])
1597
1598 ;; FP compares, step 2
1599 ;; Move the fpsw to ax.
1600
1601 (define_insn "x86_fnstsw_1"
1602   [(set (match_operand:HI 0 "register_operand" "=a")
1603         (unspec:HI [(reg:CCFP FPSR_REG)] UNSPEC_FNSTSW))]
1604   "TARGET_80387"
1605   "fnstsw\t%0"
1606   [(set_attr "length" "2")
1607    (set_attr "mode" "SI")
1608    (set_attr "unit" "i387")])
1609
1610 ;; FP compares, step 3
1611 ;; Get ax into flags, general case.
1612
1613 (define_insn "x86_sahf_1"
1614   [(set (reg:CC FLAGS_REG)
1615         (unspec:CC [(match_operand:HI 0 "register_operand" "a")]
1616                    UNSPEC_SAHF))]
1617   "TARGET_SAHF"
1618 {
1619 #ifndef HAVE_AS_IX86_SAHF
1620   if (TARGET_64BIT)
1621     return ASM_BYTE "0x9e";
1622   else
1623 #endif
1624   return "sahf";
1625 }
1626   [(set_attr "length" "1")
1627    (set_attr "athlon_decode" "vector")
1628    (set_attr "amdfam10_decode" "direct")
1629    (set_attr "bdver1_decode" "direct")
1630    (set_attr "mode" "SI")])
1631
1632 ;; Pentium Pro can do steps 1 through 3 in one go.
1633 ;; comi*, ucomi*, fcomi*, ficomi*, fucomi*
1634 ;; (these i387 instructions set flags directly)
1635
1636 (define_mode_iterator FPCMP [CCFP CCFPU])
1637 (define_mode_attr unord [(CCFP "") (CCFPU "u")])
1638
1639 (define_insn "*cmpi<FPCMP:unord><MODEF:mode>_mixed"
1640   [(set (reg:FPCMP FLAGS_REG)
1641         (compare:FPCMP
1642           (match_operand:MODEF 0 "register_operand" "f,x")
1643           (match_operand:MODEF 1 "nonimmediate_operand" "f,xm")))]
1644   "TARGET_MIX_SSE_I387
1645    && SSE_FLOAT_MODE_P (<MODEF:MODE>mode)"
1646   "* return output_fp_compare (insn, operands, true,
1647                                <FPCMP:MODE>mode == CCFPUmode);"
1648   [(set_attr "type" "fcmp,ssecomi")
1649    (set_attr "prefix" "orig,maybe_vex")
1650    (set_attr "mode" "<MODEF:MODE>")
1651    (set (attr "prefix_rep")
1652         (if_then_else (eq_attr "type" "ssecomi")
1653                       (const_string "0")
1654                       (const_string "*")))
1655    (set (attr "prefix_data16")
1656         (cond [(eq_attr "type" "fcmp")
1657                  (const_string "*")
1658                (eq_attr "mode" "DF")
1659                  (const_string "1")
1660               ]
1661               (const_string "0")))
1662    (set_attr "athlon_decode" "vector")
1663    (set_attr "amdfam10_decode" "direct")
1664    (set_attr "bdver1_decode" "double")])
1665
1666 (define_insn "*cmpi<FPCMP:unord><MODEF:mode>_sse"
1667   [(set (reg:FPCMP FLAGS_REG)
1668         (compare:FPCMP
1669           (match_operand:MODEF 0 "register_operand" "x")
1670           (match_operand:MODEF 1 "nonimmediate_operand" "xm")))]
1671   "TARGET_SSE_MATH
1672    && SSE_FLOAT_MODE_P (<MODEF:MODE>mode)"
1673   "* return output_fp_compare (insn, operands, true,
1674                                <FPCMP:MODE>mode == CCFPUmode);"
1675   [(set_attr "type" "ssecomi")
1676    (set_attr "prefix" "maybe_vex")
1677    (set_attr "mode" "<MODEF:MODE>")
1678    (set_attr "prefix_rep" "0")
1679    (set (attr "prefix_data16")
1680         (if_then_else (eq_attr "mode" "DF")
1681                       (const_string "1")
1682                       (const_string "0")))
1683    (set_attr "athlon_decode" "vector")
1684    (set_attr "amdfam10_decode" "direct")
1685    (set_attr "bdver1_decode" "double")])
1686
1687 (define_insn "*cmpi<FPCMP:unord><X87MODEF:mode>_i387"
1688   [(set (reg:FPCMP FLAGS_REG)
1689         (compare:FPCMP
1690           (match_operand:X87MODEF 0 "register_operand" "f")
1691           (match_operand:X87MODEF 1 "register_operand" "f")))]
1692   "TARGET_80387 && TARGET_CMOVE
1693    && !(SSE_FLOAT_MODE_P (<X87MODEF:MODE>mode) && TARGET_SSE_MATH)"
1694   "* return output_fp_compare (insn, operands, true,
1695                                <FPCMP:MODE>mode == CCFPUmode);"
1696   [(set_attr "type" "fcmp")
1697    (set_attr "mode" "<X87MODEF:MODE>")
1698    (set_attr "athlon_decode" "vector")
1699    (set_attr "amdfam10_decode" "direct")
1700    (set_attr "bdver1_decode" "double")])
1701 \f
1702 ;; Push/pop instructions.
1703
1704 (define_insn "*push<mode>2"
1705   [(set (match_operand:DWI 0 "push_operand" "=<")
1706         (match_operand:DWI 1 "general_no_elim_operand" "riF*o"))]
1707   ""
1708   "#"
1709   [(set_attr "type" "multi")
1710    (set_attr "mode" "<MODE>")])
1711
1712 (define_split
1713   [(set (match_operand:TI 0 "push_operand")
1714         (match_operand:TI 1 "general_operand"))]
1715   "TARGET_64BIT && reload_completed
1716    && !SSE_REG_P (operands[1])"
1717   [(const_int 0)]
1718   "ix86_split_long_move (operands); DONE;")
1719
1720 (define_insn "*pushdi2_rex64"
1721   [(set (match_operand:DI 0 "push_operand" "=<,!<")
1722         (match_operand:DI 1 "general_no_elim_operand" "re*m,n"))]
1723   "TARGET_64BIT"
1724   "@
1725    push{q}\t%1
1726    #"
1727   [(set_attr "type" "push,multi")
1728    (set_attr "mode" "DI")])
1729
1730 ;; Convert impossible pushes of immediate to existing instructions.
1731 ;; First try to get scratch register and go through it.  In case this
1732 ;; fails, push sign extended lower part first and then overwrite
1733 ;; upper part by 32bit move.
1734 (define_peephole2
1735   [(match_scratch:DI 2 "r")
1736    (set (match_operand:DI 0 "push_operand")
1737         (match_operand:DI 1 "immediate_operand"))]
1738   "TARGET_64BIT && !symbolic_operand (operands[1], DImode)
1739    && !x86_64_immediate_operand (operands[1], DImode)"
1740   [(set (match_dup 2) (match_dup 1))
1741    (set (match_dup 0) (match_dup 2))])
1742
1743 ;; We need to define this as both peepholer and splitter for case
1744 ;; peephole2 pass is not run.
1745 ;; "&& 1" is needed to keep it from matching the previous pattern.
1746 (define_peephole2
1747   [(set (match_operand:DI 0 "push_operand")
1748         (match_operand:DI 1 "immediate_operand"))]
1749   "TARGET_64BIT && !symbolic_operand (operands[1], DImode)
1750    && !x86_64_immediate_operand (operands[1], DImode) && 1"
1751   [(set (match_dup 0) (match_dup 1))
1752    (set (match_dup 2) (match_dup 3))]
1753 {
1754   split_double_mode (DImode, &operands[1], 1, &operands[2], &operands[3]);
1755
1756   operands[1] = gen_lowpart (DImode, operands[2]);
1757   operands[2] = gen_rtx_MEM (SImode, gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1758                                                    GEN_INT (4)));
1759 })
1760
1761 (define_split
1762   [(set (match_operand:DI 0 "push_operand")
1763         (match_operand:DI 1 "immediate_operand"))]
1764   "TARGET_64BIT && ((optimize > 0 && flag_peephole2)
1765                     ? epilogue_completed : reload_completed)
1766    && !symbolic_operand (operands[1], DImode)
1767    && !x86_64_immediate_operand (operands[1], DImode)"
1768   [(set (match_dup 0) (match_dup 1))
1769    (set (match_dup 2) (match_dup 3))]
1770 {
1771   split_double_mode (DImode, &operands[1], 1, &operands[2], &operands[3]);
1772
1773   operands[1] = gen_lowpart (DImode, operands[2]);
1774   operands[2] = gen_rtx_MEM (SImode, gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1775                                                    GEN_INT (4)));
1776 })
1777
1778 (define_split
1779   [(set (match_operand:DI 0 "push_operand")
1780         (match_operand:DI 1 "general_operand"))]
1781   "!TARGET_64BIT && reload_completed
1782    && !(MMX_REG_P (operands[1]) || SSE_REG_P (operands[1]))"
1783   [(const_int 0)]
1784   "ix86_split_long_move (operands); DONE;")
1785
1786 (define_insn "*pushsi2"
1787   [(set (match_operand:SI 0 "push_operand" "=<")
1788         (match_operand:SI 1 "general_no_elim_operand" "ri*m"))]
1789   "!TARGET_64BIT"
1790   "push{l}\t%1"
1791   [(set_attr "type" "push")
1792    (set_attr "mode" "SI")])
1793
1794 ;; emit_push_insn when it calls move_by_pieces requires an insn to
1795 ;; "push a byte/word".  But actually we use pushl, which has the effect
1796 ;; of rounding the amount pushed up to a word.
1797
1798 ;; For TARGET_64BIT we always round up to 8 bytes.
1799 (define_insn "*push<mode>2_rex64"
1800   [(set (match_operand:SWI124 0 "push_operand" "=X")
1801         (match_operand:SWI124 1 "nonmemory_no_elim_operand" "r<i>"))]
1802   "TARGET_64BIT"
1803   "push{q}\t%q1"
1804   [(set_attr "type" "push")
1805    (set_attr "mode" "DI")])
1806
1807 (define_insn "*push<mode>2"
1808   [(set (match_operand:SWI12 0 "push_operand" "=X")
1809         (match_operand:SWI12 1 "nonmemory_no_elim_operand" "rn"))]
1810   "!TARGET_64BIT"
1811   "push{l}\t%k1"
1812   [(set_attr "type" "push")
1813    (set_attr "mode" "SI")])
1814
1815 (define_insn "*push<mode>2_prologue"
1816   [(set (match_operand:W 0 "push_operand" "=<")
1817         (match_operand:W 1 "general_no_elim_operand" "r<i>*m"))
1818    (clobber (mem:BLK (scratch)))]
1819   ""
1820   "push{<imodesuffix>}\t%1"
1821   [(set_attr "type" "push")
1822    (set_attr "mode" "<MODE>")])
1823
1824 (define_insn "*pop<mode>1"
1825   [(set (match_operand:W 0 "nonimmediate_operand" "=r*m")
1826         (match_operand:W 1 "pop_operand" ">"))]
1827   ""
1828   "pop{<imodesuffix>}\t%0"
1829   [(set_attr "type" "pop")
1830    (set_attr "mode" "<MODE>")])
1831
1832 (define_insn "*pop<mode>1_epilogue"
1833   [(set (match_operand:W 0 "nonimmediate_operand" "=r*m")
1834         (match_operand:W 1 "pop_operand" ">"))
1835    (clobber (mem:BLK (scratch)))]
1836   ""
1837   "pop{<imodesuffix>}\t%0"
1838   [(set_attr "type" "pop")
1839    (set_attr "mode" "<MODE>")])
1840
1841 (define_insn "*pushfl<mode>2"
1842   [(set (match_operand:W 0 "push_operand" "=<")
1843         (match_operand:W 1 "flags_reg_operand"))]
1844   ""
1845   "pushf{<imodesuffix>}"
1846   [(set_attr "type" "push")
1847    (set_attr "mode" "<MODE>")])
1848
1849 (define_insn "*popfl<mode>1"
1850   [(set (match_operand:W 0 "flags_reg_operand")
1851         (match_operand:W 1 "pop_operand" ">"))]
1852   ""
1853   "popf{<imodesuffix>}"
1854   [(set_attr "type" "pop")
1855    (set_attr "mode" "<MODE>")])
1856
1857 \f
1858 ;; Move instructions.
1859
1860 (define_expand "movxi"
1861   [(set (match_operand:XI 0 "nonimmediate_operand")
1862         (match_operand:XI 1 "general_operand"))]
1863   "TARGET_AVX512F"
1864   "ix86_expand_move (XImode, operands); DONE;")
1865
1866 ;; Reload patterns to support multi-word load/store
1867 ;; with non-offsetable address.
1868 (define_expand "reload_noff_store"
1869   [(parallel [(match_operand 0 "memory_operand" "=m")
1870               (match_operand 1 "register_operand" "r")
1871               (match_operand:DI 2 "register_operand" "=&r")])]
1872   "TARGET_64BIT"
1873 {
1874   rtx mem = operands[0];
1875   rtx addr = XEXP (mem, 0);
1876
1877   emit_move_insn (operands[2], addr);
1878   mem = replace_equiv_address_nv (mem, operands[2]);
1879
1880   emit_insn (gen_rtx_SET (VOIDmode, mem, operands[1]));
1881   DONE;
1882 })
1883
1884 (define_expand "reload_noff_load"
1885   [(parallel [(match_operand 0 "register_operand" "=r")
1886               (match_operand 1 "memory_operand" "m")
1887               (match_operand:DI 2 "register_operand" "=r")])]
1888   "TARGET_64BIT"
1889 {
1890   rtx mem = operands[1];
1891   rtx addr = XEXP (mem, 0);
1892
1893   emit_move_insn (operands[2], addr);
1894   mem = replace_equiv_address_nv (mem, operands[2]);
1895
1896   emit_insn (gen_rtx_SET (VOIDmode, operands[0], mem));
1897   DONE;
1898 })
1899
1900 (define_expand "movoi"
1901   [(set (match_operand:OI 0 "nonimmediate_operand")
1902         (match_operand:OI 1 "general_operand"))]
1903   "TARGET_AVX"
1904   "ix86_expand_move (OImode, operands); DONE;")
1905
1906 (define_expand "movti"
1907   [(set (match_operand:TI 0 "nonimmediate_operand")
1908         (match_operand:TI 1 "nonimmediate_operand"))]
1909   "TARGET_64BIT || TARGET_SSE"
1910 {
1911   if (TARGET_64BIT)
1912     ix86_expand_move (TImode, operands);
1913   else
1914     ix86_expand_vector_move (TImode, operands);
1915   DONE;
1916 })
1917
1918 ;; This expands to what emit_move_complex would generate if we didn't
1919 ;; have a movti pattern.  Having this avoids problems with reload on
1920 ;; 32-bit targets when SSE is present, but doesn't seem to be harmful
1921 ;; to have around all the time.
1922 (define_expand "movcdi"
1923   [(set (match_operand:CDI 0 "nonimmediate_operand")
1924         (match_operand:CDI 1 "general_operand"))]
1925   ""
1926 {
1927   if (push_operand (operands[0], CDImode))
1928     emit_move_complex_push (CDImode, operands[0], operands[1]);
1929   else
1930     emit_move_complex_parts (operands[0], operands[1]);
1931   DONE;
1932 })
1933
1934 (define_expand "mov<mode>"
1935   [(set (match_operand:SWI1248x 0 "nonimmediate_operand")
1936         (match_operand:SWI1248x 1 "general_operand"))]
1937   ""
1938   "ix86_expand_move (<MODE>mode, operands); DONE;")
1939
1940 (define_insn "*mov<mode>_xor"
1941   [(set (match_operand:SWI48 0 "register_operand" "=r")
1942         (match_operand:SWI48 1 "const0_operand"))
1943    (clobber (reg:CC FLAGS_REG))]
1944   "reload_completed"
1945   "xor{l}\t%k0, %k0"
1946   [(set_attr "type" "alu1")
1947    (set_attr "mode" "SI")
1948    (set_attr "length_immediate" "0")])
1949
1950 (define_insn "*mov<mode>_or"
1951   [(set (match_operand:SWI48 0 "register_operand" "=r")
1952         (match_operand:SWI48 1 "const_int_operand"))
1953    (clobber (reg:CC FLAGS_REG))]
1954   "reload_completed
1955    && operands[1] == constm1_rtx"
1956   "or{<imodesuffix>}\t{%1, %0|%0, %1}"
1957   [(set_attr "type" "alu1")
1958    (set_attr "mode" "<MODE>")
1959    (set_attr "length_immediate" "1")])
1960
1961 (define_insn "*movxi_internal_avx512f"
1962   [(set (match_operand:XI 0 "nonimmediate_operand" "=x,x ,m")
1963         (match_operand:XI 1 "vector_move_operand"  "C ,xm,x"))]
1964   "TARGET_AVX512F && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
1965 {
1966   switch (which_alternative)
1967     {
1968     case 0:
1969       return standard_sse_constant_opcode (insn, operands[1]);
1970     case 1:
1971     case 2:
1972       if (misaligned_operand (operands[0], XImode)
1973           || misaligned_operand (operands[1], XImode))
1974         return "vmovdqu32\t{%1, %0|%0, %1}";
1975       else
1976         return "vmovdqa32\t{%1, %0|%0, %1}";
1977     default:
1978       gcc_unreachable ();
1979     }
1980 }
1981   [(set_attr "type" "sselog1,ssemov,ssemov")
1982    (set_attr "prefix" "evex")
1983    (set_attr "mode" "XI")])
1984
1985 (define_insn "*movoi_internal_avx"
1986   [(set (match_operand:OI 0 "nonimmediate_operand" "=v,v ,m")
1987         (match_operand:OI 1 "vector_move_operand"  "C ,vm,v"))]
1988   "TARGET_AVX && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
1989 {
1990   switch (get_attr_type (insn))
1991     {
1992     case TYPE_SSELOG1:
1993       return standard_sse_constant_opcode (insn, operands[1]);
1994
1995     case TYPE_SSEMOV:
1996       if (misaligned_operand (operands[0], OImode)
1997           || misaligned_operand (operands[1], OImode))
1998         {
1999           if (get_attr_mode (insn) == MODE_V8SF)
2000             return "vmovups\t{%1, %0|%0, %1}";
2001           else if (get_attr_mode (insn) == MODE_XI)
2002             return "vmovdqu32\t{%1, %0|%0, %1}";
2003           else
2004             return "vmovdqu\t{%1, %0|%0, %1}";
2005         }
2006       else
2007         {
2008           if (get_attr_mode (insn) == MODE_V8SF)
2009             return "vmovaps\t{%1, %0|%0, %1}";
2010           else if (get_attr_mode (insn) == MODE_XI)
2011             return "vmovdqa32\t{%1, %0|%0, %1}";
2012           else
2013             return "vmovdqa\t{%1, %0|%0, %1}";
2014         }
2015
2016     default:
2017       gcc_unreachable ();
2018     }
2019 }
2020   [(set_attr "type" "sselog1,ssemov,ssemov")
2021    (set_attr "prefix" "vex")
2022    (set (attr "mode")
2023         (cond [(ior (match_operand 0 "ext_sse_reg_operand")
2024                     (match_operand 1 "ext_sse_reg_operand"))
2025                  (const_string "XI")
2026                (match_test "TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL")
2027                  (const_string "V8SF")
2028                (and (eq_attr "alternative" "2")
2029                     (match_test "TARGET_SSE_TYPELESS_STORES"))
2030                  (const_string "V8SF")
2031               ]
2032               (const_string "OI")))])
2033
2034 (define_insn "*movti_internal"
2035   [(set (match_operand:TI 0 "nonimmediate_operand" "=!r ,o ,v,v ,m")
2036         (match_operand:TI 1 "general_operand"      "riFo,re,C,vm,v"))]
2037   "(TARGET_64BIT || TARGET_SSE)
2038    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
2039 {
2040   switch (get_attr_type (insn))
2041     {
2042     case TYPE_MULTI:
2043       return "#";
2044
2045     case TYPE_SSELOG1:
2046       return standard_sse_constant_opcode (insn, operands[1]);
2047
2048     case TYPE_SSEMOV:
2049       /* TDmode values are passed as TImode on the stack.  Moving them
2050          to stack may result in unaligned memory access.  */
2051       if (misaligned_operand (operands[0], TImode)
2052           || misaligned_operand (operands[1], TImode))
2053         {
2054           if (get_attr_mode (insn) == MODE_V4SF)
2055             return "%vmovups\t{%1, %0|%0, %1}";
2056           else if (get_attr_mode (insn) == MODE_XI)
2057             return "vmovdqu32\t{%1, %0|%0, %1}";
2058           else
2059             return "%vmovdqu\t{%1, %0|%0, %1}";
2060         }
2061       else
2062         {
2063           if (get_attr_mode (insn) == MODE_V4SF)
2064             return "%vmovaps\t{%1, %0|%0, %1}";
2065           else if (get_attr_mode (insn) == MODE_XI)
2066             return "vmovdqa32\t{%1, %0|%0, %1}";
2067           else
2068             return "%vmovdqa\t{%1, %0|%0, %1}";
2069         }
2070
2071     default:
2072       gcc_unreachable ();
2073     }
2074 }
2075   [(set_attr "isa" "x64,x64,*,*,*")
2076    (set_attr "type" "multi,multi,sselog1,ssemov,ssemov")
2077    (set (attr "prefix")
2078      (if_then_else (eq_attr "type" "sselog1,ssemov")
2079        (const_string "maybe_vex")
2080        (const_string "orig")))
2081    (set (attr "mode")
2082         (cond [(ior (match_operand 0 "ext_sse_reg_operand")
2083                     (match_operand 1 "ext_sse_reg_operand"))
2084                  (const_string "XI")
2085                (eq_attr "alternative" "0,1")
2086                  (const_string "DI")
2087                (ior (not (match_test "TARGET_SSE2"))
2088                     (match_test "TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL"))
2089                  (const_string "V4SF")
2090                (and (eq_attr "alternative" "4")
2091                     (match_test "TARGET_SSE_TYPELESS_STORES"))
2092                  (const_string "V4SF")
2093                (match_test "TARGET_AVX")
2094                  (const_string "TI")
2095                (match_test "optimize_function_for_size_p (cfun)")
2096                  (const_string "V4SF")
2097                ]
2098                (const_string "TI")))])
2099
2100 (define_split
2101   [(set (match_operand:TI 0 "nonimmediate_operand")
2102         (match_operand:TI 1 "general_operand"))]
2103   "reload_completed
2104    && !SSE_REG_P (operands[0]) && !SSE_REG_P (operands[1])"
2105   [(const_int 0)]
2106   "ix86_split_long_move (operands); DONE;")
2107
2108 (define_insn "*movdi_internal"
2109   [(set (match_operand:DI 0 "nonimmediate_operand"
2110     "=r  ,o  ,r,r  ,r,m ,*y,*y,?*y,?m,?r ,?*Ym,*v,*v,*v,m ,?r ,?r,?*Yi,?*Ym,?*Yi,*k,*k ,*r ,*m")
2111         (match_operand:DI 1 "general_operand"
2112     "riFo,riF,Z,rem,i,re,C ,*y,m  ,*y,*Yn,r   ,C ,*v,m ,*v,*Yj,*v,r   ,*Yj ,*Yn ,*r ,*km,*k,*k"))]
2113   "!(MEM_P (operands[0]) && MEM_P (operands[1]))"
2114 {
2115   switch (get_attr_type (insn))
2116     {
2117     case TYPE_MSKMOV:
2118       return "kmovq\t{%1, %0|%0, %1}";
2119
2120     case TYPE_MULTI:
2121       return "#";
2122
2123     case TYPE_MMX:
2124       return "pxor\t%0, %0";
2125
2126     case TYPE_MMXMOV:
2127       /* Handle broken assemblers that require movd instead of movq.  */
2128       if (!HAVE_AS_IX86_INTERUNIT_MOVQ
2129           && (GENERAL_REG_P (operands[0]) || GENERAL_REG_P (operands[1])))
2130         return "movd\t{%1, %0|%0, %1}";
2131       return "movq\t{%1, %0|%0, %1}";
2132
2133     case TYPE_SSELOG1:
2134       if (GENERAL_REG_P (operands[0]))
2135         return "%vpextrq\t{$0, %1, %0|%0, %1, 0}";
2136
2137       return standard_sse_constant_opcode (insn, operands[1]);
2138
2139     case TYPE_SSEMOV:
2140       switch (get_attr_mode (insn))
2141         {
2142         case MODE_DI:
2143           /* Handle broken assemblers that require movd instead of movq.  */
2144           if (!HAVE_AS_IX86_INTERUNIT_MOVQ
2145               && (GENERAL_REG_P (operands[0]) || GENERAL_REG_P (operands[1])))
2146             return "%vmovd\t{%1, %0|%0, %1}";
2147           return "%vmovq\t{%1, %0|%0, %1}";
2148         case MODE_TI:
2149           return "%vmovdqa\t{%1, %0|%0, %1}";
2150         case MODE_XI:
2151           return "vmovdqa64\t{%g1, %g0|%g0, %g1}";
2152
2153         case MODE_V2SF:
2154           gcc_assert (!TARGET_AVX);
2155           return "movlps\t{%1, %0|%0, %1}";
2156         case MODE_V4SF:
2157           return "%vmovaps\t{%1, %0|%0, %1}";
2158
2159         default:
2160           gcc_unreachable ();
2161         }
2162
2163     case TYPE_SSECVT:
2164       if (SSE_REG_P (operands[0]))
2165         return "movq2dq\t{%1, %0|%0, %1}";
2166       else
2167         return "movdq2q\t{%1, %0|%0, %1}";
2168
2169     case TYPE_LEA:
2170       return "lea{q}\t{%E1, %0|%0, %E1}";
2171
2172     case TYPE_IMOV:
2173       gcc_assert (!flag_pic || LEGITIMATE_PIC_OPERAND_P (operands[1]));
2174       if (get_attr_mode (insn) == MODE_SI)
2175         return "mov{l}\t{%k1, %k0|%k0, %k1}";
2176       else if (which_alternative == 4)
2177         return "movabs{q}\t{%1, %0|%0, %1}";
2178       else if (ix86_use_lea_for_mov (insn, operands))
2179         return "lea{q}\t{%E1, %0|%0, %E1}";
2180       else
2181         return "mov{q}\t{%1, %0|%0, %1}";
2182
2183     default:
2184       gcc_unreachable ();
2185     }
2186 }
2187   [(set (attr "isa")
2188      (cond [(eq_attr "alternative" "0,1")
2189               (const_string "nox64")
2190             (eq_attr "alternative" "2,3,4,5,10,11,16,18,21,23")
2191               (const_string "x64")
2192             (eq_attr "alternative" "17")
2193               (const_string "x64_sse4")
2194            ]
2195            (const_string "*")))
2196    (set (attr "type")
2197      (cond [(eq_attr "alternative" "0,1")
2198               (const_string "multi")
2199             (eq_attr "alternative" "6")
2200               (const_string "mmx")
2201             (eq_attr "alternative" "7,8,9,10,11")
2202               (const_string "mmxmov")
2203             (eq_attr "alternative" "12,17")
2204               (const_string "sselog1")
2205             (eq_attr "alternative" "13,14,15,16,18")
2206               (const_string "ssemov")
2207             (eq_attr "alternative" "19,20")
2208               (const_string "ssecvt")
2209             (eq_attr "alternative" "21,22,23,24")
2210               (const_string "mskmov")
2211             (and (match_operand 0 "register_operand")
2212                  (match_operand 1 "pic_32bit_operand"))
2213               (const_string "lea")
2214            ]
2215            (const_string "imov")))
2216    (set (attr "modrm")
2217      (if_then_else
2218        (and (eq_attr "alternative" "4") (eq_attr "type" "imov"))
2219          (const_string "0")
2220          (const_string "*")))
2221    (set (attr "length_immediate")
2222      (cond [(and (eq_attr "alternative" "4") (eq_attr "type" "imov"))
2223               (const_string "8")
2224             (eq_attr "alternative" "17")
2225               (const_string "1")
2226            ]
2227            (const_string "*")))
2228    (set (attr "prefix_rex")
2229      (if_then_else (eq_attr "alternative" "10,11,16,17,18")
2230        (const_string "1")
2231        (const_string "*")))
2232    (set (attr "prefix_extra")
2233      (if_then_else (eq_attr "alternative" "17")
2234        (const_string "1")
2235        (const_string "*")))
2236    (set (attr "prefix")
2237      (if_then_else (eq_attr "type" "sselog1,ssemov")
2238        (const_string "maybe_vex")
2239        (const_string "orig")))
2240    (set (attr "prefix_data16")
2241      (if_then_else (and (eq_attr "type" "ssemov") (eq_attr "mode" "DI"))
2242        (const_string "1")
2243        (const_string "*")))
2244    (set (attr "mode")
2245      (cond [(eq_attr "alternative" "2")
2246               (const_string "SI")
2247             (eq_attr "alternative" "12,13")
2248               (cond [(ior (match_operand 0 "ext_sse_reg_operand")
2249                           (match_operand 1 "ext_sse_reg_operand"))
2250                        (const_string "XI")
2251                      (ior (not (match_test "TARGET_SSE2"))
2252                           (match_test "TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL"))
2253                        (const_string "V4SF")
2254                      (match_test "TARGET_AVX")
2255                        (const_string "TI")
2256                      (match_test "optimize_function_for_size_p (cfun)")
2257                        (const_string "V4SF")
2258                     ]
2259                     (const_string "TI"))
2260
2261             (and (eq_attr "alternative" "14,15")
2262                  (not (match_test "TARGET_SSE2")))
2263               (const_string "V2SF")
2264             (eq_attr "alternative" "17")
2265               (const_string "TI")
2266            ]
2267            (const_string "DI")))])
2268
2269 (define_split
2270   [(set (match_operand:DI 0 "nonimmediate_operand")
2271         (match_operand:DI 1 "general_operand"))]
2272   "!TARGET_64BIT && reload_completed
2273    && !(MMX_REG_P (operands[0])
2274         || SSE_REG_P (operands[0])
2275         || MASK_REG_P (operands[0]))
2276    && !(MMX_REG_P (operands[1])
2277         || SSE_REG_P (operands[1])
2278         || MASK_REG_P (operands[1]))"
2279   [(const_int 0)]
2280   "ix86_split_long_move (operands); DONE;")
2281
2282 (define_insn "*movsi_internal"
2283   [(set (match_operand:SI 0 "nonimmediate_operand"
2284                         "=r,m ,*y,*y,?rm,?*y,*v,*v,*v,m ,?r ,?r,?*Yi,*k  ,*rm")
2285         (match_operand:SI 1 "general_operand"
2286                         "g ,re,C ,*y,*y ,rm ,C ,*v,m ,*v,*Yj,*v,r   ,*krm,*k"))]
2287   "!(MEM_P (operands[0]) && MEM_P (operands[1]))"
2288 {
2289   switch (get_attr_type (insn))
2290     {
2291     case TYPE_SSELOG1:
2292       if (GENERAL_REG_P (operands[0]))
2293         return "%vpextrd\t{$0, %1, %0|%0, %1, 0}";
2294
2295       return standard_sse_constant_opcode (insn, operands[1]);
2296
2297     case TYPE_MSKMOV:
2298       return "kmovd\t{%1, %0|%0, %1}";
2299
2300     case TYPE_SSEMOV:
2301       switch (get_attr_mode (insn))
2302         {
2303         case MODE_SI:
2304           return "%vmovd\t{%1, %0|%0, %1}";
2305         case MODE_TI:
2306           return "%vmovdqa\t{%1, %0|%0, %1}";
2307         case MODE_XI:
2308           return "vmovdqa32\t{%g1, %g0|%g0, %g1}";
2309
2310         case MODE_V4SF:
2311           return "%vmovaps\t{%1, %0|%0, %1}";
2312
2313         case MODE_SF:
2314           gcc_assert (!TARGET_AVX);
2315           return "movss\t{%1, %0|%0, %1}";
2316
2317         default:
2318           gcc_unreachable ();
2319         }
2320
2321     case TYPE_MMX:
2322       return "pxor\t%0, %0";
2323
2324     case TYPE_MMXMOV:
2325       switch (get_attr_mode (insn))
2326         {
2327         case MODE_DI:
2328           return "movq\t{%1, %0|%0, %1}";
2329         case MODE_SI:
2330           return "movd\t{%1, %0|%0, %1}";
2331
2332         default:
2333           gcc_unreachable ();
2334         }
2335
2336     case TYPE_LEA:
2337       return "lea{l}\t{%E1, %0|%0, %E1}";
2338
2339     case TYPE_IMOV:
2340       gcc_assert (!flag_pic || LEGITIMATE_PIC_OPERAND_P (operands[1]));
2341       if (ix86_use_lea_for_mov (insn, operands))
2342         return "lea{l}\t{%E1, %0|%0, %E1}";
2343       else
2344         return "mov{l}\t{%1, %0|%0, %1}";
2345
2346     default:
2347       gcc_unreachable ();
2348     }
2349 }
2350   [(set (attr "isa")
2351      (if_then_else (eq_attr "alternative" "11")
2352        (const_string "sse4")
2353        (const_string "*")))
2354    (set (attr "type")
2355      (cond [(eq_attr "alternative" "2")
2356               (const_string "mmx")
2357             (eq_attr "alternative" "3,4,5")
2358               (const_string "mmxmov")
2359             (eq_attr "alternative" "6,11")
2360               (const_string "sselog1")
2361             (eq_attr "alternative" "7,8,9,10,12")
2362               (const_string "ssemov")
2363             (eq_attr "alternative" "13,14")
2364               (const_string "mskmov")
2365             (and (match_operand 0 "register_operand")
2366                  (match_operand 1 "pic_32bit_operand"))
2367               (const_string "lea")
2368            ]
2369            (const_string "imov")))
2370    (set (attr "length_immediate")
2371      (if_then_else (eq_attr "alternative" "11")
2372        (const_string "1")
2373        (const_string "*")))
2374    (set (attr "prefix_extra")
2375      (if_then_else (eq_attr "alternative" "11")
2376        (const_string "1")
2377        (const_string "*")))
2378    (set (attr "prefix")
2379      (if_then_else (eq_attr "type" "sselog1,ssemov")
2380        (const_string "maybe_vex")
2381        (const_string "orig")))
2382    (set (attr "prefix_data16")
2383      (if_then_else (and (eq_attr "type" "ssemov") (eq_attr "mode" "SI"))
2384        (const_string "1")
2385        (const_string "*")))
2386    (set (attr "mode")
2387      (cond [(eq_attr "alternative" "2,3")
2388               (const_string "DI")
2389             (eq_attr "alternative" "6,7")
2390               (cond [(ior (match_operand 0 "ext_sse_reg_operand")
2391                           (match_operand 1 "ext_sse_reg_operand"))
2392                        (const_string "XI")
2393                      (ior (not (match_test "TARGET_SSE2"))
2394                           (match_test "TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL"))
2395                        (const_string "V4SF")
2396                      (match_test "TARGET_AVX")
2397                        (const_string "TI")
2398                      (match_test "optimize_function_for_size_p (cfun)")
2399                        (const_string "V4SF")
2400                     ]
2401                     (const_string "TI"))
2402
2403             (and (eq_attr "alternative" "8,9")
2404                  (not (match_test "TARGET_SSE2")))
2405               (const_string "SF")
2406             (eq_attr "alternative" "11")
2407               (const_string "TI")
2408            ]
2409            (const_string "SI")))])
2410
2411 (define_insn "kmovw"
2412   [(set (match_operand:HI 0 "nonimmediate_operand" "=k,k")
2413         (unspec:HI
2414           [(match_operand:HI 1 "nonimmediate_operand" "rm,k")]
2415           UNSPEC_KMOV))]
2416   "!(MEM_P (operands[0]) && MEM_P (operands[1])) && TARGET_AVX512F"
2417   "@
2418    kmovw\t{%k1, %0|%0, %k1}
2419    kmovw\t{%1, %0|%0, %1}";
2420   [(set_attr "mode" "HI")
2421    (set_attr "type" "mskmov")
2422    (set_attr "prefix" "vex")])
2423
2424
2425 (define_insn "*movhi_internal"
2426   [(set (match_operand:HI 0 "nonimmediate_operand" "=r,r ,r ,m ,k,k,rm")
2427         (match_operand:HI 1 "general_operand"      "r ,rn,rm,rn,rm,k,k"))]
2428   "!(MEM_P (operands[0]) && MEM_P (operands[1]))"
2429 {
2430   switch (get_attr_type (insn))
2431     {
2432     case TYPE_IMOVX:
2433       /* movzwl is faster than movw on p2 due to partial word stalls,
2434          though not as fast as an aligned movl.  */
2435       return "movz{wl|x}\t{%1, %k0|%k0, %1}";
2436
2437     case TYPE_MSKMOV:
2438       switch (which_alternative)
2439         {
2440         case 4: return "kmovw\t{%k1, %0|%0, %k1}";
2441         case 5: return "kmovw\t{%1, %0|%0, %1}";
2442         case 6: return "kmovw\t{%1, %k0|%k0, %1}";
2443         default: gcc_unreachable ();
2444         }
2445
2446     default:
2447       if (get_attr_mode (insn) == MODE_SI)
2448         return "mov{l}\t{%k1, %k0|%k0, %k1}";
2449       else
2450         return "mov{w}\t{%1, %0|%0, %1}";
2451     }
2452 }
2453   [(set (attr "type")
2454      (cond [(eq_attr "alternative" "4,5,6")
2455               (const_string "mskmov")
2456             (match_test "optimize_function_for_size_p (cfun)")
2457               (const_string "imov")
2458             (and (eq_attr "alternative" "0")
2459                  (ior (not (match_test "TARGET_PARTIAL_REG_STALL"))
2460                       (not (match_test "TARGET_HIMODE_MATH"))))
2461               (const_string "imov")
2462             (and (eq_attr "alternative" "1,2")
2463                  (match_operand:HI 1 "aligned_operand"))
2464               (const_string "imov")
2465             (and (match_test "TARGET_MOVX")
2466                  (eq_attr "alternative" "0,2"))
2467               (const_string "imovx")
2468            ]
2469            (const_string "imov")))
2470     (set (attr "prefix")
2471       (if_then_else (eq_attr "alternative" "4,5,6")
2472         (const_string "vex")
2473         (const_string "orig")))
2474     (set (attr "mode")
2475       (cond [(eq_attr "type" "imovx")
2476                (const_string "SI")
2477              (and (eq_attr "alternative" "1,2")
2478                   (match_operand:HI 1 "aligned_operand"))
2479                (const_string "SI")
2480              (and (eq_attr "alternative" "0")
2481                   (ior (not (match_test "TARGET_PARTIAL_REG_STALL"))
2482                        (not (match_test "TARGET_HIMODE_MATH"))))
2483                (const_string "SI")
2484             ]
2485             (const_string "HI")))])
2486
2487 ;; Situation is quite tricky about when to choose full sized (SImode) move
2488 ;; over QImode moves.  For Q_REG -> Q_REG move we use full size only for
2489 ;; partial register dependency machines (such as AMD Athlon), where QImode
2490 ;; moves issue extra dependency and for partial register stalls machines
2491 ;; that don't use QImode patterns (and QImode move cause stall on the next
2492 ;; instruction).
2493 ;;
2494 ;; For loads of Q_REG to NONQ_REG we use full sized moves except for partial
2495 ;; register stall machines with, where we use QImode instructions, since
2496 ;; partial register stall can be caused there.  Then we use movzx.
2497
2498 (define_insn "*movqi_internal"
2499   [(set (match_operand:QI 0 "nonimmediate_operand"
2500                         "=q,q ,q ,r,r ,?r,m ,k,k,r ,m,k")
2501         (match_operand:QI 1 "general_operand"
2502                         "q ,qn,qm,q,rn,qm,qn,r ,k,k,k,m"))]
2503   "!(MEM_P (operands[0]) && MEM_P (operands[1]))"
2504 {
2505   switch (get_attr_type (insn))
2506     {
2507     case TYPE_IMOVX:
2508       gcc_assert (ANY_QI_REG_P (operands[1]) || MEM_P (operands[1]));
2509       return "movz{bl|x}\t{%1, %k0|%k0, %1}";
2510
2511     case TYPE_MSKMOV:
2512       switch (which_alternative)
2513         {
2514         case 7: return TARGET_AVX512DQ ? "kmovb\t{%k1, %0|%0, %k1}"
2515                                        : "kmovw\t{%k1, %0|%0, %k1}";
2516         case 8: return TARGET_AVX512DQ ? "kmovb\t{%1, %0|%0, %1}"
2517                                        : "kmovw\t{%1, %0|%0, %1}";
2518         case 9: return TARGET_AVX512DQ ? "kmovb\t{%1, %k0|%k0, %1}"
2519                                        : "kmovw\t{%1, %k0|%k0, %1}";
2520         case 10:
2521         case 11:
2522           gcc_assert (TARGET_AVX512DQ);
2523           return "kmovb\t{%1, %0|%0, %1}";
2524         default: gcc_unreachable ();
2525         }
2526
2527     default:
2528       if (get_attr_mode (insn) == MODE_SI)
2529         return "mov{l}\t{%k1, %k0|%k0, %k1}";
2530       else
2531         return "mov{b}\t{%1, %0|%0, %1}";
2532     }
2533 }
2534   [(set_attr "isa" "*,*,*,*,*,*,*,*,*,*,avx512dq,avx512dq")
2535    (set (attr "type")
2536      (cond [(eq_attr "alternative" "3,5")
2537               (const_string "imovx")
2538             (eq_attr "alternative" "7,8,9,10,11")
2539               (const_string "mskmov")
2540             (and (eq_attr "alternative" "5")
2541                  (not (match_operand:QI 1 "aligned_operand")))
2542               (const_string "imovx")
2543             (match_test "optimize_function_for_size_p (cfun)")
2544               (const_string "imov")
2545             (and (eq_attr "alternative" "3")
2546                  (ior (not (match_test "TARGET_PARTIAL_REG_STALL"))
2547                       (not (match_test "TARGET_QIMODE_MATH"))))
2548               (const_string "imov")
2549             (and (match_test "TARGET_MOVX")
2550                  (eq_attr "alternative" "2"))
2551               (const_string "imovx")
2552            ]
2553            (const_string "imov")))
2554    (set (attr "prefix")
2555      (if_then_else (eq_attr "alternative" "7,8,9")
2556        (const_string "vex")
2557        (const_string "orig")))
2558    (set (attr "mode")
2559       (cond [(eq_attr "alternative" "3,4,5")
2560                (const_string "SI")
2561              (eq_attr "alternative" "6")
2562                (const_string "QI")
2563              (eq_attr "type" "imovx")
2564                (const_string "SI")
2565              (and (eq_attr "type" "imov")
2566                   (and (eq_attr "alternative" "0,1")
2567                        (and (match_test "TARGET_PARTIAL_REG_DEPENDENCY")
2568                             (and (not (match_test "optimize_function_for_size_p (cfun)"))
2569                                  (not (match_test "TARGET_PARTIAL_REG_STALL"))))))
2570                (const_string "SI")
2571              ;; Avoid partial register stalls when not using QImode arithmetic
2572              (and (eq_attr "type" "imov")
2573                   (and (eq_attr "alternative" "0,1")
2574                        (and (match_test "TARGET_PARTIAL_REG_STALL")
2575                             (not (match_test "TARGET_QIMODE_MATH")))))
2576                (const_string "SI")
2577            ]
2578            (const_string "QI")))])
2579
2580 ;; Stores and loads of ax to arbitrary constant address.
2581 ;; We fake an second form of instruction to force reload to load address
2582 ;; into register when rax is not available
2583 (define_insn "*movabs<mode>_1"
2584   [(set (mem:SWI1248x (match_operand:DI 0 "x86_64_movabs_operand" "i,r"))
2585         (match_operand:SWI1248x 1 "nonmemory_operand" "a,r<i>"))]
2586   "TARGET_LP64 && ix86_check_movabs (insn, 0)"
2587   "@
2588    movabs{<imodesuffix>}\t{%1, %P0|[%P0], %1}
2589    mov{<imodesuffix>}\t{%1, %a0|<iptrsize> PTR %a0, %1}"
2590   [(set_attr "type" "imov")
2591    (set_attr "modrm" "0,*")
2592    (set_attr "length_address" "8,0")
2593    (set_attr "length_immediate" "0,*")
2594    (set_attr "memory" "store")
2595    (set_attr "mode" "<MODE>")])
2596
2597 (define_insn "*movabs<mode>_2"
2598   [(set (match_operand:SWI1248x 0 "register_operand" "=a,r")
2599         (mem:SWI1248x (match_operand:DI 1 "x86_64_movabs_operand" "i,r")))]
2600   "TARGET_LP64 && ix86_check_movabs (insn, 1)"
2601   "@
2602    movabs{<imodesuffix>}\t{%P1, %0|%0, [%P1]}
2603    mov{<imodesuffix>}\t{%a1, %0|%0, <iptrsize> PTR %a1}"
2604   [(set_attr "type" "imov")
2605    (set_attr "modrm" "0,*")
2606    (set_attr "length_address" "8,0")
2607    (set_attr "length_immediate" "0")
2608    (set_attr "memory" "load")
2609    (set_attr "mode" "<MODE>")])
2610
2611 (define_insn "*swap<mode>"
2612   [(set (match_operand:SWI48 0 "register_operand" "+r")
2613         (match_operand:SWI48 1 "register_operand" "+r"))
2614    (set (match_dup 1)
2615         (match_dup 0))]
2616   ""
2617   "xchg{<imodesuffix>}\t%1, %0"
2618   [(set_attr "type" "imov")
2619    (set_attr "mode" "<MODE>")
2620    (set_attr "pent_pair" "np")
2621    (set_attr "athlon_decode" "vector")
2622    (set_attr "amdfam10_decode" "double")
2623    (set_attr "bdver1_decode" "double")])
2624
2625 (define_insn "*swap<mode>_1"
2626   [(set (match_operand:SWI12 0 "register_operand" "+r")
2627         (match_operand:SWI12 1 "register_operand" "+r"))
2628    (set (match_dup 1)
2629         (match_dup 0))]
2630   "!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun)"
2631   "xchg{l}\t%k1, %k0"
2632   [(set_attr "type" "imov")
2633    (set_attr "mode" "SI")
2634    (set_attr "pent_pair" "np")
2635    (set_attr "athlon_decode" "vector")
2636    (set_attr "amdfam10_decode" "double")
2637    (set_attr "bdver1_decode" "double")])
2638
2639 ;; Not added amdfam10_decode since TARGET_PARTIAL_REG_STALL
2640 ;; is disabled for AMDFAM10
2641 (define_insn "*swap<mode>_2"
2642   [(set (match_operand:SWI12 0 "register_operand" "+<r>")
2643         (match_operand:SWI12 1 "register_operand" "+<r>"))
2644    (set (match_dup 1)
2645         (match_dup 0))]
2646   "TARGET_PARTIAL_REG_STALL"
2647   "xchg{<imodesuffix>}\t%1, %0"
2648   [(set_attr "type" "imov")
2649    (set_attr "mode" "<MODE>")
2650    (set_attr "pent_pair" "np")
2651    (set_attr "athlon_decode" "vector")])
2652
2653 (define_expand "movstrict<mode>"
2654   [(set (strict_low_part (match_operand:SWI12 0 "nonimmediate_operand"))
2655         (match_operand:SWI12 1 "general_operand"))]
2656   ""
2657 {
2658   if (TARGET_PARTIAL_REG_STALL && optimize_function_for_speed_p (cfun))
2659     FAIL;
2660   if (GET_CODE (operands[0]) == SUBREG
2661       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (operands[0]))) != MODE_INT)
2662     FAIL;
2663   /* Don't generate memory->memory moves, go through a register */
2664   if (MEM_P (operands[0]) && MEM_P (operands[1]))
2665     operands[1] = force_reg (<MODE>mode, operands[1]);
2666 })
2667
2668 (define_insn "*movstrict<mode>_1"
2669   [(set (strict_low_part
2670           (match_operand:SWI12 0 "nonimmediate_operand" "+<r>m,<r>"))
2671         (match_operand:SWI12 1 "general_operand" "<r>n,m"))]
2672   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
2673    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
2674   "mov{<imodesuffix>}\t{%1, %0|%0, %1}"
2675   [(set_attr "type" "imov")
2676    (set_attr "mode" "<MODE>")])
2677
2678 (define_insn "*movstrict<mode>_xor"
2679   [(set (strict_low_part (match_operand:SWI12 0 "register_operand" "+<r>"))
2680         (match_operand:SWI12 1 "const0_operand"))
2681    (clobber (reg:CC FLAGS_REG))]
2682   "reload_completed"
2683   "xor{<imodesuffix>}\t%0, %0"
2684   [(set_attr "type" "alu1")
2685    (set_attr "mode" "<MODE>")
2686    (set_attr "length_immediate" "0")])
2687
2688 (define_insn "*mov<mode>_extv_1"
2689   [(set (match_operand:SWI24 0 "register_operand" "=R")
2690         (sign_extract:SWI24 (match_operand 1 "ext_register_operand" "Q")
2691                             (const_int 8)
2692                             (const_int 8)))]
2693   ""
2694   "movs{bl|x}\t{%h1, %k0|%k0, %h1}"
2695   [(set_attr "type" "imovx")
2696    (set_attr "mode" "SI")])
2697
2698 (define_insn "*movqi_extv_1"
2699   [(set (match_operand:QI 0 "nonimmediate_x64nomem_operand" "=Q,?R,m")
2700         (sign_extract:QI (match_operand 1 "ext_register_operand" "Q,Q,Q")
2701                          (const_int 8)
2702                          (const_int 8)))]
2703   ""
2704 {
2705   switch (get_attr_type (insn))
2706     {
2707     case TYPE_IMOVX:
2708       return "movs{bl|x}\t{%h1, %k0|%k0, %h1}";
2709     default:
2710       return "mov{b}\t{%h1, %0|%0, %h1}";
2711     }
2712 }
2713   [(set_attr "isa" "*,*,nox64")
2714    (set (attr "type")
2715      (if_then_else (and (match_operand:QI 0 "register_operand")
2716                         (ior (not (match_operand:QI 0 "QIreg_operand"))
2717                              (match_test "TARGET_MOVX")))
2718         (const_string "imovx")
2719         (const_string "imov")))
2720    (set (attr "mode")
2721      (if_then_else (eq_attr "type" "imovx")
2722         (const_string "SI")
2723         (const_string "QI")))])
2724
2725 (define_insn "*mov<mode>_extzv_1"
2726   [(set (match_operand:SWI48 0 "register_operand" "=R")
2727         (zero_extract:SWI48 (match_operand 1 "ext_register_operand" "Q")
2728                             (const_int 8)
2729                             (const_int 8)))]
2730   ""
2731   "movz{bl|x}\t{%h1, %k0|%k0, %h1}"
2732   [(set_attr "type" "imovx")
2733    (set_attr "mode" "SI")])
2734
2735 (define_insn "*movqi_extzv_2"
2736   [(set (match_operand:QI 0 "nonimmediate_x64nomem_operand" "=Q,?R,m")
2737         (subreg:QI
2738           (zero_extract:SI (match_operand 1 "ext_register_operand" "Q,Q,Q")
2739                            (const_int 8)
2740                            (const_int 8)) 0))]
2741   ""
2742 {
2743   switch (get_attr_type (insn))
2744     {
2745     case TYPE_IMOVX:
2746       return "movz{bl|x}\t{%h1, %k0|%k0, %h1}";
2747     default:
2748       return "mov{b}\t{%h1, %0|%0, %h1}";
2749     }
2750 }
2751   [(set_attr "isa" "*,*,nox64")
2752    (set (attr "type")
2753      (if_then_else (and (match_operand:QI 0 "register_operand")
2754                         (ior (not (match_operand:QI 0 "QIreg_operand"))
2755                              (match_test "TARGET_MOVX")))
2756         (const_string "imovx")
2757         (const_string "imov")))
2758    (set (attr "mode")
2759      (if_then_else (eq_attr "type" "imovx")
2760         (const_string "SI")
2761         (const_string "QI")))])
2762
2763 (define_insn "mov<mode>_insv_1"
2764   [(set (zero_extract:SWI48 (match_operand 0 "ext_register_operand" "+Q,Q")
2765                              (const_int 8)
2766                              (const_int 8))
2767         (match_operand:SWI48 1 "general_x64nomem_operand" "Qn,m"))]
2768   ""
2769 {
2770   if (CONST_INT_P (operands[1]))
2771     operands[1] = simplify_gen_subreg (QImode, operands[1], <MODE>mode, 0);
2772   return "mov{b}\t{%b1, %h0|%h0, %b1}";
2773 }
2774   [(set_attr "isa" "*,nox64")
2775    (set_attr "type" "imov")
2776    (set_attr "mode" "QI")])
2777
2778 (define_insn "*movqi_insv_2"
2779   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "+Q")
2780                          (const_int 8)
2781                          (const_int 8))
2782         (lshiftrt:SI (match_operand:SI 1 "register_operand" "Q")
2783                      (const_int 8)))]
2784   ""
2785   "mov{b}\t{%h1, %h0|%h0, %h1}"
2786   [(set_attr "type" "imov")
2787    (set_attr "mode" "QI")])
2788 \f
2789 ;; Floating point push instructions.
2790
2791 (define_insn "*pushtf"
2792   [(set (match_operand:TF 0 "push_operand" "=<,<")
2793         (match_operand:TF 1 "general_no_elim_operand" "x,*roF"))]
2794   "TARGET_64BIT || TARGET_SSE"
2795 {
2796   /* This insn should be already split before reg-stack.  */
2797   gcc_unreachable ();
2798 }
2799   [(set_attr "isa" "*,x64")
2800    (set_attr "type" "multi")
2801    (set_attr "unit" "sse,*")
2802    (set_attr "mode" "TF,DI")])
2803
2804 ;; %%% Kill this when call knows how to work this out.
2805 (define_split
2806   [(set (match_operand:TF 0 "push_operand")
2807         (match_operand:TF 1 "sse_reg_operand"))]
2808   "TARGET_SSE && reload_completed"
2809   [(set (reg:P SP_REG) (plus:P (reg:P SP_REG) (const_int -16)))
2810    (set (match_dup 0) (match_dup 1))]
2811 {
2812   /* Preserve memory attributes. */
2813   operands[0] = replace_equiv_address (operands[0], stack_pointer_rtx);
2814 })
2815
2816 (define_insn "*pushxf"
2817   [(set (match_operand:XF 0 "push_operand" "=<,<,<,<")
2818         (match_operand:XF 1 "general_no_elim_operand" "f,r,*r,oF"))]
2819   ""
2820 {
2821   /* This insn should be already split before reg-stack.  */
2822   gcc_unreachable ();
2823 }
2824   [(set_attr "type" "multi")
2825    (set_attr "unit" "i387,*,*,*")
2826    (set (attr "mode")
2827         (cond [(eq_attr "alternative" "1,2,3")
2828                  (if_then_else (match_test "TARGET_64BIT")
2829                    (const_string "DI")
2830                    (const_string "SI"))
2831               ]
2832               (const_string "XF")))
2833    (set (attr "preferred_for_size")
2834      (cond [(eq_attr "alternative" "1")
2835               (symbol_ref "false")]
2836            (symbol_ref "true")))])
2837
2838 ;; %%% Kill this when call knows how to work this out.
2839 (define_split
2840   [(set (match_operand:XF 0 "push_operand")
2841         (match_operand:XF 1 "fp_register_operand"))]
2842   "reload_completed"
2843   [(set (reg:P SP_REG) (plus:P (reg:P SP_REG) (match_dup 2)))
2844    (set (match_dup 0) (match_dup 1))]
2845 {
2846   operands[2] = GEN_INT (-GET_MODE_SIZE (XFmode));
2847   /* Preserve memory attributes. */
2848   operands[0] = replace_equiv_address (operands[0], stack_pointer_rtx);
2849 })
2850
2851 (define_insn "*pushdf"
2852   [(set (match_operand:DF 0 "push_operand" "=<,<,<,<,<,<")
2853         (match_operand:DF 1 "general_no_elim_operand" "f,r,*r,oF,rmF,x"))]
2854   ""
2855 {
2856   /* This insn should be already split before reg-stack.  */
2857   gcc_unreachable ();
2858 }
2859   [(set_attr "isa" "*,nox64,nox64,nox64,x64,sse2")
2860    (set_attr "type" "multi")
2861    (set_attr "unit" "i387,*,*,*,*,sse")
2862    (set_attr "mode" "DF,SI,SI,SI,DI,DF")
2863    (set (attr "preferred_for_size")
2864      (cond [(eq_attr "alternative" "1")
2865               (symbol_ref "false")]
2866            (symbol_ref "true")))
2867    (set (attr "preferred_for_speed")
2868      (cond [(eq_attr "alternative" "1")
2869               (symbol_ref "TARGET_INTEGER_DFMODE_MOVES")]
2870            (symbol_ref "true")))])
2871    
2872 ;; %%% Kill this when call knows how to work this out.
2873 (define_split
2874   [(set (match_operand:DF 0 "push_operand")
2875         (match_operand:DF 1 "any_fp_register_operand"))]
2876   "reload_completed"
2877   [(set (reg:P SP_REG) (plus:P (reg:P SP_REG) (const_int -8)))
2878    (set (match_dup 0) (match_dup 1))]
2879 {
2880   /* Preserve memory attributes. */
2881   operands[0] = replace_equiv_address (operands[0], stack_pointer_rtx);
2882 })
2883
2884 (define_insn "*pushsf_rex64"
2885   [(set (match_operand:SF 0 "push_operand" "=X,X,X")
2886         (match_operand:SF 1 "nonmemory_no_elim_operand" "f,rF,x"))]
2887   "TARGET_64BIT"
2888 {
2889   /* Anything else should be already split before reg-stack.  */
2890   gcc_assert (which_alternative == 1);
2891   return "push{q}\t%q1";
2892 }
2893   [(set_attr "type" "multi,push,multi")
2894    (set_attr "unit" "i387,*,*")
2895    (set_attr "mode" "SF,DI,SF")])
2896
2897 (define_insn "*pushsf"
2898   [(set (match_operand:SF 0 "push_operand" "=<,<,<")
2899         (match_operand:SF 1 "general_no_elim_operand" "f,rmF,x"))]
2900   "!TARGET_64BIT"
2901 {
2902   /* Anything else should be already split before reg-stack.  */
2903   gcc_assert (which_alternative == 1);
2904   return "push{l}\t%1";
2905 }
2906   [(set_attr "type" "multi,push,multi")
2907    (set_attr "unit" "i387,*,*")
2908    (set_attr "mode" "SF,SI,SF")])
2909
2910 ;; %%% Kill this when call knows how to work this out.
2911 (define_split
2912   [(set (match_operand:SF 0 "push_operand")
2913         (match_operand:SF 1 "any_fp_register_operand"))]
2914   "reload_completed"
2915   [(set (reg:P SP_REG) (plus:P (reg:P SP_REG) (match_dup 2)))
2916    (set (match_dup 0) (match_dup 1))]
2917 {
2918   rtx op = XEXP (operands[0], 0);
2919   if (GET_CODE (op) == PRE_DEC)
2920     {
2921       gcc_assert (!TARGET_64BIT);
2922       op = GEN_INT (-4);
2923     }
2924   else
2925     {
2926       op = XEXP (XEXP (op, 1), 1);
2927       gcc_assert (CONST_INT_P (op));
2928     }
2929   operands[2] = op;
2930   /* Preserve memory attributes. */
2931   operands[0] = replace_equiv_address (operands[0], stack_pointer_rtx);
2932 })
2933
2934 (define_split
2935   [(set (match_operand:SF 0 "push_operand")
2936         (match_operand:SF 1 "memory_operand"))]
2937   "reload_completed
2938    && (operands[2] = find_constant_src (insn))"
2939   [(set (match_dup 0) (match_dup 2))])
2940
2941 (define_split
2942   [(set (match_operand 0 "push_operand")
2943         (match_operand 1 "general_operand"))]
2944   "reload_completed
2945    && (GET_MODE (operands[0]) == TFmode
2946        || GET_MODE (operands[0]) == XFmode
2947        || GET_MODE (operands[0]) == DFmode)
2948    && !ANY_FP_REG_P (operands[1])"
2949   [(const_int 0)]
2950   "ix86_split_long_move (operands); DONE;")
2951 \f
2952 ;; Floating point move instructions.
2953
2954 (define_expand "movtf"
2955   [(set (match_operand:TF 0 "nonimmediate_operand")
2956         (match_operand:TF 1 "nonimmediate_operand"))]
2957   "TARGET_64BIT || TARGET_SSE"
2958   "ix86_expand_move (TFmode, operands); DONE;")
2959
2960 (define_expand "mov<mode>"
2961   [(set (match_operand:X87MODEF 0 "nonimmediate_operand")
2962         (match_operand:X87MODEF 1 "general_operand"))]
2963   ""
2964   "ix86_expand_move (<MODE>mode, operands); DONE;")
2965
2966 (define_insn "*movtf_internal"
2967   [(set (match_operand:TF 0 "nonimmediate_operand" "=x,x ,m,?*r ,!o")
2968         (match_operand:TF 1 "general_operand"      "C ,xm,x,*roF,*rC"))]
2969   "(TARGET_64BIT || TARGET_SSE)
2970    && !(MEM_P (operands[0]) && MEM_P (operands[1]))
2971    && (!can_create_pseudo_p ()
2972        || (ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_LARGE)
2973        || GET_CODE (operands[1]) != CONST_DOUBLE
2974        || (optimize_function_for_size_p (cfun)
2975            && standard_sse_constant_p (operands[1])
2976            && !memory_operand (operands[0], TFmode))
2977        || (!TARGET_MEMORY_MISMATCH_STALL
2978            && memory_operand (operands[0], TFmode)))"
2979 {
2980   switch (get_attr_type (insn))
2981     {
2982     case TYPE_SSELOG1:
2983       return standard_sse_constant_opcode (insn, operands[1]);
2984
2985     case TYPE_SSEMOV:
2986       /* Handle misaligned load/store since we
2987          don't have movmisaligntf pattern. */
2988       if (misaligned_operand (operands[0], TFmode)
2989           || misaligned_operand (operands[1], TFmode))
2990         {
2991           if (get_attr_mode (insn) == MODE_V4SF)
2992             return "%vmovups\t{%1, %0|%0, %1}";
2993           else
2994             return "%vmovdqu\t{%1, %0|%0, %1}";
2995         }
2996       else
2997         {
2998           if (get_attr_mode (insn) == MODE_V4SF)
2999             return "%vmovaps\t{%1, %0|%0, %1}";
3000           else
3001             return "%vmovdqa\t{%1, %0|%0, %1}";
3002         }
3003
3004     case TYPE_MULTI:
3005         return "#";
3006
3007     default:
3008       gcc_unreachable ();
3009     }
3010 }
3011   [(set_attr "isa" "*,*,*,x64,x64")
3012    (set_attr "type" "sselog1,ssemov,ssemov,multi,multi")
3013    (set (attr "prefix")
3014      (if_then_else (eq_attr "type" "sselog1,ssemov")
3015        (const_string "maybe_vex")
3016        (const_string "orig")))
3017    (set (attr "mode")
3018         (cond [(eq_attr "alternative" "3,4")
3019                  (const_string "DI")
3020                (match_test "TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL")
3021                  (const_string "V4SF")
3022                (and (eq_attr "alternative" "2")
3023                     (match_test "TARGET_SSE_TYPELESS_STORES"))
3024                  (const_string "V4SF")
3025                (match_test "TARGET_AVX")
3026                  (const_string "TI")
3027                (ior (not (match_test "TARGET_SSE2"))
3028                     (match_test "optimize_function_for_size_p (cfun)"))
3029                  (const_string "V4SF")
3030                ]
3031                (const_string "TI")))])
3032
3033 ;; Possible store forwarding (partial memory) stall
3034 ;; in alternatives 4, 6, 7 and 8.
3035 (define_insn "*movxf_internal"
3036   [(set (match_operand:XF 0 "nonimmediate_operand"
3037          "=f,m,f,?r ,!o,?*r ,!o,!o,!o")
3038         (match_operand:XF 1 "general_operand"
3039          "fm,f,G,roF,r , *roF,*r,F ,C"))]
3040   "!(MEM_P (operands[0]) && MEM_P (operands[1]))
3041    && (!can_create_pseudo_p ()
3042        || (ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_LARGE)
3043        || GET_CODE (operands[1]) != CONST_DOUBLE
3044        || (optimize_function_for_size_p (cfun)
3045            && standard_80387_constant_p (operands[1]) > 0
3046            && !memory_operand (operands[0], XFmode))
3047        || (!TARGET_MEMORY_MISMATCH_STALL
3048            && memory_operand (operands[0], XFmode)))"
3049 {
3050   switch (get_attr_type (insn))
3051     {
3052     case TYPE_FMOV:
3053       if (which_alternative == 2)
3054         return standard_80387_constant_opcode (operands[1]);
3055       return output_387_reg_move (insn, operands);
3056
3057     case TYPE_MULTI:
3058       return "#";
3059
3060     default:
3061       gcc_unreachable ();
3062     }
3063 }
3064   [(set (attr "isa")
3065         (cond [(eq_attr "alternative" "7")
3066                  (const_string "nox64")
3067                (eq_attr "alternative" "8")
3068                  (const_string "x64")
3069               ]
3070               (const_string "*")))
3071    (set (attr "type")
3072         (cond [(eq_attr "alternative" "3,4,5,6,7,8")
3073                  (const_string "multi")
3074               ]
3075               (const_string "fmov")))
3076    (set (attr "mode")
3077         (cond [(eq_attr "alternative" "3,4,5,6,7,8")
3078                  (if_then_else (match_test "TARGET_64BIT")
3079                    (const_string "DI")
3080                    (const_string "SI"))
3081               ]
3082               (const_string "XF")))
3083    (set (attr "preferred_for_size")
3084      (cond [(eq_attr "alternative" "3,4")
3085               (symbol_ref "false")]
3086            (symbol_ref "true")))])
3087    
3088 ;; Possible store forwarding (partial memory) stall in alternatives 4, 6 and 7.
3089 (define_insn "*movdf_internal"
3090   [(set (match_operand:DF 0 "nonimmediate_operand"
3091     "=Yf*f,m   ,Yf*f,?r ,!o,?*r ,!o,!o,?r,?m,?r,?r,v,v,v,m,*x,*x,*x,m ,r ,Yi")
3092         (match_operand:DF 1 "general_operand"
3093     "Yf*fm,Yf*f,G   ,roF,r ,*roF,*r,F ,rm,rC,C ,F ,C,v,m,v,C ,*x,m ,*x,Yj,r"))]
3094   "!(MEM_P (operands[0]) && MEM_P (operands[1]))
3095    && (!can_create_pseudo_p ()
3096        || (ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_LARGE)
3097        || GET_CODE (operands[1]) != CONST_DOUBLE
3098        || (optimize_function_for_size_p (cfun)
3099            && ((!(TARGET_SSE2 && TARGET_SSE_MATH)
3100                 && standard_80387_constant_p (operands[1]) > 0)
3101                || (TARGET_SSE2 && TARGET_SSE_MATH
3102                    && standard_sse_constant_p (operands[1])))
3103            && !memory_operand (operands[0], DFmode))
3104        || ((TARGET_64BIT || !TARGET_MEMORY_MISMATCH_STALL)
3105            && memory_operand (operands[0], DFmode)))"
3106 {
3107   switch (get_attr_type (insn))
3108     {
3109     case TYPE_FMOV:
3110       if (which_alternative == 2)
3111         return standard_80387_constant_opcode (operands[1]);
3112       return output_387_reg_move (insn, operands);
3113
3114     case TYPE_MULTI:
3115       return "#";
3116
3117     case TYPE_IMOV:
3118       if (get_attr_mode (insn) == MODE_SI)
3119         return "mov{l}\t{%1, %k0|%k0, %1}";
3120       else if (which_alternative == 11)
3121         return "movabs{q}\t{%1, %0|%0, %1}";
3122       else
3123         return "mov{q}\t{%1, %0|%0, %1}";
3124
3125     case TYPE_SSELOG1:
3126       return standard_sse_constant_opcode (insn, operands[1]);
3127
3128     case TYPE_SSEMOV:
3129       switch (get_attr_mode (insn))
3130         {
3131         case MODE_DF:
3132           if (TARGET_AVX && REG_P (operands[0]) && REG_P (operands[1]))
3133             return "vmovsd\t{%1, %0, %0|%0, %0, %1}";
3134           return "%vmovsd\t{%1, %0|%0, %1}";
3135
3136         case MODE_V4SF:
3137           return "%vmovaps\t{%1, %0|%0, %1}";
3138         case MODE_V8DF:
3139           return "vmovapd\t{%g1, %g0|%g0, %g1}";
3140         case MODE_V2DF:
3141           return "%vmovapd\t{%1, %0|%0, %1}";
3142
3143         case MODE_V2SF:
3144           gcc_assert (!TARGET_AVX);
3145           return "movlps\t{%1, %0|%0, %1}";
3146         case MODE_V1DF:
3147           gcc_assert (!TARGET_AVX);
3148           return "movlpd\t{%1, %0|%0, %1}";
3149
3150         case MODE_DI:
3151           /* Handle broken assemblers that require movd instead of movq.  */
3152           if (!HAVE_AS_IX86_INTERUNIT_MOVQ
3153               && (GENERAL_REG_P (operands[0]) || GENERAL_REG_P (operands[1])))
3154             return "%vmovd\t{%1, %0|%0, %1}";
3155           return "%vmovq\t{%1, %0|%0, %1}";
3156
3157         default:
3158           gcc_unreachable ();
3159         }
3160
3161     default:
3162       gcc_unreachable ();
3163     }
3164 }
3165   [(set (attr "isa")
3166         (cond [(eq_attr "alternative" "3,4,5,6,7")
3167                  (const_string "nox64")
3168                (eq_attr "alternative" "8,9,10,11,20,21")
3169                  (const_string "x64")
3170                (eq_attr "alternative" "12,13,14,15")
3171                  (const_string "sse2")
3172               ]
3173               (const_string "*")))
3174    (set (attr "type")
3175         (cond [(eq_attr "alternative" "0,1,2")
3176                  (const_string "fmov")
3177                (eq_attr "alternative" "3,4,5,6,7")
3178                  (const_string "multi")
3179                (eq_attr "alternative" "8,9,10,11")
3180                  (const_string "imov")
3181                (eq_attr "alternative" "12,16")
3182                  (const_string "sselog1")
3183               ]
3184               (const_string "ssemov")))
3185    (set (attr "modrm")
3186      (if_then_else (eq_attr "alternative" "11")
3187        (const_string "0")
3188        (const_string "*")))
3189    (set (attr "length_immediate")
3190      (if_then_else (eq_attr "alternative" "11")
3191        (const_string "8")
3192        (const_string "*")))
3193    (set (attr "prefix")
3194      (if_then_else (eq_attr "type" "sselog1,ssemov")
3195        (const_string "maybe_vex")
3196        (const_string "orig")))
3197    (set (attr "prefix_data16")
3198      (if_then_else
3199        (ior (and (eq_attr "type" "ssemov") (eq_attr "mode" "DI"))
3200             (eq_attr "mode" "V1DF"))
3201        (const_string "1")
3202        (const_string "*")))
3203    (set (attr "mode")
3204         (cond [(eq_attr "alternative" "3,4,5,6,7,10")
3205                  (const_string "SI")
3206                (eq_attr "alternative" "8,9,11,20,21")
3207                  (const_string "DI")
3208
3209                /* xorps is one byte shorter for non-AVX targets.  */
3210                (eq_attr "alternative" "12,16")
3211                  (cond [(not (match_test "TARGET_SSE2"))
3212                           (const_string "V4SF")
3213                         (match_test "TARGET_AVX512F")
3214                           (const_string "XI")
3215                         (match_test "TARGET_AVX")
3216                           (const_string "V2DF")
3217                         (match_test "optimize_function_for_size_p (cfun)")
3218                           (const_string "V4SF")
3219                         (match_test "TARGET_SSE_LOAD0_BY_PXOR")
3220                           (const_string "TI")
3221                        ]
3222                        (const_string "V2DF"))
3223
3224                /* For architectures resolving dependencies on
3225                   whole SSE registers use movapd to break dependency
3226                   chains, otherwise use short move to avoid extra work.  */
3227
3228                /* movaps is one byte shorter for non-AVX targets.  */
3229                (eq_attr "alternative" "13,17")
3230                  (cond [(ior (match_operand 0 "ext_sse_reg_operand")
3231                              (match_operand 1 "ext_sse_reg_operand"))
3232                           (const_string "V8DF")
3233                         (ior (not (match_test "TARGET_SSE2"))
3234                              (match_test "TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL"))
3235                           (const_string "V4SF")
3236                         (match_test "TARGET_SSE_PARTIAL_REG_DEPENDENCY")
3237                           (const_string "V2DF")
3238                         (match_test "TARGET_AVX")
3239                           (const_string "DF")
3240                         (match_test "optimize_function_for_size_p (cfun)")
3241                           (const_string "V4SF")
3242                        ]
3243                        (const_string "DF"))
3244
3245                /* For architectures resolving dependencies on register
3246                   parts we may avoid extra work to zero out upper part
3247                   of register.  */
3248                (eq_attr "alternative" "14,18")
3249                  (cond [(not (match_test "TARGET_SSE2"))
3250                           (const_string "V2SF")
3251                         (match_test "TARGET_AVX")
3252                           (const_string "DF")
3253                         (match_test "TARGET_SSE_SPLIT_REGS")
3254                           (const_string "V1DF")
3255                        ]
3256                        (const_string "DF"))
3257
3258                (and (eq_attr "alternative" "15,19")
3259                     (not (match_test "TARGET_SSE2")))
3260                  (const_string "V2SF")
3261               ]
3262               (const_string "DF")))
3263    (set (attr "preferred_for_size")
3264      (cond [(eq_attr "alternative" "3,4")
3265               (symbol_ref "false")]
3266            (symbol_ref "true")))
3267    (set (attr "preferred_for_speed")
3268      (cond [(eq_attr "alternative" "3,4")
3269               (symbol_ref "TARGET_INTEGER_DFMODE_MOVES")]
3270            (symbol_ref "true")))])
3271
3272 (define_insn "*movsf_internal"
3273   [(set (match_operand:SF 0 "nonimmediate_operand"
3274           "=Yf*f,m   ,Yf*f,?r ,?m,v,v,v,m,?r,?Yi,!*y,!*y,!m,!r ,!*Ym")
3275         (match_operand:SF 1 "general_operand"
3276           "Yf*fm,Yf*f,G   ,rmF,rF,C,v,m,v,Yj,r  ,*y ,m  ,*y,*Yn,r"))]
3277   "!(MEM_P (operands[0]) && MEM_P (operands[1]))
3278    && (!can_create_pseudo_p ()
3279        || (ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_LARGE)
3280        || GET_CODE (operands[1]) != CONST_DOUBLE
3281        || (optimize_function_for_size_p (cfun)
3282            && ((!TARGET_SSE_MATH
3283                 && standard_80387_constant_p (operands[1]) > 0)
3284                || (TARGET_SSE_MATH
3285                    && standard_sse_constant_p (operands[1]))))
3286        || memory_operand (operands[0], SFmode))"
3287 {
3288   switch (get_attr_type (insn))
3289     {
3290     case TYPE_FMOV:
3291       if (which_alternative == 2)
3292         return standard_80387_constant_opcode (operands[1]);
3293       return output_387_reg_move (insn, operands);
3294
3295     case TYPE_IMOV:
3296       return "mov{l}\t{%1, %0|%0, %1}";
3297
3298     case TYPE_SSELOG1:
3299       return standard_sse_constant_opcode (insn, operands[1]);
3300
3301     case TYPE_SSEMOV:
3302       switch (get_attr_mode (insn))
3303         {
3304         case MODE_SF:
3305           if (TARGET_AVX && REG_P (operands[0]) && REG_P (operands[1]))
3306             return "vmovss\t{%1, %0, %0|%0, %0, %1}";
3307           return "%vmovss\t{%1, %0|%0, %1}";
3308
3309         case MODE_V16SF:
3310           return "vmovaps\t{%g1, %g0|%g0, %g1}";
3311         case MODE_V4SF:
3312           return "%vmovaps\t{%1, %0|%0, %1}";
3313
3314         case MODE_SI:
3315           return "%vmovd\t{%1, %0|%0, %1}";
3316
3317         default:
3318           gcc_unreachable ();
3319         }
3320
3321     case TYPE_MMXMOV:
3322       switch (get_attr_mode (insn))
3323         {
3324         case MODE_DI:
3325           return "movq\t{%1, %0|%0, %1}";
3326         case MODE_SI:
3327           return "movd\t{%1, %0|%0, %1}";
3328
3329         default:
3330           gcc_unreachable ();
3331         }
3332
3333     default:
3334       gcc_unreachable ();
3335     }
3336 }
3337   [(set (attr "type")
3338         (cond [(eq_attr "alternative" "0,1,2")
3339                  (const_string "fmov")
3340                (eq_attr "alternative" "3,4")
3341                  (const_string "imov")
3342                (eq_attr "alternative" "5")
3343                  (const_string "sselog1")
3344                (eq_attr "alternative" "11,12,13,14,15")
3345                  (const_string "mmxmov")
3346               ]
3347               (const_string "ssemov")))
3348    (set (attr "prefix")
3349      (if_then_else (eq_attr "type" "sselog1,ssemov")
3350        (const_string "maybe_vex")
3351        (const_string "orig")))
3352    (set (attr "prefix_data16")
3353      (if_then_else (and (eq_attr "type" "ssemov") (eq_attr "mode" "SI"))
3354        (const_string "1")
3355        (const_string "*")))
3356    (set (attr "mode")
3357         (cond [(eq_attr "alternative" "3,4,9,10,12,13,14,15")
3358                  (const_string "SI")
3359                (eq_attr "alternative" "11")
3360                  (const_string "DI")
3361                (eq_attr "alternative" "5")
3362                  (cond [(not (match_test "TARGET_SSE2"))
3363                           (const_string "V4SF")
3364                         (match_test "TARGET_AVX512F")
3365                           (const_string "V16SF")
3366                         (match_test "TARGET_AVX")
3367                           (const_string "V4SF")
3368                         (match_test "optimize_function_for_size_p (cfun)")
3369                           (const_string "V4SF")
3370                         (match_test "TARGET_SSE_LOAD0_BY_PXOR")
3371                           (const_string "TI")
3372                        ]
3373                        (const_string "V4SF"))
3374
3375                /* For architectures resolving dependencies on
3376                   whole SSE registers use APS move to break dependency
3377                   chains, otherwise use short move to avoid extra work.
3378
3379                   Do the same for architectures resolving dependencies on
3380                   the parts.  While in DF mode it is better to always handle
3381                   just register parts, the SF mode is different due to lack
3382                   of instructions to load just part of the register.  It is
3383                   better to maintain the whole registers in single format
3384                   to avoid problems on using packed logical operations.  */
3385                (eq_attr "alternative" "6")
3386                  (cond [(ior  (match_operand 0 "ext_sse_reg_operand")
3387                               (match_operand 1 "ext_sse_reg_operand"))
3388                           (const_string "V16SF")
3389                         (ior (match_test "TARGET_SSE_PARTIAL_REG_DEPENDENCY")
3390                              (match_test "TARGET_SSE_SPLIT_REGS"))
3391                           (const_string "V4SF")
3392                        ]
3393                        (const_string "SF"))
3394               ]
3395               (const_string "SF")))])
3396
3397 (define_split
3398   [(set (match_operand 0 "any_fp_register_operand")
3399         (match_operand 1 "memory_operand"))]
3400   "reload_completed
3401    && (GET_MODE (operands[0]) == TFmode
3402        || GET_MODE (operands[0]) == XFmode
3403        || GET_MODE (operands[0]) == DFmode
3404        || GET_MODE (operands[0]) == SFmode)
3405    && (operands[2] = find_constant_src (insn))"
3406   [(set (match_dup 0) (match_dup 2))]
3407 {
3408   rtx c = operands[2];
3409   int r = REGNO (operands[0]);
3410
3411   if ((SSE_REGNO_P (r) && !standard_sse_constant_p (c))
3412       || (STACK_REGNO_P (r) && standard_80387_constant_p (c) < 1))
3413     FAIL;
3414 })
3415
3416 (define_split
3417   [(set (match_operand 0 "any_fp_register_operand")
3418         (float_extend (match_operand 1 "memory_operand")))]
3419   "reload_completed
3420    && (GET_MODE (operands[0]) == TFmode
3421        || GET_MODE (operands[0]) == XFmode
3422        || GET_MODE (operands[0]) == DFmode)
3423    && (operands[2] = find_constant_src (insn))"
3424   [(set (match_dup 0) (match_dup 2))]
3425 {
3426   rtx c = operands[2];
3427   int r = REGNO (operands[0]);
3428
3429   if ((SSE_REGNO_P (r) && !standard_sse_constant_p (c))
3430       || (STACK_REGNO_P (r) && standard_80387_constant_p (c) < 1))
3431     FAIL;
3432 })
3433
3434 ;; Split the load of -0.0 or -1.0 into fldz;fchs or fld1;fchs sequence
3435 (define_split
3436   [(set (match_operand:X87MODEF 0 "fp_register_operand")
3437         (match_operand:X87MODEF 1 "immediate_operand"))]
3438   "reload_completed
3439    && (standard_80387_constant_p (operands[1]) == 8
3440        || standard_80387_constant_p (operands[1]) == 9)"
3441   [(set (match_dup 0)(match_dup 1))
3442    (set (match_dup 0)
3443         (neg:X87MODEF (match_dup 0)))]
3444 {
3445   REAL_VALUE_TYPE r;
3446
3447   REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
3448   if (real_isnegzero (&r))
3449     operands[1] = CONST0_RTX (<MODE>mode);
3450   else
3451     operands[1] = CONST1_RTX (<MODE>mode);
3452 })
3453
3454 (define_split
3455   [(set (match_operand 0 "nonimmediate_operand")
3456         (match_operand 1 "general_operand"))]
3457   "reload_completed
3458    && (GET_MODE (operands[0]) == TFmode
3459        || GET_MODE (operands[0]) == XFmode
3460        || GET_MODE (operands[0]) == DFmode)
3461    && !(ANY_FP_REG_P (operands[0]) || ANY_FP_REG_P (operands[1]))"
3462   [(const_int 0)]
3463   "ix86_split_long_move (operands); DONE;")
3464
3465 (define_insn "swapxf"
3466   [(set (match_operand:XF 0 "register_operand" "+f")
3467         (match_operand:XF 1 "register_operand" "+f"))
3468    (set (match_dup 1)
3469         (match_dup 0))]
3470   "TARGET_80387"
3471 {
3472   if (STACK_TOP_P (operands[0]))
3473     return "fxch\t%1";
3474   else
3475     return "fxch\t%0";
3476 }
3477   [(set_attr "type" "fxch")
3478    (set_attr "mode" "XF")])
3479
3480 (define_insn "*swap<mode>"
3481   [(set (match_operand:MODEF 0 "fp_register_operand" "+f")
3482         (match_operand:MODEF 1 "fp_register_operand" "+f"))
3483    (set (match_dup 1)
3484         (match_dup 0))]
3485   "TARGET_80387 || reload_completed"
3486 {
3487   if (STACK_TOP_P (operands[0]))
3488     return "fxch\t%1";
3489   else
3490     return "fxch\t%0";
3491 }
3492   [(set_attr "type" "fxch")
3493    (set_attr "mode" "<MODE>")])
3494 \f
3495 ;; Zero extension instructions
3496
3497 (define_expand "zero_extendsidi2"
3498   [(set (match_operand:DI 0 "nonimmediate_operand")
3499         (zero_extend:DI (match_operand:SI 1 "nonimmediate_operand")))])
3500
3501 (define_insn "*zero_extendsidi2"
3502   [(set (match_operand:DI 0 "nonimmediate_operand"
3503                         "=r,?r,?o,r   ,o,?*Ym,?!*y,?r ,?r,?*Yi,?*x")
3504         (zero_extend:DI
3505          (match_operand:SI 1 "x86_64_zext_operand"
3506                         "0 ,rm,r ,rmWz,0,r   ,m   ,*Yj,*x,r   ,m")))]
3507   ""
3508 {
3509   switch (get_attr_type (insn))
3510     {
3511     case TYPE_IMOVX:
3512       if (ix86_use_lea_for_mov (insn, operands))
3513         return "lea{l}\t{%E1, %k0|%k0, %E1}";
3514       else
3515         return "mov{l}\t{%1, %k0|%k0, %1}";
3516
3517     case TYPE_MULTI:
3518       return "#";
3519
3520     case TYPE_MMXMOV:
3521       return "movd\t{%1, %0|%0, %1}";
3522
3523     case TYPE_SSELOG1:
3524       return "%vpextrd\t{$0, %1, %k0|%k0, %1, 0}";
3525
3526     case TYPE_SSEMOV:
3527       if (GENERAL_REG_P (operands[0]))
3528         return "%vmovd\t{%1, %k0|%k0, %1}";
3529
3530       return "%vmovd\t{%1, %0|%0, %1}";
3531
3532     default:
3533       gcc_unreachable ();
3534     }
3535 }
3536   [(set (attr "isa")
3537      (cond [(eq_attr "alternative" "0,1,2")
3538               (const_string "nox64")
3539             (eq_attr "alternative" "3,7")
3540               (const_string "x64")
3541             (eq_attr "alternative" "8")
3542               (const_string "x64_sse4")
3543             (eq_attr "alternative" "10")
3544               (const_string "sse2")
3545            ]
3546            (const_string "*")))
3547    (set (attr "type")
3548      (cond [(eq_attr "alternative" "0,1,2,4")
3549               (const_string "multi")
3550             (eq_attr "alternative" "5,6")
3551               (const_string "mmxmov")
3552             (eq_attr "alternative" "7,9,10")
3553               (const_string "ssemov")
3554             (eq_attr "alternative" "8")
3555               (const_string "sselog1")
3556            ]
3557            (const_string "imovx")))
3558    (set (attr "prefix_extra")
3559      (if_then_else (eq_attr "alternative" "8")
3560        (const_string "1")
3561        (const_string "*")))
3562    (set (attr "length_immediate")
3563      (if_then_else (eq_attr "alternative" "8")
3564        (const_string "1")
3565        (const_string "*")))
3566    (set (attr "prefix")
3567      (if_then_else (eq_attr "type" "ssemov,sselog1")
3568        (const_string "maybe_vex")
3569        (const_string "orig")))
3570    (set (attr "prefix_0f")
3571      (if_then_else (eq_attr "type" "imovx")
3572        (const_string "0")
3573        (const_string "*")))
3574    (set (attr "mode")
3575      (cond [(eq_attr "alternative" "5,6")
3576               (const_string "DI")
3577             (eq_attr "alternative" "7,8,9")
3578               (const_string "TI")
3579            ]
3580            (const_string "SI")))])
3581
3582 (define_split
3583   [(set (match_operand:DI 0 "memory_operand")
3584         (zero_extend:DI (match_operand:SI 1 "memory_operand")))]
3585   "reload_completed"
3586   [(set (match_dup 4) (const_int 0))]
3587   "split_double_mode (DImode, &operands[0], 1, &operands[3], &operands[4]);")
3588
3589 (define_split
3590   [(set (match_operand:DI 0 "register_operand")
3591         (zero_extend:DI (match_operand:SI 1 "register_operand")))]
3592   "!TARGET_64BIT && reload_completed
3593    && !(MMX_REG_P (operands[0]) || SSE_REG_P (operands[0]))
3594    && true_regnum (operands[0]) == true_regnum (operands[1])"
3595   [(set (match_dup 4) (const_int 0))]
3596   "split_double_mode (DImode, &operands[0], 1, &operands[3], &operands[4]);")
3597
3598 (define_split
3599   [(set (match_operand:DI 0 "nonimmediate_operand")
3600         (zero_extend:DI (match_operand:SI 1 "nonimmediate_operand")))]
3601   "!TARGET_64BIT && reload_completed
3602    && !(MEM_P (operands[0]) && MEM_P (operands[1]))
3603    && !(MMX_REG_P (operands[0]) || SSE_REG_P (operands[0]))"
3604   [(set (match_dup 3) (match_dup 1))
3605    (set (match_dup 4) (const_int 0))]
3606   "split_double_mode (DImode, &operands[0], 1, &operands[3], &operands[4]);")
3607
3608 (define_insn "zero_extend<mode>di2"
3609   [(set (match_operand:DI 0 "register_operand" "=r")
3610         (zero_extend:DI
3611          (match_operand:SWI12 1 "nonimmediate_operand" "<r>m")))]
3612   "TARGET_64BIT"
3613   "movz{<imodesuffix>l|x}\t{%1, %k0|%k0, %1}"
3614   [(set_attr "type" "imovx")
3615    (set_attr "mode" "SI")])
3616
3617 (define_expand "zero_extend<mode>si2"
3618   [(set (match_operand:SI 0 "register_operand")
3619         (zero_extend:SI (match_operand:SWI12 1 "nonimmediate_operand")))]
3620   ""
3621 {
3622   if (TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun))
3623     {
3624       operands[1] = force_reg (<MODE>mode, operands[1]);
3625       emit_insn (gen_zero_extend<mode>si2_and (operands[0], operands[1]));
3626       DONE;
3627     }
3628 })
3629
3630 (define_insn_and_split "zero_extend<mode>si2_and"
3631   [(set (match_operand:SI 0 "register_operand" "=r,?&<r>")
3632         (zero_extend:SI
3633           (match_operand:SWI12 1 "nonimmediate_operand" "0,<r>m")))
3634    (clobber (reg:CC FLAGS_REG))]
3635   "TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun)"
3636   "#"
3637   "&& reload_completed"
3638   [(parallel [(set (match_dup 0) (and:SI (match_dup 0) (match_dup 2)))
3639               (clobber (reg:CC FLAGS_REG))])]
3640 {
3641   if (true_regnum (operands[0]) != true_regnum (operands[1]))
3642     {
3643       ix86_expand_clear (operands[0]);
3644
3645       gcc_assert (!TARGET_PARTIAL_REG_STALL);
3646       emit_insn (gen_movstrict<mode>
3647                   (gen_lowpart (<MODE>mode, operands[0]), operands[1]));
3648       DONE;
3649     }
3650
3651   operands[2] = GEN_INT (GET_MODE_MASK (<MODE>mode));
3652 }
3653   [(set_attr "type" "alu1")
3654    (set_attr "mode" "SI")])
3655
3656 (define_insn "*zero_extend<mode>si2"
3657   [(set (match_operand:SI 0 "register_operand" "=r")
3658         (zero_extend:SI
3659           (match_operand:SWI12 1 "nonimmediate_operand" "<r>m")))]
3660   "!(TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun))"
3661   "movz{<imodesuffix>l|x}\t{%1, %0|%0, %1}"
3662   [(set_attr "type" "imovx")
3663    (set_attr "mode" "SI")])
3664
3665 (define_expand "zero_extendqihi2"
3666   [(set (match_operand:HI 0 "register_operand")
3667         (zero_extend:HI (match_operand:QI 1 "nonimmediate_operand")))]
3668   ""
3669 {
3670   if (TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun))
3671     {
3672       operands[1] = force_reg (QImode, operands[1]);
3673       emit_insn (gen_zero_extendqihi2_and (operands[0], operands[1]));
3674       DONE;
3675     }
3676 })
3677
3678 (define_insn_and_split "zero_extendqihi2_and"
3679   [(set (match_operand:HI 0 "register_operand" "=r,?&q")
3680         (zero_extend:HI (match_operand:QI 1 "nonimmediate_operand" "0,qm")))
3681    (clobber (reg:CC FLAGS_REG))]
3682   "TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun)"
3683   "#"
3684   "&& reload_completed"
3685   [(parallel [(set (match_dup 0) (and:SI (match_dup 0) (const_int 255)))
3686               (clobber (reg:CC FLAGS_REG))])]
3687 {
3688   if (true_regnum (operands[0]) != true_regnum (operands[1]))
3689     {
3690       ix86_expand_clear (operands[0]);
3691
3692       gcc_assert (!TARGET_PARTIAL_REG_STALL);
3693       emit_insn (gen_movstrictqi
3694                   (gen_lowpart (QImode, operands[0]), operands[1]));
3695       DONE;
3696     }
3697
3698   operands[0] = gen_lowpart (SImode, operands[0]);
3699 }
3700   [(set_attr "type" "alu1")
3701    (set_attr "mode" "SI")])
3702
3703 ; zero extend to SImode to avoid partial register stalls
3704 (define_insn "*zero_extendqihi2"
3705   [(set (match_operand:HI 0 "register_operand" "=r")
3706         (zero_extend:HI (match_operand:QI 1 "nonimmediate_operand" "qm")))]
3707   "!(TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun))"
3708   "movz{bl|x}\t{%1, %k0|%k0, %1}"
3709   [(set_attr "type" "imovx")
3710    (set_attr "mode" "SI")])
3711 \f
3712 ;; Sign extension instructions
3713
3714 (define_expand "extendsidi2"
3715   [(set (match_operand:DI 0 "register_operand")
3716         (sign_extend:DI (match_operand:SI 1 "register_operand")))]
3717   ""
3718 {
3719   if (!TARGET_64BIT)
3720     {
3721       emit_insn (gen_extendsidi2_1 (operands[0], operands[1]));
3722       DONE;
3723     }
3724 })
3725
3726 (define_insn "*extendsidi2_rex64"
3727   [(set (match_operand:DI 0 "register_operand" "=*a,r")
3728         (sign_extend:DI (match_operand:SI 1 "nonimmediate_operand" "*0,rm")))]
3729   "TARGET_64BIT"
3730   "@
3731    {cltq|cdqe}
3732    movs{lq|x}\t{%1, %0|%0, %1}"
3733   [(set_attr "type" "imovx")
3734    (set_attr "mode" "DI")
3735    (set_attr "prefix_0f" "0")
3736    (set_attr "modrm" "0,1")])
3737
3738 (define_insn "extendsidi2_1"
3739   [(set (match_operand:DI 0 "nonimmediate_operand" "=*A,r,?r,?*o")
3740         (sign_extend:DI (match_operand:SI 1 "register_operand" "0,0,r,r")))
3741    (clobber (reg:CC FLAGS_REG))
3742    (clobber (match_scratch:SI 2 "=X,X,X,&r"))]
3743   "!TARGET_64BIT"
3744   "#")
3745
3746 ;; Split the memory case.  If the source register doesn't die, it will stay
3747 ;; this way, if it does die, following peephole2s take care of it.
3748 (define_split
3749   [(set (match_operand:DI 0 "memory_operand")
3750         (sign_extend:DI (match_operand:SI 1 "register_operand")))
3751    (clobber (reg:CC FLAGS_REG))
3752    (clobber (match_operand:SI 2 "register_operand"))]
3753   "reload_completed"
3754   [(const_int 0)]
3755 {
3756   split_double_mode (DImode, &operands[0], 1, &operands[3], &operands[4]);
3757
3758   emit_move_insn (operands[3], operands[1]);
3759
3760   /* Generate a cltd if possible and doing so it profitable.  */
3761   if ((optimize_function_for_size_p (cfun) || TARGET_USE_CLTD)
3762       && true_regnum (operands[1]) == AX_REG
3763       && true_regnum (operands[2]) == DX_REG)
3764     {
3765       emit_insn (gen_ashrsi3_cvt (operands[2], operands[1], GEN_INT (31)));
3766     }
3767   else
3768     {
3769       emit_move_insn (operands[2], operands[1]);
3770       emit_insn (gen_ashrsi3_cvt (operands[2], operands[2], GEN_INT (31)));
3771     }
3772   emit_move_insn (operands[4], operands[2]);
3773   DONE;
3774 })
3775
3776 ;; Peepholes for the case where the source register does die, after
3777 ;; being split with the above splitter.
3778 (define_peephole2
3779   [(set (match_operand:SI 0 "memory_operand")
3780         (match_operand:SI 1 "register_operand"))
3781    (set (match_operand:SI 2 "register_operand") (match_dup 1))
3782    (parallel [(set (match_dup 2)
3783                    (ashiftrt:SI (match_dup 2) (const_int 31)))
3784                (clobber (reg:CC FLAGS_REG))])
3785    (set (match_operand:SI 3 "memory_operand") (match_dup 2))]
3786   "REGNO (operands[1]) != REGNO (operands[2])
3787    && peep2_reg_dead_p (2, operands[1])
3788    && peep2_reg_dead_p (4, operands[2])
3789    && !reg_mentioned_p (operands[2], operands[3])"
3790   [(set (match_dup 0) (match_dup 1))
3791    (parallel [(set (match_dup 1) (ashiftrt:SI (match_dup 1) (const_int 31)))
3792               (clobber (reg:CC FLAGS_REG))])
3793    (set (match_dup 3) (match_dup 1))])
3794
3795 (define_peephole2
3796   [(set (match_operand:SI 0 "memory_operand")
3797         (match_operand:SI 1 "register_operand"))
3798    (parallel [(set (match_operand:SI 2 "register_operand")
3799                    (ashiftrt:SI (match_dup 1) (const_int 31)))
3800                (clobber (reg:CC FLAGS_REG))])
3801    (set (match_operand:SI 3 "memory_operand") (match_dup 2))]
3802   "/* cltd is shorter than sarl $31, %eax */
3803    !optimize_function_for_size_p (cfun)
3804    && true_regnum (operands[1]) == AX_REG
3805    && true_regnum (operands[2]) == DX_REG
3806    && peep2_reg_dead_p (2, operands[1])
3807    && peep2_reg_dead_p (3, operands[2])
3808    && !reg_mentioned_p (operands[2], operands[3])"
3809   [(set (match_dup 0) (match_dup 1))
3810    (parallel [(set (match_dup 1) (ashiftrt:SI (match_dup 1) (const_int 31)))
3811               (clobber (reg:CC FLAGS_REG))])
3812    (set (match_dup 3) (match_dup 1))])
3813
3814 ;; Extend to register case.  Optimize case where source and destination
3815 ;; registers match and cases where we can use cltd.
3816 (define_split
3817   [(set (match_operand:DI 0 "register_operand")
3818         (sign_extend:DI (match_operand:SI 1 "register_operand")))
3819    (clobber (reg:CC FLAGS_REG))
3820    (clobber (match_scratch:SI 2))]
3821   "reload_completed"
3822   [(const_int 0)]
3823 {
3824   split_double_mode (DImode, &operands[0], 1, &operands[3], &operands[4]);
3825
3826   if (true_regnum (operands[3]) != true_regnum (operands[1]))
3827     emit_move_insn (operands[3], operands[1]);
3828
3829   /* Generate a cltd if possible and doing so it profitable.  */
3830   if ((optimize_function_for_size_p (cfun) || TARGET_USE_CLTD)
3831       && true_regnum (operands[3]) == AX_REG
3832       && true_regnum (operands[4]) == DX_REG)
3833     {
3834       emit_insn (gen_ashrsi3_cvt (operands[4], operands[3], GEN_INT (31)));
3835       DONE;
3836     }
3837
3838   if (true_regnum (operands[4]) != true_regnum (operands[1]))
3839     emit_move_insn (operands[4], operands[1]);
3840
3841   emit_insn (gen_ashrsi3_cvt (operands[4], operands[4], GEN_INT (31)));
3842   DONE;
3843 })
3844
3845 (define_insn "extend<mode>di2"
3846   [(set (match_operand:DI 0 "register_operand" "=r")
3847         (sign_extend:DI
3848          (match_operand:SWI12 1 "nonimmediate_operand" "<r>m")))]
3849   "TARGET_64BIT"
3850   "movs{<imodesuffix>q|x}\t{%1, %0|%0, %1}"
3851   [(set_attr "type" "imovx")
3852    (set_attr "mode" "DI")])
3853
3854 (define_insn "extendhisi2"
3855   [(set (match_operand:SI 0 "register_operand" "=*a,r")
3856         (sign_extend:SI (match_operand:HI 1 "nonimmediate_operand" "*0,rm")))]
3857   ""
3858 {
3859   switch (get_attr_prefix_0f (insn))
3860     {
3861     case 0:
3862       return "{cwtl|cwde}";
3863     default:
3864       return "movs{wl|x}\t{%1, %0|%0, %1}";
3865     }
3866 }
3867   [(set_attr "type" "imovx")
3868    (set_attr "mode" "SI")
3869    (set (attr "prefix_0f")
3870      ;; movsx is short decodable while cwtl is vector decoded.
3871      (if_then_else (and (eq_attr "cpu" "!k6")
3872                         (eq_attr "alternative" "0"))
3873         (const_string "0")
3874         (const_string "1")))
3875    (set (attr "modrm")
3876      (if_then_else (eq_attr "prefix_0f" "0")
3877         (const_string "0")
3878         (const_string "1")))])
3879
3880 (define_insn "*extendhisi2_zext"
3881   [(set (match_operand:DI 0 "register_operand" "=*a,r")
3882         (zero_extend:DI
3883          (sign_extend:SI
3884           (match_operand:HI 1 "nonimmediate_operand" "*0,rm"))))]
3885   "TARGET_64BIT"
3886 {
3887   switch (get_attr_prefix_0f (insn))
3888     {
3889     case 0:
3890       return "{cwtl|cwde}";
3891     default:
3892       return "movs{wl|x}\t{%1, %k0|%k0, %1}";
3893     }
3894 }
3895   [(set_attr "type" "imovx")
3896    (set_attr "mode" "SI")
3897    (set (attr "prefix_0f")
3898      ;; movsx is short decodable while cwtl is vector decoded.
3899      (if_then_else (and (eq_attr "cpu" "!k6")
3900                         (eq_attr "alternative" "0"))
3901         (const_string "0")
3902         (const_string "1")))
3903    (set (attr "modrm")
3904      (if_then_else (eq_attr "prefix_0f" "0")
3905         (const_string "0")
3906         (const_string "1")))])
3907
3908 (define_insn "extendqisi2"
3909   [(set (match_operand:SI 0 "register_operand" "=r")
3910         (sign_extend:SI (match_operand:QI 1 "nonimmediate_operand" "qm")))]
3911   ""
3912   "movs{bl|x}\t{%1, %0|%0, %1}"
3913    [(set_attr "type" "imovx")
3914     (set_attr "mode" "SI")])
3915
3916 (define_insn "*extendqisi2_zext"
3917   [(set (match_operand:DI 0 "register_operand" "=r")
3918         (zero_extend:DI
3919           (sign_extend:SI (match_operand:QI 1 "nonimmediate_operand" "qm"))))]
3920   "TARGET_64BIT"
3921   "movs{bl|x}\t{%1, %k0|%k0, %1}"
3922    [(set_attr "type" "imovx")
3923     (set_attr "mode" "SI")])
3924
3925 (define_insn "extendqihi2"
3926   [(set (match_operand:HI 0 "register_operand" "=*a,r")
3927         (sign_extend:HI (match_operand:QI 1 "nonimmediate_operand" "*0,qm")))]
3928   ""
3929 {
3930   switch (get_attr_prefix_0f (insn))
3931     {
3932     case 0:
3933       return "{cbtw|cbw}";
3934     default:
3935       return "movs{bw|x}\t{%1, %0|%0, %1}";
3936     }
3937 }
3938   [(set_attr "type" "imovx")
3939    (set_attr "mode" "HI")
3940    (set (attr "prefix_0f")
3941      ;; movsx is short decodable while cwtl is vector decoded.
3942      (if_then_else (and (eq_attr "cpu" "!k6")
3943                         (eq_attr "alternative" "0"))
3944         (const_string "0")
3945         (const_string "1")))
3946    (set (attr "modrm")
3947      (if_then_else (eq_attr "prefix_0f" "0")
3948         (const_string "0")
3949         (const_string "1")))])
3950 \f
3951 ;; Conversions between float and double.
3952
3953 ;; These are all no-ops in the model used for the 80387.
3954 ;; So just emit moves.
3955
3956 ;; %%% Kill these when call knows how to work out a DFmode push earlier.
3957 (define_split
3958   [(set (match_operand:DF 0 "push_operand")
3959         (float_extend:DF (match_operand:SF 1 "fp_register_operand")))]
3960   "reload_completed"
3961   [(set (reg:P SP_REG) (plus:P (reg:P SP_REG) (const_int -8)))
3962    (set (mem:DF (reg:P SP_REG)) (float_extend:DF (match_dup 1)))])
3963
3964 (define_split
3965   [(set (match_operand:XF 0 "push_operand")
3966         (float_extend:XF (match_operand:MODEF 1 "fp_register_operand")))]
3967   "reload_completed"
3968   [(set (reg:P SP_REG) (plus:P (reg:P SP_REG) (match_dup 2)))
3969    (set (mem:XF (reg:P SP_REG)) (float_extend:XF (match_dup 1)))]
3970   "operands[2] = GEN_INT (-GET_MODE_SIZE (XFmode));")
3971
3972 (define_expand "extendsfdf2"
3973   [(set (match_operand:DF 0 "nonimmediate_operand")
3974         (float_extend:DF (match_operand:SF 1 "general_operand")))]
3975   "TARGET_80387 || (TARGET_SSE2 && TARGET_SSE_MATH)"
3976 {
3977   /* ??? Needed for compress_float_constant since all fp constants
3978      are TARGET_LEGITIMATE_CONSTANT_P.  */
3979   if (GET_CODE (operands[1]) == CONST_DOUBLE)
3980     {
3981       if ((!TARGET_SSE2 || TARGET_MIX_SSE_I387)
3982           && standard_80387_constant_p (operands[1]) > 0)
3983         {
3984           operands[1] = simplify_const_unary_operation
3985             (FLOAT_EXTEND, DFmode, operands[1], SFmode);
3986           emit_move_insn_1 (operands[0], operands[1]);
3987           DONE;
3988         }
3989       operands[1] = validize_mem (force_const_mem (SFmode, operands[1]));
3990     }
3991 })
3992
3993 /* For converting SF(xmm2) to DF(xmm1), use the following code instead of
3994    cvtss2sd:
3995       unpcklps xmm2,xmm2   ; packed conversion might crash on signaling NaNs
3996       cvtps2pd xmm2,xmm1
3997    We do the conversion post reload to avoid producing of 128bit spills
3998    that might lead to ICE on 32bit target.  The sequence unlikely combine
3999    anyway.  */
4000 (define_split
4001   [(set (match_operand:DF 0 "register_operand")
4002         (float_extend:DF
4003           (match_operand:SF 1 "nonimmediate_operand")))]
4004   "TARGET_USE_VECTOR_FP_CONVERTS
4005    && optimize_insn_for_speed_p ()
4006    && reload_completed && SSE_REG_P (operands[0])"
4007    [(set (match_dup 2)
4008          (float_extend:V2DF
4009            (vec_select:V2SF
4010              (match_dup 3)
4011              (parallel [(const_int 0) (const_int 1)]))))]
4012 {
4013   operands[2] = simplify_gen_subreg (V2DFmode, operands[0], DFmode, 0);
4014   operands[3] = simplify_gen_subreg (V4SFmode, operands[0], DFmode, 0);
4015   /* Use movss for loading from memory, unpcklps reg, reg for registers.
4016      Try to avoid move when unpacking can be done in source.  */
4017   if (REG_P (operands[1]))
4018     {
4019       /* If it is unsafe to overwrite upper half of source, we need
4020          to move to destination and unpack there.  */
4021       if ((ORIGINAL_REGNO (operands[1]) < FIRST_PSEUDO_REGISTER
4022            || PSEUDO_REGNO_BYTES (ORIGINAL_REGNO (operands[1])) > 4)
4023           && true_regnum (operands[0]) != true_regnum (operands[1]))
4024         {
4025           rtx tmp = gen_rtx_REG (SFmode, true_regnum (operands[0]));
4026           emit_move_insn (tmp, operands[1]);
4027         }
4028       else
4029         operands[3] = simplify_gen_subreg (V4SFmode, operands[1], SFmode, 0);
4030       emit_insn (gen_vec_interleave_lowv4sf (operands[3], operands[3],
4031                                              operands[3]));
4032     }
4033   else
4034     emit_insn (gen_vec_setv4sf_0 (operands[3],
4035                                   CONST0_RTX (V4SFmode), operands[1]));
4036 })
4037
4038 ;; It's more profitable to split and then extend in the same register.
4039 (define_peephole2
4040   [(set (match_operand:DF 0 "register_operand")
4041         (float_extend:DF
4042           (match_operand:SF 1 "memory_operand")))]
4043   "TARGET_SPLIT_MEM_OPND_FOR_FP_CONVERTS
4044    && optimize_insn_for_speed_p ()
4045    && SSE_REG_P (operands[0])"
4046   [(set (match_dup 2) (match_dup 1))
4047    (set (match_dup 0) (float_extend:DF (match_dup 2)))]
4048   "operands[2] = gen_rtx_REG (SFmode, REGNO (operands[0]));")
4049
4050 (define_insn "*extendsfdf2_mixed"
4051   [(set (match_operand:DF 0 "nonimmediate_operand" "=f,m,x")
4052         (float_extend:DF
4053           (match_operand:SF 1 "nonimmediate_operand" "fm,f,xm")))]
4054   "TARGET_SSE2 && TARGET_MIX_SSE_I387"
4055 {
4056   switch (which_alternative)
4057     {
4058     case 0:
4059     case 1:
4060       return output_387_reg_move (insn, operands);
4061
4062     case 2:
4063       return "%vcvtss2sd\t{%1, %d0|%d0, %1}";
4064
4065     default:
4066       gcc_unreachable ();
4067     }
4068 }
4069   [(set_attr "type" "fmov,fmov,ssecvt")
4070    (set_attr "prefix" "orig,orig,maybe_vex")
4071    (set_attr "mode" "SF,XF,DF")])
4072
4073 (define_insn "*extendsfdf2_sse"
4074   [(set (match_operand:DF 0 "nonimmediate_operand" "=x")
4075         (float_extend:DF (match_operand:SF 1 "nonimmediate_operand" "xm")))]
4076   "TARGET_SSE2 && TARGET_SSE_MATH"
4077   "%vcvtss2sd\t{%1, %d0|%d0, %1}"
4078   [(set_attr "type" "ssecvt")
4079    (set_attr "prefix" "maybe_vex")
4080    (set_attr "mode" "DF")])
4081
4082 (define_insn "*extendsfdf2_i387"
4083   [(set (match_operand:DF 0 "nonimmediate_operand" "=f,m")
4084         (float_extend:DF (match_operand:SF 1 "nonimmediate_operand" "fm,f")))]
4085   "TARGET_80387"
4086   "* return output_387_reg_move (insn, operands);"
4087   [(set_attr "type" "fmov")
4088    (set_attr "mode" "SF,XF")])
4089
4090 (define_expand "extend<mode>xf2"
4091   [(set (match_operand:XF 0 "nonimmediate_operand")
4092         (float_extend:XF (match_operand:MODEF 1 "general_operand")))]
4093   "TARGET_80387"
4094 {
4095   /* ??? Needed for compress_float_constant since all fp constants
4096      are TARGET_LEGITIMATE_CONSTANT_P.  */
4097   if (GET_CODE (operands[1]) == CONST_DOUBLE)
4098     {
4099       if (standard_80387_constant_p (operands[1]) > 0)
4100         {
4101           operands[1] = simplify_const_unary_operation
4102             (FLOAT_EXTEND, XFmode, operands[1], <MODE>mode);
4103           emit_move_insn_1 (operands[0], operands[1]);
4104           DONE;
4105         }
4106       operands[1] = validize_mem (force_const_mem (<MODE>mode, operands[1]));
4107     }
4108 })
4109
4110 (define_insn "*extend<mode>xf2_i387"
4111   [(set (match_operand:XF 0 "nonimmediate_operand" "=f,m")
4112         (float_extend:XF
4113           (match_operand:MODEF 1 "nonimmediate_operand" "fm,f")))]
4114   "TARGET_80387"
4115   "* return output_387_reg_move (insn, operands);"
4116   [(set_attr "type" "fmov")
4117    (set_attr "mode" "<MODE>,XF")])
4118
4119 ;; %%% This seems bad bad news.
4120 ;; This cannot output into an f-reg because there is no way to be sure
4121 ;; of truncating in that case.  Otherwise this is just like a simple move
4122 ;; insn.  So we pretend we can output to a reg in order to get better
4123 ;; register preferencing, but we really use a stack slot.
4124
4125 ;; Conversion from DFmode to SFmode.
4126
4127 (define_expand "truncdfsf2"
4128   [(set (match_operand:SF 0 "nonimmediate_operand")
4129         (float_truncate:SF
4130           (match_operand:DF 1 "nonimmediate_operand")))]
4131   "TARGET_80387 || (TARGET_SSE2 && TARGET_SSE_MATH)"
4132 {
4133   if (TARGET_SSE2 && TARGET_SSE_MATH && !TARGET_MIX_SSE_I387)
4134     ;
4135   else if (flag_unsafe_math_optimizations)
4136     ;
4137   else
4138     {
4139       rtx temp = assign_386_stack_local (SFmode, SLOT_TEMP);
4140       emit_insn (gen_truncdfsf2_with_temp (operands[0], operands[1], temp));
4141       DONE;
4142     }
4143 })
4144
4145 /* For converting DF(xmm2) to SF(xmm1), use the following code instead of
4146    cvtsd2ss:
4147       unpcklpd xmm2,xmm2   ; packed conversion might crash on signaling NaNs
4148       cvtpd2ps xmm2,xmm1
4149    We do the conversion post reload to avoid producing of 128bit spills
4150    that might lead to ICE on 32bit target.  The sequence unlikely combine
4151    anyway.  */
4152 (define_split
4153   [(set (match_operand:SF 0 "register_operand")
4154         (float_truncate:SF
4155           (match_operand:DF 1 "nonimmediate_operand")))]
4156   "TARGET_USE_VECTOR_FP_CONVERTS
4157    && optimize_insn_for_speed_p ()
4158    && reload_completed && SSE_REG_P (operands[0])"
4159    [(set (match_dup 2)
4160          (vec_concat:V4SF
4161            (float_truncate:V2SF
4162              (match_dup 4))
4163            (match_dup 3)))]
4164 {
4165   operands[2] = simplify_gen_subreg (V4SFmode, operands[0], SFmode, 0);
4166   operands[3] = CONST0_RTX (V2SFmode);
4167   operands[4] = simplify_gen_subreg (V2DFmode, operands[0], SFmode, 0);
4168   /* Use movsd for loading from memory, unpcklpd for registers.
4169      Try to avoid move when unpacking can be done in source, or SSE3
4170      movddup is available.  */
4171   if (REG_P (operands[1]))
4172     {
4173       if (!TARGET_SSE3
4174           && true_regnum (operands[0]) != true_regnum (operands[1])
4175           && (ORIGINAL_REGNO (operands[1]) < FIRST_PSEUDO_REGISTER
4176               || PSEUDO_REGNO_BYTES (ORIGINAL_REGNO (operands[1])) > 8))
4177         {
4178           rtx tmp = simplify_gen_subreg (DFmode, operands[0], SFmode, 0);
4179           emit_move_insn (tmp, operands[1]);
4180           operands[1] = tmp;
4181         }
4182       else if (!TARGET_SSE3)
4183         operands[4] = simplify_gen_subreg (V2DFmode, operands[1], DFmode, 0);
4184       emit_insn (gen_vec_dupv2df (operands[4], operands[1]));
4185     }
4186   else
4187     emit_insn (gen_sse2_loadlpd (operands[4],
4188                                  CONST0_RTX (V2DFmode), operands[1]));
4189 })
4190
4191 ;; It's more profitable to split and then extend in the same register.
4192 (define_peephole2
4193   [(set (match_operand:SF 0 "register_operand")
4194         (float_truncate:SF
4195           (match_operand:DF 1 "memory_operand")))]
4196   "TARGET_SPLIT_MEM_OPND_FOR_FP_CONVERTS
4197    && optimize_insn_for_speed_p ()
4198    && SSE_REG_P (operands[0])"
4199   [(set (match_dup 2) (match_dup 1))
4200    (set (match_dup 0) (float_truncate:SF (match_dup 2)))]
4201   "operands[2] = gen_rtx_REG (DFmode, REGNO (operands[0]));")
4202
4203 (define_expand "truncdfsf2_with_temp"
4204   [(parallel [(set (match_operand:SF 0)
4205                    (float_truncate:SF (match_operand:DF 1)))
4206               (clobber (match_operand:SF 2))])])
4207
4208 (define_insn "*truncdfsf_fast_mixed"
4209   [(set (match_operand:SF 0 "nonimmediate_operand"   "=fm,x")
4210         (float_truncate:SF
4211           (match_operand:DF 1 "nonimmediate_operand" "f  ,xm")))]
4212   "TARGET_SSE2 && TARGET_MIX_SSE_I387 && flag_unsafe_math_optimizations"
4213 {
4214   switch (which_alternative)
4215     {
4216     case 0:
4217       return output_387_reg_move (insn, operands);
4218     case 1:
4219       return "%vcvtsd2ss\t{%1, %d0|%d0, %1}";
4220     default:
4221       gcc_unreachable ();
4222     }
4223 }
4224   [(set_attr "type" "fmov,ssecvt")
4225    (set_attr "prefix" "orig,maybe_vex")
4226    (set_attr "mode" "SF")])
4227
4228 ;; Yes, this one doesn't depend on flag_unsafe_math_optimizations,
4229 ;; because nothing we do here is unsafe.
4230 (define_insn "*truncdfsf_fast_sse"
4231   [(set (match_operand:SF 0 "nonimmediate_operand"   "=x")
4232         (float_truncate:SF
4233           (match_operand:DF 1 "nonimmediate_operand" "xm")))]
4234   "TARGET_SSE2 && TARGET_SSE_MATH"
4235   "%vcvtsd2ss\t{%1, %d0|%d0, %1}"
4236   [(set_attr "type" "ssecvt")
4237    (set_attr "prefix" "maybe_vex")
4238    (set_attr "mode" "SF")])
4239
4240 (define_insn "*truncdfsf_fast_i387"
4241   [(set (match_operand:SF 0 "nonimmediate_operand"   "=fm")
4242         (float_truncate:SF
4243           (match_operand:DF 1 "nonimmediate_operand" "f")))]
4244   "TARGET_80387 && flag_unsafe_math_optimizations"
4245   "* return output_387_reg_move (insn, operands);"
4246   [(set_attr "type" "fmov")
4247    (set_attr "mode" "SF")])
4248
4249 (define_insn "*truncdfsf_mixed"
4250   [(set (match_operand:SF 0 "nonimmediate_operand"   "=m,x ,?f,?x,?*r")
4251         (float_truncate:SF
4252           (match_operand:DF 1 "nonimmediate_operand" "f ,xm,f ,f ,f")))
4253    (clobber (match_operand:SF 2 "memory_operand"     "=X,X ,m ,m ,m"))]
4254   "TARGET_MIX_SSE_I387"
4255 {
4256   switch (which_alternative)
4257     {
4258     case 0:
4259       return output_387_reg_move (insn, operands);
4260     case 1:
4261       return "%vcvtsd2ss\t{%1, %d0|%d0, %1}";
4262
4263     default:
4264       return "#";
4265     }
4266 }
4267   [(set_attr "isa" "*,sse2,*,*,*")
4268    (set_attr "type" "fmov,ssecvt,multi,multi,multi")
4269    (set_attr "unit" "*,*,i387,i387,i387")
4270    (set_attr "prefix" "orig,maybe_vex,orig,orig,orig")
4271    (set_attr "mode" "SF")])
4272
4273 (define_insn "*truncdfsf_i387"
4274   [(set (match_operand:SF 0 "nonimmediate_operand"   "=m,?f,?x,?*r")
4275         (float_truncate:SF
4276           (match_operand:DF 1 "nonimmediate_operand" "f ,f ,f ,f")))
4277    (clobber (match_operand:SF 2 "memory_operand"     "=X,m ,m ,m"))]
4278   "TARGET_80387"
4279 {
4280   switch (which_alternative)
4281     {
4282     case 0:
4283       return output_387_reg_move (insn, operands);
4284
4285     default:
4286       return "#";
4287     }
4288 }
4289   [(set_attr "type" "fmov,multi,multi,multi")
4290    (set_attr "unit" "*,i387,i387,i387")
4291    (set_attr "mode" "SF")])
4292
4293 (define_insn "*truncdfsf2_i387_1"
4294   [(set (match_operand:SF 0 "memory_operand" "=m")
4295         (float_truncate:SF
4296           (match_operand:DF 1 "register_operand" "f")))]
4297   "TARGET_80387
4298    && !(TARGET_SSE2 && TARGET_SSE_MATH)
4299    && !TARGET_MIX_SSE_I387"
4300   "* return output_387_reg_move (insn, operands);"
4301   [(set_attr "type" "fmov")
4302    (set_attr "mode" "SF")])
4303
4304 (define_split
4305   [(set (match_operand:SF 0 "register_operand")
4306         (float_truncate:SF
4307          (match_operand:DF 1 "fp_register_operand")))
4308    (clobber (match_operand 2))]
4309   "reload_completed"
4310   [(set (match_dup 2) (match_dup 1))
4311    (set (match_dup 0) (match_dup 2))]
4312   "operands[1] = gen_rtx_REG (SFmode, true_regnum (operands[1]));")
4313
4314 ;; Conversion from XFmode to {SF,DF}mode
4315
4316 (define_expand "truncxf<mode>2"
4317   [(parallel [(set (match_operand:MODEF 0 "nonimmediate_operand")
4318                    (float_truncate:MODEF
4319                      (match_operand:XF 1 "register_operand")))
4320               (clobber (match_dup 2))])]
4321   "TARGET_80387"
4322 {
4323   if (flag_unsafe_math_optimizations)
4324     {
4325       rtx reg = REG_P (operands[0]) ? operands[0] : gen_reg_rtx (<MODE>mode);
4326       emit_insn (gen_truncxf<mode>2_i387_noop (reg, operands[1]));
4327       if (reg != operands[0])
4328         emit_move_insn (operands[0], reg);
4329       DONE;
4330     }
4331   else
4332     operands[2] = assign_386_stack_local (<MODE>mode, SLOT_TEMP);
4333 })
4334
4335 (define_insn "*truncxfsf2_mixed"
4336   [(set (match_operand:SF 0 "nonimmediate_operand" "=m,?f,?x,?*r")
4337         (float_truncate:SF
4338           (match_operand:XF 1 "register_operand"   "f ,f ,f ,f")))
4339    (clobber (match_operand:SF 2 "memory_operand"   "=X,m ,m ,m"))]
4340   "TARGET_80387"
4341 {
4342   gcc_assert (!which_alternative);
4343   return output_387_reg_move (insn, operands);
4344 }
4345   [(set_attr "type" "fmov,multi,multi,multi")
4346    (set_attr "unit" "*,i387,i387,i387")
4347    (set_attr "mode" "SF")])
4348
4349 (define_insn "*truncxfdf2_mixed"
4350   [(set (match_operand:DF 0 "nonimmediate_operand" "=m,?f,?x,?*r")
4351         (float_truncate:DF
4352           (match_operand:XF 1 "register_operand"   "f ,f ,f  ,f")))
4353    (clobber (match_operand:DF 2 "memory_operand"   "=X,m ,m  ,m"))]
4354   "TARGET_80387"
4355 {
4356   gcc_assert (!which_alternative);
4357   return output_387_reg_move (insn, operands);
4358 }
4359   [(set_attr "isa" "*,*,sse2,*")
4360    (set_attr "type" "fmov,multi,multi,multi")
4361    (set_attr "unit" "*,i387,i387,i387")
4362    (set_attr "mode" "DF")])
4363
4364 (define_insn "truncxf<mode>2_i387_noop"
4365   [(set (match_operand:MODEF 0 "register_operand" "=f")
4366         (float_truncate:MODEF
4367           (match_operand:XF 1 "register_operand" "f")))]
4368   "TARGET_80387 && flag_unsafe_math_optimizations"
4369   "* return output_387_reg_move (insn, operands);"
4370   [(set_attr "type" "fmov")
4371    (set_attr "mode" "<MODE>")])
4372
4373 (define_insn "*truncxf<mode>2_i387"
4374   [(set (match_operand:MODEF 0 "memory_operand" "=m")
4375         (float_truncate:MODEF
4376           (match_operand:XF 1 "register_operand" "f")))]
4377   "TARGET_80387"
4378   "* return output_387_reg_move (insn, operands);"
4379   [(set_attr "type" "fmov")
4380    (set_attr "mode" "<MODE>")])
4381
4382 (define_split
4383   [(set (match_operand:MODEF 0 "register_operand")
4384         (float_truncate:MODEF
4385           (match_operand:XF 1 "register_operand")))
4386    (clobber (match_operand:MODEF 2 "memory_operand"))]
4387   "TARGET_80387 && reload_completed"
4388   [(set (match_dup 2) (float_truncate:MODEF (match_dup 1)))
4389    (set (match_dup 0) (match_dup 2))])
4390
4391 (define_split
4392   [(set (match_operand:MODEF 0 "memory_operand")
4393         (float_truncate:MODEF
4394           (match_operand:XF 1 "register_operand")))
4395    (clobber (match_operand:MODEF 2 "memory_operand"))]
4396   "TARGET_80387"
4397   [(set (match_dup 0) (float_truncate:MODEF (match_dup 1)))])
4398 \f
4399 ;; Signed conversion to DImode.
4400
4401 (define_expand "fix_truncxfdi2"
4402   [(parallel [(set (match_operand:DI 0 "nonimmediate_operand")
4403                    (fix:DI (match_operand:XF 1 "register_operand")))
4404               (clobber (reg:CC FLAGS_REG))])]
4405   "TARGET_80387"
4406 {
4407   if (TARGET_FISTTP)
4408    {
4409      emit_insn (gen_fix_truncdi_fisttp_i387_1 (operands[0], operands[1]));
4410      DONE;
4411    }
4412 })
4413
4414 (define_expand "fix_trunc<mode>di2"
4415   [(parallel [(set (match_operand:DI 0 "nonimmediate_operand")
4416                    (fix:DI (match_operand:MODEF 1 "register_operand")))
4417               (clobber (reg:CC FLAGS_REG))])]
4418   "TARGET_80387 || (TARGET_64BIT && SSE_FLOAT_MODE_P (<MODE>mode))"
4419 {
4420   if (TARGET_FISTTP
4421       && !(TARGET_64BIT && SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH))
4422    {
4423      emit_insn (gen_fix_truncdi_fisttp_i387_1 (operands[0], operands[1]));
4424      DONE;
4425    }
4426   if (TARGET_64BIT && SSE_FLOAT_MODE_P (<MODE>mode))
4427    {
4428      rtx out = REG_P (operands[0]) ? operands[0] : gen_reg_rtx (DImode);
4429      emit_insn (gen_fix_trunc<mode>di_sse (out, operands[1]));
4430      if (out != operands[0])
4431         emit_move_insn (operands[0], out);
4432      DONE;
4433    }
4434 })
4435
4436 ;; Signed conversion to SImode.
4437
4438 (define_expand "fix_truncxfsi2"
4439   [(parallel [(set (match_operand:SI 0 "nonimmediate_operand")
4440                    (fix:SI (match_operand:XF 1 "register_operand")))
4441               (clobber (reg:CC FLAGS_REG))])]
4442   "TARGET_80387"
4443 {
4444   if (TARGET_FISTTP)
4445    {
4446      emit_insn (gen_fix_truncsi_fisttp_i387_1 (operands[0], operands[1]));
4447      DONE;
4448    }
4449 })
4450
4451 (define_expand "fix_trunc<mode>si2"
4452   [(parallel [(set (match_operand:SI 0 "nonimmediate_operand")
4453                    (fix:SI (match_operand:MODEF 1 "register_operand")))
4454               (clobber (reg:CC FLAGS_REG))])]
4455   "TARGET_80387 || SSE_FLOAT_MODE_P (<MODE>mode)"
4456 {
4457   if (TARGET_FISTTP
4458       && !(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH))
4459    {
4460      emit_insn (gen_fix_truncsi_fisttp_i387_1 (operands[0], operands[1]));
4461      DONE;
4462    }
4463   if (SSE_FLOAT_MODE_P (<MODE>mode))
4464    {
4465      rtx out = REG_P (operands[0]) ? operands[0] : gen_reg_rtx (SImode);
4466      emit_insn (gen_fix_trunc<mode>si_sse (out, operands[1]));
4467      if (out != operands[0])
4468         emit_move_insn (operands[0], out);
4469      DONE;
4470    }
4471 })
4472
4473 ;; Signed conversion to HImode.
4474
4475 (define_expand "fix_trunc<mode>hi2"
4476   [(parallel [(set (match_operand:HI 0 "nonimmediate_operand")
4477                    (fix:HI (match_operand:X87MODEF 1 "register_operand")))
4478               (clobber (reg:CC FLAGS_REG))])]
4479   "TARGET_80387
4480    && !(SSE_FLOAT_MODE_P (<MODE>mode) && (!TARGET_FISTTP || TARGET_SSE_MATH))"
4481 {
4482   if (TARGET_FISTTP)
4483    {
4484      emit_insn (gen_fix_trunchi_fisttp_i387_1 (operands[0], operands[1]));
4485      DONE;
4486    }
4487 })
4488
4489 ;; Unsigned conversion to SImode.
4490
4491 (define_expand "fixuns_trunc<mode>si2"
4492   [(parallel
4493     [(set (match_operand:SI 0 "register_operand")
4494           (unsigned_fix:SI
4495             (match_operand:MODEF 1 "nonimmediate_operand")))
4496      (use (match_dup 2))
4497      (clobber (match_scratch:<ssevecmode> 3))
4498      (clobber (match_scratch:<ssevecmode> 4))])]
4499   "!TARGET_64BIT && TARGET_SSE2 && TARGET_SSE_MATH"
4500 {
4501   machine_mode mode = <MODE>mode;
4502   machine_mode vecmode = <ssevecmode>mode;
4503   REAL_VALUE_TYPE TWO31r;
4504   rtx two31;
4505
4506   if (optimize_insn_for_size_p ())
4507     FAIL;
4508
4509   real_ldexp (&TWO31r, &dconst1, 31);
4510   two31 = const_double_from_real_value (TWO31r, mode);
4511   two31 = ix86_build_const_vector (vecmode, true, two31);
4512   operands[2] = force_reg (vecmode, two31);
4513 })
4514
4515 (define_insn_and_split "*fixuns_trunc<mode>_1"
4516   [(set (match_operand:SI 0 "register_operand" "=&x,&x")
4517         (unsigned_fix:SI
4518           (match_operand:MODEF 3 "nonimmediate_operand" "xm,xm")))
4519    (use (match_operand:<ssevecmode> 4  "nonimmediate_operand" "m,x"))
4520    (clobber (match_scratch:<ssevecmode> 1 "=x,&x"))
4521    (clobber (match_scratch:<ssevecmode> 2 "=x,x"))]
4522   "!TARGET_64BIT && TARGET_SSE2 && TARGET_SSE_MATH
4523    && optimize_function_for_speed_p (cfun)"
4524   "#"
4525   "&& reload_completed"
4526   [(const_int 0)]
4527 {
4528   ix86_split_convert_uns_si_sse (operands);
4529   DONE;
4530 })
4531
4532 ;; Unsigned conversion to HImode.
4533 ;; Without these patterns, we'll try the unsigned SI conversion which
4534 ;; is complex for SSE, rather than the signed SI conversion, which isn't.
4535
4536 (define_expand "fixuns_trunc<mode>hi2"
4537   [(set (match_dup 2)
4538         (fix:SI (match_operand:MODEF 1 "nonimmediate_operand")))
4539    (set (match_operand:HI 0 "nonimmediate_operand")
4540         (subreg:HI (match_dup 2) 0))]
4541   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH"
4542   "operands[2] = gen_reg_rtx (SImode);")
4543
4544 ;; When SSE is available, it is always faster to use it!
4545 (define_insn "fix_trunc<MODEF:mode><SWI48:mode>_sse"
4546   [(set (match_operand:SWI48 0 "register_operand" "=r,r")
4547         (fix:SWI48 (match_operand:MODEF 1 "nonimmediate_operand" "x,m")))]
4548   "SSE_FLOAT_MODE_P (<MODEF:MODE>mode)
4549    && (!TARGET_FISTTP || TARGET_SSE_MATH)"
4550   "%vcvtt<MODEF:ssemodesuffix>2si<SWI48:rex64suffix>\t{%1, %0|%0, %1}"
4551   [(set_attr "type" "sseicvt")
4552    (set_attr "prefix" "maybe_vex")
4553    (set (attr "prefix_rex")
4554         (if_then_else
4555           (match_test "<SWI48:MODE>mode == DImode")
4556           (const_string "1")
4557           (const_string "*")))
4558    (set_attr "mode" "<MODEF:MODE>")
4559    (set_attr "athlon_decode" "double,vector")
4560    (set_attr "amdfam10_decode" "double,double")
4561    (set_attr "bdver1_decode" "double,double")])
4562
4563 ;; Avoid vector decoded forms of the instruction.
4564 (define_peephole2
4565   [(match_scratch:MODEF 2 "x")
4566    (set (match_operand:SWI48 0 "register_operand")
4567         (fix:SWI48 (match_operand:MODEF 1 "memory_operand")))]
4568   "TARGET_AVOID_VECTOR_DECODE
4569    && SSE_FLOAT_MODE_P (<MODEF:MODE>mode)
4570    && optimize_insn_for_speed_p ()"
4571   [(set (match_dup 2) (match_dup 1))
4572    (set (match_dup 0) (fix:SWI48 (match_dup 2)))])
4573
4574 (define_insn_and_split "fix_trunc<mode>_fisttp_i387_1"
4575   [(set (match_operand:SWI248x 0 "nonimmediate_operand")
4576         (fix:SWI248x (match_operand 1 "register_operand")))]
4577   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4578    && TARGET_FISTTP
4579    && !((SSE_FLOAT_MODE_P (GET_MODE (operands[1]))
4580          && (TARGET_64BIT || <MODE>mode != DImode))
4581         && TARGET_SSE_MATH)
4582    && can_create_pseudo_p ()"
4583   "#"
4584   "&& 1"
4585   [(const_int 0)]
4586 {
4587   if (memory_operand (operands[0], VOIDmode))
4588     emit_insn (gen_fix_trunc<mode>_i387_fisttp (operands[0], operands[1]));
4589   else
4590     {
4591       operands[2] = assign_386_stack_local (<MODE>mode, SLOT_TEMP);
4592       emit_insn (gen_fix_trunc<mode>_i387_fisttp_with_temp (operands[0],
4593                                                             operands[1],
4594                                                             operands[2]));
4595     }
4596   DONE;
4597 }
4598   [(set_attr "type" "fisttp")
4599    (set_attr "mode" "<MODE>")])
4600
4601 (define_insn "fix_trunc<mode>_i387_fisttp"
4602   [(set (match_operand:SWI248x 0 "memory_operand" "=m")
4603         (fix:SWI248x (match_operand 1 "register_operand" "f")))
4604    (clobber (match_scratch:XF 2 "=&1f"))]
4605   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4606    && TARGET_FISTTP
4607    && !((SSE_FLOAT_MODE_P (GET_MODE (operands[1]))
4608          && (TARGET_64BIT || <MODE>mode != DImode))
4609         && TARGET_SSE_MATH)"
4610   "* return output_fix_trunc (insn, operands, true);"
4611   [(set_attr "type" "fisttp")
4612    (set_attr "mode" "<MODE>")])
4613
4614 (define_insn "fix_trunc<mode>_i387_fisttp_with_temp"
4615   [(set (match_operand:SWI248x 0 "nonimmediate_operand" "=m,?r")
4616         (fix:SWI248x (match_operand 1 "register_operand" "f,f")))
4617    (clobber (match_operand:SWI248x 2 "memory_operand" "=X,m"))
4618    (clobber (match_scratch:XF 3 "=&1f,&1f"))]
4619   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4620    && TARGET_FISTTP
4621    && !((SSE_FLOAT_MODE_P (GET_MODE (operands[1]))
4622         && (TARGET_64BIT || <MODE>mode != DImode))
4623         && TARGET_SSE_MATH)"
4624   "#"
4625   [(set_attr "type" "fisttp")
4626    (set_attr "mode" "<MODE>")])
4627
4628 (define_split
4629   [(set (match_operand:SWI248x 0 "register_operand")
4630         (fix:SWI248x (match_operand 1 "register_operand")))
4631    (clobber (match_operand:SWI248x 2 "memory_operand"))
4632    (clobber (match_scratch 3))]
4633   "reload_completed"
4634   [(parallel [(set (match_dup 2) (fix:SWI248x (match_dup 1)))
4635               (clobber (match_dup 3))])
4636    (set (match_dup 0) (match_dup 2))])
4637
4638 (define_split
4639   [(set (match_operand:SWI248x 0 "memory_operand")
4640         (fix:SWI248x (match_operand 1 "register_operand")))
4641    (clobber (match_operand:SWI248x 2 "memory_operand"))
4642    (clobber (match_scratch 3))]
4643   "reload_completed"
4644   [(parallel [(set (match_dup 0) (fix:SWI248x (match_dup 1)))
4645               (clobber (match_dup 3))])])
4646
4647 ;; See the comments in i386.h near OPTIMIZE_MODE_SWITCHING for the description
4648 ;; of the machinery. Please note the clobber of FLAGS_REG. In i387 control
4649 ;; word calculation (inserted by LCM in mode switching pass) a FLAGS_REG
4650 ;; clobbering insns can be used. Look at emit_i387_cw_initialization ()
4651 ;; function in i386.c.
4652 (define_insn_and_split "*fix_trunc<mode>_i387_1"
4653   [(set (match_operand:SWI248x 0 "nonimmediate_operand")
4654         (fix:SWI248x (match_operand 1 "register_operand")))
4655    (clobber (reg:CC FLAGS_REG))]
4656   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4657    && !TARGET_FISTTP
4658    && !(SSE_FLOAT_MODE_P (GET_MODE (operands[1]))
4659          && (TARGET_64BIT || <MODE>mode != DImode))
4660    && can_create_pseudo_p ()"
4661   "#"
4662   "&& 1"
4663   [(const_int 0)]
4664 {
4665   ix86_optimize_mode_switching[I387_TRUNC] = 1;
4666
4667   operands[2] = assign_386_stack_local (HImode, SLOT_CW_STORED);
4668   operands[3] = assign_386_stack_local (HImode, SLOT_CW_TRUNC);
4669   if (memory_operand (operands[0], VOIDmode))
4670     emit_insn (gen_fix_trunc<mode>_i387 (operands[0], operands[1],
4671                                          operands[2], operands[3]));
4672   else
4673     {
4674       operands[4] = assign_386_stack_local (<MODE>mode, SLOT_TEMP);
4675       emit_insn (gen_fix_trunc<mode>_i387_with_temp (operands[0], operands[1],
4676                                                      operands[2], operands[3],
4677                                                      operands[4]));
4678     }
4679   DONE;
4680 }
4681   [(set_attr "type" "fistp")
4682    (set_attr "i387_cw" "trunc")
4683    (set_attr "mode" "<MODE>")])
4684
4685 (define_insn "fix_truncdi_i387"
4686   [(set (match_operand:DI 0 "memory_operand" "=m")
4687         (fix:DI (match_operand 1 "register_operand" "f")))
4688    (use (match_operand:HI 2 "memory_operand" "m"))
4689    (use (match_operand:HI 3 "memory_operand" "m"))
4690    (clobber (match_scratch:XF 4 "=&1f"))]
4691   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4692    && !TARGET_FISTTP
4693    && !(TARGET_64BIT && SSE_FLOAT_MODE_P (GET_MODE (operands[1])))"
4694   "* return output_fix_trunc (insn, operands, false);"
4695   [(set_attr "type" "fistp")
4696    (set_attr "i387_cw" "trunc")
4697    (set_attr "mode" "DI")])
4698
4699 (define_insn "fix_truncdi_i387_with_temp"
4700   [(set (match_operand:DI 0 "nonimmediate_operand" "=m,?r")
4701         (fix:DI (match_operand 1 "register_operand" "f,f")))
4702    (use (match_operand:HI 2 "memory_operand" "m,m"))
4703    (use (match_operand:HI 3 "memory_operand" "m,m"))
4704    (clobber (match_operand:DI 4 "memory_operand" "=X,m"))
4705    (clobber (match_scratch:XF 5 "=&1f,&1f"))]
4706   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4707    && !TARGET_FISTTP
4708    && !(TARGET_64BIT && SSE_FLOAT_MODE_P (GET_MODE (operands[1])))"
4709   "#"
4710   [(set_attr "type" "fistp")
4711    (set_attr "i387_cw" "trunc")
4712    (set_attr "mode" "DI")])
4713
4714 (define_split
4715   [(set (match_operand:DI 0 "register_operand")
4716         (fix:DI (match_operand 1 "register_operand")))
4717    (use (match_operand:HI 2 "memory_operand"))
4718    (use (match_operand:HI 3 "memory_operand"))
4719    (clobber (match_operand:DI 4 "memory_operand"))
4720    (clobber (match_scratch 5))]
4721   "reload_completed"
4722   [(parallel [(set (match_dup 4) (fix:DI (match_dup 1)))
4723               (use (match_dup 2))
4724               (use (match_dup 3))
4725               (clobber (match_dup 5))])
4726    (set (match_dup 0) (match_dup 4))])
4727
4728 (define_split
4729   [(set (match_operand:DI 0 "memory_operand")
4730         (fix:DI (match_operand 1 "register_operand")))
4731    (use (match_operand:HI 2 "memory_operand"))
4732    (use (match_operand:HI 3 "memory_operand"))
4733    (clobber (match_operand:DI 4 "memory_operand"))
4734    (clobber (match_scratch 5))]
4735   "reload_completed"
4736   [(parallel [(set (match_dup 0) (fix:DI (match_dup 1)))
4737               (use (match_dup 2))
4738               (use (match_dup 3))
4739               (clobber (match_dup 5))])])
4740
4741 (define_insn "fix_trunc<mode>_i387"
4742   [(set (match_operand:SWI24 0 "memory_operand" "=m")
4743         (fix:SWI24 (match_operand 1 "register_operand" "f")))
4744    (use (match_operand:HI 2 "memory_operand" "m"))
4745    (use (match_operand:HI 3 "memory_operand" "m"))]
4746   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4747    && !TARGET_FISTTP
4748    && !SSE_FLOAT_MODE_P (GET_MODE (operands[1]))"
4749   "* return output_fix_trunc (insn, operands, false);"
4750   [(set_attr "type" "fistp")
4751    (set_attr "i387_cw" "trunc")
4752    (set_attr "mode" "<MODE>")])
4753
4754 (define_insn "fix_trunc<mode>_i387_with_temp"
4755   [(set (match_operand:SWI24 0 "nonimmediate_operand" "=m,?r")
4756         (fix:SWI24 (match_operand 1 "register_operand" "f,f")))
4757    (use (match_operand:HI 2 "memory_operand" "m,m"))
4758    (use (match_operand:HI 3 "memory_operand" "m,m"))
4759    (clobber (match_operand:SWI24 4 "memory_operand" "=X,m"))]
4760   "X87_FLOAT_MODE_P (GET_MODE (operands[1]))
4761    && !TARGET_FISTTP
4762    && !SSE_FLOAT_MODE_P (GET_MODE (operands[1]))"
4763   "#"
4764   [(set_attr "type" "fistp")
4765    (set_attr "i387_cw" "trunc")
4766    (set_attr "mode" "<MODE>")])
4767
4768 (define_split
4769   [(set (match_operand:SWI24 0 "register_operand")
4770         (fix:SWI24 (match_operand 1 "register_operand")))
4771    (use (match_operand:HI 2 "memory_operand"))
4772    (use (match_operand:HI 3 "memory_operand"))
4773    (clobber (match_operand:SWI24 4 "memory_operand"))]
4774   "reload_completed"
4775   [(parallel [(set (match_dup 4) (fix:SWI24 (match_dup 1)))
4776               (use (match_dup 2))
4777               (use (match_dup 3))])
4778    (set (match_dup 0) (match_dup 4))])
4779
4780 (define_split
4781   [(set (match_operand:SWI24 0 "memory_operand")
4782         (fix:SWI24 (match_operand 1 "register_operand")))
4783    (use (match_operand:HI 2 "memory_operand"))
4784    (use (match_operand:HI 3 "memory_operand"))
4785    (clobber (match_operand:SWI24 4 "memory_operand"))]
4786   "reload_completed"
4787   [(parallel [(set (match_dup 0) (fix:SWI24 (match_dup 1)))
4788               (use (match_dup 2))
4789               (use (match_dup 3))])])
4790
4791 (define_insn "x86_fnstcw_1"
4792   [(set (match_operand:HI 0 "memory_operand" "=m")
4793         (unspec:HI [(reg:HI FPCR_REG)] UNSPEC_FSTCW))]
4794   "TARGET_80387"
4795   "fnstcw\t%0"
4796   [(set (attr "length")
4797         (symbol_ref "ix86_attr_length_address_default (insn) + 2"))
4798    (set_attr "mode" "HI")
4799    (set_attr "unit" "i387")
4800    (set_attr "bdver1_decode" "vector")])
4801
4802 (define_insn "x86_fldcw_1"
4803   [(set (reg:HI FPCR_REG)
4804         (unspec:HI [(match_operand:HI 0 "memory_operand" "m")] UNSPEC_FLDCW))]
4805   "TARGET_80387"
4806   "fldcw\t%0"
4807   [(set (attr "length")
4808         (symbol_ref "ix86_attr_length_address_default (insn) + 2"))
4809    (set_attr "mode" "HI")
4810    (set_attr "unit" "i387")
4811    (set_attr "athlon_decode" "vector")
4812    (set_attr "amdfam10_decode" "vector")
4813    (set_attr "bdver1_decode" "vector")])
4814 \f
4815 ;; Conversion between fixed point and floating point.
4816
4817 ;; Even though we only accept memory inputs, the backend _really_
4818 ;; wants to be able to do this between registers.  Thankfully, LRA
4819 ;; will fix this up for us during register allocation.
4820
4821 (define_insn "floathi<mode>2"
4822   [(set (match_operand:X87MODEF 0 "register_operand" "=f")
4823         (float:X87MODEF (match_operand:HI 1 "nonimmediate_operand" "m")))]
4824   "TARGET_80387
4825    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
4826        || TARGET_MIX_SSE_I387)"
4827   "fild%Z1\t%1"
4828   [(set_attr "type" "fmov")
4829    (set_attr "mode" "<MODE>")
4830    (set_attr "fp_int_src" "true")])
4831
4832 (define_insn "float<SWI48x:mode>xf2"
4833   [(set (match_operand:XF 0 "register_operand" "=f")
4834         (float:XF (match_operand:SWI48x 1 "nonimmediate_operand" "m")))]
4835   "TARGET_80387"
4836   "fild%Z1\t%1"
4837   [(set_attr "type" "fmov")
4838    (set_attr "mode" "XF")
4839    (set_attr "fp_int_src" "true")])
4840
4841 (define_expand "float<SWI48:mode><MODEF:mode>2"
4842   [(set (match_operand:MODEF 0 "register_operand")
4843         (float:MODEF (match_operand:SWI48 1 "nonimmediate_operand")))]
4844   "TARGET_80387 || (SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH)"
4845 {
4846   if (!(SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH)
4847       && !X87_ENABLE_FLOAT (<MODEF:MODE>mode, <SWI48:MODE>mode))
4848     {
4849       rtx reg = gen_reg_rtx (XFmode);
4850       rtx (*insn)(rtx, rtx);
4851
4852       emit_insn (gen_float<SWI48:mode>xf2 (reg, operands[1]));
4853
4854       if (<MODEF:MODE>mode == SFmode)
4855         insn = gen_truncxfsf2;
4856       else if (<MODEF:MODE>mode == DFmode)
4857         insn = gen_truncxfdf2;
4858       else
4859         gcc_unreachable ();
4860
4861       emit_insn (insn (operands[0], reg));
4862       DONE;
4863     }
4864 })
4865
4866 (define_insn "*float<SWI48:mode><MODEF:mode>2_sse"
4867   [(set (match_operand:MODEF 0 "register_operand" "=f,x,x")
4868         (float:MODEF
4869           (match_operand:SWI48 1 "nonimmediate_operand" "m,r,m")))]
4870   "SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH"
4871   "@
4872    fild%Z1\t%1
4873    %vcvtsi2<MODEF:ssemodesuffix><SWI48:rex64suffix>\t{%1, %d0|%d0, %1}
4874    %vcvtsi2<MODEF:ssemodesuffix><SWI48:rex64suffix>\t{%1, %d0|%d0, %1}"
4875   [(set_attr "type" "fmov,sseicvt,sseicvt")
4876    (set_attr "prefix" "orig,maybe_vex,maybe_vex")
4877    (set_attr "mode" "<MODEF:MODE>")
4878    (set (attr "prefix_rex")
4879      (if_then_else
4880        (and (eq_attr "prefix" "maybe_vex")
4881             (match_test "<SWI48:MODE>mode == DImode"))
4882        (const_string "1")
4883        (const_string "*")))
4884    (set_attr "unit" "i387,*,*")
4885    (set_attr "athlon_decode" "*,double,direct")
4886    (set_attr "amdfam10_decode" "*,vector,double")
4887    (set_attr "bdver1_decode" "*,double,direct")
4888    (set_attr "fp_int_src" "true")
4889    (set (attr "enabled")
4890      (cond [(eq_attr "alternative" "0")
4891               (symbol_ref "TARGET_MIX_SSE_I387
4892                            && X87_ENABLE_FLOAT (<MODEF:MODE>mode,
4893                                                 <SWI48:MODE>mode)")
4894            ]
4895            (symbol_ref "true")))
4896    (set (attr "preferred_for_speed")
4897      (cond [(eq_attr "alternative" "1")
4898               (symbol_ref "TARGET_INTER_UNIT_CONVERSIONS")]
4899            (symbol_ref "true")))])
4900
4901 (define_insn "*float<SWI48x:mode><MODEF:mode>2_i387"
4902   [(set (match_operand:MODEF 0 "register_operand" "=f")
4903         (float:MODEF (match_operand:SWI48x 1 "nonimmediate_operand" "m")))]
4904   "TARGET_80387 && X87_ENABLE_FLOAT (<MODEF:MODE>mode, <SWI48x:MODE>mode)"
4905   "fild%Z1\t%1"
4906   [(set_attr "type" "fmov")
4907    (set_attr "mode" "<MODEF:MODE>")
4908    (set_attr "fp_int_src" "true")])
4909
4910 ;; Try TARGET_USE_VECTOR_CONVERTS, but not so hard as to require extra memory
4911 ;; slots when !TARGET_INTER_UNIT_MOVES_TO_VEC disables the general_regs
4912 ;; alternative in sse2_loadld.
4913 (define_split
4914   [(set (match_operand:MODEF 0 "register_operand")
4915         (float:MODEF (match_operand:SI 1 "nonimmediate_operand")))]
4916   "TARGET_SSE2 && TARGET_SSE_MATH
4917    && TARGET_USE_VECTOR_CONVERTS && optimize_function_for_speed_p (cfun)
4918    && reload_completed && SSE_REG_P (operands[0])
4919    && (MEM_P (operands[1]) || TARGET_INTER_UNIT_MOVES_TO_VEC)"
4920   [(const_int 0)]
4921 {
4922   operands[3] = simplify_gen_subreg (<ssevecmode>mode, operands[0],
4923                                      <MODE>mode, 0);
4924   operands[4] = simplify_gen_subreg (V4SImode, operands[0], <MODE>mode, 0);
4925
4926   emit_insn (gen_sse2_loadld (operands[4],
4927                               CONST0_RTX (V4SImode), operands[1]));
4928
4929   if (<ssevecmode>mode == V4SFmode)
4930     emit_insn (gen_floatv4siv4sf2 (operands[3], operands[4]));
4931   else
4932     emit_insn (gen_sse2_cvtdq2pd (operands[3], operands[4]));
4933   DONE;
4934 })
4935
4936 ;; Avoid partial SSE register dependency stalls
4937 (define_split
4938   [(set (match_operand:MODEF 0 "register_operand")
4939         (float:MODEF (match_operand:SWI48 1 "nonimmediate_operand")))]
4940   "TARGET_SSE2 && TARGET_SSE_MATH
4941    && TARGET_SSE_PARTIAL_REG_DEPENDENCY
4942    && optimize_function_for_speed_p (cfun)
4943    && reload_completed && SSE_REG_P (operands[0])"
4944   [(const_int 0)]
4945 {
4946   const machine_mode vmode = <MODEF:ssevecmode>mode;
4947   const machine_mode mode = <MODEF:MODE>mode;
4948   rtx t, op0 = simplify_gen_subreg (vmode, operands[0], mode, 0);
4949
4950   emit_move_insn (op0, CONST0_RTX (vmode));
4951
4952   t = gen_rtx_FLOAT (mode, operands[1]);
4953   t = gen_rtx_VEC_DUPLICATE (vmode, t);
4954   t = gen_rtx_VEC_MERGE (vmode, t, op0, const1_rtx);
4955   emit_insn (gen_rtx_SET (VOIDmode, op0, t));
4956   DONE;
4957 })
4958
4959 ;; Break partial reg stall for cvtsd2ss.
4960
4961 (define_peephole2
4962   [(set (match_operand:SF 0 "register_operand")
4963         (float_truncate:SF
4964           (match_operand:DF 1 "nonimmediate_operand")))]
4965   "TARGET_SSE2 && TARGET_SSE_MATH
4966    && TARGET_SSE_PARTIAL_REG_DEPENDENCY
4967    && optimize_function_for_speed_p (cfun)
4968    && SSE_REG_P (operands[0])
4969    && (!SSE_REG_P (operands[1])
4970        || REGNO (operands[0]) != REGNO (operands[1]))"
4971   [(set (match_dup 0)
4972         (vec_merge:V4SF
4973           (vec_duplicate:V4SF
4974             (float_truncate:V2SF
4975               (match_dup 1)))
4976           (match_dup 0)
4977           (const_int 1)))]
4978 {
4979   operands[0] = simplify_gen_subreg (V4SFmode, operands[0],
4980                                      SFmode, 0);
4981   operands[1] = simplify_gen_subreg (V2DFmode, operands[1],
4982                                      DFmode, 0);
4983   emit_move_insn (operands[0], CONST0_RTX (V4SFmode));
4984 })
4985
4986 ;; Break partial reg stall for cvtss2sd.
4987
4988 (define_peephole2
4989   [(set (match_operand:DF 0 "register_operand")
4990         (float_extend:DF
4991           (match_operand:SF 1 "nonimmediate_operand")))]
4992   "TARGET_SSE2 && TARGET_SSE_MATH
4993    && TARGET_SSE_PARTIAL_REG_DEPENDENCY
4994    && optimize_function_for_speed_p (cfun)
4995    && SSE_REG_P (operands[0])
4996    && (!SSE_REG_P (operands[1])
4997        || REGNO (operands[0]) != REGNO (operands[1]))"
4998   [(set (match_dup 0)
4999         (vec_merge:V2DF
5000           (float_extend:V2DF
5001             (vec_select:V2SF
5002               (match_dup 1)
5003               (parallel [(const_int 0) (const_int 1)])))
5004           (match_dup 0)
5005           (const_int 1)))]
5006 {
5007   operands[0] = simplify_gen_subreg (V2DFmode, operands[0],
5008                                      DFmode, 0);
5009   operands[1] = simplify_gen_subreg (V4SFmode, operands[1],
5010                                      SFmode, 0);
5011   emit_move_insn (operands[0], CONST0_RTX (V2DFmode));
5012 })
5013
5014 ;; Avoid store forwarding (partial memory) stall penalty
5015 ;; by passing DImode value through XMM registers.  */
5016
5017 (define_insn "floatdi<X87MODEF:mode>2_i387_with_xmm"
5018   [(set (match_operand:X87MODEF 0 "register_operand" "=f,f")
5019         (float:X87MODEF
5020           (match_operand:DI 1 "nonimmediate_operand" "m,?r")))
5021    (clobber (match_scratch:V4SI 3 "=X,x"))
5022    (clobber (match_scratch:V4SI 4 "=X,x"))
5023    (clobber (match_operand:DI 2 "memory_operand" "=X,m"))]
5024   "TARGET_80387 && X87_ENABLE_FLOAT (<X87MODEF:MODE>mode, DImode)
5025    && TARGET_SSE2 && TARGET_INTER_UNIT_MOVES_TO_VEC
5026    && !TARGET_64BIT && optimize_function_for_speed_p (cfun)"
5027   "#"
5028   [(set_attr "type" "multi")
5029    (set_attr "mode" "<X87MODEF:MODE>")
5030    (set_attr "unit" "i387")
5031    (set_attr "fp_int_src" "true")])
5032
5033 (define_split
5034   [(set (match_operand:X87MODEF 0 "fp_register_operand")
5035         (float:X87MODEF (match_operand:DI 1 "register_operand")))
5036    (clobber (match_scratch:V4SI 3))
5037    (clobber (match_scratch:V4SI 4))
5038    (clobber (match_operand:DI 2 "memory_operand"))]
5039   "TARGET_80387 && X87_ENABLE_FLOAT (<X87MODEF:MODE>mode, DImode)
5040    && TARGET_SSE2 && TARGET_INTER_UNIT_MOVES_TO_VEC
5041    && !TARGET_64BIT && optimize_function_for_speed_p (cfun)
5042    && reload_completed"
5043   [(set (match_dup 2) (match_dup 3))
5044    (set (match_dup 0) (float:X87MODEF (match_dup 2)))]
5045 {
5046   /* The DImode arrived in a pair of integral registers (e.g. %edx:%eax).
5047      Assemble the 64-bit DImode value in an xmm register.  */
5048   emit_insn (gen_sse2_loadld (operands[3], CONST0_RTX (V4SImode),
5049                               gen_rtx_SUBREG (SImode, operands[1], 0)));
5050   emit_insn (gen_sse2_loadld (operands[4], CONST0_RTX (V4SImode),
5051                               gen_rtx_SUBREG (SImode, operands[1], 4)));
5052   emit_insn (gen_vec_interleave_lowv4si (operands[3], operands[3],
5053                                          operands[4]));
5054
5055   operands[3] = gen_rtx_REG (DImode, REGNO (operands[3]));
5056 })
5057
5058 (define_split
5059   [(set (match_operand:X87MODEF 0 "fp_register_operand")
5060         (float:X87MODEF (match_operand:DI 1 "memory_operand")))
5061    (clobber (match_scratch:V4SI 3))
5062    (clobber (match_scratch:V4SI 4))
5063    (clobber (match_operand:DI 2 "memory_operand"))]
5064   "TARGET_80387 && X87_ENABLE_FLOAT (<X87MODEF:MODE>mode, DImode)
5065    && TARGET_SSE2 && TARGET_INTER_UNIT_MOVES_TO_VEC
5066    && !TARGET_64BIT && optimize_function_for_speed_p (cfun)
5067    && reload_completed"
5068   [(set (match_dup 0) (float:X87MODEF (match_dup 1)))])
5069
5070 (define_expand "floatuns<SWI12:mode><MODEF:mode>2"
5071   [(set (match_operand:MODEF 0 "register_operand")
5072         (unsigned_float:MODEF
5073           (match_operand:SWI12 1 "nonimmediate_operand")))]
5074   "!TARGET_64BIT
5075    && SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH"
5076 {
5077   operands[1] = convert_to_mode (SImode, operands[1], 1);
5078   emit_insn (gen_floatsi<MODEF:mode>2 (operands[0], operands[1]));
5079   DONE;
5080 })
5081
5082 ;; Avoid store forwarding (partial memory) stall penalty by extending
5083 ;; SImode value to DImode through XMM register instead of pushing two
5084 ;; SImode values to stack. Also note that fild loads from memory only.
5085
5086 (define_insn_and_split "*floatunssi<mode>2_i387_with_xmm"
5087   [(set (match_operand:X87MODEF 0 "register_operand" "=f")
5088         (unsigned_float:X87MODEF
5089           (match_operand:SI 1 "nonimmediate_operand" "rm")))
5090    (clobber (match_scratch:DI 3 "=x"))
5091    (clobber (match_operand:DI 2 "memory_operand" "=m"))]
5092   "!TARGET_64BIT
5093    && TARGET_80387 && X87_ENABLE_FLOAT (<X87MODEF:MODE>mode, DImode)
5094    && TARGET_SSE2 && TARGET_INTER_UNIT_MOVES_TO_VEC"
5095   "#"
5096   "&& reload_completed"
5097   [(set (match_dup 3) (zero_extend:DI (match_dup 1)))
5098    (set (match_dup 2) (match_dup 3))
5099    (set (match_dup 0)
5100         (float:X87MODEF (match_dup 2)))]
5101   ""
5102   [(set_attr "type" "multi")
5103    (set_attr "mode" "<MODE>")])
5104
5105 (define_expand "floatunssi<mode>2"
5106   [(parallel
5107      [(set (match_operand:X87MODEF 0 "register_operand")
5108            (unsigned_float:X87MODEF
5109              (match_operand:SI 1 "nonimmediate_operand")))
5110       (clobber (match_scratch:DI 3))
5111       (clobber (match_dup 2))])]
5112   "!TARGET_64BIT
5113    && ((TARGET_80387 && X87_ENABLE_FLOAT (<X87MODEF:MODE>mode, DImode)
5114         && TARGET_SSE2 && TARGET_INTER_UNIT_MOVES_TO_VEC)
5115        || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH))"
5116 {
5117   if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
5118     {
5119       ix86_expand_convert_uns_si<mode>_sse (operands[0], operands[1]);
5120       DONE;
5121     }
5122   else
5123     operands[2] = assign_386_stack_local (DImode, SLOT_TEMP);
5124 })
5125
5126 (define_expand "floatunsdisf2"
5127   [(use (match_operand:SF 0 "register_operand"))
5128    (use (match_operand:DI 1 "nonimmediate_operand"))]
5129   "TARGET_64BIT && TARGET_SSE_MATH"
5130   "x86_emit_floatuns (operands); DONE;")
5131
5132 (define_expand "floatunsdidf2"
5133   [(use (match_operand:DF 0 "register_operand"))
5134    (use (match_operand:DI 1 "nonimmediate_operand"))]
5135   "(TARGET_64BIT || TARGET_KEEPS_VECTOR_ALIGNED_STACK)
5136    && TARGET_SSE2 && TARGET_SSE_MATH"
5137 {
5138   if (TARGET_64BIT)
5139     x86_emit_floatuns (operands);
5140   else
5141     ix86_expand_convert_uns_didf_sse (operands[0], operands[1]);
5142   DONE;
5143 })
5144 \f
5145 ;; Load effective address instructions
5146
5147 (define_insn_and_split "*lea<mode>"
5148   [(set (match_operand:SWI48 0 "register_operand" "=r")
5149         (match_operand:SWI48 1 "address_no_seg_operand" "Ts"))]
5150   ""
5151 {
5152   if (SImode_address_operand (operands[1], VOIDmode))
5153     {
5154       gcc_assert (TARGET_64BIT);
5155       return "lea{l}\t{%E1, %k0|%k0, %E1}";
5156     }
5157   else 
5158     return "lea{<imodesuffix>}\t{%E1, %0|%0, %E1}";
5159 }
5160   "reload_completed && ix86_avoid_lea_for_addr (insn, operands)"
5161   [(const_int 0)]
5162 {
5163   machine_mode mode = <MODE>mode;
5164   rtx pat;
5165
5166   /* ix86_avoid_lea_for_addr re-recognizes insn and may
5167      change operands[] array behind our back.  */
5168   pat = PATTERN (curr_insn);
5169
5170   operands[0] = SET_DEST (pat);
5171   operands[1] = SET_SRC (pat);
5172
5173   /* Emit all operations in SImode for zero-extended addresses.  */
5174   if (SImode_address_operand (operands[1], VOIDmode))
5175     mode = SImode;
5176
5177   ix86_split_lea_for_addr (curr_insn, operands, mode);
5178
5179   /* Zero-extend return register to DImode for zero-extended addresses.  */
5180   if (mode != <MODE>mode)
5181     emit_insn (gen_zero_extendsidi2
5182                (operands[0], gen_lowpart (mode, operands[0])));
5183
5184   DONE;
5185 }
5186   [(set_attr "type" "lea")
5187    (set (attr "mode")
5188      (if_then_else
5189        (match_operand 1 "SImode_address_operand")
5190        (const_string "SI")
5191        (const_string "<MODE>")))])
5192 \f
5193 ;; Add instructions
5194
5195 (define_expand "add<mode>3"
5196   [(set (match_operand:SDWIM 0 "nonimmediate_operand")
5197         (plus:SDWIM (match_operand:SDWIM 1 "nonimmediate_operand")
5198                     (match_operand:SDWIM 2 "<general_operand>")))]
5199   ""
5200   "ix86_expand_binary_operator (PLUS, <MODE>mode, operands); DONE;")
5201
5202 (define_insn_and_split "*add<dwi>3_doubleword"
5203   [(set (match_operand:<DWI> 0 "nonimmediate_operand" "=r,o")
5204         (plus:<DWI>
5205           (match_operand:<DWI> 1 "nonimmediate_operand" "%0,0")
5206           (match_operand:<DWI> 2 "<general_operand>" "ro<di>,r<di>")))
5207    (clobber (reg:CC FLAGS_REG))]
5208   "ix86_binary_operator_ok (PLUS, <DWI>mode, operands)"
5209   "#"
5210   "reload_completed"
5211   [(parallel [(set (reg:CC FLAGS_REG)
5212                    (unspec:CC [(match_dup 1) (match_dup 2)]
5213                               UNSPEC_ADD_CARRY))
5214               (set (match_dup 0)
5215                    (plus:DWIH (match_dup 1) (match_dup 2)))])
5216    (parallel [(set (match_dup 3)
5217                    (plus:DWIH
5218                      (match_dup 4)
5219                      (plus:DWIH
5220                        (ltu:DWIH (reg:CC FLAGS_REG) (const_int 0))
5221                        (match_dup 5))))
5222               (clobber (reg:CC FLAGS_REG))])]
5223   "split_double_mode (<DWI>mode, &operands[0], 3, &operands[0], &operands[3]);")
5224
5225 (define_insn "*add<mode>3_cc"
5226   [(set (reg:CC FLAGS_REG)
5227         (unspec:CC
5228           [(match_operand:SWI48 1 "nonimmediate_operand" "%0,0")
5229            (match_operand:SWI48 2 "<general_operand>" "r<i>,rm")]
5230           UNSPEC_ADD_CARRY))
5231    (set (match_operand:SWI48 0 "nonimmediate_operand" "=rm,r")
5232         (plus:SWI48 (match_dup 1) (match_dup 2)))]
5233   "ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
5234   "add{<imodesuffix>}\t{%2, %0|%0, %2}"
5235   [(set_attr "type" "alu")
5236    (set_attr "mode" "<MODE>")])
5237
5238 (define_insn "addqi3_cc"
5239   [(set (reg:CC FLAGS_REG)
5240         (unspec:CC
5241           [(match_operand:QI 1 "nonimmediate_operand" "%0,0")
5242            (match_operand:QI 2 "general_operand" "qn,qm")]
5243           UNSPEC_ADD_CARRY))
5244    (set (match_operand:QI 0 "nonimmediate_operand" "=qm,q")
5245         (plus:QI (match_dup 1) (match_dup 2)))]
5246   "ix86_binary_operator_ok (PLUS, QImode, operands)"
5247   "add{b}\t{%2, %0|%0, %2}"
5248   [(set_attr "type" "alu")
5249    (set_attr "mode" "QI")])
5250
5251 (define_insn "*add<mode>_1"
5252   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=r,rm,r,r")
5253         (plus:SWI48
5254           (match_operand:SWI48 1 "nonimmediate_operand" "%0,0,r,r")
5255           (match_operand:SWI48 2 "x86_64_general_operand" "rme,re,0,le")))
5256    (clobber (reg:CC FLAGS_REG))]
5257   "ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
5258 {
5259   switch (get_attr_type (insn))
5260     {
5261     case TYPE_LEA:
5262       return "#";
5263
5264     case TYPE_INCDEC:
5265       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5266       if (operands[2] == const1_rtx)
5267         return "inc{<imodesuffix>}\t%0";
5268       else
5269         {
5270           gcc_assert (operands[2] == constm1_rtx);
5271           return "dec{<imodesuffix>}\t%0";
5272         }
5273
5274     default:
5275       /* For most processors, ADD is faster than LEA.  This alternative
5276          was added to use ADD as much as possible.  */
5277       if (which_alternative == 2)
5278         std::swap (operands[1], operands[2]);
5279         
5280       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5281       if (x86_maybe_negate_const_int (&operands[2], <MODE>mode))
5282         return "sub{<imodesuffix>}\t{%2, %0|%0, %2}";
5283
5284       return "add{<imodesuffix>}\t{%2, %0|%0, %2}";
5285     }
5286 }
5287   [(set (attr "type")
5288      (cond [(eq_attr "alternative" "3")
5289               (const_string "lea")
5290             (match_operand:SWI48 2 "incdec_operand")
5291               (const_string "incdec")
5292            ]
5293            (const_string "alu")))
5294    (set (attr "length_immediate")
5295       (if_then_else
5296         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5297         (const_string "1")
5298         (const_string "*")))
5299    (set_attr "mode" "<MODE>")])
5300
5301 ;; It may seem that nonimmediate operand is proper one for operand 1.
5302 ;; The addsi_1 pattern allows nonimmediate operand at that place and
5303 ;; we take care in ix86_binary_operator_ok to not allow two memory
5304 ;; operands so proper swapping will be done in reload.  This allow
5305 ;; patterns constructed from addsi_1 to match.
5306
5307 (define_insn "addsi_1_zext"
5308   [(set (match_operand:DI 0 "register_operand" "=r,r,r")
5309         (zero_extend:DI
5310           (plus:SI (match_operand:SI 1 "nonimmediate_operand" "%0,r,r")
5311                    (match_operand:SI 2 "x86_64_general_operand" "rme,0,le"))))
5312    (clobber (reg:CC FLAGS_REG))]
5313   "TARGET_64BIT && ix86_binary_operator_ok (PLUS, SImode, operands)"
5314 {
5315   switch (get_attr_type (insn))
5316     {
5317     case TYPE_LEA:
5318       return "#";
5319
5320     case TYPE_INCDEC:
5321       if (operands[2] == const1_rtx)
5322         return "inc{l}\t%k0";
5323       else
5324         {
5325           gcc_assert (operands[2] == constm1_rtx);
5326           return "dec{l}\t%k0";
5327         }
5328
5329     default:
5330       /* For most processors, ADD is faster than LEA.  This alternative
5331          was added to use ADD as much as possible.  */
5332       if (which_alternative == 1)
5333         std::swap (operands[1], operands[2]);
5334
5335       if (x86_maybe_negate_const_int (&operands[2], SImode))
5336         return "sub{l}\t{%2, %k0|%k0, %2}";
5337
5338       return "add{l}\t{%2, %k0|%k0, %2}";
5339     }
5340 }
5341   [(set (attr "type")
5342      (cond [(eq_attr "alternative" "2")
5343               (const_string "lea")
5344             (match_operand:SI 2 "incdec_operand")
5345               (const_string "incdec")
5346            ]
5347            (const_string "alu")))
5348    (set (attr "length_immediate")
5349       (if_then_else
5350         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5351         (const_string "1")
5352         (const_string "*")))
5353    (set_attr "mode" "SI")])
5354
5355 (define_insn "*addhi_1"
5356   [(set (match_operand:HI 0 "nonimmediate_operand" "=rm,r,r,Yp")
5357         (plus:HI (match_operand:HI 1 "nonimmediate_operand" "%0,0,r,Yp")
5358                  (match_operand:HI 2 "general_operand" "rn,rm,0,ln")))
5359    (clobber (reg:CC FLAGS_REG))]
5360   "ix86_binary_operator_ok (PLUS, HImode, operands)"
5361 {
5362   switch (get_attr_type (insn))
5363     {
5364     case TYPE_LEA:
5365       return "#";
5366
5367     case TYPE_INCDEC:
5368       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5369       if (operands[2] == const1_rtx)
5370         return "inc{w}\t%0";
5371       else
5372         {
5373           gcc_assert (operands[2] == constm1_rtx);
5374           return "dec{w}\t%0";
5375         }
5376
5377     default:
5378       /* For most processors, ADD is faster than LEA.  This alternative
5379          was added to use ADD as much as possible.  */
5380       if (which_alternative == 2)
5381         std::swap (operands[1], operands[2]);
5382
5383       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5384       if (x86_maybe_negate_const_int (&operands[2], HImode))
5385         return "sub{w}\t{%2, %0|%0, %2}";
5386
5387       return "add{w}\t{%2, %0|%0, %2}";
5388     }
5389 }
5390   [(set (attr "type")
5391      (cond [(eq_attr "alternative" "3")
5392               (const_string "lea")
5393             (match_operand:HI 2 "incdec_operand")
5394               (const_string "incdec")
5395            ]
5396            (const_string "alu")))
5397    (set (attr "length_immediate")
5398       (if_then_else
5399         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5400         (const_string "1")
5401         (const_string "*")))
5402    (set_attr "mode" "HI,HI,HI,SI")])
5403
5404 ;; %%% Potential partial reg stall on alternatives 3 and 4.  What to do?
5405 (define_insn "*addqi_1"
5406   [(set (match_operand:QI 0 "nonimmediate_operand" "=qm,q,q,r,r,Yp")
5407         (plus:QI (match_operand:QI 1 "nonimmediate_operand" "%0,0,q,0,r,Yp")
5408                  (match_operand:QI 2 "general_operand" "qn,qm,0,rn,0,ln")))
5409    (clobber (reg:CC FLAGS_REG))]
5410   "ix86_binary_operator_ok (PLUS, QImode, operands)"
5411 {
5412   bool widen = (which_alternative == 3 || which_alternative == 4);
5413
5414   switch (get_attr_type (insn))
5415     {
5416     case TYPE_LEA:
5417       return "#";
5418
5419     case TYPE_INCDEC:
5420       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5421       if (operands[2] == const1_rtx)
5422         return widen ? "inc{l}\t%k0" : "inc{b}\t%0";
5423       else
5424         {
5425           gcc_assert (operands[2] == constm1_rtx);
5426           return widen ? "dec{l}\t%k0" : "dec{b}\t%0";
5427         }
5428
5429     default:
5430       /* For most processors, ADD is faster than LEA.  These alternatives
5431          were added to use ADD as much as possible.  */
5432       if (which_alternative == 2 || which_alternative == 4)
5433         std::swap (operands[1], operands[2]);
5434
5435       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5436       if (x86_maybe_negate_const_int (&operands[2], QImode))
5437         {
5438           if (widen)
5439             return "sub{l}\t{%2, %k0|%k0, %2}";
5440           else
5441             return "sub{b}\t{%2, %0|%0, %2}";
5442         }
5443       if (widen)
5444         return "add{l}\t{%k2, %k0|%k0, %k2}";
5445       else
5446         return "add{b}\t{%2, %0|%0, %2}";
5447     }
5448 }
5449   [(set (attr "type")
5450      (cond [(eq_attr "alternative" "5")
5451               (const_string "lea")
5452             (match_operand:QI 2 "incdec_operand")
5453               (const_string "incdec")
5454            ]
5455            (const_string "alu")))
5456    (set (attr "length_immediate")
5457       (if_then_else
5458         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5459         (const_string "1")
5460         (const_string "*")))
5461    (set_attr "mode" "QI,QI,QI,SI,SI,SI")])
5462
5463 (define_insn "*addqi_1_slp"
5464   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm,q"))
5465         (plus:QI (match_dup 0)
5466                  (match_operand:QI 1 "general_operand" "qn,qm")))
5467    (clobber (reg:CC FLAGS_REG))]
5468   "(! TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
5469    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
5470 {
5471   switch (get_attr_type (insn))
5472     {
5473     case TYPE_INCDEC:
5474       if (operands[1] == const1_rtx)
5475         return "inc{b}\t%0";
5476       else
5477         {
5478           gcc_assert (operands[1] == constm1_rtx);
5479           return "dec{b}\t%0";
5480         }
5481
5482     default:
5483       if (x86_maybe_negate_const_int (&operands[1], QImode))
5484         return "sub{b}\t{%1, %0|%0, %1}";
5485
5486       return "add{b}\t{%1, %0|%0, %1}";
5487     }
5488 }
5489   [(set (attr "type")
5490      (if_then_else (match_operand:QI 1 "incdec_operand")
5491         (const_string "incdec")
5492         (const_string "alu1")))
5493    (set (attr "memory")
5494      (if_then_else (match_operand 1 "memory_operand")
5495         (const_string "load")
5496         (const_string "none")))
5497    (set_attr "mode" "QI")])
5498
5499 ;; Split non destructive adds if we cannot use lea.
5500 (define_split
5501   [(set (match_operand:SWI48 0 "register_operand")
5502         (plus:SWI48 (match_operand:SWI48 1 "register_operand")
5503                     (match_operand:SWI48 2 "x86_64_nonmemory_operand")))
5504    (clobber (reg:CC FLAGS_REG))]
5505   "reload_completed && ix86_avoid_lea_for_add (insn, operands)"
5506   [(set (match_dup 0) (match_dup 1))
5507    (parallel [(set (match_dup 0) (plus:SWI48 (match_dup 0) (match_dup 2)))
5508               (clobber (reg:CC FLAGS_REG))])])
5509
5510 ;; Convert add to the lea pattern to avoid flags dependency.
5511 (define_split
5512   [(set (match_operand:SWI 0 "register_operand")
5513         (plus:SWI (match_operand:SWI 1 "register_operand")
5514                   (match_operand:SWI 2 "<nonmemory_operand>")))
5515    (clobber (reg:CC FLAGS_REG))]
5516   "reload_completed && ix86_lea_for_add_ok (insn, operands)" 
5517   [(const_int 0)]
5518 {
5519   machine_mode mode = <MODE>mode;
5520   rtx pat;
5521
5522   if (<MODE_SIZE> < GET_MODE_SIZE (SImode))
5523     { 
5524       mode = SImode; 
5525       operands[0] = gen_lowpart (mode, operands[0]);
5526       operands[1] = gen_lowpart (mode, operands[1]);
5527       operands[2] = gen_lowpart (mode, operands[2]);
5528     }
5529
5530   pat = gen_rtx_PLUS (mode, operands[1], operands[2]);
5531
5532   emit_insn (gen_rtx_SET (VOIDmode, operands[0], pat));
5533   DONE;
5534 })
5535
5536 ;; Split non destructive adds if we cannot use lea.
5537 (define_split
5538   [(set (match_operand:DI 0 "register_operand")
5539         (zero_extend:DI
5540           (plus:SI (match_operand:SI 1 "register_operand")
5541                    (match_operand:SI 2 "x86_64_nonmemory_operand"))))
5542    (clobber (reg:CC FLAGS_REG))]
5543   "TARGET_64BIT
5544    && reload_completed && ix86_avoid_lea_for_add (insn, operands)"
5545   [(set (match_dup 3) (match_dup 1))
5546    (parallel [(set (match_dup 0)
5547                    (zero_extend:DI (plus:SI (match_dup 3) (match_dup 2))))
5548               (clobber (reg:CC FLAGS_REG))])]
5549   "operands[3] = gen_lowpart (SImode, operands[0]);")
5550
5551 ;; Convert add to the lea pattern to avoid flags dependency.
5552 (define_split
5553   [(set (match_operand:DI 0 "register_operand")
5554         (zero_extend:DI
5555           (plus:SI (match_operand:SI 1 "register_operand")
5556                    (match_operand:SI 2 "x86_64_nonmemory_operand"))))
5557    (clobber (reg:CC FLAGS_REG))]
5558   "TARGET_64BIT && reload_completed && ix86_lea_for_add_ok (insn, operands)"
5559   [(set (match_dup 0)
5560         (zero_extend:DI (plus:SI (match_dup 1) (match_dup 2))))])
5561
5562 (define_insn "*add<mode>_2"
5563   [(set (reg FLAGS_REG)
5564         (compare
5565           (plus:SWI
5566             (match_operand:SWI 1 "nonimmediate_operand" "%0,0,<r>")
5567             (match_operand:SWI 2 "<general_operand>" "<g>,<r><i>,0"))
5568           (const_int 0)))
5569    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>,<r>m,<r>")
5570         (plus:SWI (match_dup 1) (match_dup 2)))]
5571   "ix86_match_ccmode (insn, CCGOCmode)
5572    && ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
5573 {
5574   switch (get_attr_type (insn))
5575     {
5576     case TYPE_INCDEC:
5577       if (operands[2] == const1_rtx)
5578         return "inc{<imodesuffix>}\t%0";
5579       else
5580         {
5581           gcc_assert (operands[2] == constm1_rtx);
5582           return "dec{<imodesuffix>}\t%0";
5583         }
5584
5585     default:
5586       if (which_alternative == 2)
5587         std::swap (operands[1], operands[2]);
5588         
5589       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5590       if (x86_maybe_negate_const_int (&operands[2], <MODE>mode))
5591         return "sub{<imodesuffix>}\t{%2, %0|%0, %2}";
5592
5593       return "add{<imodesuffix>}\t{%2, %0|%0, %2}";
5594     }
5595 }
5596   [(set (attr "type")
5597      (if_then_else (match_operand:SWI 2 "incdec_operand")
5598         (const_string "incdec")
5599         (const_string "alu")))
5600    (set (attr "length_immediate")
5601       (if_then_else
5602         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5603         (const_string "1")
5604         (const_string "*")))
5605    (set_attr "mode" "<MODE>")])
5606
5607 ;; See comment for addsi_1_zext why we do use nonimmediate_operand
5608 (define_insn "*addsi_2_zext"
5609   [(set (reg FLAGS_REG)
5610         (compare
5611           (plus:SI (match_operand:SI 1 "nonimmediate_operand" "%0,r")
5612                    (match_operand:SI 2 "x86_64_general_operand" "rme,0"))
5613           (const_int 0)))
5614    (set (match_operand:DI 0 "register_operand" "=r,r")
5615         (zero_extend:DI (plus:SI (match_dup 1) (match_dup 2))))]
5616   "TARGET_64BIT && ix86_match_ccmode (insn, CCGOCmode)
5617    && ix86_binary_operator_ok (PLUS, SImode, operands)"
5618 {
5619   switch (get_attr_type (insn))
5620     {
5621     case TYPE_INCDEC:
5622       if (operands[2] == const1_rtx)
5623         return "inc{l}\t%k0";
5624       else
5625         {
5626           gcc_assert (operands[2] == constm1_rtx);
5627           return "dec{l}\t%k0";
5628         }
5629
5630     default:
5631       if (which_alternative == 1)
5632         std::swap (operands[1], operands[2]);
5633
5634       if (x86_maybe_negate_const_int (&operands[2], SImode))
5635         return "sub{l}\t{%2, %k0|%k0, %2}";
5636
5637       return "add{l}\t{%2, %k0|%k0, %2}";
5638     }
5639 }
5640   [(set (attr "type")
5641      (if_then_else (match_operand:SI 2 "incdec_operand")
5642         (const_string "incdec")
5643         (const_string "alu")))
5644    (set (attr "length_immediate")
5645       (if_then_else
5646         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5647         (const_string "1")
5648         (const_string "*")))
5649    (set_attr "mode" "SI")])
5650
5651 (define_insn "*add<mode>_3"
5652   [(set (reg FLAGS_REG)
5653         (compare
5654           (neg:SWI (match_operand:SWI 2 "<general_operand>" "<g>,0"))
5655           (match_operand:SWI 1 "nonimmediate_operand" "%0,<r>")))
5656    (clobber (match_scratch:SWI 0 "=<r>,<r>"))]
5657   "ix86_match_ccmode (insn, CCZmode)
5658    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
5659 {
5660   switch (get_attr_type (insn))
5661     {
5662     case TYPE_INCDEC:
5663       if (operands[2] == const1_rtx)
5664         return "inc{<imodesuffix>}\t%0";
5665       else
5666         {
5667           gcc_assert (operands[2] == constm1_rtx);
5668           return "dec{<imodesuffix>}\t%0";
5669         }
5670
5671     default:
5672       if (which_alternative == 1)
5673         std::swap (operands[1], operands[2]);
5674
5675       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5676       if (x86_maybe_negate_const_int (&operands[2], <MODE>mode))
5677         return "sub{<imodesuffix>}\t{%2, %0|%0, %2}";
5678
5679       return "add{<imodesuffix>}\t{%2, %0|%0, %2}";
5680     }
5681 }
5682   [(set (attr "type")
5683      (if_then_else (match_operand:SWI 2 "incdec_operand")
5684         (const_string "incdec")
5685         (const_string "alu")))
5686    (set (attr "length_immediate")
5687       (if_then_else
5688         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5689         (const_string "1")
5690         (const_string "*")))
5691    (set_attr "mode" "<MODE>")])
5692
5693 ;; See comment for addsi_1_zext why we do use nonimmediate_operand
5694 (define_insn "*addsi_3_zext"
5695   [(set (reg FLAGS_REG)
5696         (compare
5697           (neg:SI (match_operand:SI 2 "x86_64_general_operand" "rme,0"))
5698           (match_operand:SI 1 "nonimmediate_operand" "%0,r")))
5699    (set (match_operand:DI 0 "register_operand" "=r,r")
5700         (zero_extend:DI (plus:SI (match_dup 1) (match_dup 2))))]
5701   "TARGET_64BIT && ix86_match_ccmode (insn, CCZmode)
5702    && ix86_binary_operator_ok (PLUS, SImode, operands)"
5703 {
5704   switch (get_attr_type (insn))
5705     {
5706     case TYPE_INCDEC:
5707       if (operands[2] == const1_rtx)
5708         return "inc{l}\t%k0";
5709       else
5710         {
5711           gcc_assert (operands[2] == constm1_rtx);
5712           return "dec{l}\t%k0";
5713         }
5714
5715     default:
5716       if (which_alternative == 1)
5717         std::swap (operands[1], operands[2]);
5718
5719       if (x86_maybe_negate_const_int (&operands[2], SImode))
5720         return "sub{l}\t{%2, %k0|%k0, %2}";
5721
5722       return "add{l}\t{%2, %k0|%k0, %2}";
5723     }
5724 }
5725   [(set (attr "type")
5726      (if_then_else (match_operand:SI 2 "incdec_operand")
5727         (const_string "incdec")
5728         (const_string "alu")))
5729    (set (attr "length_immediate")
5730       (if_then_else
5731         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5732         (const_string "1")
5733         (const_string "*")))
5734    (set_attr "mode" "SI")])
5735
5736 ; For comparisons against 1, -1 and 128, we may generate better code
5737 ; by converting cmp to add, inc or dec as done by peephole2.  This pattern
5738 ; is matched then.  We can't accept general immediate, because for
5739 ; case of overflows,  the result is messed up.
5740 ; Also carry flag is reversed compared to cmp, so this conversion is valid
5741 ; only for comparisons not depending on it.
5742
5743 (define_insn "*adddi_4"
5744   [(set (reg FLAGS_REG)
5745         (compare
5746           (match_operand:DI 1 "nonimmediate_operand" "0")
5747           (match_operand:DI 2 "x86_64_immediate_operand" "e")))
5748    (clobber (match_scratch:DI 0 "=rm"))]
5749   "TARGET_64BIT
5750    && ix86_match_ccmode (insn, CCGCmode)"
5751 {
5752   switch (get_attr_type (insn))
5753     {
5754     case TYPE_INCDEC:
5755       if (operands[2] == constm1_rtx)
5756         return "inc{q}\t%0";
5757       else
5758         {
5759           gcc_assert (operands[2] == const1_rtx);
5760           return "dec{q}\t%0";
5761         }
5762
5763     default:
5764       if (x86_maybe_negate_const_int (&operands[2], DImode))
5765         return "add{q}\t{%2, %0|%0, %2}";
5766
5767       return "sub{q}\t{%2, %0|%0, %2}";
5768     }
5769 }
5770   [(set (attr "type")
5771      (if_then_else (match_operand:DI 2 "incdec_operand")
5772         (const_string "incdec")
5773         (const_string "alu")))
5774    (set (attr "length_immediate")
5775       (if_then_else
5776         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5777         (const_string "1")
5778         (const_string "*")))
5779    (set_attr "mode" "DI")])
5780
5781 ; For comparisons against 1, -1 and 128, we may generate better code
5782 ; by converting cmp to add, inc or dec as done by peephole2.  This pattern
5783 ; is matched then.  We can't accept general immediate, because for
5784 ; case of overflows,  the result is messed up.
5785 ; Also carry flag is reversed compared to cmp, so this conversion is valid
5786 ; only for comparisons not depending on it.
5787
5788 (define_insn "*add<mode>_4"
5789   [(set (reg FLAGS_REG)
5790         (compare
5791           (match_operand:SWI124 1 "nonimmediate_operand" "0")
5792           (match_operand:SWI124 2 "const_int_operand" "n")))
5793    (clobber (match_scratch:SWI124 0 "=<r>m"))]
5794   "ix86_match_ccmode (insn, CCGCmode)"
5795 {
5796   switch (get_attr_type (insn))
5797     {
5798     case TYPE_INCDEC:
5799       if (operands[2] == constm1_rtx)
5800         return "inc{<imodesuffix>}\t%0";
5801       else
5802         {
5803           gcc_assert (operands[2] == const1_rtx);
5804           return "dec{<imodesuffix>}\t%0";
5805         }
5806
5807     default:
5808       if (x86_maybe_negate_const_int (&operands[2], <MODE>mode))
5809         return "add{<imodesuffix>}\t{%2, %0|%0, %2}";
5810
5811       return "sub{<imodesuffix>}\t{%2, %0|%0, %2}";
5812     }
5813 }
5814   [(set (attr "type")
5815      (if_then_else (match_operand:<MODE> 2 "incdec_operand")
5816         (const_string "incdec")
5817         (const_string "alu")))
5818    (set (attr "length_immediate")
5819       (if_then_else
5820         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5821         (const_string "1")
5822         (const_string "*")))
5823    (set_attr "mode" "<MODE>")])
5824
5825 (define_insn "*add<mode>_5"
5826   [(set (reg FLAGS_REG)
5827         (compare
5828           (plus:SWI
5829             (match_operand:SWI 1 "nonimmediate_operand" "%0,<r>")
5830             (match_operand:SWI 2 "<general_operand>" "<g>,0"))
5831           (const_int 0)))
5832    (clobber (match_scratch:SWI 0 "=<r>,<r>"))]
5833   "ix86_match_ccmode (insn, CCGOCmode)
5834    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
5835 {
5836   switch (get_attr_type (insn))
5837     {
5838     case TYPE_INCDEC:
5839       if (operands[2] == const1_rtx)
5840         return "inc{<imodesuffix>}\t%0";
5841       else
5842         {
5843           gcc_assert (operands[2] == constm1_rtx);
5844           return "dec{<imodesuffix>}\t%0";
5845         }
5846
5847     default:
5848       if (which_alternative == 1)
5849         std::swap (operands[1], operands[2]);
5850
5851       gcc_assert (rtx_equal_p (operands[0], operands[1]));
5852       if (x86_maybe_negate_const_int (&operands[2], <MODE>mode))
5853         return "sub{<imodesuffix>}\t{%2, %0|%0, %2}";
5854
5855       return "add{<imodesuffix>}\t{%2, %0|%0, %2}";
5856     }
5857 }
5858   [(set (attr "type")
5859      (if_then_else (match_operand:SWI 2 "incdec_operand")
5860         (const_string "incdec")
5861         (const_string "alu")))
5862    (set (attr "length_immediate")
5863       (if_then_else
5864         (and (eq_attr "type" "alu") (match_operand 2 "const128_operand"))
5865         (const_string "1")
5866         (const_string "*")))
5867    (set_attr "mode" "<MODE>")])
5868
5869 (define_insn "addqi_ext_1"
5870   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q,Q")
5871                          (const_int 8)
5872                          (const_int 8))
5873         (plus:SI
5874           (zero_extract:SI
5875             (match_operand 1 "ext_register_operand" "0,0")
5876             (const_int 8)
5877             (const_int 8))
5878           (match_operand:QI 2 "general_x64nomem_operand" "Qn,m")))
5879    (clobber (reg:CC FLAGS_REG))]
5880   ""
5881 {
5882   switch (get_attr_type (insn))
5883     {
5884     case TYPE_INCDEC:
5885       if (operands[2] == const1_rtx)
5886         return "inc{b}\t%h0";
5887       else
5888         {
5889           gcc_assert (operands[2] == constm1_rtx);
5890           return "dec{b}\t%h0";
5891         }
5892
5893     default:
5894       return "add{b}\t{%2, %h0|%h0, %2}";
5895     }
5896 }
5897   [(set_attr "isa" "*,nox64")
5898    (set (attr "type")
5899      (if_then_else (match_operand:QI 2 "incdec_operand")
5900         (const_string "incdec")
5901         (const_string "alu")))
5902    (set_attr "modrm" "1")
5903    (set_attr "mode" "QI")])
5904
5905 (define_insn "*addqi_ext_2"
5906   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q")
5907                          (const_int 8)
5908                          (const_int 8))
5909         (plus:SI
5910           (zero_extract:SI
5911             (match_operand 1 "ext_register_operand" "%0")
5912             (const_int 8)
5913             (const_int 8))
5914           (zero_extract:SI
5915             (match_operand 2 "ext_register_operand" "Q")
5916             (const_int 8)
5917             (const_int 8))))
5918    (clobber (reg:CC FLAGS_REG))]
5919   ""
5920   "add{b}\t{%h2, %h0|%h0, %h2}"
5921   [(set_attr "type" "alu")
5922    (set_attr "mode" "QI")])
5923
5924 ;; Add with jump on overflow.
5925 (define_expand "addv<mode>4"
5926   [(parallel [(set (reg:CCO FLAGS_REG)
5927                    (eq:CCO (plus:<DWI>
5928                               (sign_extend:<DWI>
5929                                  (match_operand:SWI 1 "nonimmediate_operand"))
5930                               (match_dup 4))
5931                            (sign_extend:<DWI>
5932                               (plus:SWI (match_dup 1)
5933                                         (match_operand:SWI 2
5934                                            "<general_operand>")))))
5935               (set (match_operand:SWI 0 "register_operand")
5936                    (plus:SWI (match_dup 1) (match_dup 2)))])
5937    (set (pc) (if_then_else
5938                (eq (reg:CCO FLAGS_REG) (const_int 0))
5939                (label_ref (match_operand 3))
5940                (pc)))]
5941   ""
5942 {
5943   ix86_fixup_binary_operands_no_copy (PLUS, <MODE>mode, operands);
5944   if (CONST_INT_P (operands[2]))
5945     operands[4] = operands[2];
5946   else
5947     operands[4] = gen_rtx_SIGN_EXTEND (<DWI>mode, operands[2]);
5948 })
5949
5950 (define_insn "*addv<mode>4"
5951   [(set (reg:CCO FLAGS_REG)
5952         (eq:CCO (plus:<DWI>
5953                    (sign_extend:<DWI>
5954                       (match_operand:SWI 1 "nonimmediate_operand" "%0,0"))
5955                    (sign_extend:<DWI>
5956                       (match_operand:SWI 2 "<general_sext_operand>"
5957                                            "<r>mWe,<r>We")))
5958                 (sign_extend:<DWI>
5959                    (plus:SWI (match_dup 1) (match_dup 2)))))
5960    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>,<r>m")
5961         (plus:SWI (match_dup 1) (match_dup 2)))]
5962   "ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
5963   "add{<imodesuffix>}\t{%2, %0|%0, %2}"
5964   [(set_attr "type" "alu")
5965    (set_attr "mode" "<MODE>")])
5966
5967 (define_insn "*addv<mode>4_1"
5968   [(set (reg:CCO FLAGS_REG)
5969         (eq:CCO (plus:<DWI>
5970                    (sign_extend:<DWI>
5971                       (match_operand:SWI 1 "nonimmediate_operand" "0"))
5972                    (match_operand:<DWI> 3 "const_int_operand" "i"))
5973                 (sign_extend:<DWI>
5974                    (plus:SWI (match_dup 1)
5975                              (match_operand:SWI 2 "x86_64_immediate_operand"
5976                                                   "<i>")))))
5977    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
5978         (plus:SWI (match_dup 1) (match_dup 2)))]
5979   "ix86_binary_operator_ok (PLUS, <MODE>mode, operands)
5980    && CONST_INT_P (operands[2])
5981    && INTVAL (operands[2]) == INTVAL (operands[3])"
5982   "add{<imodesuffix>}\t{%2, %0|%0, %2}"
5983   [(set_attr "type" "alu")
5984    (set_attr "mode" "<MODE>")
5985    (set (attr "length_immediate")
5986         (cond [(match_test "IN_RANGE (INTVAL (operands[2]), -128, 127)")
5987                   (const_string "1")
5988                (match_test "<MODE_SIZE> == 8")
5989                   (const_string "4")]
5990               (const_string "<MODE_SIZE>")))])
5991
5992 ;; The lea patterns for modes less than 32 bits need to be matched by
5993 ;; several insns converted to real lea by splitters.
5994
5995 (define_insn_and_split "*lea_general_1"
5996   [(set (match_operand 0 "register_operand" "=r")
5997         (plus (plus (match_operand 1 "index_register_operand" "l")
5998                     (match_operand 2 "register_operand" "r"))
5999               (match_operand 3 "immediate_operand" "i")))]
6000   "(GET_MODE (operands[0]) == QImode || GET_MODE (operands[0]) == HImode)
6001    && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
6002    && GET_MODE (operands[0]) == GET_MODE (operands[1])
6003    && GET_MODE (operands[0]) == GET_MODE (operands[2])
6004    && (GET_MODE (operands[0]) == GET_MODE (operands[3])
6005        || GET_MODE (operands[3]) == VOIDmode)"
6006   "#"
6007   "&& reload_completed"
6008   [(const_int 0)]
6009 {
6010   machine_mode mode = SImode;
6011   rtx pat;
6012
6013   operands[0] = gen_lowpart (mode, operands[0]);
6014   operands[1] = gen_lowpart (mode, operands[1]);
6015   operands[2] = gen_lowpart (mode, operands[2]);
6016   operands[3] = gen_lowpart (mode, operands[3]);
6017
6018   pat = gen_rtx_PLUS (mode, gen_rtx_PLUS (mode, operands[1], operands[2]),
6019                       operands[3]);
6020
6021   emit_insn (gen_rtx_SET (VOIDmode, operands[0], pat));
6022   DONE;
6023 }
6024   [(set_attr "type" "lea")
6025    (set_attr "mode" "SI")])
6026
6027 (define_insn_and_split "*lea_general_2"
6028   [(set (match_operand 0 "register_operand" "=r")
6029         (plus (mult (match_operand 1 "index_register_operand" "l")
6030                     (match_operand 2 "const248_operand" "n"))
6031               (match_operand 3 "nonmemory_operand" "ri")))]
6032   "(GET_MODE (operands[0]) == QImode || GET_MODE (operands[0]) == HImode)
6033    && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
6034    && GET_MODE (operands[0]) == GET_MODE (operands[1])
6035    && (GET_MODE (operands[0]) == GET_MODE (operands[3])
6036        || GET_MODE (operands[3]) == VOIDmode)"
6037   "#"
6038   "&& reload_completed"
6039   [(const_int 0)]
6040 {
6041   machine_mode mode = SImode;
6042   rtx pat;
6043
6044   operands[0] = gen_lowpart (mode, operands[0]);
6045   operands[1] = gen_lowpart (mode, operands[1]);
6046   operands[3] = gen_lowpart (mode, operands[3]);
6047
6048   pat = gen_rtx_PLUS (mode, gen_rtx_MULT (mode, operands[1], operands[2]),
6049                       operands[3]);
6050
6051   emit_insn (gen_rtx_SET (VOIDmode, operands[0], pat));
6052   DONE;
6053 }
6054   [(set_attr "type" "lea")
6055    (set_attr "mode" "SI")])
6056
6057 (define_insn_and_split "*lea_general_3"
6058   [(set (match_operand 0 "register_operand" "=r")
6059         (plus (plus (mult (match_operand 1 "index_register_operand" "l")
6060                           (match_operand 2 "const248_operand" "n"))
6061                     (match_operand 3 "register_operand" "r"))
6062               (match_operand 4 "immediate_operand" "i")))]
6063   "(GET_MODE (operands[0]) == QImode || GET_MODE (operands[0]) == HImode)
6064    && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
6065    && GET_MODE (operands[0]) == GET_MODE (operands[1])
6066    && GET_MODE (operands[0]) == GET_MODE (operands[3])"
6067   "#"
6068   "&& reload_completed"
6069   [(const_int 0)]
6070 {
6071   machine_mode mode = SImode;
6072   rtx pat;
6073
6074   operands[0] = gen_lowpart (mode, operands[0]);
6075   operands[1] = gen_lowpart (mode, operands[1]);
6076   operands[3] = gen_lowpart (mode, operands[3]);
6077   operands[4] = gen_lowpart (mode, operands[4]);
6078
6079   pat = gen_rtx_PLUS (mode,
6080                       gen_rtx_PLUS (mode,
6081                                     gen_rtx_MULT (mode, operands[1],
6082                                                         operands[2]),
6083                                     operands[3]),
6084                       operands[4]);
6085
6086   emit_insn (gen_rtx_SET (VOIDmode, operands[0], pat));
6087   DONE;
6088 }
6089   [(set_attr "type" "lea")
6090    (set_attr "mode" "SI")])
6091
6092 (define_insn_and_split "*lea_general_4"
6093   [(set (match_operand 0 "register_operand" "=r")
6094         (any_or (ashift
6095                   (match_operand 1 "index_register_operand" "l")
6096                   (match_operand 2 "const_int_operand" "n"))
6097                 (match_operand 3 "const_int_operand" "n")))]
6098   "(((GET_MODE (operands[0]) == QImode || GET_MODE (operands[0]) == HImode)
6099       && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun)))
6100     || GET_MODE (operands[0]) == SImode
6101     || (TARGET_64BIT && GET_MODE (operands[0]) == DImode))
6102    && GET_MODE (operands[0]) == GET_MODE (operands[1])
6103    && ((unsigned HOST_WIDE_INT) INTVAL (operands[2])) - 1 < 3
6104    && ((unsigned HOST_WIDE_INT) INTVAL (operands[3])
6105        < ((unsigned HOST_WIDE_INT) 1 << INTVAL (operands[2])))"
6106   "#"
6107   "&& reload_completed"
6108   [(const_int 0)]
6109 {
6110   machine_mode mode = GET_MODE (operands[0]);
6111   rtx pat;
6112
6113   if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (SImode))
6114     { 
6115       mode = SImode; 
6116       operands[0] = gen_lowpart (mode, operands[0]);
6117       operands[1] = gen_lowpart (mode, operands[1]);
6118     }
6119
6120   operands[2] = GEN_INT (1 << INTVAL (operands[2]));
6121
6122   pat = plus_constant (mode, gen_rtx_MULT (mode, operands[1], operands[2]),
6123                        INTVAL (operands[3]));
6124
6125   emit_insn (gen_rtx_SET (VOIDmode, operands[0], pat));
6126   DONE;
6127 }
6128   [(set_attr "type" "lea")
6129    (set (attr "mode")
6130       (if_then_else (match_operand:DI 0)
6131         (const_string "DI")
6132         (const_string "SI")))])
6133 \f
6134 ;; Subtract instructions
6135
6136 (define_expand "sub<mode>3"
6137   [(set (match_operand:SDWIM 0 "nonimmediate_operand")
6138         (minus:SDWIM (match_operand:SDWIM 1 "nonimmediate_operand")
6139                      (match_operand:SDWIM 2 "<general_operand>")))]
6140   ""
6141   "ix86_expand_binary_operator (MINUS, <MODE>mode, operands); DONE;")
6142
6143 (define_insn_and_split "*sub<dwi>3_doubleword"
6144   [(set (match_operand:<DWI> 0 "nonimmediate_operand" "=r,o")
6145         (minus:<DWI>
6146           (match_operand:<DWI> 1 "nonimmediate_operand" "0,0")
6147           (match_operand:<DWI> 2 "<general_operand>" "ro<di>,r<di>")))
6148    (clobber (reg:CC FLAGS_REG))]
6149   "ix86_binary_operator_ok (MINUS, <MODE>mode, operands)"
6150   "#"
6151   "reload_completed"
6152   [(parallel [(set (reg:CC FLAGS_REG)
6153                    (compare:CC (match_dup 1) (match_dup 2)))
6154               (set (match_dup 0)
6155                    (minus:DWIH (match_dup 1) (match_dup 2)))])
6156    (parallel [(set (match_dup 3)
6157                    (minus:DWIH
6158                      (match_dup 4)
6159                      (plus:DWIH
6160                        (ltu:DWIH (reg:CC FLAGS_REG) (const_int 0))
6161                        (match_dup 5))))
6162               (clobber (reg:CC FLAGS_REG))])]
6163   "split_double_mode (<DWI>mode, &operands[0], 3, &operands[0], &operands[3]);")
6164
6165 (define_insn "*sub<mode>_1"
6166   [(set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>")
6167         (minus:SWI
6168           (match_operand:SWI 1 "nonimmediate_operand" "0,0")
6169           (match_operand:SWI 2 "<general_operand>" "<r><i>,<r>m")))
6170    (clobber (reg:CC FLAGS_REG))]
6171   "ix86_binary_operator_ok (MINUS, <MODE>mode, operands)"
6172   "sub{<imodesuffix>}\t{%2, %0|%0, %2}"
6173   [(set_attr "type" "alu")
6174    (set_attr "mode" "<MODE>")])
6175
6176 (define_insn "*subsi_1_zext"
6177   [(set (match_operand:DI 0 "register_operand" "=r")
6178         (zero_extend:DI
6179           (minus:SI (match_operand:SI 1 "register_operand" "0")
6180                     (match_operand:SI 2 "x86_64_general_operand" "rme"))))
6181    (clobber (reg:CC FLAGS_REG))]
6182   "TARGET_64BIT && ix86_binary_operator_ok (MINUS, SImode, operands)"
6183   "sub{l}\t{%2, %k0|%k0, %2}"
6184   [(set_attr "type" "alu")
6185    (set_attr "mode" "SI")])
6186
6187 (define_insn "*subqi_1_slp"
6188   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm,q"))
6189         (minus:QI (match_dup 0)
6190                   (match_operand:QI 1 "general_operand" "qn,qm")))
6191    (clobber (reg:CC FLAGS_REG))]
6192   "(! TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
6193    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
6194   "sub{b}\t{%1, %0|%0, %1}"
6195   [(set_attr "type" "alu1")
6196    (set_attr "mode" "QI")])
6197
6198 (define_insn "*sub<mode>_2"
6199   [(set (reg FLAGS_REG)
6200         (compare
6201           (minus:SWI
6202             (match_operand:SWI 1 "nonimmediate_operand" "0,0")
6203             (match_operand:SWI 2 "<general_operand>" "<r><i>,<r>m"))
6204           (const_int 0)))
6205    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>")
6206         (minus:SWI (match_dup 1) (match_dup 2)))]
6207   "ix86_match_ccmode (insn, CCGOCmode)
6208    && ix86_binary_operator_ok (MINUS, <MODE>mode, operands)"
6209   "sub{<imodesuffix>}\t{%2, %0|%0, %2}"
6210   [(set_attr "type" "alu")
6211    (set_attr "mode" "<MODE>")])
6212
6213 (define_insn "*subsi_2_zext"
6214   [(set (reg FLAGS_REG)
6215         (compare
6216           (minus:SI (match_operand:SI 1 "register_operand" "0")
6217                     (match_operand:SI 2 "x86_64_general_operand" "rme"))
6218           (const_int 0)))
6219    (set (match_operand:DI 0 "register_operand" "=r")
6220         (zero_extend:DI
6221           (minus:SI (match_dup 1)
6222                     (match_dup 2))))]
6223   "TARGET_64BIT && ix86_match_ccmode (insn, CCGOCmode)
6224    && ix86_binary_operator_ok (MINUS, SImode, operands)"
6225   "sub{l}\t{%2, %k0|%k0, %2}"
6226   [(set_attr "type" "alu")
6227    (set_attr "mode" "SI")])
6228
6229 ;; Subtract with jump on overflow.
6230 (define_expand "subv<mode>4"
6231   [(parallel [(set (reg:CCO FLAGS_REG)
6232                    (eq:CCO (minus:<DWI>
6233                               (sign_extend:<DWI>
6234                                  (match_operand:SWI 1 "nonimmediate_operand"))
6235                               (match_dup 4))
6236                            (sign_extend:<DWI>
6237                               (minus:SWI (match_dup 1)
6238                                          (match_operand:SWI 2
6239                                             "<general_operand>")))))
6240               (set (match_operand:SWI 0 "register_operand")
6241                    (minus:SWI (match_dup 1) (match_dup 2)))])
6242    (set (pc) (if_then_else
6243                (eq (reg:CCO FLAGS_REG) (const_int 0))
6244                (label_ref (match_operand 3))
6245                (pc)))]
6246   ""
6247 {
6248   ix86_fixup_binary_operands_no_copy (MINUS, <MODE>mode, operands);
6249   if (CONST_INT_P (operands[2]))
6250     operands[4] = operands[2];
6251   else
6252     operands[4] = gen_rtx_SIGN_EXTEND (<DWI>mode, operands[2]);
6253 })
6254
6255 (define_insn "*subv<mode>4"
6256   [(set (reg:CCO FLAGS_REG)
6257         (eq:CCO (minus:<DWI>
6258                    (sign_extend:<DWI>
6259                       (match_operand:SWI 1 "nonimmediate_operand" "0,0"))
6260                    (sign_extend:<DWI>
6261                       (match_operand:SWI 2 "<general_sext_operand>"
6262                                            "<r>We,<r>m")))
6263                 (sign_extend:<DWI>
6264                    (minus:SWI (match_dup 1) (match_dup 2)))))
6265    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>")
6266         (minus:SWI (match_dup 1) (match_dup 2)))]
6267   "ix86_binary_operator_ok (MINUS, <MODE>mode, operands)"
6268   "sub{<imodesuffix>}\t{%2, %0|%0, %2}"
6269   [(set_attr "type" "alu")
6270    (set_attr "mode" "<MODE>")])
6271
6272 (define_insn "*subv<mode>4_1"
6273   [(set (reg:CCO FLAGS_REG)
6274         (eq:CCO (minus:<DWI>
6275                    (sign_extend:<DWI>
6276                       (match_operand:SWI 1 "nonimmediate_operand" "0"))
6277                    (match_operand:<DWI> 3 "const_int_operand" "i"))
6278                 (sign_extend:<DWI>
6279                    (minus:SWI (match_dup 1)
6280                               (match_operand:SWI 2 "x86_64_immediate_operand"
6281                                                    "<i>")))))
6282    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
6283         (minus:SWI (match_dup 1) (match_dup 2)))]
6284   "ix86_binary_operator_ok (MINUS, <MODE>mode, operands)
6285    && CONST_INT_P (operands[2])
6286    && INTVAL (operands[2]) == INTVAL (operands[3])"
6287   "sub{<imodesuffix>}\t{%2, %0|%0, %2}"
6288   [(set_attr "type" "alu")
6289    (set_attr "mode" "<MODE>")
6290    (set (attr "length_immediate")
6291         (cond [(match_test "IN_RANGE (INTVAL (operands[2]), -128, 127)")
6292                   (const_string "1")
6293                (match_test "<MODE_SIZE> == 8")
6294                   (const_string "4")]
6295               (const_string "<MODE_SIZE>")))])
6296
6297 (define_insn "*sub<mode>_3"
6298   [(set (reg FLAGS_REG)
6299         (compare (match_operand:SWI 1 "nonimmediate_operand" "0,0")
6300                  (match_operand:SWI 2 "<general_operand>" "<r><i>,<r>m")))
6301    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>")
6302         (minus:SWI (match_dup 1) (match_dup 2)))]
6303   "ix86_match_ccmode (insn, CCmode)
6304    && ix86_binary_operator_ok (MINUS, <MODE>mode, operands)"
6305   "sub{<imodesuffix>}\t{%2, %0|%0, %2}"
6306   [(set_attr "type" "alu")
6307    (set_attr "mode" "<MODE>")])
6308
6309 (define_insn "*subsi_3_zext"
6310   [(set (reg FLAGS_REG)
6311         (compare (match_operand:SI 1 "register_operand" "0")
6312                  (match_operand:SI 2 "x86_64_general_operand" "rme")))
6313    (set (match_operand:DI 0 "register_operand" "=r")
6314         (zero_extend:DI
6315           (minus:SI (match_dup 1)
6316                     (match_dup 2))))]
6317   "TARGET_64BIT && ix86_match_ccmode (insn, CCmode)
6318    && ix86_binary_operator_ok (MINUS, SImode, operands)"
6319   "sub{l}\t{%2, %1|%1, %2}"
6320   [(set_attr "type" "alu")
6321    (set_attr "mode" "SI")])
6322 \f
6323 ;; Add with carry and subtract with borrow
6324
6325 (define_expand "<plusminus_insn><mode>3_carry"
6326   [(parallel
6327     [(set (match_operand:SWI 0 "nonimmediate_operand")
6328           (plusminus:SWI
6329             (match_operand:SWI 1 "nonimmediate_operand")
6330             (plus:SWI (match_operator:SWI 4 "ix86_carry_flag_operator"
6331                        [(match_operand 3 "flags_reg_operand")
6332                         (const_int 0)])
6333                       (match_operand:SWI 2 "<general_operand>"))))
6334      (clobber (reg:CC FLAGS_REG))])]
6335   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)")
6336
6337 (define_insn "*<plusminus_insn><mode>3_carry"
6338   [(set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>")
6339         (plusminus:SWI
6340           (match_operand:SWI 1 "nonimmediate_operand" "<comm>0,0")
6341           (plus:SWI
6342             (match_operator 3 "ix86_carry_flag_operator"
6343              [(reg FLAGS_REG) (const_int 0)])
6344             (match_operand:SWI 2 "<general_operand>" "<r><i>,<r>m"))))
6345    (clobber (reg:CC FLAGS_REG))]
6346   "ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
6347   "<plusminus_carry_mnemonic>{<imodesuffix>}\t{%2, %0|%0, %2}"
6348   [(set_attr "type" "alu")
6349    (set_attr "use_carry" "1")
6350    (set_attr "pent_pair" "pu")
6351    (set_attr "mode" "<MODE>")])
6352
6353 (define_insn "*addsi3_carry_zext"
6354   [(set (match_operand:DI 0 "register_operand" "=r")
6355         (zero_extend:DI
6356           (plus:SI (match_operand:SI 1 "nonimmediate_operand" "%0")
6357                    (plus:SI (match_operator 3 "ix86_carry_flag_operator"
6358                              [(reg FLAGS_REG) (const_int 0)])
6359                             (match_operand:SI 2 "x86_64_general_operand" "rme")))))
6360    (clobber (reg:CC FLAGS_REG))]
6361   "TARGET_64BIT && ix86_binary_operator_ok (PLUS, SImode, operands)"
6362   "adc{l}\t{%2, %k0|%k0, %2}"
6363   [(set_attr "type" "alu")
6364    (set_attr "use_carry" "1")
6365    (set_attr "pent_pair" "pu")
6366    (set_attr "mode" "SI")])
6367
6368 (define_insn "*subsi3_carry_zext"
6369   [(set (match_operand:DI 0 "register_operand" "=r")
6370         (zero_extend:DI
6371           (minus:SI (match_operand:SI 1 "register_operand" "0")
6372                     (plus:SI (match_operator 3 "ix86_carry_flag_operator"
6373                               [(reg FLAGS_REG) (const_int 0)])
6374                              (match_operand:SI 2 "x86_64_general_operand" "rme")))))
6375    (clobber (reg:CC FLAGS_REG))]
6376   "TARGET_64BIT && ix86_binary_operator_ok (MINUS, SImode, operands)"
6377   "sbb{l}\t{%2, %k0|%k0, %2}"
6378   [(set_attr "type" "alu")
6379    (set_attr "pent_pair" "pu")
6380    (set_attr "mode" "SI")])
6381 \f
6382 ;; ADCX instruction
6383
6384 (define_insn "adcx<mode>3"
6385   [(set (reg:CCC FLAGS_REG)
6386         (compare:CCC
6387           (plus:SWI48
6388             (match_operand:SWI48 1 "nonimmediate_operand" "%0")
6389             (plus:SWI48
6390               (match_operator 4 "ix86_carry_flag_operator"
6391                [(match_operand 3 "flags_reg_operand") (const_int 0)])
6392               (match_operand:SWI48 2 "nonimmediate_operand" "rm")))
6393           (const_int 0)))
6394    (set (match_operand:SWI48 0 "register_operand" "=r")
6395         (plus:SWI48 (match_dup 1)
6396                     (plus:SWI48 (match_op_dup 4
6397                                  [(match_dup 3) (const_int 0)])
6398                                 (match_dup 2))))]
6399   "TARGET_ADX && ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
6400   "adcx\t{%2, %0|%0, %2}"
6401   [(set_attr "type" "alu")
6402    (set_attr "use_carry" "1")
6403    (set_attr "mode" "<MODE>")])
6404 \f
6405 ;; Overflow setting add instructions
6406
6407 (define_insn "*add<mode>3_cconly_overflow"
6408   [(set (reg:CCC FLAGS_REG)
6409         (compare:CCC
6410           (plus:SWI
6411             (match_operand:SWI 1 "nonimmediate_operand" "%0")
6412             (match_operand:SWI 2 "<general_operand>" "<g>"))
6413           (match_dup 1)))
6414    (clobber (match_scratch:SWI 0 "=<r>"))]
6415   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
6416   "add{<imodesuffix>}\t{%2, %0|%0, %2}"
6417   [(set_attr "type" "alu")
6418    (set_attr "mode" "<MODE>")])
6419
6420 (define_insn "*add<mode>3_cc_overflow"
6421   [(set (reg:CCC FLAGS_REG)
6422         (compare:CCC
6423             (plus:SWI
6424                 (match_operand:SWI 1 "nonimmediate_operand" "%0,0")
6425                 (match_operand:SWI 2 "<general_operand>" "<r><i>,<r>m"))
6426             (match_dup 1)))
6427    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>")
6428         (plus:SWI (match_dup 1) (match_dup 2)))]
6429   "ix86_binary_operator_ok (PLUS, <MODE>mode, operands)"
6430   "add{<imodesuffix>}\t{%2, %0|%0, %2}"
6431   [(set_attr "type" "alu")
6432    (set_attr "mode" "<MODE>")])
6433
6434 (define_insn "*addsi3_zext_cc_overflow"
6435   [(set (reg:CCC FLAGS_REG)
6436         (compare:CCC
6437           (plus:SI
6438             (match_operand:SI 1 "nonimmediate_operand" "%0")
6439             (match_operand:SI 2 "x86_64_general_operand" "rme"))
6440           (match_dup 1)))
6441    (set (match_operand:DI 0 "register_operand" "=r")
6442         (zero_extend:DI (plus:SI (match_dup 1) (match_dup 2))))]
6443   "TARGET_64BIT && ix86_binary_operator_ok (PLUS, SImode, operands)"
6444   "add{l}\t{%2, %k0|%k0, %2}"
6445   [(set_attr "type" "alu")
6446    (set_attr "mode" "SI")])
6447
6448 ;; The patterns that match these are at the end of this file.
6449
6450 (define_expand "<plusminus_insn>xf3"
6451   [(set (match_operand:XF 0 "register_operand")
6452         (plusminus:XF
6453           (match_operand:XF 1 "register_operand")
6454           (match_operand:XF 2 "register_operand")))]
6455   "TARGET_80387")
6456
6457 (define_expand "<plusminus_insn><mode>3"
6458   [(set (match_operand:MODEF 0 "register_operand")
6459         (plusminus:MODEF
6460           (match_operand:MODEF 1 "register_operand")
6461           (match_operand:MODEF 2 "nonimmediate_operand")))]
6462   "(TARGET_80387 && X87_ENABLE_ARITH (<MODE>mode))
6463     || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)")
6464 \f
6465 ;; Multiply instructions
6466
6467 (define_expand "mul<mode>3"
6468   [(parallel [(set (match_operand:SWIM248 0 "register_operand")
6469                    (mult:SWIM248
6470                      (match_operand:SWIM248 1 "register_operand")
6471                      (match_operand:SWIM248 2 "<general_operand>")))
6472               (clobber (reg:CC FLAGS_REG))])])
6473
6474 (define_expand "mulqi3"
6475   [(parallel [(set (match_operand:QI 0 "register_operand")
6476                    (mult:QI
6477                      (match_operand:QI 1 "register_operand")
6478                      (match_operand:QI 2 "nonimmediate_operand")))
6479               (clobber (reg:CC FLAGS_REG))])]
6480   "TARGET_QIMODE_MATH")
6481
6482 ;; On AMDFAM10
6483 ;; IMUL reg32/64, reg32/64, imm8        Direct
6484 ;; IMUL reg32/64, mem32/64, imm8        VectorPath
6485 ;; IMUL reg32/64, reg32/64, imm32       Direct
6486 ;; IMUL reg32/64, mem32/64, imm32       VectorPath
6487 ;; IMUL reg32/64, reg32/64              Direct
6488 ;; IMUL reg32/64, mem32/64              Direct
6489 ;;
6490 ;; On BDVER1, all above IMULs use DirectPath
6491
6492 (define_insn "*mul<mode>3_1"
6493   [(set (match_operand:SWI48 0 "register_operand" "=r,r,r")
6494         (mult:SWI48
6495           (match_operand:SWI48 1 "nonimmediate_operand" "%rm,rm,0")
6496           (match_operand:SWI48 2 "<general_operand>" "K,<i>,mr")))
6497    (clobber (reg:CC FLAGS_REG))]
6498   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
6499   "@
6500    imul{<imodesuffix>}\t{%2, %1, %0|%0, %1, %2}
6501    imul{<imodesuffix>}\t{%2, %1, %0|%0, %1, %2}
6502    imul{<imodesuffix>}\t{%2, %0|%0, %2}"
6503   [(set_attr "type" "imul")
6504    (set_attr "prefix_0f" "0,0,1")
6505    (set (attr "athlon_decode")
6506         (cond [(eq_attr "cpu" "athlon")
6507                   (const_string "vector")
6508                (eq_attr "alternative" "1")
6509                   (const_string "vector")
6510                (and (eq_attr "alternative" "2")
6511                     (match_operand 1 "memory_operand"))
6512                   (const_string "vector")]
6513               (const_string "direct")))
6514    (set (attr "amdfam10_decode")
6515         (cond [(and (eq_attr "alternative" "0,1")
6516                     (match_operand 1 "memory_operand"))
6517                   (const_string "vector")]
6518               (const_string "direct")))
6519    (set_attr "bdver1_decode" "direct")
6520    (set_attr "mode" "<MODE>")])
6521
6522 (define_insn "*mulsi3_1_zext"
6523   [(set (match_operand:DI 0 "register_operand" "=r,r,r")
6524         (zero_extend:DI
6525           (mult:SI (match_operand:SI 1 "nonimmediate_operand" "%rm,rm,0")
6526                    (match_operand:SI 2 "x86_64_general_operand" "K,e,mr"))))
6527    (clobber (reg:CC FLAGS_REG))]
6528   "TARGET_64BIT
6529    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6530   "@
6531    imul{l}\t{%2, %1, %k0|%k0, %1, %2}
6532    imul{l}\t{%2, %1, %k0|%k0, %1, %2}
6533    imul{l}\t{%2, %k0|%k0, %2}"
6534   [(set_attr "type" "imul")
6535    (set_attr "prefix_0f" "0,0,1")
6536    (set (attr "athlon_decode")
6537         (cond [(eq_attr "cpu" "athlon")
6538                   (const_string "vector")
6539                (eq_attr "alternative" "1")
6540                   (const_string "vector")
6541                (and (eq_attr "alternative" "2")
6542                     (match_operand 1 "memory_operand"))
6543                   (const_string "vector")]
6544               (const_string "direct")))
6545    (set (attr "amdfam10_decode")
6546         (cond [(and (eq_attr "alternative" "0,1")
6547                     (match_operand 1 "memory_operand"))
6548                   (const_string "vector")]
6549               (const_string "direct")))
6550    (set_attr "bdver1_decode" "direct")
6551    (set_attr "mode" "SI")])
6552
6553 ;; On AMDFAM10
6554 ;; IMUL reg16, reg16, imm8      VectorPath
6555 ;; IMUL reg16, mem16, imm8      VectorPath
6556 ;; IMUL reg16, reg16, imm16     VectorPath
6557 ;; IMUL reg16, mem16, imm16     VectorPath
6558 ;; IMUL reg16, reg16            Direct
6559 ;; IMUL reg16, mem16            Direct
6560 ;;
6561 ;; On BDVER1, all HI MULs use DoublePath
6562
6563 (define_insn "*mulhi3_1"
6564   [(set (match_operand:HI 0 "register_operand" "=r,r,r")
6565         (mult:HI (match_operand:HI 1 "nonimmediate_operand" "%rm,rm,0")
6566                  (match_operand:HI 2 "general_operand" "K,n,mr")))
6567    (clobber (reg:CC FLAGS_REG))]
6568   "TARGET_HIMODE_MATH
6569    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6570   "@
6571    imul{w}\t{%2, %1, %0|%0, %1, %2}
6572    imul{w}\t{%2, %1, %0|%0, %1, %2}
6573    imul{w}\t{%2, %0|%0, %2}"
6574   [(set_attr "type" "imul")
6575    (set_attr "prefix_0f" "0,0,1")
6576    (set (attr "athlon_decode")
6577         (cond [(eq_attr "cpu" "athlon")
6578                   (const_string "vector")
6579                (eq_attr "alternative" "1,2")
6580                   (const_string "vector")]
6581               (const_string "direct")))
6582    (set (attr "amdfam10_decode")
6583         (cond [(eq_attr "alternative" "0,1")
6584                   (const_string "vector")]
6585               (const_string "direct")))
6586    (set_attr "bdver1_decode" "double")
6587    (set_attr "mode" "HI")])
6588
6589 ;;On AMDFAM10 and BDVER1
6590 ;; MUL reg8     Direct
6591 ;; MUL mem8     Direct
6592
6593 (define_insn "*mulqi3_1"
6594   [(set (match_operand:QI 0 "register_operand" "=a")
6595         (mult:QI (match_operand:QI 1 "nonimmediate_operand" "%0")
6596                  (match_operand:QI 2 "nonimmediate_operand" "qm")))
6597    (clobber (reg:CC FLAGS_REG))]
6598   "TARGET_QIMODE_MATH
6599    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6600   "mul{b}\t%2"
6601   [(set_attr "type" "imul")
6602    (set_attr "length_immediate" "0")
6603    (set (attr "athlon_decode")
6604      (if_then_else (eq_attr "cpu" "athlon")
6605         (const_string "vector")
6606         (const_string "direct")))
6607    (set_attr "amdfam10_decode" "direct")
6608    (set_attr "bdver1_decode" "direct")
6609    (set_attr "mode" "QI")])
6610
6611 ;; Multiply with jump on overflow.
6612 (define_expand "mulv<mode>4"
6613   [(parallel [(set (reg:CCO FLAGS_REG)
6614                    (eq:CCO (mult:<DWI>
6615                               (sign_extend:<DWI>
6616                                  (match_operand:SWI48 1 "register_operand"))
6617                               (match_dup 4))
6618                            (sign_extend:<DWI>
6619                               (mult:SWI48 (match_dup 1)
6620                                           (match_operand:SWI48 2
6621                                              "<general_operand>")))))
6622               (set (match_operand:SWI48 0 "register_operand")
6623                    (mult:SWI48 (match_dup 1) (match_dup 2)))])
6624    (set (pc) (if_then_else
6625                (eq (reg:CCO FLAGS_REG) (const_int 0))
6626                (label_ref (match_operand 3))
6627                (pc)))]
6628   ""
6629 {
6630   if (CONST_INT_P (operands[2]))
6631     operands[4] = operands[2];
6632   else
6633     operands[4] = gen_rtx_SIGN_EXTEND (<DWI>mode, operands[2]);
6634 })
6635
6636 (define_insn "*mulv<mode>4"
6637   [(set (reg:CCO FLAGS_REG)
6638         (eq:CCO (mult:<DWI>
6639                    (sign_extend:<DWI>
6640                       (match_operand:SWI48 1 "nonimmediate_operand" "%rm,0"))
6641                    (sign_extend:<DWI>
6642                       (match_operand:SWI48 2 "<general_sext_operand>"
6643                                              "We,mr")))
6644                 (sign_extend:<DWI>
6645                    (mult:SWI48 (match_dup 1) (match_dup 2)))))
6646    (set (match_operand:SWI48 0 "register_operand" "=r,r")
6647         (mult:SWI48 (match_dup 1) (match_dup 2)))]
6648   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
6649   "@
6650    imul{<imodesuffix>}\t{%2, %1, %0|%0, %1, %2}
6651    imul{<imodesuffix>}\t{%2, %0|%0, %2}"
6652   [(set_attr "type" "imul")
6653    (set_attr "prefix_0f" "0,1")
6654    (set (attr "athlon_decode")
6655         (cond [(eq_attr "cpu" "athlon")
6656                   (const_string "vector")
6657                (eq_attr "alternative" "0")
6658                   (const_string "vector")
6659                (and (eq_attr "alternative" "1")
6660                     (match_operand 1 "memory_operand"))
6661                   (const_string "vector")]
6662               (const_string "direct")))
6663    (set (attr "amdfam10_decode")
6664         (cond [(and (eq_attr "alternative" "1")
6665                     (match_operand 1 "memory_operand"))
6666                   (const_string "vector")]
6667               (const_string "direct")))
6668    (set_attr "bdver1_decode" "direct")
6669    (set_attr "mode" "<MODE>")])
6670
6671 (define_insn "*mulv<mode>4_1"
6672   [(set (reg:CCO FLAGS_REG)
6673         (eq:CCO (mult:<DWI>
6674                    (sign_extend:<DWI>
6675                       (match_operand:SWI48 1 "nonimmediate_operand" "rm,rm"))
6676                    (match_operand:<DWI> 3 "const_int_operand" "K,i"))
6677                 (sign_extend:<DWI>
6678                    (mult:SWI48 (match_dup 1)
6679                                (match_operand:SWI 2 "x86_64_immediate_operand"
6680                                                     "K,<i>")))))
6681    (set (match_operand:SWI48 0 "register_operand" "=r,r")
6682         (mult:SWI48 (match_dup 1) (match_dup 2)))]
6683   "!(MEM_P (operands[1]) && MEM_P (operands[2]))
6684    && CONST_INT_P (operands[2])
6685    && INTVAL (operands[2]) == INTVAL (operands[3])"
6686   "@
6687    imul{<imodesuffix>}\t{%2, %1, %0|%0, %1, %2}
6688    imul{<imodesuffix>}\t{%2, %1, %0|%0, %1, %2}"
6689   [(set_attr "type" "imul")
6690    (set (attr "athlon_decode")
6691         (cond [(eq_attr "cpu" "athlon")
6692                   (const_string "vector")
6693                (eq_attr "alternative" "1")
6694                   (const_string "vector")]
6695               (const_string "direct")))
6696    (set (attr "amdfam10_decode")
6697         (cond [(match_operand 1 "memory_operand")
6698                   (const_string "vector")]
6699               (const_string "direct")))
6700    (set_attr "bdver1_decode" "direct")
6701    (set_attr "mode" "<MODE>")
6702    (set (attr "length_immediate")
6703         (cond [(match_test "IN_RANGE (INTVAL (operands[2]), -128, 127)")
6704                   (const_string "1")
6705                (match_test "<MODE_SIZE> == 8")
6706                   (const_string "4")]
6707               (const_string "<MODE_SIZE>")))])
6708
6709 (define_expand "umulv<mode>4"
6710   [(parallel [(set (reg:CCO FLAGS_REG)
6711                    (eq:CCO (mult:<DWI>
6712                               (zero_extend:<DWI>
6713                                  (match_operand:SWI48 1
6714                                                       "nonimmediate_operand"))
6715                               (zero_extend:<DWI>
6716                                  (match_operand:SWI48 2
6717                                                       "nonimmediate_operand")))
6718                            (zero_extend:<DWI>
6719                               (mult:SWI48 (match_dup 1) (match_dup 2)))))
6720               (set (match_operand:SWI48 0 "register_operand")
6721                    (mult:SWI48 (match_dup 1) (match_dup 2)))
6722               (clobber (match_scratch:SWI48 4))])
6723    (set (pc) (if_then_else
6724                (eq (reg:CCO FLAGS_REG) (const_int 0))
6725                (label_ref (match_operand 3))
6726                (pc)))]
6727   ""
6728 {
6729   if (MEM_P (operands[1]) && MEM_P (operands[2]))
6730     operands[1] = force_reg (<MODE>mode, operands[1]);
6731 })
6732
6733 (define_insn "*umulv<mode>4"
6734   [(set (reg:CCO FLAGS_REG)
6735         (eq:CCO (mult:<DWI>
6736                    (zero_extend:<DWI>
6737                       (match_operand:SWI48 1 "nonimmediate_operand" "%0"))
6738                    (zero_extend:<DWI>
6739                       (match_operand:SWI48 2 "nonimmediate_operand" "rm")))
6740                 (zero_extend:<DWI>
6741                    (mult:SWI48 (match_dup 1) (match_dup 2)))))
6742    (set (match_operand:SWI48 0 "register_operand" "=a")
6743         (mult:SWI48 (match_dup 1) (match_dup 2)))
6744    (clobber (match_scratch:SWI48 3 "=d"))]
6745   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
6746   "mul{<imodesuffix>}\t%2"
6747   [(set_attr "type" "imul")
6748    (set_attr "length_immediate" "0")
6749    (set (attr "athlon_decode")
6750      (if_then_else (eq_attr "cpu" "athlon")
6751        (const_string "vector")
6752        (const_string "double")))
6753    (set_attr "amdfam10_decode" "double")
6754    (set_attr "bdver1_decode" "direct")
6755    (set_attr "mode" "<MODE>")])
6756
6757 (define_expand "<u>mulvqi4"
6758   [(parallel [(set (reg:CCO FLAGS_REG)
6759                    (eq:CCO (mult:HI
6760                               (any_extend:HI
6761                                  (match_operand:QI 1 "nonimmediate_operand"))
6762                               (any_extend:HI
6763                                  (match_operand:QI 2 "nonimmediate_operand")))
6764                            (any_extend:HI
6765                               (mult:QI (match_dup 1) (match_dup 2)))))
6766               (set (match_operand:QI 0 "register_operand")
6767                    (mult:QI (match_dup 1) (match_dup 2)))])
6768    (set (pc) (if_then_else
6769                (eq (reg:CCO FLAGS_REG) (const_int 0))
6770                (label_ref (match_operand 3))
6771                (pc)))]
6772   "TARGET_QIMODE_MATH"
6773 {
6774   if (MEM_P (operands[1]) && MEM_P (operands[2]))
6775     operands[1] = force_reg (QImode, operands[1]);
6776 })
6777
6778 (define_insn "*<u>mulvqi4"
6779   [(set (reg:CCO FLAGS_REG)
6780         (eq:CCO (mult:HI
6781                    (any_extend:HI
6782                       (match_operand:QI 1 "nonimmediate_operand" "%0"))
6783                    (any_extend:HI
6784                       (match_operand:QI 2 "nonimmediate_operand" "qm")))
6785                 (any_extend:HI
6786                    (mult:QI (match_dup 1) (match_dup 2)))))
6787    (set (match_operand:QI 0 "register_operand" "=a")
6788         (mult:QI (match_dup 1) (match_dup 2)))]
6789   "TARGET_QIMODE_MATH
6790    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6791   "<sgnprefix>mul{b}\t%2"
6792   [(set_attr "type" "imul")
6793    (set_attr "length_immediate" "0")
6794    (set (attr "athlon_decode")
6795      (if_then_else (eq_attr "cpu" "athlon")
6796         (const_string "vector")
6797         (const_string "direct")))
6798    (set_attr "amdfam10_decode" "direct")
6799    (set_attr "bdver1_decode" "direct")
6800    (set_attr "mode" "QI")])
6801
6802 (define_expand "<u>mul<mode><dwi>3"
6803   [(parallel [(set (match_operand:<DWI> 0 "register_operand")
6804                    (mult:<DWI>
6805                      (any_extend:<DWI>
6806                        (match_operand:DWIH 1 "nonimmediate_operand"))
6807                      (any_extend:<DWI>
6808                        (match_operand:DWIH 2 "register_operand"))))
6809               (clobber (reg:CC FLAGS_REG))])])
6810
6811 (define_expand "<u>mulqihi3"
6812   [(parallel [(set (match_operand:HI 0 "register_operand")
6813                    (mult:HI
6814                      (any_extend:HI
6815                        (match_operand:QI 1 "nonimmediate_operand"))
6816                      (any_extend:HI
6817                        (match_operand:QI 2 "register_operand"))))
6818               (clobber (reg:CC FLAGS_REG))])]
6819   "TARGET_QIMODE_MATH")
6820
6821 (define_insn "*bmi2_umulditi3_1"
6822   [(set (match_operand:DI 0 "register_operand" "=r")
6823         (mult:DI
6824           (match_operand:DI 2 "nonimmediate_operand" "%d")
6825           (match_operand:DI 3 "nonimmediate_operand" "rm")))
6826    (set (match_operand:DI 1 "register_operand" "=r")
6827         (truncate:DI
6828           (lshiftrt:TI
6829             (mult:TI (zero_extend:TI (match_dup 2))
6830                      (zero_extend:TI (match_dup 3)))
6831             (const_int 64))))]
6832   "TARGET_64BIT && TARGET_BMI2
6833    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6834   "mulx\t{%3, %0, %1|%1, %0, %3}"
6835   [(set_attr "type" "imulx")
6836    (set_attr "prefix" "vex")
6837    (set_attr "mode" "DI")])
6838
6839 (define_insn "*bmi2_umulsidi3_1"
6840   [(set (match_operand:SI 0 "register_operand" "=r")
6841         (mult:SI
6842           (match_operand:SI 2 "nonimmediate_operand" "%d")
6843           (match_operand:SI 3 "nonimmediate_operand" "rm")))
6844    (set (match_operand:SI 1 "register_operand" "=r")
6845         (truncate:SI
6846           (lshiftrt:DI
6847             (mult:DI (zero_extend:DI (match_dup 2))
6848                      (zero_extend:DI (match_dup 3)))
6849             (const_int 32))))]
6850   "!TARGET_64BIT && TARGET_BMI2
6851    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6852   "mulx\t{%3, %0, %1|%1, %0, %3}"
6853   [(set_attr "type" "imulx")
6854    (set_attr "prefix" "vex")
6855    (set_attr "mode" "SI")])
6856
6857 (define_insn "*umul<mode><dwi>3_1"
6858   [(set (match_operand:<DWI> 0 "register_operand" "=r,A")
6859         (mult:<DWI>
6860           (zero_extend:<DWI>
6861             (match_operand:DWIH 1 "nonimmediate_operand" "%d,0"))
6862           (zero_extend:<DWI>
6863             (match_operand:DWIH 2 "nonimmediate_operand" "rm,rm"))))
6864    (clobber (reg:CC FLAGS_REG))]
6865   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
6866   "@
6867    #
6868    mul{<imodesuffix>}\t%2"
6869   [(set_attr "isa" "bmi2,*")
6870    (set_attr "type" "imulx,imul")
6871    (set_attr "length_immediate" "*,0")
6872    (set (attr "athlon_decode")
6873         (cond [(eq_attr "alternative" "1")
6874                  (if_then_else (eq_attr "cpu" "athlon")
6875                    (const_string "vector")
6876                    (const_string "double"))]
6877               (const_string "*")))
6878    (set_attr "amdfam10_decode" "*,double")
6879    (set_attr "bdver1_decode" "*,direct")
6880    (set_attr "prefix" "vex,orig")
6881    (set_attr "mode" "<MODE>")])
6882
6883 ;; Convert mul to the mulx pattern to avoid flags dependency.
6884 (define_split
6885  [(set (match_operand:<DWI> 0 "register_operand")
6886        (mult:<DWI>
6887          (zero_extend:<DWI>
6888            (match_operand:DWIH 1 "register_operand"))
6889          (zero_extend:<DWI>
6890            (match_operand:DWIH 2 "nonimmediate_operand"))))
6891   (clobber (reg:CC FLAGS_REG))]
6892  "TARGET_BMI2 && reload_completed
6893   && true_regnum (operands[1]) == DX_REG"
6894   [(parallel [(set (match_dup 3)
6895                    (mult:DWIH (match_dup 1) (match_dup 2)))
6896               (set (match_dup 4)
6897                    (truncate:DWIH
6898                      (lshiftrt:<DWI>
6899                        (mult:<DWI> (zero_extend:<DWI> (match_dup 1))
6900                                    (zero_extend:<DWI> (match_dup 2)))
6901                        (match_dup 5))))])]
6902 {
6903   split_double_mode (<DWI>mode, &operands[0], 1, &operands[3], &operands[4]);
6904
6905   operands[5] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode));
6906 })
6907
6908 (define_insn "*mul<mode><dwi>3_1"
6909   [(set (match_operand:<DWI> 0 "register_operand" "=A")
6910         (mult:<DWI>
6911           (sign_extend:<DWI>
6912             (match_operand:DWIH 1 "nonimmediate_operand" "%0"))
6913           (sign_extend:<DWI>
6914             (match_operand:DWIH 2 "nonimmediate_operand" "rm"))))
6915    (clobber (reg:CC FLAGS_REG))]
6916   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
6917   "imul{<imodesuffix>}\t%2"
6918   [(set_attr "type" "imul")
6919    (set_attr "length_immediate" "0")
6920    (set (attr "athlon_decode")
6921      (if_then_else (eq_attr "cpu" "athlon")
6922         (const_string "vector")
6923         (const_string "double")))
6924    (set_attr "amdfam10_decode" "double")
6925    (set_attr "bdver1_decode" "direct")
6926    (set_attr "mode" "<MODE>")])
6927
6928 (define_insn "*<u>mulqihi3_1"
6929   [(set (match_operand:HI 0 "register_operand" "=a")
6930         (mult:HI
6931           (any_extend:HI
6932             (match_operand:QI 1 "nonimmediate_operand" "%0"))
6933           (any_extend:HI
6934             (match_operand:QI 2 "nonimmediate_operand" "qm"))))
6935    (clobber (reg:CC FLAGS_REG))]
6936   "TARGET_QIMODE_MATH
6937    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6938   "<sgnprefix>mul{b}\t%2"
6939   [(set_attr "type" "imul")
6940    (set_attr "length_immediate" "0")
6941    (set (attr "athlon_decode")
6942      (if_then_else (eq_attr "cpu" "athlon")
6943         (const_string "vector")
6944         (const_string "direct")))
6945    (set_attr "amdfam10_decode" "direct")
6946    (set_attr "bdver1_decode" "direct")
6947    (set_attr "mode" "QI")])
6948
6949 (define_expand "<s>mul<mode>3_highpart"
6950   [(parallel [(set (match_operand:SWI48 0 "register_operand")
6951                    (truncate:SWI48
6952                      (lshiftrt:<DWI>
6953                        (mult:<DWI>
6954                          (any_extend:<DWI>
6955                            (match_operand:SWI48 1 "nonimmediate_operand"))
6956                          (any_extend:<DWI>
6957                            (match_operand:SWI48 2 "register_operand")))
6958                        (match_dup 4))))
6959               (clobber (match_scratch:SWI48 3))
6960               (clobber (reg:CC FLAGS_REG))])]
6961   ""
6962   "operands[4] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode));")
6963
6964 (define_insn "*<s>muldi3_highpart_1"
6965   [(set (match_operand:DI 0 "register_operand" "=d")
6966         (truncate:DI
6967           (lshiftrt:TI
6968             (mult:TI
6969               (any_extend:TI
6970                 (match_operand:DI 1 "nonimmediate_operand" "%a"))
6971               (any_extend:TI
6972                 (match_operand:DI 2 "nonimmediate_operand" "rm")))
6973             (const_int 64))))
6974    (clobber (match_scratch:DI 3 "=1"))
6975    (clobber (reg:CC FLAGS_REG))]
6976   "TARGET_64BIT
6977    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
6978   "<sgnprefix>mul{q}\t%2"
6979   [(set_attr "type" "imul")
6980    (set_attr "length_immediate" "0")
6981    (set (attr "athlon_decode")
6982      (if_then_else (eq_attr "cpu" "athlon")
6983         (const_string "vector")
6984         (const_string "double")))
6985    (set_attr "amdfam10_decode" "double")
6986    (set_attr "bdver1_decode" "direct")
6987    (set_attr "mode" "DI")])
6988
6989 (define_insn "*<s>mulsi3_highpart_1"
6990   [(set (match_operand:SI 0 "register_operand" "=d")
6991         (truncate:SI
6992           (lshiftrt:DI
6993             (mult:DI
6994               (any_extend:DI
6995                 (match_operand:SI 1 "nonimmediate_operand" "%a"))
6996               (any_extend:DI
6997                 (match_operand:SI 2 "nonimmediate_operand" "rm")))
6998             (const_int 32))))
6999    (clobber (match_scratch:SI 3 "=1"))
7000    (clobber (reg:CC FLAGS_REG))]
7001   "!(MEM_P (operands[1]) && MEM_P (operands[2]))"
7002   "<sgnprefix>mul{l}\t%2"
7003   [(set_attr "type" "imul")
7004    (set_attr "length_immediate" "0")
7005    (set (attr "athlon_decode")
7006      (if_then_else (eq_attr "cpu" "athlon")
7007         (const_string "vector")
7008         (const_string "double")))
7009    (set_attr "amdfam10_decode" "double")
7010    (set_attr "bdver1_decode" "direct")
7011    (set_attr "mode" "SI")])
7012
7013 (define_insn "*<s>mulsi3_highpart_zext"
7014   [(set (match_operand:DI 0 "register_operand" "=d")
7015         (zero_extend:DI (truncate:SI
7016           (lshiftrt:DI
7017             (mult:DI (any_extend:DI
7018                        (match_operand:SI 1 "nonimmediate_operand" "%a"))
7019                      (any_extend:DI
7020                        (match_operand:SI 2 "nonimmediate_operand" "rm")))
7021             (const_int 32)))))
7022    (clobber (match_scratch:SI 3 "=1"))
7023    (clobber (reg:CC FLAGS_REG))]
7024   "TARGET_64BIT
7025    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
7026   "<sgnprefix>mul{l}\t%2"
7027   [(set_attr "type" "imul")
7028    (set_attr "length_immediate" "0")
7029    (set (attr "athlon_decode")
7030      (if_then_else (eq_attr "cpu" "athlon")
7031         (const_string "vector")
7032         (const_string "double")))
7033    (set_attr "amdfam10_decode" "double")
7034    (set_attr "bdver1_decode" "direct")
7035    (set_attr "mode" "SI")])
7036
7037 ;; The patterns that match these are at the end of this file.
7038
7039 (define_expand "mulxf3"
7040   [(set (match_operand:XF 0 "register_operand")
7041         (mult:XF (match_operand:XF 1 "register_operand")
7042                  (match_operand:XF 2 "register_operand")))]
7043   "TARGET_80387")
7044
7045 (define_expand "mul<mode>3"
7046   [(set (match_operand:MODEF 0 "register_operand")
7047         (mult:MODEF (match_operand:MODEF 1 "register_operand")
7048                     (match_operand:MODEF 2 "nonimmediate_operand")))]
7049   "(TARGET_80387 && X87_ENABLE_ARITH (<MODE>mode))
7050     || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)")
7051 \f
7052 ;; Divide instructions
7053
7054 ;; The patterns that match these are at the end of this file.
7055
7056 (define_expand "divxf3"
7057   [(set (match_operand:XF 0 "register_operand")
7058         (div:XF (match_operand:XF 1 "register_operand")
7059                 (match_operand:XF 2 "register_operand")))]
7060   "TARGET_80387")
7061
7062 (define_expand "divdf3"
7063   [(set (match_operand:DF 0 "register_operand")
7064         (div:DF (match_operand:DF 1 "register_operand")
7065                 (match_operand:DF 2 "nonimmediate_operand")))]
7066    "(TARGET_80387 && X87_ENABLE_ARITH (DFmode))
7067     || (TARGET_SSE2 && TARGET_SSE_MATH)")
7068
7069 (define_expand "divsf3"
7070   [(set (match_operand:SF 0 "register_operand")
7071         (div:SF (match_operand:SF 1 "register_operand")
7072                 (match_operand:SF 2 "nonimmediate_operand")))]
7073   "(TARGET_80387 && X87_ENABLE_ARITH (SFmode))
7074     || TARGET_SSE_MATH"
7075 {
7076   if (TARGET_SSE_MATH
7077       && TARGET_RECIP_DIV
7078       && optimize_insn_for_speed_p ()
7079       && flag_finite_math_only && !flag_trapping_math
7080       && flag_unsafe_math_optimizations)
7081     {
7082       ix86_emit_swdivsf (operands[0], operands[1],
7083                          operands[2], SFmode);
7084       DONE;
7085     }
7086 })
7087 \f
7088 ;; Divmod instructions.
7089
7090 (define_expand "divmod<mode>4"
7091   [(parallel [(set (match_operand:SWIM248 0 "register_operand")
7092                    (div:SWIM248
7093                      (match_operand:SWIM248 1 "register_operand")
7094                      (match_operand:SWIM248 2 "nonimmediate_operand")))
7095               (set (match_operand:SWIM248 3 "register_operand")
7096                    (mod:SWIM248 (match_dup 1) (match_dup 2)))
7097               (clobber (reg:CC FLAGS_REG))])])
7098
7099 ;; Split with 8bit unsigned divide:
7100 ;;      if (dividend an divisor are in [0-255])
7101 ;;         use 8bit unsigned integer divide
7102 ;;       else
7103 ;;         use original integer divide
7104 (define_split
7105   [(set (match_operand:SWI48 0 "register_operand")
7106         (div:SWI48 (match_operand:SWI48 2 "register_operand")
7107                     (match_operand:SWI48 3 "nonimmediate_operand")))
7108    (set (match_operand:SWI48 1 "register_operand")
7109         (mod:SWI48 (match_dup 2) (match_dup 3)))
7110    (clobber (reg:CC FLAGS_REG))]
7111   "TARGET_USE_8BIT_IDIV
7112    && TARGET_QIMODE_MATH
7113    && can_create_pseudo_p ()
7114    && !optimize_insn_for_size_p ()"
7115   [(const_int 0)]
7116   "ix86_split_idivmod (<MODE>mode, operands, true); DONE;")
7117
7118 (define_insn_and_split "divmod<mode>4_1"
7119   [(set (match_operand:SWI48 0 "register_operand" "=a")
7120         (div:SWI48 (match_operand:SWI48 2 "register_operand" "0")
7121                    (match_operand:SWI48 3 "nonimmediate_operand" "rm")))
7122    (set (match_operand:SWI48 1 "register_operand" "=&d")
7123         (mod:SWI48 (match_dup 2) (match_dup 3)))
7124    (unspec [(const_int 0)] UNSPEC_DIV_ALREADY_SPLIT)
7125    (clobber (reg:CC FLAGS_REG))]
7126   ""
7127   "#"
7128   "reload_completed"
7129   [(parallel [(set (match_dup 1)
7130                    (ashiftrt:SWI48 (match_dup 4) (match_dup 5)))
7131               (clobber (reg:CC FLAGS_REG))])
7132    (parallel [(set (match_dup 0)
7133                    (div:SWI48 (match_dup 2) (match_dup 3)))
7134               (set (match_dup 1)
7135                    (mod:SWI48 (match_dup 2) (match_dup 3)))
7136               (use (match_dup 1))
7137               (clobber (reg:CC FLAGS_REG))])]
7138 {
7139   operands[5] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode)-1);
7140
7141   if (optimize_function_for_size_p (cfun) || TARGET_USE_CLTD)
7142     operands[4] = operands[2];
7143   else
7144     {
7145       /* Avoid use of cltd in favor of a mov+shift.  */
7146       emit_move_insn (operands[1], operands[2]);
7147       operands[4] = operands[1];
7148     }
7149 }
7150   [(set_attr "type" "multi")
7151    (set_attr "mode" "<MODE>")])
7152
7153 (define_insn_and_split "*divmod<mode>4"
7154   [(set (match_operand:SWIM248 0 "register_operand" "=a")
7155         (div:SWIM248 (match_operand:SWIM248 2 "register_operand" "0")
7156                     (match_operand:SWIM248 3 "nonimmediate_operand" "rm")))
7157    (set (match_operand:SWIM248 1 "register_operand" "=&d")
7158         (mod:SWIM248 (match_dup 2) (match_dup 3)))
7159    (clobber (reg:CC FLAGS_REG))]
7160   ""
7161   "#"
7162   "reload_completed"
7163   [(parallel [(set (match_dup 1)
7164                    (ashiftrt:SWIM248 (match_dup 4) (match_dup 5)))
7165               (clobber (reg:CC FLAGS_REG))])
7166    (parallel [(set (match_dup 0)
7167                    (div:SWIM248 (match_dup 2) (match_dup 3)))
7168               (set (match_dup 1)
7169                    (mod:SWIM248 (match_dup 2) (match_dup 3)))
7170               (use (match_dup 1))
7171               (clobber (reg:CC FLAGS_REG))])]
7172 {
7173   operands[5] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode)-1);
7174
7175   if (<MODE>mode != HImode
7176       && (optimize_function_for_size_p (cfun) || TARGET_USE_CLTD))
7177     operands[4] = operands[2];
7178   else
7179     {
7180       /* Avoid use of cltd in favor of a mov+shift.  */
7181       emit_move_insn (operands[1], operands[2]);
7182       operands[4] = operands[1];
7183     }
7184 }
7185   [(set_attr "type" "multi")
7186    (set_attr "mode" "<MODE>")])
7187
7188 (define_insn "*divmod<mode>4_noext"
7189   [(set (match_operand:SWIM248 0 "register_operand" "=a")
7190         (div:SWIM248 (match_operand:SWIM248 2 "register_operand" "0")
7191                     (match_operand:SWIM248 3 "nonimmediate_operand" "rm")))
7192    (set (match_operand:SWIM248 1 "register_operand" "=d")
7193         (mod:SWIM248 (match_dup 2) (match_dup 3)))
7194    (use (match_operand:SWIM248 4 "register_operand" "1"))
7195    (clobber (reg:CC FLAGS_REG))]
7196   ""
7197   "idiv{<imodesuffix>}\t%3"
7198   [(set_attr "type" "idiv")
7199    (set_attr "mode" "<MODE>")])
7200
7201 (define_expand "divmodqi4"
7202   [(parallel [(set (match_operand:QI 0 "register_operand")
7203                    (div:QI
7204                      (match_operand:QI 1 "register_operand")
7205                      (match_operand:QI 2 "nonimmediate_operand")))
7206               (set (match_operand:QI 3 "register_operand")
7207                    (mod:QI (match_dup 1) (match_dup 2)))
7208               (clobber (reg:CC FLAGS_REG))])]
7209   "TARGET_QIMODE_MATH"
7210 {
7211   rtx div, mod, insn;
7212   rtx tmp0, tmp1;
7213   
7214   tmp0 = gen_reg_rtx (HImode);
7215   tmp1 = gen_reg_rtx (HImode);
7216
7217   /* Extend operands[1] to HImode.  Generate 8bit divide.  Result is
7218      in AX.  */
7219   emit_insn (gen_extendqihi2 (tmp1, operands[1]));
7220   emit_insn (gen_divmodhiqi3 (tmp0, tmp1, operands[2]));
7221
7222   /* Extract remainder from AH.  */
7223   tmp1 = gen_rtx_SIGN_EXTRACT (QImode, tmp0, GEN_INT (8), GEN_INT (8));
7224   insn = emit_move_insn (operands[3], tmp1);
7225
7226   mod = gen_rtx_MOD (QImode, operands[1], operands[2]);
7227   set_unique_reg_note (insn, REG_EQUAL, mod);
7228
7229   /* Extract quotient from AL.  */
7230   insn = emit_move_insn (operands[0], gen_lowpart (QImode, tmp0));
7231
7232   div = gen_rtx_DIV (QImode, operands[1], operands[2]);
7233   set_unique_reg_note (insn, REG_EQUAL, div);
7234
7235   DONE;
7236 })
7237
7238 ;; Divide AX by r/m8, with result stored in
7239 ;; AL <- Quotient
7240 ;; AH <- Remainder
7241 ;; Change div/mod to HImode and extend the second argument to HImode
7242 ;; so that mode of div/mod matches with mode of arguments.  Otherwise
7243 ;; combine may fail.
7244 (define_insn "divmodhiqi3"
7245   [(set (match_operand:HI 0 "register_operand" "=a")
7246         (ior:HI
7247           (ashift:HI
7248             (zero_extend:HI
7249               (truncate:QI
7250                 (mod:HI (match_operand:HI 1 "register_operand" "0")
7251                         (sign_extend:HI
7252                           (match_operand:QI 2 "nonimmediate_operand" "qm")))))
7253             (const_int 8))
7254           (zero_extend:HI
7255             (truncate:QI
7256               (div:HI (match_dup 1) (sign_extend:HI (match_dup 2)))))))
7257    (clobber (reg:CC FLAGS_REG))]
7258   "TARGET_QIMODE_MATH"
7259   "idiv{b}\t%2"
7260   [(set_attr "type" "idiv")
7261    (set_attr "mode" "QI")])
7262
7263 (define_expand "udivmod<mode>4"
7264   [(parallel [(set (match_operand:SWIM248 0 "register_operand")
7265                    (udiv:SWIM248
7266                      (match_operand:SWIM248 1 "register_operand")
7267                      (match_operand:SWIM248 2 "nonimmediate_operand")))
7268               (set (match_operand:SWIM248 3 "register_operand")
7269                    (umod:SWIM248 (match_dup 1) (match_dup 2)))
7270               (clobber (reg:CC FLAGS_REG))])])
7271
7272 ;; Split with 8bit unsigned divide:
7273 ;;      if (dividend an divisor are in [0-255])
7274 ;;         use 8bit unsigned integer divide
7275 ;;       else
7276 ;;         use original integer divide
7277 (define_split
7278   [(set (match_operand:SWI48 0 "register_operand")
7279         (udiv:SWI48 (match_operand:SWI48 2 "register_operand")
7280                     (match_operand:SWI48 3 "nonimmediate_operand")))
7281    (set (match_operand:SWI48 1 "register_operand")
7282         (umod:SWI48 (match_dup 2) (match_dup 3)))
7283    (clobber (reg:CC FLAGS_REG))]
7284   "TARGET_USE_8BIT_IDIV
7285    && TARGET_QIMODE_MATH
7286    && can_create_pseudo_p ()
7287    && !optimize_insn_for_size_p ()"
7288   [(const_int 0)]
7289   "ix86_split_idivmod (<MODE>mode, operands, false); DONE;")
7290
7291 (define_insn_and_split "udivmod<mode>4_1"
7292   [(set (match_operand:SWI48 0 "register_operand" "=a")
7293         (udiv:SWI48 (match_operand:SWI48 2 "register_operand" "0")
7294                     (match_operand:SWI48 3 "nonimmediate_operand" "rm")))
7295    (set (match_operand:SWI48 1 "register_operand" "=&d")
7296         (umod:SWI48 (match_dup 2) (match_dup 3)))
7297    (unspec [(const_int 0)] UNSPEC_DIV_ALREADY_SPLIT)
7298    (clobber (reg:CC FLAGS_REG))]
7299   ""
7300   "#"
7301   "reload_completed"
7302   [(set (match_dup 1) (const_int 0))
7303    (parallel [(set (match_dup 0)
7304                    (udiv:SWI48 (match_dup 2) (match_dup 3)))
7305               (set (match_dup 1)
7306                    (umod:SWI48 (match_dup 2) (match_dup 3)))
7307               (use (match_dup 1))
7308               (clobber (reg:CC FLAGS_REG))])]
7309   ""
7310   [(set_attr "type" "multi")
7311    (set_attr "mode" "<MODE>")])
7312
7313 (define_insn_and_split "*udivmod<mode>4"
7314   [(set (match_operand:SWIM248 0 "register_operand" "=a")
7315         (udiv:SWIM248 (match_operand:SWIM248 2 "register_operand" "0")
7316                       (match_operand:SWIM248 3 "nonimmediate_operand" "rm")))
7317    (set (match_operand:SWIM248 1 "register_operand" "=&d")
7318         (umod:SWIM248 (match_dup 2) (match_dup 3)))
7319    (clobber (reg:CC FLAGS_REG))]
7320   ""
7321   "#"
7322   "reload_completed"
7323   [(set (match_dup 1) (const_int 0))
7324    (parallel [(set (match_dup 0)
7325                    (udiv:SWIM248 (match_dup 2) (match_dup 3)))
7326               (set (match_dup 1)
7327                    (umod:SWIM248 (match_dup 2) (match_dup 3)))
7328               (use (match_dup 1))
7329               (clobber (reg:CC FLAGS_REG))])]
7330   ""
7331   [(set_attr "type" "multi")
7332    (set_attr "mode" "<MODE>")])
7333
7334 ;; Optimize division or modulo by constant power of 2, if the constant
7335 ;; materializes only after expansion.
7336 (define_insn_and_split "*udivmod<mode>4_pow2"
7337   [(set (match_operand:SWI48 0 "register_operand" "=r")
7338         (udiv:SWI48 (match_operand:SWI48 2 "register_operand" "0")
7339                     (match_operand:SWI48 3 "const_int_operand" "n")))
7340    (set (match_operand:SWI48 1 "register_operand" "=r")
7341         (umod:SWI48 (match_dup 2) (match_dup 3)))
7342    (clobber (reg:CC FLAGS_REG))]
7343   "UINTVAL (operands[3]) - 2 < <MODE_SIZE> * BITS_PER_UNIT
7344    && (UINTVAL (operands[3]) & (UINTVAL (operands[3]) - 1)) == 0"
7345   "#"
7346   "&& 1"
7347   [(set (match_dup 1) (match_dup 2))
7348    (parallel [(set (match_dup 0) (lshiftrt:<MODE> (match_dup 2) (match_dup 4)))
7349               (clobber (reg:CC FLAGS_REG))])
7350    (parallel [(set (match_dup 1) (and:<MODE> (match_dup 1) (match_dup 5)))
7351               (clobber (reg:CC FLAGS_REG))])]
7352 {
7353   int v = exact_log2 (UINTVAL (operands[3]));
7354   operands[4] = GEN_INT (v);
7355   operands[5] = GEN_INT ((HOST_WIDE_INT_1U << v) - 1);
7356 }
7357   [(set_attr "type" "multi")
7358    (set_attr "mode" "<MODE>")])
7359
7360 (define_insn "*udivmod<mode>4_noext"
7361   [(set (match_operand:SWIM248 0 "register_operand" "=a")
7362         (udiv:SWIM248 (match_operand:SWIM248 2 "register_operand" "0")
7363                       (match_operand:SWIM248 3 "nonimmediate_operand" "rm")))
7364    (set (match_operand:SWIM248 1 "register_operand" "=d")
7365         (umod:SWIM248 (match_dup 2) (match_dup 3)))
7366    (use (match_operand:SWIM248 4 "register_operand" "1"))
7367    (clobber (reg:CC FLAGS_REG))]
7368   ""
7369   "div{<imodesuffix>}\t%3"
7370   [(set_attr "type" "idiv")
7371    (set_attr "mode" "<MODE>")])
7372
7373 (define_expand "udivmodqi4"
7374   [(parallel [(set (match_operand:QI 0 "register_operand")
7375                    (udiv:QI
7376                      (match_operand:QI 1 "register_operand")
7377                      (match_operand:QI 2 "nonimmediate_operand")))
7378               (set (match_operand:QI 3 "register_operand")
7379                    (umod:QI (match_dup 1) (match_dup 2)))
7380               (clobber (reg:CC FLAGS_REG))])]
7381   "TARGET_QIMODE_MATH"
7382 {
7383   rtx div, mod, insn;
7384   rtx tmp0, tmp1;
7385   
7386   tmp0 = gen_reg_rtx (HImode);
7387   tmp1 = gen_reg_rtx (HImode);
7388
7389   /* Extend operands[1] to HImode.  Generate 8bit divide.  Result is
7390      in AX.  */
7391   emit_insn (gen_zero_extendqihi2 (tmp1, operands[1]));
7392   emit_insn (gen_udivmodhiqi3 (tmp0, tmp1, operands[2]));
7393
7394   /* Extract remainder from AH.  */
7395   tmp1 = gen_rtx_ZERO_EXTRACT (SImode, tmp0, GEN_INT (8), GEN_INT (8));
7396   tmp1 = simplify_gen_subreg (QImode, tmp1, SImode, 0);
7397   insn = emit_move_insn (operands[3], tmp1);
7398
7399   mod = gen_rtx_UMOD (QImode, operands[1], operands[2]);
7400   set_unique_reg_note (insn, REG_EQUAL, mod);
7401
7402   /* Extract quotient from AL.  */
7403   insn = emit_move_insn (operands[0], gen_lowpart (QImode, tmp0));
7404
7405   div = gen_rtx_UDIV (QImode, operands[1], operands[2]);
7406   set_unique_reg_note (insn, REG_EQUAL, div);
7407
7408   DONE;
7409 })
7410
7411 (define_insn "udivmodhiqi3"
7412   [(set (match_operand:HI 0 "register_operand" "=a")
7413         (ior:HI
7414           (ashift:HI
7415             (zero_extend:HI
7416               (truncate:QI
7417                 (mod:HI (match_operand:HI 1 "register_operand" "0")
7418                         (zero_extend:HI
7419                           (match_operand:QI 2 "nonimmediate_operand" "qm")))))
7420             (const_int 8))
7421           (zero_extend:HI
7422             (truncate:QI
7423               (div:HI (match_dup 1) (zero_extend:HI (match_dup 2)))))))
7424    (clobber (reg:CC FLAGS_REG))]
7425   "TARGET_QIMODE_MATH"
7426   "div{b}\t%2"
7427   [(set_attr "type" "idiv")
7428    (set_attr "mode" "QI")])
7429
7430 ;; We cannot use div/idiv for double division, because it causes
7431 ;; "division by zero" on the overflow and that's not what we expect
7432 ;; from truncate.  Because true (non truncating) double division is
7433 ;; never generated, we can't create this insn anyway.
7434 ;
7435 ;(define_insn ""
7436 ;  [(set (match_operand:SI 0 "register_operand" "=a")
7437 ;       (truncate:SI
7438 ;         (udiv:DI (match_operand:DI 1 "register_operand" "A")
7439 ;                  (zero_extend:DI
7440 ;                    (match_operand:SI 2 "nonimmediate_operand" "rm")))))
7441 ;   (set (match_operand:SI 3 "register_operand" "=d")
7442 ;       (truncate:SI
7443 ;         (umod:DI (match_dup 1) (zero_extend:DI (match_dup 2)))))
7444 ;   (clobber (reg:CC FLAGS_REG))]
7445 ;  ""
7446 ;  "div{l}\t{%2, %0|%0, %2}"
7447 ;  [(set_attr "type" "idiv")])
7448 \f
7449 ;;- Logical AND instructions
7450
7451 ;; On Pentium, "test imm, reg" is pairable only with eax, ax, and al.
7452 ;; Note that this excludes ah.
7453
7454 (define_expand "testsi_ccno_1"
7455   [(set (reg:CCNO FLAGS_REG)
7456         (compare:CCNO
7457           (and:SI (match_operand:SI 0 "nonimmediate_operand")
7458                   (match_operand:SI 1 "x86_64_nonmemory_operand"))
7459           (const_int 0)))])
7460
7461 (define_expand "testqi_ccz_1"
7462   [(set (reg:CCZ FLAGS_REG)
7463         (compare:CCZ (and:QI (match_operand:QI 0 "nonimmediate_operand")
7464                              (match_operand:QI 1 "nonmemory_operand"))
7465                  (const_int 0)))])
7466
7467 (define_expand "testdi_ccno_1"
7468   [(set (reg:CCNO FLAGS_REG)
7469         (compare:CCNO
7470           (and:DI (match_operand:DI 0 "nonimmediate_operand")
7471                   (match_operand:DI 1 "x86_64_szext_general_operand"))
7472           (const_int 0)))]
7473   "TARGET_64BIT && !(MEM_P (operands[0]) && MEM_P (operands[1]))")
7474
7475 (define_insn "*testdi_1"
7476   [(set (reg FLAGS_REG)
7477         (compare
7478          (and:DI
7479           (match_operand:DI 0 "nonimmediate_operand" "%!*a,r,!*a,r,rm")
7480           (match_operand:DI 1 "x86_64_szext_general_operand" "Z,Z,e,e,re"))
7481          (const_int 0)))]
7482   "TARGET_64BIT && ix86_match_ccmode (insn, CCNOmode)
7483    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
7484   "@
7485    test{l}\t{%k1, %k0|%k0, %k1}
7486    test{l}\t{%k1, %k0|%k0, %k1}
7487    test{q}\t{%1, %0|%0, %1}
7488    test{q}\t{%1, %0|%0, %1}
7489    test{q}\t{%1, %0|%0, %1}"
7490   [(set_attr "type" "test")
7491    (set_attr "modrm" "0,1,0,1,1")
7492    (set_attr "mode" "SI,SI,DI,DI,DI")])
7493
7494 (define_insn "*testqi_1_maybe_si"
7495   [(set (reg FLAGS_REG)
7496         (compare
7497           (and:QI
7498             (match_operand:QI 0 "nonimmediate_operand" "%!*a,q,qm,r")
7499             (match_operand:QI 1 "general_operand" "n,n,qn,n"))
7500           (const_int 0)))]
7501    "!(MEM_P (operands[0]) && MEM_P (operands[1]))
7502     && ix86_match_ccmode (insn,
7503                          CONST_INT_P (operands[1])
7504                          && INTVAL (operands[1]) >= 0 ? CCNOmode : CCZmode)"
7505 {
7506   if (which_alternative == 3)
7507     {
7508       if (CONST_INT_P (operands[1]) && INTVAL (operands[1]) < 0)
7509         operands[1] = GEN_INT (INTVAL (operands[1]) & 0xff);
7510       return "test{l}\t{%1, %k0|%k0, %1}";
7511     }
7512   return "test{b}\t{%1, %0|%0, %1}";
7513 }
7514   [(set_attr "type" "test")
7515    (set_attr "modrm" "0,1,1,1")
7516    (set_attr "mode" "QI,QI,QI,SI")
7517    (set_attr "pent_pair" "uv,np,uv,np")])
7518
7519 (define_insn "*test<mode>_1"
7520   [(set (reg FLAGS_REG)
7521         (compare
7522          (and:SWI124
7523           (match_operand:SWI124 0 "nonimmediate_operand" "%!*a,<r>,<r>m")
7524           (match_operand:SWI124 1 "<general_operand>" "<i>,<i>,<r><i>"))
7525          (const_int 0)))]
7526   "ix86_match_ccmode (insn, CCNOmode)
7527    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
7528   "test{<imodesuffix>}\t{%1, %0|%0, %1}"
7529   [(set_attr "type" "test")
7530    (set_attr "modrm" "0,1,1")
7531    (set_attr "mode" "<MODE>")
7532    (set_attr "pent_pair" "uv,np,uv")])
7533
7534 (define_expand "testqi_ext_ccno_0"
7535   [(set (reg:CCNO FLAGS_REG)
7536         (compare:CCNO
7537           (and:SI
7538             (zero_extract:SI
7539               (match_operand 0 "ext_register_operand")
7540               (const_int 8)
7541               (const_int 8))
7542             (match_operand 1 "const_int_operand"))
7543           (const_int 0)))])
7544
7545 (define_insn "*testqi_ext_0"
7546   [(set (reg FLAGS_REG)
7547         (compare
7548           (and:SI
7549             (zero_extract:SI
7550               (match_operand 0 "ext_register_operand" "Q")
7551               (const_int 8)
7552               (const_int 8))
7553             (match_operand 1 "const_int_operand" "n"))
7554           (const_int 0)))]
7555   "ix86_match_ccmode (insn, CCNOmode)"
7556   "test{b}\t{%1, %h0|%h0, %1}"
7557   [(set_attr "type" "test")
7558    (set_attr "mode" "QI")
7559    (set_attr "length_immediate" "1")
7560    (set_attr "modrm" "1")
7561    (set_attr "pent_pair" "np")])
7562
7563 (define_insn "*testqi_ext_1"
7564   [(set (reg FLAGS_REG)
7565         (compare
7566           (and:SI
7567             (zero_extract:SI
7568               (match_operand 0 "ext_register_operand" "Q,Q")
7569               (const_int 8)
7570               (const_int 8))
7571             (zero_extend:SI
7572               (match_operand:QI 1 "nonimmediate_x64nomem_operand" "Q,m")))
7573           (const_int 0)))]
7574   "ix86_match_ccmode (insn, CCNOmode)"
7575   "test{b}\t{%1, %h0|%h0, %1}"
7576   [(set_attr "isa" "*,nox64")
7577    (set_attr "type" "test")
7578    (set_attr "mode" "QI")])
7579
7580 (define_insn "*testqi_ext_2"
7581   [(set (reg FLAGS_REG)
7582         (compare
7583           (and:SI
7584             (zero_extract:SI
7585               (match_operand 0 "ext_register_operand" "Q")
7586               (const_int 8)
7587               (const_int 8))
7588             (zero_extract:SI
7589               (match_operand 1 "ext_register_operand" "Q")
7590               (const_int 8)
7591               (const_int 8)))
7592           (const_int 0)))]
7593   "ix86_match_ccmode (insn, CCNOmode)"
7594   "test{b}\t{%h1, %h0|%h0, %h1}"
7595   [(set_attr "type" "test")
7596    (set_attr "mode" "QI")])
7597
7598 ;; Combine likes to form bit extractions for some tests.  Humor it.
7599 (define_insn "*testqi_ext_3"
7600   [(set (reg FLAGS_REG)
7601         (compare (zero_extract:SWI48
7602                    (match_operand 0 "nonimmediate_operand" "rm")
7603                    (match_operand:SWI48 1 "const_int_operand")
7604                    (match_operand:SWI48 2 "const_int_operand"))
7605                  (const_int 0)))]
7606   "ix86_match_ccmode (insn, CCNOmode)
7607    && ((TARGET_64BIT && GET_MODE (operands[0]) == DImode)
7608        || GET_MODE (operands[0]) == SImode
7609        || GET_MODE (operands[0]) == HImode
7610        || GET_MODE (operands[0]) == QImode)
7611    /* Ensure that resulting mask is zero or sign extended operand.  */
7612    && INTVAL (operands[2]) >= 0
7613    && ((INTVAL (operands[1]) > 0
7614         && INTVAL (operands[1]) + INTVAL (operands[2]) <= 32)
7615        || (<MODE>mode == DImode
7616            && INTVAL (operands[1]) > 32
7617            && INTVAL (operands[1]) + INTVAL (operands[2]) == 64))"
7618   "#")
7619
7620 (define_split
7621   [(set (match_operand 0 "flags_reg_operand")
7622         (match_operator 1 "compare_operator"
7623           [(zero_extract
7624              (match_operand 2 "nonimmediate_operand")
7625              (match_operand 3 "const_int_operand")
7626              (match_operand 4 "const_int_operand"))
7627            (const_int 0)]))]
7628   "ix86_match_ccmode (insn, CCNOmode)"
7629   [(set (match_dup 0) (match_op_dup 1 [(match_dup 2) (const_int 0)]))]
7630 {
7631   rtx val = operands[2];
7632   HOST_WIDE_INT len = INTVAL (operands[3]);
7633   HOST_WIDE_INT pos = INTVAL (operands[4]);
7634   HOST_WIDE_INT mask;
7635   machine_mode mode, submode;
7636
7637   mode = GET_MODE (val);
7638   if (MEM_P (val))
7639     {
7640       /* ??? Combine likes to put non-volatile mem extractions in QImode
7641          no matter the size of the test.  So find a mode that works.  */
7642       if (! MEM_VOLATILE_P (val))
7643         {
7644           mode = smallest_mode_for_size (pos + len, MODE_INT);
7645           val = adjust_address (val, mode, 0);
7646         }
7647     }
7648   else if (GET_CODE (val) == SUBREG
7649            && (submode = GET_MODE (SUBREG_REG (val)),
7650                GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (submode))
7651            && pos + len <= GET_MODE_BITSIZE (submode)
7652            && GET_MODE_CLASS (submode) == MODE_INT)
7653     {
7654       /* Narrow a paradoxical subreg to prevent partial register stalls.  */
7655       mode = submode;
7656       val = SUBREG_REG (val);
7657     }
7658   else if (mode == HImode && pos + len <= 8)
7659     {
7660       /* Small HImode tests can be converted to QImode.  */
7661       mode = QImode;
7662       val = gen_lowpart (QImode, val);
7663     }
7664
7665   if (len == HOST_BITS_PER_WIDE_INT)
7666     mask = -1;
7667   else
7668     mask = ((HOST_WIDE_INT)1 << len) - 1;
7669   mask <<= pos;
7670
7671   operands[2] = gen_rtx_AND (mode, val, gen_int_mode (mask, mode));
7672 })
7673
7674 ;; Convert HImode/SImode test instructions with immediate to QImode ones.
7675 ;; i386 does not allow to encode test with 8bit sign extended immediate, so
7676 ;; this is relatively important trick.
7677 ;; Do the conversion only post-reload to avoid limiting of the register class
7678 ;; to QI regs.
7679 (define_split
7680   [(set (match_operand 0 "flags_reg_operand")
7681         (match_operator 1 "compare_operator"
7682           [(and (match_operand 2 "register_operand")
7683                 (match_operand 3 "const_int_operand"))
7684            (const_int 0)]))]
7685    "reload_completed
7686     && QI_REG_P (operands[2])
7687     && GET_MODE (operands[2]) != QImode
7688     && ((ix86_match_ccmode (insn, CCZmode)
7689          && !(INTVAL (operands[3]) & ~(255 << 8)))
7690         || (ix86_match_ccmode (insn, CCNOmode)
7691             && !(INTVAL (operands[3]) & ~(127 << 8))))"
7692   [(set (match_dup 0)
7693         (match_op_dup 1
7694           [(and:SI (zero_extract:SI (match_dup 2) (const_int 8) (const_int 8))
7695                    (match_dup 3))
7696            (const_int 0)]))]
7697 {
7698   operands[2] = gen_lowpart (SImode, operands[2]);
7699   operands[3] = gen_int_mode (INTVAL (operands[3]) >> 8, SImode);
7700 })
7701
7702 (define_split
7703   [(set (match_operand 0 "flags_reg_operand")
7704         (match_operator 1 "compare_operator"
7705           [(and (match_operand 2 "nonimmediate_operand")
7706                 (match_operand 3 "const_int_operand"))
7707            (const_int 0)]))]
7708    "reload_completed
7709     && GET_MODE (operands[2]) != QImode
7710     && (!REG_P (operands[2]) || ANY_QI_REG_P (operands[2]))
7711     && ((ix86_match_ccmode (insn, CCZmode)
7712          && !(INTVAL (operands[3]) & ~255))
7713         || (ix86_match_ccmode (insn, CCNOmode)
7714             && !(INTVAL (operands[3]) & ~127)))"
7715   [(set (match_dup 0)
7716         (match_op_dup 1 [(and:QI (match_dup 2) (match_dup 3))
7717                          (const_int 0)]))]
7718 {
7719   operands[2] = gen_lowpart (QImode, operands[2]);
7720   operands[3] = gen_lowpart (QImode, operands[3]);
7721 })
7722
7723 (define_split
7724   [(set (match_operand:SWI1248x 0 "mask_reg_operand")
7725         (any_logic:SWI1248x (match_operand:SWI1248x 1 "mask_reg_operand")
7726                             (match_operand:SWI1248x 2 "mask_reg_operand")))
7727    (clobber (reg:CC FLAGS_REG))]
7728   "TARGET_AVX512F && reload_completed"
7729   [(set (match_dup 0)
7730         (any_logic:SWI1248x (match_dup 1)
7731                             (match_dup 2)))])
7732
7733 (define_insn "*k<logic><mode>"
7734   [(set (match_operand:SWI1248_AVX512BW 0 "mask_reg_operand" "=k")
7735         (any_logic:SWI1248_AVX512BW (match_operand:SWI1248_AVX512BW 1 "mask_reg_operand" "k")
7736                           (match_operand:SWI1248_AVX512BW 2 "mask_reg_operand" "k")))]
7737   "TARGET_AVX512F"
7738   {
7739     if (!TARGET_AVX512DQ && <MODE>mode == QImode)
7740       return "k<logic>w\t{%2, %1, %0|%0, %1, %2}";
7741     else
7742       return "k<logic><mskmodesuffix>\t{%2, %1, %0|%0, %1, %2}";
7743   }
7744   [(set_attr "mode" "<MODE>")
7745    (set_attr "type" "msklog")
7746    (set_attr "prefix" "vex")])
7747
7748 ;; %%% This used to optimize known byte-wide and operations to memory,
7749 ;; and sometimes to QImode registers.  If this is considered useful,
7750 ;; it should be done with splitters.
7751
7752 (define_expand "and<mode>3"
7753   [(set (match_operand:SWIM 0 "nonimmediate_operand")
7754         (and:SWIM (match_operand:SWIM 1 "nonimmediate_operand")
7755                   (match_operand:SWIM 2 "<general_szext_operand>")))]
7756   ""
7757 {
7758   machine_mode mode = <MODE>mode;
7759   rtx (*insn) (rtx, rtx);
7760
7761   if (CONST_INT_P (operands[2]) && REG_P (operands[0]))
7762     {
7763       HOST_WIDE_INT ival = INTVAL (operands[2]);
7764
7765       if (ival == (HOST_WIDE_INT) 0xffffffff)
7766         mode = SImode;
7767       else if (ival == 0xffff)
7768         mode = HImode;
7769       else if (ival == 0xff)
7770         mode = QImode;
7771       }
7772
7773   if (mode == <MODE>mode)
7774     {
7775       ix86_expand_binary_operator (AND, <MODE>mode, operands);
7776       DONE;
7777     }
7778
7779   if (<MODE>mode == DImode)
7780     insn = (mode == SImode)
7781            ? gen_zero_extendsidi2
7782            : (mode == HImode)
7783            ? gen_zero_extendhidi2
7784            : gen_zero_extendqidi2;
7785   else if (<MODE>mode == SImode)
7786     insn = (mode == HImode)
7787            ? gen_zero_extendhisi2
7788            : gen_zero_extendqisi2;
7789   else if (<MODE>mode == HImode)
7790     insn = gen_zero_extendqihi2;
7791   else
7792     gcc_unreachable ();
7793
7794   emit_insn (insn (operands[0], gen_lowpart (mode, operands[1])));
7795   DONE;
7796 })
7797
7798 (define_insn "*anddi_1"
7799   [(set (match_operand:DI 0 "nonimmediate_operand" "=r,rm,r,r,!k")
7800         (and:DI
7801          (match_operand:DI 1 "nonimmediate_operand" "%0,0,0,qm,k")
7802          (match_operand:DI 2 "x86_64_szext_general_operand" "Z,re,rm,L,k")))
7803    (clobber (reg:CC FLAGS_REG))]
7804   "TARGET_64BIT && ix86_binary_operator_ok (AND, DImode, operands)"
7805 {
7806   switch (get_attr_type (insn))
7807     {
7808     case TYPE_IMOVX:
7809       return "#";
7810
7811     case TYPE_MSKLOG:
7812       return "kandq\t{%2, %1, %0|%0, %1, %2}";
7813
7814     default:
7815       gcc_assert (rtx_equal_p (operands[0], operands[1]));
7816       if (get_attr_mode (insn) == MODE_SI)
7817         return "and{l}\t{%k2, %k0|%k0, %k2}";
7818       else
7819         return "and{q}\t{%2, %0|%0, %2}";
7820     }
7821 }
7822   [(set_attr "type" "alu,alu,alu,imovx,msklog")
7823    (set_attr "length_immediate" "*,*,*,0,0")
7824    (set (attr "prefix_rex")
7825      (if_then_else
7826        (and (eq_attr "type" "imovx")
7827             (and (match_test "INTVAL (operands[2]) == 0xff")
7828                  (match_operand 1 "ext_QIreg_operand")))
7829        (const_string "1")
7830        (const_string "*")))
7831    (set_attr "mode" "SI,DI,DI,SI,DI")])
7832
7833 (define_insn "*andsi_1"
7834   [(set (match_operand:SI 0 "nonimmediate_operand" "=rm,r,Ya,!k")
7835         (and:SI (match_operand:SI 1 "nonimmediate_operand" "%0,0,qm,k")
7836                 (match_operand:SI 2 "x86_64_general_operand" "re,rm,L,k")))
7837    (clobber (reg:CC FLAGS_REG))]
7838   "ix86_binary_operator_ok (AND, SImode, operands)"
7839 {
7840   switch (get_attr_type (insn))
7841     {
7842     case TYPE_IMOVX:
7843       return "#";
7844
7845     case TYPE_MSKLOG:
7846       return "kandd\t{%2, %1, %0|%0, %1, %2}";
7847
7848     default:
7849       gcc_assert (rtx_equal_p (operands[0], operands[1]));
7850       return "and{l}\t{%2, %0|%0, %2}";
7851     }
7852 }
7853   [(set_attr "type" "alu,alu,imovx,msklog")
7854    (set (attr "prefix_rex")
7855      (if_then_else
7856        (and (eq_attr "type" "imovx")
7857             (and (match_test "INTVAL (operands[2]) == 0xff")
7858                  (match_operand 1 "ext_QIreg_operand")))
7859        (const_string "1")
7860        (const_string "*")))
7861    (set_attr "length_immediate" "*,*,0,0")
7862    (set_attr "mode" "SI")])
7863
7864 ;; See comment for addsi_1_zext why we do use nonimmediate_operand
7865 (define_insn "*andsi_1_zext"
7866   [(set (match_operand:DI 0 "register_operand" "=r")
7867         (zero_extend:DI
7868           (and:SI (match_operand:SI 1 "nonimmediate_operand" "%0")
7869                   (match_operand:SI 2 "x86_64_general_operand" "rme"))))
7870    (clobber (reg:CC FLAGS_REG))]
7871   "TARGET_64BIT && ix86_binary_operator_ok (AND, SImode, operands)"
7872   "and{l}\t{%2, %k0|%k0, %2}"
7873   [(set_attr "type" "alu")
7874    (set_attr "mode" "SI")])
7875
7876 (define_insn "*andhi_1"
7877   [(set (match_operand:HI 0 "nonimmediate_operand" "=rm,r,Ya,!k")
7878         (and:HI (match_operand:HI 1 "nonimmediate_operand" "%0,0,qm,k")
7879                 (match_operand:HI 2 "general_operand" "rn,rm,L,k")))
7880    (clobber (reg:CC FLAGS_REG))]
7881   "ix86_binary_operator_ok (AND, HImode, operands)"
7882 {
7883   switch (get_attr_type (insn))
7884     {
7885     case TYPE_IMOVX:
7886       return "#";
7887
7888     case TYPE_MSKLOG:
7889       return "kandw\t{%2, %1, %0|%0, %1, %2}";
7890
7891     default:
7892       gcc_assert (rtx_equal_p (operands[0], operands[1]));
7893       return "and{w}\t{%2, %0|%0, %2}";
7894     }
7895 }
7896   [(set_attr "type" "alu,alu,imovx,msklog")
7897    (set_attr "length_immediate" "*,*,0,*")
7898    (set (attr "prefix_rex")
7899      (if_then_else
7900        (and (eq_attr "type" "imovx")
7901             (match_operand 1 "ext_QIreg_operand"))
7902        (const_string "1")
7903        (const_string "*")))
7904    (set_attr "mode" "HI,HI,SI,HI")])
7905
7906 ;; %%% Potential partial reg stall on alternative 2.  What to do?
7907 (define_insn "*andqi_1"
7908   [(set (match_operand:QI 0 "nonimmediate_operand" "=qm,q,r,!k")
7909         (and:QI (match_operand:QI 1 "nonimmediate_operand" "%0,0,0,k")
7910                 (match_operand:QI 2 "general_operand" "qn,qmn,rn,k")))
7911    (clobber (reg:CC FLAGS_REG))]
7912   "ix86_binary_operator_ok (AND, QImode, operands)"
7913 {
7914   switch (which_alternative)
7915     {
7916     case 0:
7917     case 1:
7918       return "and{b}\t{%2, %0|%0, %2}";
7919     case 2:
7920       return "and{l}\t{%k2, %k0|%k0, %k2}";
7921     case 3:
7922       return TARGET_AVX512DQ ? "kandb\t{%2, %1, %0|%0, %1, %2}"
7923                              : "kandw\t{%2, %1, %0|%0, %1, %2}";
7924     default:
7925       gcc_unreachable ();
7926     }
7927 }
7928   [(set_attr "type" "alu,alu,alu,msklog")
7929    (set_attr "mode" "QI,QI,SI,HI")])
7930
7931 (define_insn "*andqi_1_slp"
7932   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm,q"))
7933         (and:QI (match_dup 0)
7934                 (match_operand:QI 1 "general_operand" "qn,qmn")))
7935    (clobber (reg:CC FLAGS_REG))]
7936   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
7937    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
7938   "and{b}\t{%1, %0|%0, %1}"
7939   [(set_attr "type" "alu1")
7940    (set_attr "mode" "QI")])
7941
7942 (define_insn "kandn<mode>"
7943   [(set (match_operand:SWI12 0 "register_operand" "=r,&r,!k")
7944         (and:SWI12
7945           (not:SWI12
7946             (match_operand:SWI12 1 "register_operand" "r,0,k"))
7947           (match_operand:SWI12 2 "register_operand" "r,r,k")))
7948    (clobber (reg:CC FLAGS_REG))]
7949   "TARGET_AVX512F"
7950 {
7951   switch (which_alternative)
7952     {
7953     case 0:
7954       return "andn\t{%k2, %k1, %k0|%k0, %k1, %k2}";
7955     case 1:
7956       return "#";
7957     case 2:
7958       if (TARGET_AVX512DQ && <MODE>mode == QImode)
7959         return "kandnb\t{%2, %1, %0|%0, %1, %2}";
7960       else
7961         return "kandnw\t{%2, %1, %0|%0, %1, %2}";
7962     default:
7963       gcc_unreachable ();
7964     }
7965 }
7966   [(set_attr "isa" "bmi,*,avx512f")
7967    (set_attr "type" "bitmanip,*,msklog")
7968    (set_attr "prefix" "*,*,vex")
7969    (set_attr "btver2_decode" "direct,*,*")
7970    (set_attr "mode" "<MODE>")])
7971
7972 (define_split
7973   [(set (match_operand:SWI12 0 "general_reg_operand")
7974         (and:SWI12
7975           (not:SWI12
7976             (match_dup 0))
7977           (match_operand:SWI12 1 "general_reg_operand")))
7978    (clobber (reg:CC FLAGS_REG))]
7979   "TARGET_AVX512F && !TARGET_BMI && reload_completed"
7980   [(set (match_dup 0)
7981         (not:HI (match_dup 0)))
7982    (parallel [(set (match_dup 0)
7983                    (and:HI (match_dup 0)
7984                            (match_dup 1)))
7985               (clobber (reg:CC FLAGS_REG))])])
7986
7987 ;; Turn *anddi_1 into *andsi_1_zext if possible.
7988 (define_split
7989   [(set (match_operand:DI 0 "register_operand")
7990         (and:DI (subreg:DI (match_operand:SI 1 "register_operand") 0)
7991                 (match_operand:DI 2 "x86_64_zext_immediate_operand")))
7992    (clobber (reg:CC FLAGS_REG))]
7993   "TARGET_64BIT"
7994   [(parallel [(set (match_dup 0)
7995                    (zero_extend:DI (and:SI (match_dup 1) (match_dup 2))))
7996               (clobber (reg:CC FLAGS_REG))])]
7997   "operands[2] = gen_lowpart (SImode, operands[2]);")
7998
7999 (define_split
8000   [(set (match_operand:SWI248 0 "register_operand")
8001         (and:SWI248 (match_operand:SWI248 1 "nonimmediate_operand")
8002                     (match_operand:SWI248 2 "const_int_operand")))
8003    (clobber (reg:CC FLAGS_REG))]
8004   "reload_completed
8005    && true_regnum (operands[0]) != true_regnum (operands[1])"
8006   [(const_int 0)]
8007 {
8008   HOST_WIDE_INT ival = INTVAL (operands[2]);
8009   machine_mode mode;
8010   rtx (*insn) (rtx, rtx);
8011
8012   if (ival == (HOST_WIDE_INT) 0xffffffff)
8013     mode = SImode;
8014   else if (ival == 0xffff)
8015     mode = HImode;
8016   else
8017     {
8018       gcc_assert (ival == 0xff);
8019       mode = QImode;
8020     }
8021
8022   if (<MODE>mode == DImode)
8023     insn = (mode == SImode)
8024            ? gen_zero_extendsidi2
8025            : (mode == HImode)
8026            ? gen_zero_extendhidi2
8027            : gen_zero_extendqidi2;
8028   else
8029     {
8030       if (<MODE>mode != SImode)
8031         /* Zero extend to SImode to avoid partial register stalls.  */
8032         operands[0] = gen_lowpart (SImode, operands[0]);
8033
8034       insn = (mode == HImode)
8035              ? gen_zero_extendhisi2
8036              : gen_zero_extendqisi2;
8037     }
8038   emit_insn (insn (operands[0], gen_lowpart (mode, operands[1])));
8039   DONE;
8040 })
8041
8042 (define_split
8043   [(set (match_operand 0 "register_operand")
8044         (and (match_dup 0)
8045              (const_int -65536)))
8046    (clobber (reg:CC FLAGS_REG))]
8047   "(TARGET_FAST_PREFIX && !TARGET_PARTIAL_REG_STALL)
8048     || optimize_function_for_size_p (cfun)"
8049   [(set (strict_low_part (match_dup 1)) (const_int 0))]
8050   "operands[1] = gen_lowpart (HImode, operands[0]);")
8051
8052 (define_split
8053   [(set (match_operand 0 "ext_register_operand")
8054         (and (match_dup 0)
8055              (const_int -256)))
8056    (clobber (reg:CC FLAGS_REG))]
8057   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8058    && reload_completed"
8059   [(set (strict_low_part (match_dup 1)) (const_int 0))]
8060   "operands[1] = gen_lowpart (QImode, operands[0]);")
8061
8062 (define_split
8063   [(set (match_operand 0 "ext_register_operand")
8064         (and (match_dup 0)
8065              (const_int -65281)))
8066    (clobber (reg:CC FLAGS_REG))]
8067   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8068    && reload_completed"
8069   [(parallel [(set (zero_extract:SI (match_dup 0)
8070                                     (const_int 8)
8071                                     (const_int 8))
8072                    (xor:SI
8073                      (zero_extract:SI (match_dup 0)
8074                                       (const_int 8)
8075                                       (const_int 8))
8076                      (zero_extract:SI (match_dup 0)
8077                                       (const_int 8)
8078                                       (const_int 8))))
8079               (clobber (reg:CC FLAGS_REG))])]
8080   "operands[0] = gen_lowpart (SImode, operands[0]);")
8081
8082 (define_insn "*anddi_2"
8083   [(set (reg FLAGS_REG)
8084         (compare
8085          (and:DI
8086           (match_operand:DI 1 "nonimmediate_operand" "%0,0,0")
8087           (match_operand:DI 2 "x86_64_szext_general_operand" "Z,rem,re"))
8088          (const_int 0)))
8089    (set (match_operand:DI 0 "nonimmediate_operand" "=r,r,rm")
8090         (and:DI (match_dup 1) (match_dup 2)))]
8091   "TARGET_64BIT
8092    && ix86_match_ccmode
8093         (insn,
8094          /* If we are going to emit andl instead of andq, and the operands[2]
8095             constant might have the SImode sign bit set, make sure the sign
8096             flag isn't tested, because the instruction will set the sign flag
8097             based on bit 31 rather than bit 63.  If it isn't CONST_INT,
8098             conservatively assume it might have bit 31 set.  */
8099          (satisfies_constraint_Z (operands[2])
8100           && (!CONST_INT_P (operands[2])
8101               || val_signbit_known_set_p (SImode, INTVAL (operands[2]))))
8102          ? CCZmode : CCNOmode)
8103    && ix86_binary_operator_ok (AND, DImode, operands)"
8104   "@
8105    and{l}\t{%k2, %k0|%k0, %k2}
8106    and{q}\t{%2, %0|%0, %2}
8107    and{q}\t{%2, %0|%0, %2}"
8108   [(set_attr "type" "alu")
8109    (set_attr "mode" "SI,DI,DI")])
8110
8111 (define_insn "*andqi_2_maybe_si"
8112   [(set (reg FLAGS_REG)
8113         (compare (and:QI
8114                   (match_operand:QI 1 "nonimmediate_operand" "%0,0,0")
8115                   (match_operand:QI 2 "general_operand" "qmn,qn,n"))
8116                  (const_int 0)))
8117    (set (match_operand:QI 0 "nonimmediate_operand" "=q,qm,*r")
8118         (and:QI (match_dup 1) (match_dup 2)))]
8119   "ix86_binary_operator_ok (AND, QImode, operands)
8120    && ix86_match_ccmode (insn,
8121                          CONST_INT_P (operands[2])
8122                          && INTVAL (operands[2]) >= 0 ? CCNOmode : CCZmode)"
8123 {
8124   if (which_alternative == 2)
8125     {
8126       if (CONST_INT_P (operands[2]) && INTVAL (operands[2]) < 0)
8127         operands[2] = GEN_INT (INTVAL (operands[2]) & 0xff);
8128       return "and{l}\t{%2, %k0|%k0, %2}";
8129     }
8130   return "and{b}\t{%2, %0|%0, %2}";
8131 }
8132   [(set_attr "type" "alu")
8133    (set_attr "mode" "QI,QI,SI")])
8134
8135 (define_insn "*and<mode>_2"
8136   [(set (reg FLAGS_REG)
8137         (compare (and:SWI124
8138                   (match_operand:SWI124 1 "nonimmediate_operand" "%0,0")
8139                   (match_operand:SWI124 2 "<general_operand>" "<g>,<r><i>"))
8140                  (const_int 0)))
8141    (set (match_operand:SWI124 0 "nonimmediate_operand" "=<r>,<r>m")
8142         (and:SWI124 (match_dup 1) (match_dup 2)))]
8143   "ix86_match_ccmode (insn, CCNOmode)
8144    && ix86_binary_operator_ok (AND, <MODE>mode, operands)"
8145   "and{<imodesuffix>}\t{%2, %0|%0, %2}"
8146   [(set_attr "type" "alu")
8147    (set_attr "mode" "<MODE>")])
8148
8149 ;; See comment for addsi_1_zext why we do use nonimmediate_operand
8150 (define_insn "*andsi_2_zext"
8151   [(set (reg FLAGS_REG)
8152         (compare (and:SI
8153                   (match_operand:SI 1 "nonimmediate_operand" "%0")
8154                   (match_operand:SI 2 "x86_64_general_operand" "rme"))
8155                  (const_int 0)))
8156    (set (match_operand:DI 0 "register_operand" "=r")
8157         (zero_extend:DI (and:SI (match_dup 1) (match_dup 2))))]
8158   "TARGET_64BIT && ix86_match_ccmode (insn, CCNOmode)
8159    && ix86_binary_operator_ok (AND, SImode, operands)"
8160   "and{l}\t{%2, %k0|%k0, %2}"
8161   [(set_attr "type" "alu")
8162    (set_attr "mode" "SI")])
8163
8164 (define_insn "*andqi_2_slp"
8165   [(set (reg FLAGS_REG)
8166         (compare (and:QI
8167                    (match_operand:QI 0 "nonimmediate_operand" "+q,qm")
8168                    (match_operand:QI 1 "nonimmediate_operand" "qmn,qn"))
8169                  (const_int 0)))
8170    (set (strict_low_part (match_dup 0))
8171         (and:QI (match_dup 0) (match_dup 1)))]
8172   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8173    && ix86_match_ccmode (insn, CCNOmode)
8174    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
8175   "and{b}\t{%1, %0|%0, %1}"
8176   [(set_attr "type" "alu1")
8177    (set_attr "mode" "QI")])
8178
8179 ;; ??? A bug in recog prevents it from recognizing a const_int as an
8180 ;; operand to zero_extend in andqi_ext_1.  It was checking explicitly
8181 ;; for a QImode operand, which of course failed.
8182 (define_insn "andqi_ext_0"
8183   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q")
8184                          (const_int 8)
8185                          (const_int 8))
8186         (and:SI
8187           (zero_extract:SI
8188             (match_operand 1 "ext_register_operand" "0")
8189             (const_int 8)
8190             (const_int 8))
8191           (match_operand 2 "const_int_operand" "n")))
8192    (clobber (reg:CC FLAGS_REG))]
8193   ""
8194   "and{b}\t{%2, %h0|%h0, %2}"
8195   [(set_attr "type" "alu")
8196    (set_attr "length_immediate" "1")
8197    (set_attr "modrm" "1")
8198    (set_attr "mode" "QI")])
8199
8200 ;; Generated by peephole translating test to and.  This shows up
8201 ;; often in fp comparisons.
8202 (define_insn "*andqi_ext_0_cc"
8203   [(set (reg FLAGS_REG)
8204         (compare
8205           (and:SI
8206             (zero_extract:SI
8207               (match_operand 1 "ext_register_operand" "0")
8208               (const_int 8)
8209               (const_int 8))
8210             (match_operand 2 "const_int_operand" "n"))
8211           (const_int 0)))
8212    (set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q")
8213                          (const_int 8)
8214                          (const_int 8))
8215         (and:SI
8216           (zero_extract:SI
8217             (match_dup 1)
8218             (const_int 8)
8219             (const_int 8))
8220           (match_dup 2)))]
8221   "ix86_match_ccmode (insn, CCNOmode)"
8222   "and{b}\t{%2, %h0|%h0, %2}"
8223   [(set_attr "type" "alu")
8224    (set_attr "length_immediate" "1")
8225    (set_attr "modrm" "1")
8226    (set_attr "mode" "QI")])
8227
8228 (define_insn "*andqi_ext_1"
8229   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q,Q")
8230                          (const_int 8)
8231                          (const_int 8))
8232         (and:SI
8233           (zero_extract:SI
8234             (match_operand 1 "ext_register_operand" "0,0")
8235             (const_int 8)
8236             (const_int 8))
8237           (zero_extend:SI
8238             (match_operand:QI 2 "nonimmediate_x64nomem_operand" "Q,m"))))
8239    (clobber (reg:CC FLAGS_REG))]
8240   ""
8241   "and{b}\t{%2, %h0|%h0, %2}"
8242   [(set_attr "isa" "*,nox64")
8243    (set_attr "type" "alu")
8244    (set_attr "length_immediate" "0")
8245    (set_attr "mode" "QI")])
8246
8247 (define_insn "*andqi_ext_2"
8248   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q")
8249                          (const_int 8)
8250                          (const_int 8))
8251         (and:SI
8252           (zero_extract:SI
8253             (match_operand 1 "ext_register_operand" "%0")
8254             (const_int 8)
8255             (const_int 8))
8256           (zero_extract:SI
8257             (match_operand 2 "ext_register_operand" "Q")
8258             (const_int 8)
8259             (const_int 8))))
8260    (clobber (reg:CC FLAGS_REG))]
8261   ""
8262   "and{b}\t{%h2, %h0|%h0, %h2}"
8263   [(set_attr "type" "alu")
8264    (set_attr "length_immediate" "0")
8265    (set_attr "mode" "QI")])
8266
8267 ;; Convert wide AND instructions with immediate operand to shorter QImode
8268 ;; equivalents when possible.
8269 ;; Don't do the splitting with memory operands, since it introduces risk
8270 ;; of memory mismatch stalls.  We may want to do the splitting for optimizing
8271 ;; for size, but that can (should?) be handled by generic code instead.
8272 (define_split
8273   [(set (match_operand 0 "register_operand")
8274         (and (match_operand 1 "register_operand")
8275              (match_operand 2 "const_int_operand")))
8276    (clobber (reg:CC FLAGS_REG))]
8277    "reload_completed
8278     && QI_REG_P (operands[0])
8279     && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8280     && !(~INTVAL (operands[2]) & ~(255 << 8))
8281     && GET_MODE (operands[0]) != QImode"
8282   [(parallel [(set (zero_extract:SI (match_dup 0) (const_int 8) (const_int 8))
8283                    (and:SI (zero_extract:SI (match_dup 1)
8284                                             (const_int 8) (const_int 8))
8285                            (match_dup 2)))
8286               (clobber (reg:CC FLAGS_REG))])]
8287 {
8288   operands[0] = gen_lowpart (SImode, operands[0]);
8289   operands[1] = gen_lowpart (SImode, operands[1]);
8290   operands[2] = gen_int_mode ((INTVAL (operands[2]) >> 8) & 0xff, SImode);
8291 })
8292
8293 ;; Since AND can be encoded with sign extended immediate, this is only
8294 ;; profitable when 7th bit is not set.
8295 (define_split
8296   [(set (match_operand 0 "register_operand")
8297         (and (match_operand 1 "general_operand")
8298              (match_operand 2 "const_int_operand")))
8299    (clobber (reg:CC FLAGS_REG))]
8300    "reload_completed
8301     && ANY_QI_REG_P (operands[0])
8302     && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8303     && !(~INTVAL (operands[2]) & ~255)
8304     && !(INTVAL (operands[2]) & 128)
8305     && GET_MODE (operands[0]) != QImode"
8306   [(parallel [(set (strict_low_part (match_dup 0))
8307                    (and:QI (match_dup 1)
8308                            (match_dup 2)))
8309               (clobber (reg:CC FLAGS_REG))])]
8310 {
8311   operands[0] = gen_lowpart (QImode, operands[0]);
8312   operands[1] = gen_lowpart (QImode, operands[1]);
8313   operands[2] = gen_lowpart (QImode, operands[2]);
8314 })
8315 \f
8316 ;; Logical inclusive and exclusive OR instructions
8317
8318 ;; %%% This used to optimize known byte-wide and operations to memory.
8319 ;; If this is considered useful, it should be done with splitters.
8320
8321 (define_expand "<code><mode>3"
8322   [(set (match_operand:SWIM 0 "nonimmediate_operand")
8323         (any_or:SWIM (match_operand:SWIM 1 "nonimmediate_operand")
8324                      (match_operand:SWIM 2 "<general_operand>")))]
8325   ""
8326   "ix86_expand_binary_operator (<CODE>, <MODE>mode, operands); DONE;")
8327
8328 (define_insn "*<code><mode>_1"
8329   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=r,rm,k")
8330         (any_or:SWI48
8331          (match_operand:SWI48 1 "nonimmediate_operand" "%0,0,k")
8332          (match_operand:SWI48 2 "<general_operand>" "<g>,r<i>,k")))
8333    (clobber (reg:CC FLAGS_REG))]
8334   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
8335   "@
8336    <logic>{<imodesuffix>}\t{%2, %0|%0, %2}
8337    <logic>{<imodesuffix>}\t{%2, %0|%0, %2}
8338    k<logic><mskmodesuffix>\t{%2, %1, %0|%0, %1, %2}"
8339   [(set_attr "type" "alu,alu,msklog")
8340    (set_attr "mode" "<MODE>")])
8341
8342 (define_insn "*<code>hi_1"
8343   [(set (match_operand:HI 0 "nonimmediate_operand" "=r,rm,!k")
8344         (any_or:HI
8345          (match_operand:HI 1 "nonimmediate_operand" "%0,0,k")
8346          (match_operand:HI 2 "general_operand" "<g>,r<i>,k")))
8347    (clobber (reg:CC FLAGS_REG))]
8348   "ix86_binary_operator_ok (<CODE>, HImode, operands)"
8349   "@
8350   <logic>{w}\t{%2, %0|%0, %2}
8351   <logic>{w}\t{%2, %0|%0, %2}
8352   k<logic>w\t{%2, %1, %0|%0, %1, %2}"
8353   [(set_attr "type" "alu,alu,msklog")
8354    (set_attr "mode" "HI")])
8355
8356 ;; %%% Potential partial reg stall on alternative 2.  What to do?
8357 (define_insn "*<code>qi_1"
8358   [(set (match_operand:QI 0 "nonimmediate_operand" "=q,m,r,!k")
8359         (any_or:QI (match_operand:QI 1 "nonimmediate_operand" "%0,0,0,k")
8360                    (match_operand:QI 2 "general_operand" "qmn,qn,rn,k")))
8361    (clobber (reg:CC FLAGS_REG))]
8362   "ix86_binary_operator_ok (<CODE>, QImode, operands)"
8363   "@
8364    <logic>{b}\t{%2, %0|%0, %2}
8365    <logic>{b}\t{%2, %0|%0, %2}
8366    <logic>{l}\t{%k2, %k0|%k0, %k2}
8367    k<logic>w\t{%2, %1, %0|%0, %1, %2}"
8368   [(set_attr "type" "alu,alu,alu,msklog")
8369    (set_attr "mode" "QI,QI,SI,HI")])
8370
8371 ;; See comment for addsi_1_zext why we do use nonimmediate_operand
8372 (define_insn "*<code>si_1_zext"
8373   [(set (match_operand:DI 0 "register_operand" "=r")
8374         (zero_extend:DI
8375          (any_or:SI (match_operand:SI 1 "nonimmediate_operand" "%0")
8376                     (match_operand:SI 2 "x86_64_general_operand" "rme"))))
8377    (clobber (reg:CC FLAGS_REG))]
8378   "TARGET_64BIT && ix86_binary_operator_ok (<CODE>, SImode, operands)"
8379   "<logic>{l}\t{%2, %k0|%k0, %2}"
8380   [(set_attr "type" "alu")
8381    (set_attr "mode" "SI")])
8382
8383 (define_insn "*<code>si_1_zext_imm"
8384   [(set (match_operand:DI 0 "register_operand" "=r")
8385         (any_or:DI
8386          (zero_extend:DI (match_operand:SI 1 "register_operand" "%0"))
8387          (match_operand:DI 2 "x86_64_zext_immediate_operand" "Z")))
8388    (clobber (reg:CC FLAGS_REG))]
8389   "TARGET_64BIT && ix86_binary_operator_ok (<CODE>, SImode, operands)"
8390   "<logic>{l}\t{%2, %k0|%k0, %2}"
8391   [(set_attr "type" "alu")
8392    (set_attr "mode" "SI")])
8393
8394 (define_insn "*<code>qi_1_slp"
8395   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+q,m"))
8396         (any_or:QI (match_dup 0)
8397                    (match_operand:QI 1 "general_operand" "qmn,qn")))
8398    (clobber (reg:CC FLAGS_REG))]
8399   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8400    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
8401   "<logic>{b}\t{%1, %0|%0, %1}"
8402   [(set_attr "type" "alu1")
8403    (set_attr "mode" "QI")])
8404
8405 (define_insn "*<code><mode>_2"
8406   [(set (reg FLAGS_REG)
8407         (compare (any_or:SWI
8408                   (match_operand:SWI 1 "nonimmediate_operand" "%0,0")
8409                   (match_operand:SWI 2 "<general_operand>" "<g>,<r><i>"))
8410                  (const_int 0)))
8411    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>,<r>m")
8412         (any_or:SWI (match_dup 1) (match_dup 2)))]
8413   "ix86_match_ccmode (insn, CCNOmode)
8414    && ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
8415   "<logic>{<imodesuffix>}\t{%2, %0|%0, %2}"
8416   [(set_attr "type" "alu")
8417    (set_attr "mode" "<MODE>")])
8418
8419 (define_insn "kxnor<mode>"
8420   [(set (match_operand:SWI12 0 "register_operand" "=r,!k")
8421         (not:SWI12
8422           (xor:SWI12
8423             (match_operand:SWI12 1 "register_operand" "0,k")
8424             (match_operand:SWI12 2 "register_operand" "r,k"))))
8425    (clobber (reg:CC FLAGS_REG))]
8426   "TARGET_AVX512F"
8427 {
8428   if (which_alternative == 1 && <MODE>mode == QImode && TARGET_AVX512DQ)
8429     return "kxnorb\t{%2, %1, %0|%0, %1, %2}";
8430   return "kxnorw\t{%2, %1, %0|%0, %1, %2}";
8431 }
8432   [(set_attr "type" "*,msklog")
8433    (set_attr "prefix" "*,vex")
8434    (set_attr "mode" "<MODE>")])
8435
8436 (define_insn "kxnor<mode>"
8437   [(set (match_operand:SWI48x 0 "register_operand" "=r,!k")
8438         (not:SWI48x
8439           (xor:SWI48x
8440             (match_operand:SWI48x 1 "register_operand" "0,k")
8441             (match_operand:SWI48x 2 "register_operand" "r,k"))))
8442    (clobber (reg:CC FLAGS_REG))]
8443   "TARGET_AVX512BW"
8444   "@
8445    #
8446    kxnor<mskmodesuffix>\t{%2, %1, %0|%0, %1, %2}"
8447   [(set_attr "type" "*,msklog")
8448    (set_attr "prefix" "*,vex")
8449    (set_attr "mode" "<MODE>")])
8450
8451 (define_split
8452   [(set (match_operand:SWI1248x 0 "general_reg_operand")
8453         (not:SWI1248x
8454           (xor:SWI1248x
8455             (match_dup 0)
8456             (match_operand:SWI1248x 1 "general_reg_operand"))))
8457    (clobber (reg:CC FLAGS_REG))]
8458   "TARGET_AVX512F && reload_completed"
8459    [(parallel [(set (match_dup 0)
8460                     (xor:HI (match_dup 0)
8461                             (match_dup 1)))
8462                (clobber (reg:CC FLAGS_REG))])
8463     (set (match_dup 0)
8464          (not:HI (match_dup 0)))])
8465
8466 ;;There are kortrest[bdq] but no intrinsics for them.
8467 ;;We probably don't need to implement them.
8468 (define_insn "kortestzhi"
8469   [(set (reg:CCZ FLAGS_REG)
8470         (compare:CCZ
8471           (ior:HI
8472             (match_operand:HI 0 "register_operand" "k")
8473             (match_operand:HI 1 "register_operand" "k"))
8474           (const_int 0)))]
8475   "TARGET_AVX512F && ix86_match_ccmode (insn, CCZmode)"
8476   "kortestw\t{%1, %0|%0, %1}"
8477   [(set_attr "mode" "HI")
8478    (set_attr "type" "msklog")
8479    (set_attr "prefix" "vex")])
8480
8481 (define_insn "kortestchi"
8482   [(set (reg:CCC FLAGS_REG)
8483         (compare:CCC
8484           (ior:HI
8485             (match_operand:HI 0 "register_operand" "k")
8486             (match_operand:HI 1 "register_operand" "k"))
8487           (const_int -1)))]
8488   "TARGET_AVX512F && ix86_match_ccmode (insn, CCCmode)"
8489   "kortestw\t{%1, %0|%0, %1}"
8490   [(set_attr "mode" "HI")
8491    (set_attr "type" "msklog")
8492    (set_attr "prefix" "vex")])
8493
8494 (define_insn "kunpckhi"
8495   [(set (match_operand:HI 0 "register_operand" "=k")
8496         (ior:HI
8497           (ashift:HI
8498             (match_operand:HI 1 "register_operand" "k")
8499             (const_int 8))
8500           (zero_extend:HI (match_operand:QI 2 "register_operand" "k"))))]
8501   "TARGET_AVX512F"
8502   "kunpckbw\t{%2, %1, %0|%0, %1, %2}"
8503   [(set_attr "mode" "HI")
8504    (set_attr "type" "msklog")
8505    (set_attr "prefix" "vex")])
8506
8507 (define_insn "kunpcksi"
8508   [(set (match_operand:SI 0 "register_operand" "=k")
8509         (ior:SI
8510           (ashift:SI
8511             (match_operand:SI 1 "register_operand" "k")
8512             (const_int 16))
8513           (zero_extend:SI (subreg:HI (match_operand:SI 2 "register_operand" "k") 0))))]
8514   "TARGET_AVX512BW"
8515   "kunpckwd\t{%2, %1, %0|%0, %1, %2}"
8516   [(set_attr "mode" "SI")])
8517
8518 (define_insn "kunpckdi"
8519   [(set (match_operand:DI 0 "register_operand" "=k")
8520         (ior:DI
8521           (ashift:DI
8522             (match_operand:DI 1 "register_operand" "k")
8523             (const_int 32))
8524           (zero_extend:DI (subreg:SI (match_operand:DI 2 "register_operand" "k") 0))))]
8525   "TARGET_AVX512BW"
8526   "kunpckdq\t{%2, %1, %0|%0, %1, %2}"
8527   [(set_attr "mode" "DI")])
8528
8529 ;; See comment for addsi_1_zext why we do use nonimmediate_operand
8530 ;; ??? Special case for immediate operand is missing - it is tricky.
8531 (define_insn "*<code>si_2_zext"
8532   [(set (reg FLAGS_REG)
8533         (compare (any_or:SI (match_operand:SI 1 "nonimmediate_operand" "%0")
8534                             (match_operand:SI 2 "x86_64_general_operand" "rme"))
8535                  (const_int 0)))
8536    (set (match_operand:DI 0 "register_operand" "=r")
8537         (zero_extend:DI (any_or:SI (match_dup 1) (match_dup 2))))]
8538   "TARGET_64BIT && ix86_match_ccmode (insn, CCNOmode)
8539    && ix86_binary_operator_ok (<CODE>, SImode, operands)"
8540   "<logic>{l}\t{%2, %k0|%k0, %2}"
8541   [(set_attr "type" "alu")
8542    (set_attr "mode" "SI")])
8543
8544 (define_insn "*<code>si_2_zext_imm"
8545   [(set (reg FLAGS_REG)
8546         (compare (any_or:SI
8547                   (match_operand:SI 1 "nonimmediate_operand" "%0")
8548                   (match_operand:SI 2 "x86_64_zext_immediate_operand" "Z"))
8549                  (const_int 0)))
8550    (set (match_operand:DI 0 "register_operand" "=r")
8551         (any_or:DI (zero_extend:DI (match_dup 1)) (match_dup 2)))]
8552   "TARGET_64BIT && ix86_match_ccmode (insn, CCNOmode)
8553    && ix86_binary_operator_ok (<CODE>, SImode, operands)"
8554   "<logic>{l}\t{%2, %k0|%k0, %2}"
8555   [(set_attr "type" "alu")
8556    (set_attr "mode" "SI")])
8557
8558 (define_insn "*<code>qi_2_slp"
8559   [(set (reg FLAGS_REG)
8560         (compare (any_or:QI (match_operand:QI 0 "nonimmediate_operand" "+q,qm")
8561                             (match_operand:QI 1 "general_operand" "qmn,qn"))
8562                  (const_int 0)))
8563    (set (strict_low_part (match_dup 0))
8564         (any_or:QI (match_dup 0) (match_dup 1)))]
8565   "(!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8566    && ix86_match_ccmode (insn, CCNOmode)
8567    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
8568   "<logic>{b}\t{%1, %0|%0, %1}"
8569   [(set_attr "type" "alu1")
8570    (set_attr "mode" "QI")])
8571
8572 (define_insn "*<code><mode>_3"
8573   [(set (reg FLAGS_REG)
8574         (compare (any_or:SWI
8575                   (match_operand:SWI 1 "nonimmediate_operand" "%0")
8576                   (match_operand:SWI 2 "<general_operand>" "<g>"))
8577                  (const_int 0)))
8578    (clobber (match_scratch:SWI 0 "=<r>"))]
8579   "ix86_match_ccmode (insn, CCNOmode)
8580    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
8581   "<logic>{<imodesuffix>}\t{%2, %0|%0, %2}"
8582   [(set_attr "type" "alu")
8583    (set_attr "mode" "<MODE>")])
8584
8585 (define_insn "*<code>qi_ext_0"
8586   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q")
8587                          (const_int 8)
8588                          (const_int 8))
8589         (any_or:SI
8590           (zero_extract:SI
8591             (match_operand 1 "ext_register_operand" "0")
8592             (const_int 8)
8593             (const_int 8))
8594           (match_operand 2 "const_int_operand" "n")))
8595    (clobber (reg:CC FLAGS_REG))]
8596   "!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun)"
8597   "<logic>{b}\t{%2, %h0|%h0, %2}"
8598   [(set_attr "type" "alu")
8599    (set_attr "length_immediate" "1")
8600    (set_attr "modrm" "1")
8601    (set_attr "mode" "QI")])
8602
8603 (define_insn "*<code>qi_ext_1"
8604   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q,Q")
8605                          (const_int 8)
8606                          (const_int 8))
8607         (any_or:SI
8608           (zero_extract:SI
8609             (match_operand 1 "ext_register_operand" "0,0")
8610             (const_int 8)
8611             (const_int 8))
8612           (zero_extend:SI
8613             (match_operand:QI 2 "nonimmediate_x64nomem_operand" "Q,m"))))
8614    (clobber (reg:CC FLAGS_REG))]
8615   "!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun)"
8616   "<logic>{b}\t{%2, %h0|%h0, %2}"
8617   [(set_attr "isa" "*,nox64")
8618    (set_attr "type" "alu")
8619    (set_attr "length_immediate" "0")
8620    (set_attr "mode" "QI")])
8621
8622 (define_insn "*<code>qi_ext_2"
8623   [(set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q")
8624                          (const_int 8)
8625                          (const_int 8))
8626         (any_or:SI
8627           (zero_extract:SI (match_operand 1 "ext_register_operand" "0")
8628                            (const_int 8)
8629                            (const_int 8))
8630           (zero_extract:SI (match_operand 2 "ext_register_operand" "Q")
8631                            (const_int 8)
8632                            (const_int 8))))
8633    (clobber (reg:CC FLAGS_REG))]
8634   "!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun)"
8635   "<logic>{b}\t{%h2, %h0|%h0, %h2}"
8636   [(set_attr "type" "alu")
8637    (set_attr "length_immediate" "0")
8638    (set_attr "mode" "QI")])
8639
8640 (define_split
8641   [(set (match_operand 0 "register_operand")
8642         (any_or (match_operand 1 "register_operand")
8643                 (match_operand 2 "const_int_operand")))
8644    (clobber (reg:CC FLAGS_REG))]
8645    "reload_completed
8646     && QI_REG_P (operands[0])
8647     && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8648     && !(INTVAL (operands[2]) & ~(255 << 8))
8649     && GET_MODE (operands[0]) != QImode"
8650   [(parallel [(set (zero_extract:SI (match_dup 0) (const_int 8) (const_int 8))
8651                    (any_or:SI (zero_extract:SI (match_dup 1)
8652                                                (const_int 8) (const_int 8))
8653                               (match_dup 2)))
8654               (clobber (reg:CC FLAGS_REG))])]
8655 {
8656   operands[0] = gen_lowpart (SImode, operands[0]);
8657   operands[1] = gen_lowpart (SImode, operands[1]);
8658   operands[2] = gen_int_mode ((INTVAL (operands[2]) >> 8) & 0xff, SImode);
8659 })
8660
8661 ;; Since OR can be encoded with sign extended immediate, this is only
8662 ;; profitable when 7th bit is set.
8663 (define_split
8664   [(set (match_operand 0 "register_operand")
8665         (any_or (match_operand 1 "general_operand")
8666                 (match_operand 2 "const_int_operand")))
8667    (clobber (reg:CC FLAGS_REG))]
8668    "reload_completed
8669     && ANY_QI_REG_P (operands[0])
8670     && (!TARGET_PARTIAL_REG_STALL || optimize_function_for_size_p (cfun))
8671     && !(INTVAL (operands[2]) & ~255)
8672     && (INTVAL (operands[2]) & 128)
8673     && GET_MODE (operands[0]) != QImode"
8674   [(parallel [(set (strict_low_part (match_dup 0))
8675                    (any_or:QI (match_dup 1)
8676                               (match_dup 2)))
8677               (clobber (reg:CC FLAGS_REG))])]
8678 {
8679   operands[0] = gen_lowpart (QImode, operands[0]);
8680   operands[1] = gen_lowpart (QImode, operands[1]);
8681   operands[2] = gen_lowpart (QImode, operands[2]);
8682 })
8683
8684 (define_expand "xorqi_cc_ext_1"
8685   [(parallel [
8686      (set (reg:CCNO FLAGS_REG)
8687           (compare:CCNO
8688             (xor:SI
8689               (zero_extract:SI
8690                 (match_operand 1 "ext_register_operand")
8691                 (const_int 8)
8692                 (const_int 8))
8693               (match_operand:QI 2 "const_int_operand"))
8694             (const_int 0)))
8695      (set (zero_extract:SI (match_operand 0 "ext_register_operand")
8696                            (const_int 8)
8697                            (const_int 8))
8698           (xor:SI
8699             (zero_extract:SI
8700              (match_dup 1)
8701              (const_int 8)
8702              (const_int 8))
8703             (match_dup 2)))])])
8704
8705 (define_insn "*xorqi_cc_ext_1"
8706   [(set (reg FLAGS_REG)
8707         (compare
8708           (xor:SI
8709             (zero_extract:SI
8710               (match_operand 1 "ext_register_operand" "0,0")
8711               (const_int 8)
8712               (const_int 8))
8713             (match_operand:QI 2 "general_x64nomem_operand" "Qn,m"))
8714           (const_int 0)))
8715    (set (zero_extract:SI (match_operand 0 "ext_register_operand" "=Q,Q")
8716                          (const_int 8)
8717                          (const_int 8))
8718         (xor:SI
8719           (zero_extract:SI
8720            (match_dup 1)
8721            (const_int 8)
8722            (const_int 8))
8723           (match_dup 2)))]
8724   "ix86_match_ccmode (insn, CCNOmode)"
8725   "xor{b}\t{%2, %h0|%h0, %2}"
8726   [(set_attr "isa" "*,nox64")
8727    (set_attr "type" "alu")
8728    (set_attr "modrm" "1")
8729    (set_attr "mode" "QI")])
8730 \f
8731 ;; Negation instructions
8732
8733 (define_expand "neg<mode>2"
8734   [(set (match_operand:SDWIM 0 "nonimmediate_operand")
8735         (neg:SDWIM (match_operand:SDWIM 1 "nonimmediate_operand")))]
8736   ""
8737   "ix86_expand_unary_operator (NEG, <MODE>mode, operands); DONE;")
8738
8739 (define_insn_and_split "*neg<dwi>2_doubleword"
8740   [(set (match_operand:<DWI> 0 "nonimmediate_operand" "=ro")
8741         (neg:<DWI> (match_operand:<DWI> 1 "nonimmediate_operand" "0")))
8742    (clobber (reg:CC FLAGS_REG))]
8743   "ix86_unary_operator_ok (NEG, <DWI>mode, operands)"
8744   "#"
8745   "reload_completed"
8746   [(parallel
8747     [(set (reg:CCZ FLAGS_REG)
8748           (compare:CCZ (neg:DWIH (match_dup 1)) (const_int 0)))
8749      (set (match_dup 0) (neg:DWIH (match_dup 1)))])
8750    (parallel
8751     [(set (match_dup 2)
8752           (plus:DWIH (match_dup 3)
8753                      (plus:DWIH (ltu:DWIH (reg:CC FLAGS_REG) (const_int 0))
8754                                 (const_int 0))))
8755      (clobber (reg:CC FLAGS_REG))])
8756    (parallel
8757     [(set (match_dup 2)
8758           (neg:DWIH (match_dup 2)))
8759      (clobber (reg:CC FLAGS_REG))])]
8760   "split_double_mode (<DWI>mode, &operands[0], 2, &operands[0], &operands[2]);")
8761
8762 (define_insn "*neg<mode>2_1"
8763   [(set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
8764         (neg:SWI (match_operand:SWI 1 "nonimmediate_operand" "0")))
8765    (clobber (reg:CC FLAGS_REG))]
8766   "ix86_unary_operator_ok (NEG, <MODE>mode, operands)"
8767   "neg{<imodesuffix>}\t%0"
8768   [(set_attr "type" "negnot")
8769    (set_attr "mode" "<MODE>")])
8770
8771 ;; Combine is quite creative about this pattern.
8772 (define_insn "*negsi2_1_zext"
8773   [(set (match_operand:DI 0 "register_operand" "=r")
8774         (lshiftrt:DI
8775           (neg:DI (ashift:DI (match_operand:DI 1 "register_operand" "0")
8776                              (const_int 32)))
8777         (const_int 32)))
8778    (clobber (reg:CC FLAGS_REG))]
8779   "TARGET_64BIT && ix86_unary_operator_ok (NEG, SImode, operands)"
8780   "neg{l}\t%k0"
8781   [(set_attr "type" "negnot")
8782    (set_attr "mode" "SI")])
8783
8784 ;; The problem with neg is that it does not perform (compare x 0),
8785 ;; it really performs (compare 0 x), which leaves us with the zero
8786 ;; flag being the only useful item.
8787
8788 (define_insn "*neg<mode>2_cmpz"
8789   [(set (reg:CCZ FLAGS_REG)
8790         (compare:CCZ
8791           (neg:SWI (match_operand:SWI 1 "nonimmediate_operand" "0"))
8792                    (const_int 0)))
8793    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
8794         (neg:SWI (match_dup 1)))]
8795   "ix86_unary_operator_ok (NEG, <MODE>mode, operands)"
8796   "neg{<imodesuffix>}\t%0"
8797   [(set_attr "type" "negnot")
8798    (set_attr "mode" "<MODE>")])
8799
8800 (define_insn "*negsi2_cmpz_zext"
8801   [(set (reg:CCZ FLAGS_REG)
8802         (compare:CCZ
8803           (lshiftrt:DI
8804             (neg:DI (ashift:DI
8805                       (match_operand:DI 1 "register_operand" "0")
8806                       (const_int 32)))
8807             (const_int 32))
8808           (const_int 0)))
8809    (set (match_operand:DI 0 "register_operand" "=r")
8810         (lshiftrt:DI (neg:DI (ashift:DI (match_dup 1)
8811                                         (const_int 32)))
8812                      (const_int 32)))]
8813   "TARGET_64BIT && ix86_unary_operator_ok (NEG, SImode, operands)"
8814   "neg{l}\t%k0"
8815   [(set_attr "type" "negnot")
8816    (set_attr "mode" "SI")])
8817
8818 ;; Negate with jump on overflow.
8819 (define_expand "negv<mode>3"
8820   [(parallel [(set (reg:CCO FLAGS_REG)
8821                    (ne:CCO (match_operand:SWI 1 "register_operand")
8822                            (match_dup 3)))
8823               (set (match_operand:SWI 0 "register_operand")
8824                    (neg:SWI (match_dup 1)))])
8825    (set (pc) (if_then_else
8826                (eq (reg:CCO FLAGS_REG) (const_int 0))
8827                (label_ref (match_operand 2))
8828                (pc)))]
8829   ""
8830 {
8831   operands[3]
8832     = gen_int_mode (HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (<MODE>mode) - 1),
8833                     <MODE>mode);
8834 })
8835
8836 (define_insn "*negv<mode>3"
8837   [(set (reg:CCO FLAGS_REG)
8838         (ne:CCO (match_operand:SWI 1 "nonimmediate_operand" "0")
8839                 (match_operand:SWI 2 "const_int_operand")))
8840    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
8841         (neg:SWI (match_dup 1)))]
8842   "ix86_unary_operator_ok (NEG, <MODE>mode, operands)
8843    && mode_signbit_p (<MODE>mode, operands[2])"
8844   "neg{<imodesuffix>}\t%0"
8845   [(set_attr "type" "negnot")
8846    (set_attr "mode" "<MODE>")])
8847
8848 ;; Changing of sign for FP values is doable using integer unit too.
8849
8850 (define_expand "<code><mode>2"
8851   [(set (match_operand:X87MODEF 0 "register_operand")
8852         (absneg:X87MODEF (match_operand:X87MODEF 1 "register_operand")))]
8853   "TARGET_80387 || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
8854   "ix86_expand_fp_absneg_operator (<CODE>, <MODE>mode, operands); DONE;")
8855
8856 (define_insn "*absneg<mode>2_mixed"
8857   [(set (match_operand:MODEF 0 "register_operand" "=x,x,f,!r")
8858         (match_operator:MODEF 3 "absneg_operator"
8859           [(match_operand:MODEF 1 "register_operand" "0,x,0,0")]))
8860    (use (match_operand:<ssevecmode> 2 "nonimmediate_operand" "xm,0,X,X"))
8861    (clobber (reg:CC FLAGS_REG))]
8862   "TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (<MODE>mode)"
8863   "#")
8864
8865 (define_insn "*absneg<mode>2_sse"
8866   [(set (match_operand:MODEF 0 "register_operand" "=x,x,!r")
8867         (match_operator:MODEF 3 "absneg_operator"
8868           [(match_operand:MODEF 1 "register_operand" "0 ,x,0")]))
8869    (use (match_operand:<ssevecmode> 2 "register_operand" "xm,0,X"))
8870    (clobber (reg:CC FLAGS_REG))]
8871   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH"
8872   "#")
8873
8874 (define_insn "*absneg<mode>2_i387"
8875   [(set (match_operand:X87MODEF 0 "register_operand" "=f,!r")
8876         (match_operator:X87MODEF 3 "absneg_operator"
8877           [(match_operand:X87MODEF 1 "register_operand" "0,0")]))
8878    (use (match_operand 2))
8879    (clobber (reg:CC FLAGS_REG))]
8880   "TARGET_80387 && !(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
8881   "#")
8882
8883 (define_expand "<code>tf2"
8884   [(set (match_operand:TF 0 "register_operand")
8885         (absneg:TF (match_operand:TF 1 "register_operand")))]
8886   "TARGET_SSE"
8887   "ix86_expand_fp_absneg_operator (<CODE>, TFmode, operands); DONE;")
8888
8889 (define_insn "*absnegtf2_sse"
8890   [(set (match_operand:TF 0 "register_operand" "=x,x")
8891         (match_operator:TF 3 "absneg_operator"
8892           [(match_operand:TF 1 "register_operand" "0,x")]))
8893    (use (match_operand:TF 2 "nonimmediate_operand" "xm,0"))
8894    (clobber (reg:CC FLAGS_REG))]
8895   "TARGET_SSE"
8896   "#")
8897
8898 ;; Splitters for fp abs and neg.
8899
8900 (define_split
8901   [(set (match_operand 0 "fp_register_operand")
8902         (match_operator 1 "absneg_operator" [(match_dup 0)]))
8903    (use (match_operand 2))
8904    (clobber (reg:CC FLAGS_REG))]
8905   "reload_completed"
8906   [(set (match_dup 0) (match_op_dup 1 [(match_dup 0)]))])
8907
8908 (define_split
8909   [(set (match_operand 0 "register_operand")
8910         (match_operator 3 "absneg_operator"
8911           [(match_operand 1 "register_operand")]))
8912    (use (match_operand 2 "nonimmediate_operand"))
8913    (clobber (reg:CC FLAGS_REG))]
8914   "reload_completed && SSE_REG_P (operands[0])"
8915   [(set (match_dup 0) (match_dup 3))]
8916 {
8917   machine_mode mode = GET_MODE (operands[0]);
8918   machine_mode vmode = GET_MODE (operands[2]);
8919   rtx tmp;
8920
8921   operands[0] = simplify_gen_subreg (vmode, operands[0], mode, 0);
8922   operands[1] = simplify_gen_subreg (vmode, operands[1], mode, 0);
8923   if (operands_match_p (operands[0], operands[2]))
8924     std::swap (operands[1], operands[2]);
8925   if (GET_CODE (operands[3]) == ABS)
8926     tmp = gen_rtx_AND (vmode, operands[1], operands[2]);
8927   else
8928     tmp = gen_rtx_XOR (vmode, operands[1], operands[2]);
8929   operands[3] = tmp;
8930 })
8931
8932 (define_split
8933   [(set (match_operand:SF 0 "register_operand")
8934         (match_operator:SF 1 "absneg_operator" [(match_dup 0)]))
8935    (use (match_operand:V4SF 2))
8936    (clobber (reg:CC FLAGS_REG))]
8937   "reload_completed"
8938   [(parallel [(set (match_dup 0) (match_dup 1))
8939               (clobber (reg:CC FLAGS_REG))])]
8940 {
8941   rtx tmp;
8942   operands[0] = gen_lowpart (SImode, operands[0]);
8943   if (GET_CODE (operands[1]) == ABS)
8944     {
8945       tmp = gen_int_mode (0x7fffffff, SImode);
8946       tmp = gen_rtx_AND (SImode, operands[0], tmp);
8947     }
8948   else
8949     {
8950       tmp = gen_int_mode (0x80000000, SImode);
8951       tmp = gen_rtx_XOR (SImode, operands[0], tmp);
8952     }
8953   operands[1] = tmp;
8954 })
8955
8956 (define_split
8957   [(set (match_operand:DF 0 "register_operand")
8958         (match_operator:DF 1 "absneg_operator" [(match_dup 0)]))
8959    (use (match_operand 2))
8960    (clobber (reg:CC FLAGS_REG))]
8961   "reload_completed"
8962   [(parallel [(set (match_dup 0) (match_dup 1))
8963               (clobber (reg:CC FLAGS_REG))])]
8964 {
8965   rtx tmp;
8966   if (TARGET_64BIT)
8967     {
8968       tmp = gen_lowpart (DImode, operands[0]);
8969       tmp = gen_rtx_ZERO_EXTRACT (DImode, tmp, const1_rtx, GEN_INT (63));
8970       operands[0] = tmp;
8971
8972       if (GET_CODE (operands[1]) == ABS)
8973         tmp = const0_rtx;
8974       else
8975         tmp = gen_rtx_NOT (DImode, tmp);
8976     }
8977   else
8978     {
8979       operands[0] = gen_highpart (SImode, operands[0]);
8980       if (GET_CODE (operands[1]) == ABS)
8981         {
8982           tmp = gen_int_mode (0x7fffffff, SImode);
8983           tmp = gen_rtx_AND (SImode, operands[0], tmp);
8984         }
8985       else
8986         {
8987           tmp = gen_int_mode (0x80000000, SImode);
8988           tmp = gen_rtx_XOR (SImode, operands[0], tmp);
8989         }
8990     }
8991   operands[1] = tmp;
8992 })
8993
8994 (define_split
8995   [(set (match_operand:XF 0 "register_operand")
8996         (match_operator:XF 1 "absneg_operator" [(match_dup 0)]))
8997    (use (match_operand 2))
8998    (clobber (reg:CC FLAGS_REG))]
8999   "reload_completed"
9000   [(parallel [(set (match_dup 0) (match_dup 1))
9001               (clobber (reg:CC FLAGS_REG))])]
9002 {
9003   rtx tmp;
9004   operands[0] = gen_rtx_REG (SImode,
9005                              true_regnum (operands[0])
9006                              + (TARGET_64BIT ? 1 : 2));
9007   if (GET_CODE (operands[1]) == ABS)
9008     {
9009       tmp = GEN_INT (0x7fff);
9010       tmp = gen_rtx_AND (SImode, operands[0], tmp);
9011     }
9012   else
9013     {
9014       tmp = GEN_INT (0x8000);
9015       tmp = gen_rtx_XOR (SImode, operands[0], tmp);
9016     }
9017   operands[1] = tmp;
9018 })
9019
9020 ;; Conditionalize these after reload. If they match before reload, we
9021 ;; lose the clobber and ability to use integer instructions.
9022
9023 (define_insn "*<code><mode>2_1"
9024   [(set (match_operand:X87MODEF 0 "register_operand" "=f")
9025         (absneg:X87MODEF (match_operand:X87MODEF 1 "register_operand" "0")))]
9026   "TARGET_80387
9027    && (reload_completed
9028        || !(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH))"
9029   "f<absneg_mnemonic>"
9030   [(set_attr "type" "fsgn")
9031    (set_attr "mode" "<MODE>")])
9032
9033 (define_insn "*<code>extendsfdf2"
9034   [(set (match_operand:DF 0 "register_operand" "=f")
9035         (absneg:DF (float_extend:DF
9036                      (match_operand:SF 1 "register_operand" "0"))))]
9037   "TARGET_80387 && (!TARGET_SSE_MATH || TARGET_MIX_SSE_I387)"
9038   "f<absneg_mnemonic>"
9039   [(set_attr "type" "fsgn")
9040    (set_attr "mode" "DF")])
9041
9042 (define_insn "*<code>extendsfxf2"
9043   [(set (match_operand:XF 0 "register_operand" "=f")
9044         (absneg:XF (float_extend:XF
9045                      (match_operand:SF 1 "register_operand" "0"))))]
9046   "TARGET_80387"
9047   "f<absneg_mnemonic>"
9048   [(set_attr "type" "fsgn")
9049    (set_attr "mode" "XF")])
9050
9051 (define_insn "*<code>extenddfxf2"
9052   [(set (match_operand:XF 0 "register_operand" "=f")
9053         (absneg:XF (float_extend:XF
9054                      (match_operand:DF 1 "register_operand" "0"))))]
9055   "TARGET_80387"
9056   "f<absneg_mnemonic>"
9057   [(set_attr "type" "fsgn")
9058    (set_attr "mode" "XF")])
9059
9060 ;; Copysign instructions
9061
9062 (define_mode_iterator CSGNMODE [SF DF TF])
9063 (define_mode_attr CSGNVMODE [(SF "V4SF") (DF "V2DF") (TF "TF")])
9064
9065 (define_expand "copysign<mode>3"
9066   [(match_operand:CSGNMODE 0 "register_operand")
9067    (match_operand:CSGNMODE 1 "nonmemory_operand")
9068    (match_operand:CSGNMODE 2 "register_operand")]
9069   "(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
9070    || (TARGET_SSE && (<MODE>mode == TFmode))"
9071   "ix86_expand_copysign (operands); DONE;")
9072
9073 (define_insn_and_split "copysign<mode>3_const"
9074   [(set (match_operand:CSGNMODE 0 "register_operand" "=x")
9075         (unspec:CSGNMODE
9076           [(match_operand:<CSGNVMODE> 1 "vector_move_operand" "xmC")
9077            (match_operand:CSGNMODE 2 "register_operand" "0")
9078            (match_operand:<CSGNVMODE> 3 "nonimmediate_operand" "xm")]
9079           UNSPEC_COPYSIGN))]
9080   "(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
9081    || (TARGET_SSE && (<MODE>mode == TFmode))"
9082   "#"
9083   "&& reload_completed"
9084   [(const_int 0)]
9085   "ix86_split_copysign_const (operands); DONE;")
9086
9087 (define_insn "copysign<mode>3_var"
9088   [(set (match_operand:CSGNMODE 0 "register_operand" "=x,x,x,x,x")
9089         (unspec:CSGNMODE
9090           [(match_operand:CSGNMODE 2 "register_operand" "x,0,0,x,x")
9091            (match_operand:CSGNMODE 3 "register_operand" "1,1,x,1,x")
9092            (match_operand:<CSGNVMODE> 4 "nonimmediate_operand" "X,xm,xm,0,0")
9093            (match_operand:<CSGNVMODE> 5 "nonimmediate_operand" "0,xm,1,xm,1")]
9094           UNSPEC_COPYSIGN))
9095    (clobber (match_scratch:<CSGNVMODE> 1 "=x,x,x,x,x"))]
9096   "(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
9097    || (TARGET_SSE && (<MODE>mode == TFmode))"
9098   "#")
9099
9100 (define_split
9101   [(set (match_operand:CSGNMODE 0 "register_operand")
9102         (unspec:CSGNMODE
9103           [(match_operand:CSGNMODE 2 "register_operand")
9104            (match_operand:CSGNMODE 3 "register_operand")
9105            (match_operand:<CSGNVMODE> 4)
9106            (match_operand:<CSGNVMODE> 5)]
9107           UNSPEC_COPYSIGN))
9108    (clobber (match_scratch:<CSGNVMODE> 1))]
9109   "((SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
9110     || (TARGET_SSE && (<MODE>mode == TFmode)))
9111    && reload_completed"
9112   [(const_int 0)]
9113   "ix86_split_copysign_var (operands); DONE;")
9114 \f
9115 ;; One complement instructions
9116
9117 (define_expand "one_cmpl<mode>2"
9118   [(set (match_operand:SWIM 0 "nonimmediate_operand")
9119         (not:SWIM (match_operand:SWIM 1 "nonimmediate_operand")))]
9120   ""
9121   "ix86_expand_unary_operator (NOT, <MODE>mode, operands); DONE;")
9122
9123 (define_insn "*one_cmpl<mode>2_1"
9124   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm,k")
9125         (not:SWI48 (match_operand:SWI48 1 "nonimmediate_operand" "0,k")))]
9126   "ix86_unary_operator_ok (NOT, <MODE>mode, operands)"
9127   "@
9128    not{<imodesuffix>}\t%0
9129    knot<mskmodesuffix>\t{%1, %0|%0, %1}"
9130   [(set_attr "isa" "*,avx512bw")
9131    (set_attr "type" "negnot,msklog")
9132    (set_attr "prefix" "*,vex")
9133    (set_attr "mode" "<MODE>")])
9134
9135 (define_insn "*one_cmplhi2_1"
9136   [(set (match_operand:HI 0 "nonimmediate_operand" "=rm,!k")
9137         (not:HI (match_operand:HI 1 "nonimmediate_operand" "0,k")))]
9138   "ix86_unary_operator_ok (NOT, HImode, operands)"
9139   "@
9140    not{w}\t%0
9141    knotw\t{%1, %0|%0, %1}"
9142   [(set_attr "isa" "*,avx512f")
9143    (set_attr "type" "negnot,msklog")
9144    (set_attr "prefix" "*,vex")
9145    (set_attr "mode" "HI")])
9146
9147 ;; %%% Potential partial reg stall on alternative 1.  What to do?
9148 (define_insn "*one_cmplqi2_1"
9149   [(set (match_operand:QI 0 "nonimmediate_operand" "=qm,r,!k")
9150         (not:QI (match_operand:QI 1 "nonimmediate_operand" "0,0,k")))]
9151   "ix86_unary_operator_ok (NOT, QImode, operands)"
9152 {
9153   switch (which_alternative)
9154     {
9155     case 0:
9156       return "not{b}\t%0";
9157     case 1:
9158       return "not{l}\t%k0";
9159     case 2:
9160       if (TARGET_AVX512DQ)
9161         return "knotb\t{%1, %0|%0, %1}";
9162       return "knotw\t{%1, %0|%0, %1}";
9163     default:
9164       gcc_unreachable ();
9165     }
9166 }
9167   [(set_attr "isa" "*,*,avx512f")
9168    (set_attr "type" "negnot,negnot,msklog")
9169    (set_attr "prefix" "*,*,vex")
9170    (set_attr "mode" "QI,SI,QI")])
9171
9172 ;; ??? Currently never generated - xor is used instead.
9173 (define_insn "*one_cmplsi2_1_zext"
9174   [(set (match_operand:DI 0 "register_operand" "=r")
9175         (zero_extend:DI
9176           (not:SI (match_operand:SI 1 "register_operand" "0"))))]
9177   "TARGET_64BIT && ix86_unary_operator_ok (NOT, SImode, operands)"
9178   "not{l}\t%k0"
9179   [(set_attr "type" "negnot")
9180    (set_attr "mode" "SI")])
9181
9182 (define_insn "*one_cmpl<mode>2_2"
9183   [(set (reg FLAGS_REG)
9184         (compare (not:SWI (match_operand:SWI 1 "nonimmediate_operand" "0"))
9185                  (const_int 0)))
9186    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
9187         (not:SWI (match_dup 1)))]
9188   "ix86_match_ccmode (insn, CCNOmode)
9189    && ix86_unary_operator_ok (NOT, <MODE>mode, operands)"
9190   "#"
9191   [(set_attr "type" "alu1")
9192    (set_attr "mode" "<MODE>")])
9193
9194 (define_split
9195   [(set (match_operand 0 "flags_reg_operand")
9196         (match_operator 2 "compare_operator"
9197           [(not:SWI (match_operand:SWI 3 "nonimmediate_operand"))
9198            (const_int 0)]))
9199    (set (match_operand:SWI 1 "nonimmediate_operand")
9200         (not:SWI (match_dup 3)))]
9201   "ix86_match_ccmode (insn, CCNOmode)"
9202   [(parallel [(set (match_dup 0)
9203                    (match_op_dup 2 [(xor:SWI (match_dup 3) (const_int -1))
9204                                     (const_int 0)]))
9205               (set (match_dup 1)
9206                    (xor:SWI (match_dup 3) (const_int -1)))])])
9207
9208 ;; ??? Currently never generated - xor is used instead.
9209 (define_insn "*one_cmplsi2_2_zext"
9210   [(set (reg FLAGS_REG)
9211         (compare (not:SI (match_operand:SI 1 "register_operand" "0"))
9212                  (const_int 0)))
9213    (set (match_operand:DI 0 "register_operand" "=r")
9214         (zero_extend:DI (not:SI (match_dup 1))))]
9215   "TARGET_64BIT && ix86_match_ccmode (insn, CCNOmode)
9216    && ix86_unary_operator_ok (NOT, SImode, operands)"
9217   "#"
9218   [(set_attr "type" "alu1")
9219    (set_attr "mode" "SI")])
9220
9221 (define_split
9222   [(set (match_operand 0 "flags_reg_operand")
9223         (match_operator 2 "compare_operator"
9224           [(not:SI (match_operand:SI 3 "register_operand"))
9225            (const_int 0)]))
9226    (set (match_operand:DI 1 "register_operand")
9227         (zero_extend:DI (not:SI (match_dup 3))))]
9228   "ix86_match_ccmode (insn, CCNOmode)"
9229   [(parallel [(set (match_dup 0)
9230                    (match_op_dup 2 [(xor:SI (match_dup 3) (const_int -1))
9231                                     (const_int 0)]))
9232               (set (match_dup 1)
9233                    (zero_extend:DI (xor:SI (match_dup 3) (const_int -1))))])])
9234 \f
9235 ;; Shift instructions
9236
9237 ;; DImode shifts are implemented using the i386 "shift double" opcode,
9238 ;; which is written as "sh[lr]d[lw] imm,reg,reg/mem".  If the shift count
9239 ;; is variable, then the count is in %cl and the "imm" operand is dropped
9240 ;; from the assembler input.
9241 ;;
9242 ;; This instruction shifts the target reg/mem as usual, but instead of
9243 ;; shifting in zeros, bits are shifted in from reg operand.  If the insn
9244 ;; is a left shift double, bits are taken from the high order bits of
9245 ;; reg, else if the insn is a shift right double, bits are taken from the
9246 ;; low order bits of reg.  So if %eax is "1234" and %edx is "5678",
9247 ;; "shldl $8,%edx,%eax" leaves %edx unchanged and sets %eax to "2345".
9248 ;;
9249 ;; Since sh[lr]d does not change the `reg' operand, that is done
9250 ;; separately, making all shifts emit pairs of shift double and normal
9251 ;; shift.  Since sh[lr]d does not shift more than 31 bits, and we wish to
9252 ;; support a 63 bit shift, each shift where the count is in a reg expands
9253 ;; to a pair of shifts, a branch, a shift by 32 and a label.
9254 ;;
9255 ;; If the shift count is a constant, we need never emit more than one
9256 ;; shift pair, instead using moves and sign extension for counts greater
9257 ;; than 31.
9258
9259 (define_expand "ashl<mode>3"
9260   [(set (match_operand:SDWIM 0 "<shift_operand>")
9261         (ashift:SDWIM (match_operand:SDWIM 1 "<ashl_input_operand>")
9262                       (match_operand:QI 2 "nonmemory_operand")))]
9263   ""
9264   "ix86_expand_binary_operator (ASHIFT, <MODE>mode, operands); DONE;")
9265
9266 (define_insn "*ashl<mode>3_doubleword"
9267   [(set (match_operand:DWI 0 "register_operand" "=&r,r")
9268         (ashift:DWI (match_operand:DWI 1 "reg_or_pm1_operand" "n,0")
9269                     (match_operand:QI 2 "nonmemory_operand" "<S>c,<S>c")))
9270    (clobber (reg:CC FLAGS_REG))]
9271   ""
9272   "#"
9273   [(set_attr "type" "multi")])
9274
9275 (define_split
9276   [(set (match_operand:DWI 0 "register_operand")
9277         (ashift:DWI (match_operand:DWI 1 "nonmemory_operand")
9278                     (match_operand:QI 2 "nonmemory_operand")))
9279    (clobber (reg:CC FLAGS_REG))]
9280   "(optimize && flag_peephole2) ? epilogue_completed : reload_completed"
9281   [(const_int 0)]
9282   "ix86_split_ashl (operands, NULL_RTX, <MODE>mode); DONE;")
9283
9284 ;; By default we don't ask for a scratch register, because when DWImode
9285 ;; values are manipulated, registers are already at a premium.  But if
9286 ;; we have one handy, we won't turn it away.
9287
9288 (define_peephole2
9289   [(match_scratch:DWIH 3 "r")
9290    (parallel [(set (match_operand:<DWI> 0 "register_operand")
9291                    (ashift:<DWI>
9292                      (match_operand:<DWI> 1 "nonmemory_operand")
9293                      (match_operand:QI 2 "nonmemory_operand")))
9294               (clobber (reg:CC FLAGS_REG))])
9295    (match_dup 3)]
9296   "TARGET_CMOVE"
9297   [(const_int 0)]
9298   "ix86_split_ashl (operands, operands[3], <DWI>mode); DONE;")
9299
9300 (define_insn "x86_64_shld"
9301   [(set (match_operand:DI 0 "nonimmediate_operand" "+r*m")
9302         (ior:DI (ashift:DI (match_dup 0)
9303                   (match_operand:QI 2 "nonmemory_operand" "Jc"))
9304                 (lshiftrt:DI (match_operand:DI 1 "register_operand" "r")
9305                   (minus:QI (const_int 64) (match_dup 2)))))
9306    (clobber (reg:CC FLAGS_REG))]
9307   "TARGET_64BIT"
9308   "shld{q}\t{%s2%1, %0|%0, %1, %2}"
9309   [(set_attr "type" "ishift")
9310    (set_attr "prefix_0f" "1")
9311    (set_attr "mode" "DI")
9312    (set_attr "athlon_decode" "vector")
9313    (set_attr "amdfam10_decode" "vector")
9314    (set_attr "bdver1_decode" "vector")])
9315
9316 (define_insn "x86_shld"
9317   [(set (match_operand:SI 0 "nonimmediate_operand" "+r*m")
9318         (ior:SI (ashift:SI (match_dup 0)
9319                   (match_operand:QI 2 "nonmemory_operand" "Ic"))
9320                 (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
9321                   (minus:QI (const_int 32) (match_dup 2)))))
9322    (clobber (reg:CC FLAGS_REG))]
9323   ""
9324   "shld{l}\t{%s2%1, %0|%0, %1, %2}"
9325   [(set_attr "type" "ishift")
9326    (set_attr "prefix_0f" "1")
9327    (set_attr "mode" "SI")
9328    (set_attr "pent_pair" "np")
9329    (set_attr "athlon_decode" "vector")
9330    (set_attr "amdfam10_decode" "vector")
9331    (set_attr "bdver1_decode" "vector")])
9332
9333 (define_expand "x86_shift<mode>_adj_1"
9334   [(set (reg:CCZ FLAGS_REG)
9335         (compare:CCZ (and:QI (match_operand:QI 2 "register_operand")
9336                              (match_dup 4))
9337                      (const_int 0)))
9338    (set (match_operand:SWI48 0 "register_operand")
9339         (if_then_else:SWI48 (ne (reg:CCZ FLAGS_REG) (const_int 0))
9340                             (match_operand:SWI48 1 "register_operand")
9341                             (match_dup 0)))
9342    (set (match_dup 1)
9343         (if_then_else:SWI48 (ne (reg:CCZ FLAGS_REG) (const_int 0))
9344                             (match_operand:SWI48 3 "register_operand")
9345                             (match_dup 1)))]
9346   "TARGET_CMOVE"
9347   "operands[4] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode));")
9348
9349 (define_expand "x86_shift<mode>_adj_2"
9350   [(use (match_operand:SWI48 0 "register_operand"))
9351    (use (match_operand:SWI48 1 "register_operand"))
9352    (use (match_operand:QI 2 "register_operand"))]
9353   ""
9354 {
9355   rtx_code_label *label = gen_label_rtx ();
9356   rtx tmp;
9357
9358   emit_insn (gen_testqi_ccz_1 (operands[2],
9359                                GEN_INT (GET_MODE_BITSIZE (<MODE>mode))));
9360
9361   tmp = gen_rtx_REG (CCZmode, FLAGS_REG);
9362   tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
9363   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9364                               gen_rtx_LABEL_REF (VOIDmode, label),
9365                               pc_rtx);
9366   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
9367   JUMP_LABEL (tmp) = label;
9368
9369   emit_move_insn (operands[0], operands[1]);
9370   ix86_expand_clear (operands[1]);
9371
9372   emit_label (label);
9373   LABEL_NUSES (label) = 1;
9374
9375   DONE;
9376 })
9377
9378 ;; Avoid useless masking of count operand.
9379 (define_insn "*ashl<mode>3_mask"
9380   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm")
9381         (ashift:SWI48
9382           (match_operand:SWI48 1 "nonimmediate_operand" "0")
9383           (subreg:QI
9384             (and:SI
9385               (match_operand:SI 2 "register_operand" "c")
9386               (match_operand:SI 3 "const_int_operand" "n")) 0)))
9387    (clobber (reg:CC FLAGS_REG))]
9388   "ix86_binary_operator_ok (ASHIFT, <MODE>mode, operands)
9389    && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode)-1))
9390       == GET_MODE_BITSIZE (<MODE>mode)-1"
9391 {
9392   return "sal{<imodesuffix>}\t{%b2, %0|%0, %b2}";
9393 }
9394   [(set_attr "type" "ishift")
9395    (set_attr "mode" "<MODE>")])
9396
9397 (define_insn "*bmi2_ashl<mode>3_1"
9398   [(set (match_operand:SWI48 0 "register_operand" "=r")
9399         (ashift:SWI48 (match_operand:SWI48 1 "nonimmediate_operand" "rm")
9400                       (match_operand:SWI48 2 "register_operand" "r")))]
9401   "TARGET_BMI2"
9402   "shlx\t{%2, %1, %0|%0, %1, %2}"
9403   [(set_attr "type" "ishiftx")
9404    (set_attr "mode" "<MODE>")])
9405
9406 (define_insn "*ashl<mode>3_1"
9407   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm,r,r")
9408         (ashift:SWI48 (match_operand:SWI48 1 "nonimmediate_operand" "0,l,rm")
9409                       (match_operand:QI 2 "nonmemory_operand" "c<S>,M,r")))
9410    (clobber (reg:CC FLAGS_REG))]
9411   "ix86_binary_operator_ok (ASHIFT, <MODE>mode, operands)"
9412 {
9413   switch (get_attr_type (insn))
9414     {
9415     case TYPE_LEA:
9416     case TYPE_ISHIFTX:
9417       return "#";
9418
9419     case TYPE_ALU:
9420       gcc_assert (operands[2] == const1_rtx);
9421       gcc_assert (rtx_equal_p (operands[0], operands[1]));
9422       return "add{<imodesuffix>}\t%0, %0";
9423
9424     default:
9425       if (operands[2] == const1_rtx
9426           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9427         return "sal{<imodesuffix>}\t%0";
9428       else
9429         return "sal{<imodesuffix>}\t{%2, %0|%0, %2}";
9430     }
9431 }
9432   [(set_attr "isa" "*,*,bmi2")
9433    (set (attr "type")
9434      (cond [(eq_attr "alternative" "1")
9435               (const_string "lea")
9436             (eq_attr "alternative" "2")
9437               (const_string "ishiftx")
9438             (and (and (match_test "TARGET_DOUBLE_WITH_ADD")
9439                       (match_operand 0 "register_operand"))
9440                  (match_operand 2 "const1_operand"))
9441               (const_string "alu")
9442            ]
9443            (const_string "ishift")))
9444    (set (attr "length_immediate")
9445      (if_then_else
9446        (ior (eq_attr "type" "alu")
9447             (and (eq_attr "type" "ishift")
9448                  (and (match_operand 2 "const1_operand")
9449                       (ior (match_test "TARGET_SHIFT1")
9450                            (match_test "optimize_function_for_size_p (cfun)")))))
9451        (const_string "0")
9452        (const_string "*")))
9453    (set_attr "mode" "<MODE>")])
9454
9455 ;; Convert shift to the shiftx pattern to avoid flags dependency.
9456 (define_split
9457   [(set (match_operand:SWI48 0 "register_operand")
9458         (ashift:SWI48 (match_operand:SWI48 1 "nonimmediate_operand")
9459                       (match_operand:QI 2 "register_operand")))
9460    (clobber (reg:CC FLAGS_REG))]
9461   "TARGET_BMI2 && reload_completed"
9462   [(set (match_dup 0)
9463         (ashift:SWI48 (match_dup 1) (match_dup 2)))]
9464   "operands[2] = gen_lowpart (<MODE>mode, operands[2]);")
9465
9466 (define_insn "*bmi2_ashlsi3_1_zext"
9467   [(set (match_operand:DI 0 "register_operand" "=r")
9468         (zero_extend:DI
9469           (ashift:SI (match_operand:SI 1 "nonimmediate_operand" "rm")
9470                      (match_operand:SI 2 "register_operand" "r"))))]
9471   "TARGET_64BIT && TARGET_BMI2"
9472   "shlx\t{%2, %1, %k0|%k0, %1, %2}"
9473   [(set_attr "type" "ishiftx")
9474    (set_attr "mode" "SI")])
9475
9476 (define_insn "*ashlsi3_1_zext"
9477   [(set (match_operand:DI 0 "register_operand" "=r,r,r")
9478         (zero_extend:DI
9479           (ashift:SI (match_operand:SI 1 "nonimmediate_operand" "0,l,rm")
9480                      (match_operand:QI 2 "nonmemory_operand" "cI,M,r"))))
9481    (clobber (reg:CC FLAGS_REG))]
9482   "TARGET_64BIT && ix86_binary_operator_ok (ASHIFT, SImode, operands)"
9483 {
9484   switch (get_attr_type (insn))
9485     {
9486     case TYPE_LEA:
9487     case TYPE_ISHIFTX:
9488       return "#";
9489
9490     case TYPE_ALU:
9491       gcc_assert (operands[2] == const1_rtx);
9492       return "add{l}\t%k0, %k0";
9493
9494     default:
9495       if (operands[2] == const1_rtx
9496           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9497         return "sal{l}\t%k0";
9498       else
9499         return "sal{l}\t{%2, %k0|%k0, %2}";
9500     }
9501 }
9502   [(set_attr "isa" "*,*,bmi2")
9503    (set (attr "type")
9504      (cond [(eq_attr "alternative" "1")
9505               (const_string "lea")
9506             (eq_attr "alternative" "2")
9507               (const_string "ishiftx")
9508             (and (match_test "TARGET_DOUBLE_WITH_ADD")
9509                  (match_operand 2 "const1_operand"))
9510               (const_string "alu")
9511            ]
9512            (const_string "ishift")))
9513    (set (attr "length_immediate")
9514      (if_then_else
9515        (ior (eq_attr "type" "alu")
9516             (and (eq_attr "type" "ishift")
9517                  (and (match_operand 2 "const1_operand")
9518                       (ior (match_test "TARGET_SHIFT1")
9519                            (match_test "optimize_function_for_size_p (cfun)")))))
9520        (const_string "0")
9521        (const_string "*")))
9522    (set_attr "mode" "SI")])
9523
9524 ;; Convert shift to the shiftx pattern to avoid flags dependency.
9525 (define_split
9526   [(set (match_operand:DI 0 "register_operand")
9527         (zero_extend:DI
9528           (ashift:SI (match_operand:SI 1 "nonimmediate_operand")
9529                      (match_operand:QI 2 "register_operand"))))
9530    (clobber (reg:CC FLAGS_REG))]
9531   "TARGET_64BIT && TARGET_BMI2 && reload_completed"
9532   [(set (match_dup 0)
9533         (zero_extend:DI (ashift:SI (match_dup 1) (match_dup 2))))]
9534   "operands[2] = gen_lowpart (SImode, operands[2]);")
9535
9536 (define_insn "*ashlhi3_1"
9537   [(set (match_operand:HI 0 "nonimmediate_operand" "=rm,Yp")
9538         (ashift:HI (match_operand:HI 1 "nonimmediate_operand" "0,l")
9539                    (match_operand:QI 2 "nonmemory_operand" "cI,M")))
9540    (clobber (reg:CC FLAGS_REG))]
9541   "ix86_binary_operator_ok (ASHIFT, HImode, operands)"
9542 {
9543   switch (get_attr_type (insn))
9544     {
9545     case TYPE_LEA:
9546       return "#";
9547
9548     case TYPE_ALU:
9549       gcc_assert (operands[2] == const1_rtx);
9550       return "add{w}\t%0, %0";
9551
9552     default:
9553       if (operands[2] == const1_rtx
9554           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9555         return "sal{w}\t%0";
9556       else
9557         return "sal{w}\t{%2, %0|%0, %2}";
9558     }
9559 }
9560   [(set (attr "type")
9561      (cond [(eq_attr "alternative" "1")
9562               (const_string "lea")
9563             (and (and (match_test "TARGET_DOUBLE_WITH_ADD")
9564                       (match_operand 0 "register_operand"))
9565                  (match_operand 2 "const1_operand"))
9566               (const_string "alu")
9567            ]
9568            (const_string "ishift")))
9569    (set (attr "length_immediate")
9570      (if_then_else
9571        (ior (eq_attr "type" "alu")
9572             (and (eq_attr "type" "ishift")
9573                  (and (match_operand 2 "const1_operand")
9574                       (ior (match_test "TARGET_SHIFT1")
9575                            (match_test "optimize_function_for_size_p (cfun)")))))
9576        (const_string "0")
9577        (const_string "*")))
9578    (set_attr "mode" "HI,SI")])
9579
9580 ;; %%% Potential partial reg stall on alternative 1.  What to do?
9581 (define_insn "*ashlqi3_1"
9582   [(set (match_operand:QI 0 "nonimmediate_operand" "=qm,r,Yp")
9583         (ashift:QI (match_operand:QI 1 "nonimmediate_operand" "0,0,l")
9584                    (match_operand:QI 2 "nonmemory_operand" "cI,cI,M")))
9585    (clobber (reg:CC FLAGS_REG))]
9586   "ix86_binary_operator_ok (ASHIFT, QImode, operands)"
9587 {
9588   switch (get_attr_type (insn))
9589     {
9590     case TYPE_LEA:
9591       return "#";
9592
9593     case TYPE_ALU:
9594       gcc_assert (operands[2] == const1_rtx);
9595       if (REG_P (operands[1]) && !ANY_QI_REG_P (operands[1]))
9596         return "add{l}\t%k0, %k0";
9597       else
9598         return "add{b}\t%0, %0";
9599
9600     default:
9601       if (operands[2] == const1_rtx
9602           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9603         {
9604           if (get_attr_mode (insn) == MODE_SI)
9605             return "sal{l}\t%k0";
9606           else
9607             return "sal{b}\t%0";
9608         }
9609       else
9610         {
9611           if (get_attr_mode (insn) == MODE_SI)
9612             return "sal{l}\t{%2, %k0|%k0, %2}";
9613           else
9614             return "sal{b}\t{%2, %0|%0, %2}";
9615         }
9616     }
9617 }
9618   [(set (attr "type")
9619      (cond [(eq_attr "alternative" "2")
9620               (const_string "lea")
9621             (and (and (match_test "TARGET_DOUBLE_WITH_ADD")
9622                       (match_operand 0 "register_operand"))
9623                  (match_operand 2 "const1_operand"))
9624               (const_string "alu")
9625            ]
9626            (const_string "ishift")))
9627    (set (attr "length_immediate")
9628      (if_then_else
9629        (ior (eq_attr "type" "alu")
9630             (and (eq_attr "type" "ishift")
9631                  (and (match_operand 2 "const1_operand")
9632                       (ior (match_test "TARGET_SHIFT1")
9633                            (match_test "optimize_function_for_size_p (cfun)")))))
9634        (const_string "0")
9635        (const_string "*")))
9636    (set_attr "mode" "QI,SI,SI")])
9637
9638 (define_insn "*ashlqi3_1_slp"
9639   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm"))
9640         (ashift:QI (match_dup 0)
9641                    (match_operand:QI 1 "nonmemory_operand" "cI")))
9642    (clobber (reg:CC FLAGS_REG))]
9643   "(optimize_function_for_size_p (cfun)
9644     || !TARGET_PARTIAL_FLAG_REG_STALL
9645     || (operands[1] == const1_rtx
9646         && (TARGET_SHIFT1
9647             || (TARGET_DOUBLE_WITH_ADD && REG_P (operands[0])))))"
9648 {
9649   switch (get_attr_type (insn))
9650     {
9651     case TYPE_ALU:
9652       gcc_assert (operands[1] == const1_rtx);
9653       return "add{b}\t%0, %0";
9654
9655     default:
9656       if (operands[1] == const1_rtx
9657           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9658         return "sal{b}\t%0";
9659       else
9660         return "sal{b}\t{%1, %0|%0, %1}";
9661     }
9662 }
9663   [(set (attr "type")
9664      (cond [(and (and (match_test "TARGET_DOUBLE_WITH_ADD")
9665                       (match_operand 0 "register_operand"))
9666                  (match_operand 1 "const1_operand"))
9667               (const_string "alu")
9668            ]
9669            (const_string "ishift1")))
9670    (set (attr "length_immediate")
9671      (if_then_else
9672        (ior (eq_attr "type" "alu")
9673             (and (eq_attr "type" "ishift1")
9674                  (and (match_operand 1 "const1_operand")
9675                       (ior (match_test "TARGET_SHIFT1")
9676                            (match_test "optimize_function_for_size_p (cfun)")))))
9677        (const_string "0")
9678        (const_string "*")))
9679    (set_attr "mode" "QI")])
9680
9681 ;; Convert ashift to the lea pattern to avoid flags dependency.
9682 (define_split
9683   [(set (match_operand 0 "register_operand")
9684         (ashift (match_operand 1 "index_register_operand")
9685                 (match_operand:QI 2 "const_int_operand")))
9686    (clobber (reg:CC FLAGS_REG))]
9687   "GET_MODE (operands[0]) == GET_MODE (operands[1])
9688    && reload_completed
9689    && true_regnum (operands[0]) != true_regnum (operands[1])"
9690   [(const_int 0)]
9691 {
9692   machine_mode mode = GET_MODE (operands[0]);
9693   rtx pat;
9694
9695   if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (SImode))
9696     { 
9697       mode = SImode; 
9698       operands[0] = gen_lowpart (mode, operands[0]);
9699       operands[1] = gen_lowpart (mode, operands[1]);
9700     }
9701
9702   operands[2] = gen_int_mode (1 << INTVAL (operands[2]), mode);
9703
9704   pat = gen_rtx_MULT (mode, operands[1], operands[2]);
9705
9706   emit_insn (gen_rtx_SET (VOIDmode, operands[0], pat));
9707   DONE;
9708 })
9709
9710 ;; Convert ashift to the lea pattern to avoid flags dependency.
9711 (define_split
9712   [(set (match_operand:DI 0 "register_operand")
9713         (zero_extend:DI
9714           (ashift:SI (match_operand:SI 1 "index_register_operand")
9715                      (match_operand:QI 2 "const_int_operand"))))
9716    (clobber (reg:CC FLAGS_REG))]
9717   "TARGET_64BIT && reload_completed
9718    && true_regnum (operands[0]) != true_regnum (operands[1])"
9719   [(set (match_dup 0)
9720         (zero_extend:DI (mult:SI (match_dup 1) (match_dup 2))))]
9721 {
9722   operands[1] = gen_lowpart (SImode, operands[1]);
9723   operands[2] = gen_int_mode (1 << INTVAL (operands[2]), SImode);
9724 })
9725
9726 ;; This pattern can't accept a variable shift count, since shifts by
9727 ;; zero don't affect the flags.  We assume that shifts by constant
9728 ;; zero are optimized away.
9729 (define_insn "*ashl<mode>3_cmp"
9730   [(set (reg FLAGS_REG)
9731         (compare
9732           (ashift:SWI (match_operand:SWI 1 "nonimmediate_operand" "0")
9733                       (match_operand:QI 2 "<shift_immediate_operand>" "<S>"))
9734           (const_int 0)))
9735    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
9736         (ashift:SWI (match_dup 1) (match_dup 2)))]
9737   "(optimize_function_for_size_p (cfun)
9738     || !TARGET_PARTIAL_FLAG_REG_STALL
9739     || (operands[2] == const1_rtx
9740         && (TARGET_SHIFT1
9741             || (TARGET_DOUBLE_WITH_ADD && REG_P (operands[0])))))
9742    && ix86_match_ccmode (insn, CCGOCmode)
9743    && ix86_binary_operator_ok (ASHIFT, <MODE>mode, operands)"
9744 {
9745   switch (get_attr_type (insn))
9746     {
9747     case TYPE_ALU:
9748       gcc_assert (operands[2] == const1_rtx);
9749       return "add{<imodesuffix>}\t%0, %0";
9750
9751     default:
9752       if (operands[2] == const1_rtx
9753           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9754         return "sal{<imodesuffix>}\t%0";
9755       else
9756         return "sal{<imodesuffix>}\t{%2, %0|%0, %2}";
9757     }
9758 }
9759   [(set (attr "type")
9760      (cond [(and (and (match_test "TARGET_DOUBLE_WITH_ADD")
9761                       (match_operand 0 "register_operand"))
9762                  (match_operand 2 "const1_operand"))
9763               (const_string "alu")
9764            ]
9765            (const_string "ishift")))
9766    (set (attr "length_immediate")
9767      (if_then_else
9768        (ior (eq_attr "type" "alu")
9769             (and (eq_attr "type" "ishift")
9770                  (and (match_operand 2 "const1_operand")
9771                       (ior (match_test "TARGET_SHIFT1")
9772                            (match_test "optimize_function_for_size_p (cfun)")))))
9773        (const_string "0")
9774        (const_string "*")))
9775    (set_attr "mode" "<MODE>")])
9776
9777 (define_insn "*ashlsi3_cmp_zext"
9778   [(set (reg FLAGS_REG)
9779         (compare
9780           (ashift:SI (match_operand:SI 1 "register_operand" "0")
9781                      (match_operand:QI 2 "const_1_to_31_operand" "I"))
9782           (const_int 0)))
9783    (set (match_operand:DI 0 "register_operand" "=r")
9784         (zero_extend:DI (ashift:SI (match_dup 1) (match_dup 2))))]
9785   "TARGET_64BIT
9786    && (optimize_function_for_size_p (cfun)
9787        || !TARGET_PARTIAL_FLAG_REG_STALL
9788        || (operands[2] == const1_rtx
9789            && (TARGET_SHIFT1
9790                || TARGET_DOUBLE_WITH_ADD)))
9791    && ix86_match_ccmode (insn, CCGOCmode)
9792    && ix86_binary_operator_ok (ASHIFT, SImode, operands)"
9793 {
9794   switch (get_attr_type (insn))
9795     {
9796     case TYPE_ALU:
9797       gcc_assert (operands[2] == const1_rtx);
9798       return "add{l}\t%k0, %k0";
9799
9800     default:
9801       if (operands[2] == const1_rtx
9802           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9803         return "sal{l}\t%k0";
9804       else
9805         return "sal{l}\t{%2, %k0|%k0, %2}";
9806     }
9807 }
9808   [(set (attr "type")
9809      (cond [(and (match_test "TARGET_DOUBLE_WITH_ADD")
9810                  (match_operand 2 "const1_operand"))
9811               (const_string "alu")
9812            ]
9813            (const_string "ishift")))
9814    (set (attr "length_immediate")
9815      (if_then_else
9816        (ior (eq_attr "type" "alu")
9817             (and (eq_attr "type" "ishift")
9818                  (and (match_operand 2 "const1_operand")
9819                       (ior (match_test "TARGET_SHIFT1")
9820                            (match_test "optimize_function_for_size_p (cfun)")))))
9821        (const_string "0")
9822        (const_string "*")))
9823    (set_attr "mode" "SI")])
9824
9825 (define_insn "*ashl<mode>3_cconly"
9826   [(set (reg FLAGS_REG)
9827         (compare
9828           (ashift:SWI (match_operand:SWI 1 "register_operand" "0")
9829                       (match_operand:QI 2 "<shift_immediate_operand>" "<S>"))
9830           (const_int 0)))
9831    (clobber (match_scratch:SWI 0 "=<r>"))]
9832   "(optimize_function_for_size_p (cfun)
9833     || !TARGET_PARTIAL_FLAG_REG_STALL
9834     || (operands[2] == const1_rtx
9835         && (TARGET_SHIFT1
9836             || TARGET_DOUBLE_WITH_ADD)))
9837    && ix86_match_ccmode (insn, CCGOCmode)"
9838 {
9839   switch (get_attr_type (insn))
9840     {
9841     case TYPE_ALU:
9842       gcc_assert (operands[2] == const1_rtx);
9843       return "add{<imodesuffix>}\t%0, %0";
9844
9845     default:
9846       if (operands[2] == const1_rtx
9847           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
9848         return "sal{<imodesuffix>}\t%0";
9849       else
9850         return "sal{<imodesuffix>}\t{%2, %0|%0, %2}";
9851     }
9852 }
9853   [(set (attr "type")
9854      (cond [(and (and (match_test "TARGET_DOUBLE_WITH_ADD")
9855                       (match_operand 0 "register_operand"))
9856                  (match_operand 2 "const1_operand"))
9857               (const_string "alu")
9858            ]
9859            (const_string "ishift")))
9860    (set (attr "length_immediate")
9861      (if_then_else
9862        (ior (eq_attr "type" "alu")
9863             (and (eq_attr "type" "ishift")
9864                  (and (match_operand 2 "const1_operand")
9865                       (ior (match_test "TARGET_SHIFT1")
9866                            (match_test "optimize_function_for_size_p (cfun)")))))
9867        (const_string "0")
9868        (const_string "*")))
9869    (set_attr "mode" "<MODE>")])
9870
9871 ;; See comment above `ashl<mode>3' about how this works.
9872
9873 (define_expand "<shift_insn><mode>3"
9874   [(set (match_operand:SDWIM 0 "<shift_operand>")
9875         (any_shiftrt:SDWIM (match_operand:SDWIM 1 "<shift_operand>")
9876                            (match_operand:QI 2 "nonmemory_operand")))]
9877   ""
9878   "ix86_expand_binary_operator (<CODE>, <MODE>mode, operands); DONE;")
9879
9880 ;; Avoid useless masking of count operand.
9881 (define_insn "*<shift_insn><mode>3_mask"
9882   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm")
9883         (any_shiftrt:SWI48
9884           (match_operand:SWI48 1 "nonimmediate_operand" "0")
9885           (subreg:QI
9886             (and:SI
9887               (match_operand:SI 2 "register_operand" "c")
9888               (match_operand:SI 3 "const_int_operand" "n")) 0)))
9889    (clobber (reg:CC FLAGS_REG))]
9890   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)
9891    && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode)-1))
9892       == GET_MODE_BITSIZE (<MODE>mode)-1"
9893 {
9894   return "<shift>{<imodesuffix>}\t{%b2, %0|%0, %b2}";
9895 }
9896   [(set_attr "type" "ishift")
9897    (set_attr "mode" "<MODE>")])
9898
9899 (define_insn_and_split "*<shift_insn><mode>3_doubleword"
9900   [(set (match_operand:DWI 0 "register_operand" "=r")
9901         (any_shiftrt:DWI (match_operand:DWI 1 "register_operand" "0")
9902                          (match_operand:QI 2 "nonmemory_operand" "<S>c")))
9903    (clobber (reg:CC FLAGS_REG))]
9904   ""
9905   "#"
9906   "(optimize && flag_peephole2) ? epilogue_completed : reload_completed"
9907   [(const_int 0)]
9908   "ix86_split_<shift_insn> (operands, NULL_RTX, <MODE>mode); DONE;"
9909   [(set_attr "type" "multi")])
9910
9911 ;; By default we don't ask for a scratch register, because when DWImode
9912 ;; values are manipulated, registers are already at a premium.  But if
9913 ;; we have one handy, we won't turn it away.
9914
9915 (define_peephole2
9916   [(match_scratch:DWIH 3 "r")
9917    (parallel [(set (match_operand:<DWI> 0 "register_operand")
9918                    (any_shiftrt:<DWI>
9919                      (match_operand:<DWI> 1 "register_operand")
9920                      (match_operand:QI 2 "nonmemory_operand")))
9921               (clobber (reg:CC FLAGS_REG))])
9922    (match_dup 3)]
9923   "TARGET_CMOVE"
9924   [(const_int 0)]
9925   "ix86_split_<shift_insn> (operands, operands[3], <DWI>mode); DONE;")
9926
9927 (define_insn "x86_64_shrd"
9928   [(set (match_operand:DI 0 "nonimmediate_operand" "+r*m")
9929         (ior:DI (lshiftrt:DI (match_dup 0)
9930                   (match_operand:QI 2 "nonmemory_operand" "Jc"))
9931                 (ashift:DI (match_operand:DI 1 "register_operand" "r")
9932                   (minus:QI (const_int 64) (match_dup 2)))))
9933    (clobber (reg:CC FLAGS_REG))]
9934   "TARGET_64BIT"
9935   "shrd{q}\t{%s2%1, %0|%0, %1, %2}"
9936   [(set_attr "type" "ishift")
9937    (set_attr "prefix_0f" "1")
9938    (set_attr "mode" "DI")
9939    (set_attr "athlon_decode" "vector")
9940    (set_attr "amdfam10_decode" "vector")
9941    (set_attr "bdver1_decode" "vector")])
9942
9943 (define_insn "x86_shrd"
9944   [(set (match_operand:SI 0 "nonimmediate_operand" "+r*m")
9945         (ior:SI (lshiftrt:SI (match_dup 0)
9946                   (match_operand:QI 2 "nonmemory_operand" "Ic"))
9947                 (ashift:SI (match_operand:SI 1 "register_operand" "r")
9948                   (minus:QI (const_int 32) (match_dup 2)))))
9949    (clobber (reg:CC FLAGS_REG))]
9950   ""
9951   "shrd{l}\t{%s2%1, %0|%0, %1, %2}"
9952   [(set_attr "type" "ishift")
9953    (set_attr "prefix_0f" "1")
9954    (set_attr "mode" "SI")
9955    (set_attr "pent_pair" "np")
9956    (set_attr "athlon_decode" "vector")
9957    (set_attr "amdfam10_decode" "vector")
9958    (set_attr "bdver1_decode" "vector")])
9959
9960 (define_insn "ashrdi3_cvt"
9961   [(set (match_operand:DI 0 "nonimmediate_operand" "=*d,rm")
9962         (ashiftrt:DI (match_operand:DI 1 "nonimmediate_operand" "*a,0")
9963                      (match_operand:QI 2 "const_int_operand")))
9964    (clobber (reg:CC FLAGS_REG))]
9965   "TARGET_64BIT && INTVAL (operands[2]) == 63
9966    && (TARGET_USE_CLTD || optimize_function_for_size_p (cfun))
9967    && ix86_binary_operator_ok (ASHIFTRT, DImode, operands)"
9968   "@
9969    {cqto|cqo}
9970    sar{q}\t{%2, %0|%0, %2}"
9971   [(set_attr "type" "imovx,ishift")
9972    (set_attr "prefix_0f" "0,*")
9973    (set_attr "length_immediate" "0,*")
9974    (set_attr "modrm" "0,1")
9975    (set_attr "mode" "DI")])
9976
9977 (define_insn "ashrsi3_cvt"
9978   [(set (match_operand:SI 0 "nonimmediate_operand" "=*d,rm")
9979         (ashiftrt:SI (match_operand:SI 1 "nonimmediate_operand" "*a,0")
9980                      (match_operand:QI 2 "const_int_operand")))
9981    (clobber (reg:CC FLAGS_REG))]
9982   "INTVAL (operands[2]) == 31
9983    && (TARGET_USE_CLTD || optimize_function_for_size_p (cfun))
9984    && ix86_binary_operator_ok (ASHIFTRT, SImode, operands)"
9985   "@
9986    {cltd|cdq}
9987    sar{l}\t{%2, %0|%0, %2}"
9988   [(set_attr "type" "imovx,ishift")
9989    (set_attr "prefix_0f" "0,*")
9990    (set_attr "length_immediate" "0,*")
9991    (set_attr "modrm" "0,1")
9992    (set_attr "mode" "SI")])
9993
9994 (define_insn "*ashrsi3_cvt_zext"
9995   [(set (match_operand:DI 0 "register_operand" "=*d,r")
9996         (zero_extend:DI
9997           (ashiftrt:SI (match_operand:SI 1 "register_operand" "*a,0")
9998                        (match_operand:QI 2 "const_int_operand"))))
9999    (clobber (reg:CC FLAGS_REG))]
10000   "TARGET_64BIT && INTVAL (operands[2]) == 31
10001    && (TARGET_USE_CLTD || optimize_function_for_size_p (cfun))
10002    && ix86_binary_operator_ok (ASHIFTRT, SImode, operands)"
10003   "@
10004    {cltd|cdq}
10005    sar{l}\t{%2, %k0|%k0, %2}"
10006   [(set_attr "type" "imovx,ishift")
10007    (set_attr "prefix_0f" "0,*")
10008    (set_attr "length_immediate" "0,*")
10009    (set_attr "modrm" "0,1")
10010    (set_attr "mode" "SI")])
10011
10012 (define_expand "x86_shift<mode>_adj_3"
10013   [(use (match_operand:SWI48 0 "register_operand"))
10014    (use (match_operand:SWI48 1 "register_operand"))
10015    (use (match_operand:QI 2 "register_operand"))]
10016   ""
10017 {
10018   rtx_code_label *label = gen_label_rtx ();
10019   rtx tmp;
10020
10021   emit_insn (gen_testqi_ccz_1 (operands[2],
10022                                GEN_INT (GET_MODE_BITSIZE (<MODE>mode))));
10023
10024   tmp = gen_rtx_REG (CCZmode, FLAGS_REG);
10025   tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
10026   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10027                               gen_rtx_LABEL_REF (VOIDmode, label),
10028                               pc_rtx);
10029   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10030   JUMP_LABEL (tmp) = label;
10031
10032   emit_move_insn (operands[0], operands[1]);
10033   emit_insn (gen_ashr<mode>3_cvt (operands[1], operands[1],
10034                                   GEN_INT (GET_MODE_BITSIZE (<MODE>mode)-1)));
10035   emit_label (label);
10036   LABEL_NUSES (label) = 1;
10037
10038   DONE;
10039 })
10040
10041 (define_insn "*bmi2_<shift_insn><mode>3_1"
10042   [(set (match_operand:SWI48 0 "register_operand" "=r")
10043         (any_shiftrt:SWI48 (match_operand:SWI48 1 "nonimmediate_operand" "rm")
10044                            (match_operand:SWI48 2 "register_operand" "r")))]
10045   "TARGET_BMI2"
10046   "<shift>x\t{%2, %1, %0|%0, %1, %2}"
10047   [(set_attr "type" "ishiftx")
10048    (set_attr "mode" "<MODE>")])
10049
10050 (define_insn "*<shift_insn><mode>3_1"
10051   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm,r")
10052         (any_shiftrt:SWI48
10053           (match_operand:SWI48 1 "nonimmediate_operand" "0,rm")
10054           (match_operand:QI 2 "nonmemory_operand" "c<S>,r")))
10055    (clobber (reg:CC FLAGS_REG))]
10056   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
10057 {
10058   switch (get_attr_type (insn))
10059     {
10060     case TYPE_ISHIFTX:
10061       return "#";
10062
10063     default:
10064       if (operands[2] == const1_rtx
10065           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10066         return "<shift>{<imodesuffix>}\t%0";
10067       else
10068         return "<shift>{<imodesuffix>}\t{%2, %0|%0, %2}";
10069     }
10070 }
10071   [(set_attr "isa" "*,bmi2")
10072    (set_attr "type" "ishift,ishiftx")
10073    (set (attr "length_immediate")
10074      (if_then_else
10075        (and (match_operand 2 "const1_operand")
10076             (ior (match_test "TARGET_SHIFT1")
10077                  (match_test "optimize_function_for_size_p (cfun)")))
10078        (const_string "0")
10079        (const_string "*")))
10080    (set_attr "mode" "<MODE>")])
10081
10082 ;; Convert shift to the shiftx pattern to avoid flags dependency.
10083 (define_split
10084   [(set (match_operand:SWI48 0 "register_operand")
10085         (any_shiftrt:SWI48 (match_operand:SWI48 1 "nonimmediate_operand")
10086                            (match_operand:QI 2 "register_operand")))
10087    (clobber (reg:CC FLAGS_REG))]
10088   "TARGET_BMI2 && reload_completed"
10089   [(set (match_dup 0)
10090         (any_shiftrt:SWI48 (match_dup 1) (match_dup 2)))]
10091   "operands[2] = gen_lowpart (<MODE>mode, operands[2]);")
10092
10093 (define_insn "*bmi2_<shift_insn>si3_1_zext"
10094   [(set (match_operand:DI 0 "register_operand" "=r")
10095         (zero_extend:DI
10096           (any_shiftrt:SI (match_operand:SI 1 "nonimmediate_operand" "rm")
10097                           (match_operand:SI 2 "register_operand" "r"))))]
10098   "TARGET_64BIT && TARGET_BMI2"
10099   "<shift>x\t{%2, %1, %k0|%k0, %1, %2}"
10100   [(set_attr "type" "ishiftx")
10101    (set_attr "mode" "SI")])
10102
10103 (define_insn "*<shift_insn>si3_1_zext"
10104   [(set (match_operand:DI 0 "register_operand" "=r,r")
10105         (zero_extend:DI
10106           (any_shiftrt:SI (match_operand:SI 1 "nonimmediate_operand" "0,rm")
10107                           (match_operand:QI 2 "nonmemory_operand" "cI,r"))))
10108    (clobber (reg:CC FLAGS_REG))]
10109   "TARGET_64BIT && ix86_binary_operator_ok (<CODE>, SImode, operands)"
10110 {
10111   switch (get_attr_type (insn))
10112     {
10113     case TYPE_ISHIFTX:
10114       return "#";
10115
10116     default:
10117       if (operands[2] == const1_rtx
10118           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10119         return "<shift>{l}\t%k0";
10120       else
10121         return "<shift>{l}\t{%2, %k0|%k0, %2}";
10122     }
10123 }
10124   [(set_attr "isa" "*,bmi2")
10125    (set_attr "type" "ishift,ishiftx")
10126    (set (attr "length_immediate")
10127      (if_then_else
10128        (and (match_operand 2 "const1_operand")
10129             (ior (match_test "TARGET_SHIFT1")
10130                  (match_test "optimize_function_for_size_p (cfun)")))
10131        (const_string "0")
10132        (const_string "*")))
10133    (set_attr "mode" "SI")])
10134
10135 ;; Convert shift to the shiftx pattern to avoid flags dependency.
10136 (define_split
10137   [(set (match_operand:DI 0 "register_operand")
10138         (zero_extend:DI
10139           (any_shiftrt:SI (match_operand:SI 1 "nonimmediate_operand")
10140                           (match_operand:QI 2 "register_operand"))))
10141    (clobber (reg:CC FLAGS_REG))]
10142   "TARGET_64BIT && TARGET_BMI2 && reload_completed"
10143   [(set (match_dup 0)
10144         (zero_extend:DI (any_shiftrt:SI (match_dup 1) (match_dup 2))))]
10145   "operands[2] = gen_lowpart (SImode, operands[2]);")
10146
10147 (define_insn "*<shift_insn><mode>3_1"
10148   [(set (match_operand:SWI12 0 "nonimmediate_operand" "=<r>m")
10149         (any_shiftrt:SWI12
10150           (match_operand:SWI12 1 "nonimmediate_operand" "0")
10151           (match_operand:QI 2 "nonmemory_operand" "c<S>")))
10152    (clobber (reg:CC FLAGS_REG))]
10153   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
10154 {
10155   if (operands[2] == const1_rtx
10156       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10157     return "<shift>{<imodesuffix>}\t%0";
10158   else
10159     return "<shift>{<imodesuffix>}\t{%2, %0|%0, %2}";
10160 }
10161   [(set_attr "type" "ishift")
10162    (set (attr "length_immediate")
10163      (if_then_else
10164        (and (match_operand 2 "const1_operand")
10165             (ior (match_test "TARGET_SHIFT1")
10166                  (match_test "optimize_function_for_size_p (cfun)")))
10167        (const_string "0")
10168        (const_string "*")))
10169    (set_attr "mode" "<MODE>")])
10170
10171 (define_insn "*<shift_insn>qi3_1_slp"
10172   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm"))
10173         (any_shiftrt:QI (match_dup 0)
10174                         (match_operand:QI 1 "nonmemory_operand" "cI")))
10175    (clobber (reg:CC FLAGS_REG))]
10176   "(optimize_function_for_size_p (cfun)
10177     || !TARGET_PARTIAL_REG_STALL
10178     || (operands[1] == const1_rtx
10179         && TARGET_SHIFT1))"
10180 {
10181   if (operands[1] == const1_rtx
10182       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10183     return "<shift>{b}\t%0";
10184   else
10185     return "<shift>{b}\t{%1, %0|%0, %1}";
10186 }
10187   [(set_attr "type" "ishift1")
10188    (set (attr "length_immediate")
10189      (if_then_else
10190        (and (match_operand 1 "const1_operand")
10191             (ior (match_test "TARGET_SHIFT1")
10192                  (match_test "optimize_function_for_size_p (cfun)")))
10193        (const_string "0")
10194        (const_string "*")))
10195    (set_attr "mode" "QI")])
10196
10197 ;; This pattern can't accept a variable shift count, since shifts by
10198 ;; zero don't affect the flags.  We assume that shifts by constant
10199 ;; zero are optimized away.
10200 (define_insn "*<shift_insn><mode>3_cmp"
10201   [(set (reg FLAGS_REG)
10202         (compare
10203           (any_shiftrt:SWI
10204             (match_operand:SWI 1 "nonimmediate_operand" "0")
10205             (match_operand:QI 2 "<shift_immediate_operand>" "<S>"))
10206           (const_int 0)))
10207    (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m")
10208         (any_shiftrt:SWI (match_dup 1) (match_dup 2)))]
10209   "(optimize_function_for_size_p (cfun)
10210     || !TARGET_PARTIAL_FLAG_REG_STALL
10211     || (operands[2] == const1_rtx
10212         && TARGET_SHIFT1))
10213    && ix86_match_ccmode (insn, CCGOCmode)
10214    && ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
10215 {
10216   if (operands[2] == const1_rtx
10217       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10218     return "<shift>{<imodesuffix>}\t%0";
10219   else
10220     return "<shift>{<imodesuffix>}\t{%2, %0|%0, %2}";
10221 }
10222   [(set_attr "type" "ishift")
10223    (set (attr "length_immediate")
10224      (if_then_else
10225        (and (match_operand 2 "const1_operand")
10226             (ior (match_test "TARGET_SHIFT1")
10227                  (match_test "optimize_function_for_size_p (cfun)")))
10228        (const_string "0")
10229        (const_string "*")))
10230    (set_attr "mode" "<MODE>")])
10231
10232 (define_insn "*<shift_insn>si3_cmp_zext"
10233   [(set (reg FLAGS_REG)
10234         (compare
10235           (any_shiftrt:SI (match_operand:SI 1 "register_operand" "0")
10236                           (match_operand:QI 2 "const_1_to_31_operand" "I"))
10237           (const_int 0)))
10238    (set (match_operand:DI 0 "register_operand" "=r")
10239         (zero_extend:DI (any_shiftrt:SI (match_dup 1) (match_dup 2))))]
10240   "TARGET_64BIT
10241    && (optimize_function_for_size_p (cfun)
10242        || !TARGET_PARTIAL_FLAG_REG_STALL
10243        || (operands[2] == const1_rtx
10244            && TARGET_SHIFT1))
10245    && ix86_match_ccmode (insn, CCGOCmode)
10246    && ix86_binary_operator_ok (<CODE>, SImode, operands)"
10247 {
10248   if (operands[2] == const1_rtx
10249       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10250     return "<shift>{l}\t%k0";
10251   else
10252     return "<shift>{l}\t{%2, %k0|%k0, %2}";
10253 }
10254   [(set_attr "type" "ishift")
10255    (set (attr "length_immediate")
10256      (if_then_else
10257        (and (match_operand 2 "const1_operand")
10258             (ior (match_test "TARGET_SHIFT1")
10259                  (match_test "optimize_function_for_size_p (cfun)")))
10260        (const_string "0")
10261        (const_string "*")))
10262    (set_attr "mode" "SI")])
10263
10264 (define_insn "*<shift_insn><mode>3_cconly"
10265   [(set (reg FLAGS_REG)
10266         (compare
10267           (any_shiftrt:SWI
10268             (match_operand:SWI 1 "register_operand" "0")
10269             (match_operand:QI 2 "<shift_immediate_operand>" "<S>"))
10270           (const_int 0)))
10271    (clobber (match_scratch:SWI 0 "=<r>"))]
10272   "(optimize_function_for_size_p (cfun)
10273     || !TARGET_PARTIAL_FLAG_REG_STALL
10274     || (operands[2] == const1_rtx
10275         && TARGET_SHIFT1))
10276    && ix86_match_ccmode (insn, CCGOCmode)"
10277 {
10278   if (operands[2] == const1_rtx
10279       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10280     return "<shift>{<imodesuffix>}\t%0";
10281   else
10282     return "<shift>{<imodesuffix>}\t{%2, %0|%0, %2}";
10283 }
10284   [(set_attr "type" "ishift")
10285    (set (attr "length_immediate")
10286      (if_then_else
10287        (and (match_operand 2 "const1_operand")
10288             (ior (match_test "TARGET_SHIFT1")
10289                  (match_test "optimize_function_for_size_p (cfun)")))
10290        (const_string "0")
10291        (const_string "*")))
10292    (set_attr "mode" "<MODE>")])
10293 \f
10294 ;; Rotate instructions
10295
10296 (define_expand "<rotate_insn>ti3"
10297   [(set (match_operand:TI 0 "register_operand")
10298         (any_rotate:TI (match_operand:TI 1 "register_operand")
10299                        (match_operand:QI 2 "nonmemory_operand")))]
10300   "TARGET_64BIT"
10301 {
10302   if (const_1_to_63_operand (operands[2], VOIDmode))
10303     emit_insn (gen_ix86_<rotate_insn>ti3_doubleword
10304                 (operands[0], operands[1], operands[2]));
10305   else
10306     FAIL;
10307
10308   DONE;
10309 })
10310
10311 (define_expand "<rotate_insn>di3"
10312   [(set (match_operand:DI 0 "shiftdi_operand")
10313         (any_rotate:DI (match_operand:DI 1 "shiftdi_operand")
10314                        (match_operand:QI 2 "nonmemory_operand")))]
10315  ""
10316 {
10317   if (TARGET_64BIT)
10318     ix86_expand_binary_operator (<CODE>, DImode, operands);
10319   else if (const_1_to_31_operand (operands[2], VOIDmode))
10320     emit_insn (gen_ix86_<rotate_insn>di3_doubleword
10321                 (operands[0], operands[1], operands[2]));
10322   else
10323     FAIL;
10324
10325   DONE;
10326 })
10327
10328 (define_expand "<rotate_insn><mode>3"
10329   [(set (match_operand:SWIM124 0 "nonimmediate_operand")
10330         (any_rotate:SWIM124 (match_operand:SWIM124 1 "nonimmediate_operand")
10331                             (match_operand:QI 2 "nonmemory_operand")))]
10332   ""
10333   "ix86_expand_binary_operator (<CODE>, <MODE>mode, operands); DONE;")
10334
10335 ;; Avoid useless masking of count operand.
10336 (define_insn "*<rotate_insn><mode>3_mask"
10337   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm")
10338         (any_rotate:SWI48
10339           (match_operand:SWI48 1 "nonimmediate_operand" "0")
10340           (subreg:QI
10341             (and:SI
10342               (match_operand:SI 2 "register_operand" "c")
10343               (match_operand:SI 3 "const_int_operand" "n")) 0)))
10344    (clobber (reg:CC FLAGS_REG))]
10345   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)
10346    && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode)-1))
10347       == GET_MODE_BITSIZE (<MODE>mode)-1"
10348 {
10349   return "<rotate>{<imodesuffix>}\t{%b2, %0|%0, %b2}";
10350 }
10351   [(set_attr "type" "rotate")
10352    (set_attr "mode" "<MODE>")])
10353
10354 ;; Implement rotation using two double-precision
10355 ;; shift instructions and a scratch register.
10356
10357 (define_insn_and_split "ix86_rotl<dwi>3_doubleword"
10358  [(set (match_operand:<DWI> 0 "register_operand" "=r")
10359        (rotate:<DWI> (match_operand:<DWI> 1 "register_operand" "0")
10360                      (match_operand:QI 2 "<shift_immediate_operand>" "<S>")))
10361   (clobber (reg:CC FLAGS_REG))
10362   (clobber (match_scratch:DWIH 3 "=&r"))]
10363  ""
10364  "#"
10365  "reload_completed"
10366  [(set (match_dup 3) (match_dup 4))
10367   (parallel
10368    [(set (match_dup 4)
10369          (ior:DWIH (ashift:DWIH (match_dup 4) (match_dup 2))
10370                    (lshiftrt:DWIH (match_dup 5)
10371                                   (minus:QI (match_dup 6) (match_dup 2)))))
10372     (clobber (reg:CC FLAGS_REG))])
10373   (parallel
10374    [(set (match_dup 5)
10375          (ior:DWIH (ashift:DWIH (match_dup 5) (match_dup 2))
10376                    (lshiftrt:DWIH (match_dup 3)
10377                                   (minus:QI (match_dup 6) (match_dup 2)))))
10378     (clobber (reg:CC FLAGS_REG))])]
10379 {
10380   operands[6] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode));
10381
10382   split_double_mode (<DWI>mode, &operands[0], 1, &operands[4], &operands[5]);
10383 })
10384
10385 (define_insn_and_split "ix86_rotr<dwi>3_doubleword"
10386  [(set (match_operand:<DWI> 0 "register_operand" "=r")
10387        (rotatert:<DWI> (match_operand:<DWI> 1 "register_operand" "0")
10388                        (match_operand:QI 2 "<shift_immediate_operand>" "<S>")))
10389   (clobber (reg:CC FLAGS_REG))
10390   (clobber (match_scratch:DWIH 3 "=&r"))]
10391  ""
10392  "#"
10393  "reload_completed"
10394  [(set (match_dup 3) (match_dup 4))
10395   (parallel
10396    [(set (match_dup 4)
10397          (ior:DWIH (lshiftrt:DWIH (match_dup 4) (match_dup 2))
10398                    (ashift:DWIH (match_dup 5)
10399                                 (minus:QI (match_dup 6) (match_dup 2)))))
10400     (clobber (reg:CC FLAGS_REG))])
10401   (parallel
10402    [(set (match_dup 5)
10403          (ior:DWIH (lshiftrt:DWIH (match_dup 5) (match_dup 2))
10404                    (ashift:DWIH (match_dup 3)
10405                                 (minus:QI (match_dup 6) (match_dup 2)))))
10406     (clobber (reg:CC FLAGS_REG))])]
10407 {
10408   operands[6] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode));
10409
10410   split_double_mode (<DWI>mode, &operands[0], 1, &operands[4], &operands[5]);
10411 })
10412
10413 (define_insn "*bmi2_rorx<mode>3_1"
10414   [(set (match_operand:SWI48 0 "register_operand" "=r")
10415         (rotatert:SWI48 (match_operand:SWI48 1 "nonimmediate_operand" "rm")
10416                         (match_operand:QI 2 "immediate_operand" "<S>")))]
10417   "TARGET_BMI2"
10418   "rorx\t{%2, %1, %0|%0, %1, %2}"
10419   [(set_attr "type" "rotatex")
10420    (set_attr "mode" "<MODE>")])
10421
10422 (define_insn "*<rotate_insn><mode>3_1"
10423   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm,r")
10424         (any_rotate:SWI48
10425           (match_operand:SWI48 1 "nonimmediate_operand" "0,rm")
10426           (match_operand:QI 2 "nonmemory_operand" "c<S>,<S>")))
10427    (clobber (reg:CC FLAGS_REG))]
10428   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
10429 {
10430   switch (get_attr_type (insn))
10431     {
10432     case TYPE_ROTATEX:
10433       return "#";
10434
10435     default:
10436       if (operands[2] == const1_rtx
10437           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10438         return "<rotate>{<imodesuffix>}\t%0";
10439       else
10440         return "<rotate>{<imodesuffix>}\t{%2, %0|%0, %2}";
10441     }
10442 }
10443   [(set_attr "isa" "*,bmi2")
10444    (set_attr "type" "rotate,rotatex")
10445    (set (attr "length_immediate")
10446      (if_then_else
10447        (and (eq_attr "type" "rotate")
10448             (and (match_operand 2 "const1_operand")
10449                  (ior (match_test "TARGET_SHIFT1")
10450                       (match_test "optimize_function_for_size_p (cfun)"))))
10451        (const_string "0")
10452        (const_string "*")))
10453    (set_attr "mode" "<MODE>")])
10454
10455 ;; Convert rotate to the rotatex pattern to avoid flags dependency.
10456 (define_split
10457   [(set (match_operand:SWI48 0 "register_operand")
10458         (rotate:SWI48 (match_operand:SWI48 1 "nonimmediate_operand")
10459                       (match_operand:QI 2 "immediate_operand")))
10460    (clobber (reg:CC FLAGS_REG))]
10461   "TARGET_BMI2 && reload_completed"
10462   [(set (match_dup 0)
10463         (rotatert:SWI48 (match_dup 1) (match_dup 2)))]
10464 {
10465   operands[2]
10466     = GEN_INT (GET_MODE_BITSIZE (<MODE>mode) - INTVAL (operands[2]));
10467 })
10468
10469 (define_split
10470   [(set (match_operand:SWI48 0 "register_operand")
10471         (rotatert:SWI48 (match_operand:SWI48 1 "nonimmediate_operand")
10472                         (match_operand:QI 2 "immediate_operand")))
10473    (clobber (reg:CC FLAGS_REG))]
10474   "TARGET_BMI2 && reload_completed"
10475   [(set (match_dup 0)
10476         (rotatert:SWI48 (match_dup 1) (match_dup 2)))])
10477
10478 (define_insn "*bmi2_rorxsi3_1_zext"
10479   [(set (match_operand:DI 0 "register_operand" "=r")
10480         (zero_extend:DI
10481           (rotatert:SI (match_operand:SI 1 "nonimmediate_operand" "rm")
10482                        (match_operand:QI 2 "immediate_operand" "I"))))]
10483   "TARGET_64BIT && TARGET_BMI2"
10484   "rorx\t{%2, %1, %k0|%k0, %1, %2}"
10485   [(set_attr "type" "rotatex")
10486    (set_attr "mode" "SI")])
10487
10488 (define_insn "*<rotate_insn>si3_1_zext"
10489   [(set (match_operand:DI 0 "register_operand" "=r,r")
10490         (zero_extend:DI
10491           (any_rotate:SI (match_operand:SI 1 "nonimmediate_operand" "0,rm")
10492                          (match_operand:QI 2 "nonmemory_operand" "cI,I"))))
10493    (clobber (reg:CC FLAGS_REG))]
10494   "TARGET_64BIT && ix86_binary_operator_ok (<CODE>, SImode, operands)"
10495 {
10496   switch (get_attr_type (insn))
10497     {
10498     case TYPE_ROTATEX:
10499       return "#";
10500
10501     default:
10502       if (operands[2] == const1_rtx
10503           && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10504         return "<rotate>{l}\t%k0";
10505       else
10506         return "<rotate>{l}\t{%2, %k0|%k0, %2}";
10507     }
10508 }
10509   [(set_attr "isa" "*,bmi2")
10510    (set_attr "type" "rotate,rotatex")
10511    (set (attr "length_immediate")
10512      (if_then_else
10513        (and (eq_attr "type" "rotate")
10514             (and (match_operand 2 "const1_operand")
10515                  (ior (match_test "TARGET_SHIFT1")
10516                       (match_test "optimize_function_for_size_p (cfun)"))))
10517        (const_string "0")
10518        (const_string "*")))
10519    (set_attr "mode" "SI")])
10520
10521 ;; Convert rotate to the rotatex pattern to avoid flags dependency.
10522 (define_split
10523   [(set (match_operand:DI 0 "register_operand")
10524         (zero_extend:DI
10525           (rotate:SI (match_operand:SI 1 "nonimmediate_operand")
10526                      (match_operand:QI 2 "immediate_operand"))))
10527    (clobber (reg:CC FLAGS_REG))]
10528   "TARGET_64BIT && TARGET_BMI2 && reload_completed"
10529   [(set (match_dup 0)
10530         (zero_extend:DI (rotatert:SI (match_dup 1) (match_dup 2))))]
10531 {
10532   operands[2]
10533     = GEN_INT (GET_MODE_BITSIZE (SImode) - INTVAL (operands[2]));
10534 })
10535
10536 (define_split
10537   [(set (match_operand:DI 0 "register_operand")
10538         (zero_extend:DI
10539           (rotatert:SI (match_operand:SI 1 "nonimmediate_operand")
10540                        (match_operand:QI 2 "immediate_operand"))))
10541    (clobber (reg:CC FLAGS_REG))]
10542   "TARGET_64BIT && TARGET_BMI2 && reload_completed"
10543   [(set (match_dup 0)
10544         (zero_extend:DI (rotatert:SI (match_dup 1) (match_dup 2))))])
10545
10546 (define_insn "*<rotate_insn><mode>3_1"
10547   [(set (match_operand:SWI12 0 "nonimmediate_operand" "=<r>m")
10548         (any_rotate:SWI12 (match_operand:SWI12 1 "nonimmediate_operand" "0")
10549                           (match_operand:QI 2 "nonmemory_operand" "c<S>")))
10550    (clobber (reg:CC FLAGS_REG))]
10551   "ix86_binary_operator_ok (<CODE>, <MODE>mode, operands)"
10552 {
10553   if (operands[2] == const1_rtx
10554       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10555     return "<rotate>{<imodesuffix>}\t%0";
10556   else
10557     return "<rotate>{<imodesuffix>}\t{%2, %0|%0, %2}";
10558 }
10559   [(set_attr "type" "rotate")
10560    (set (attr "length_immediate")
10561      (if_then_else
10562        (and (match_operand 2 "const1_operand")
10563             (ior (match_test "TARGET_SHIFT1")
10564                  (match_test "optimize_function_for_size_p (cfun)")))
10565        (const_string "0")
10566        (const_string "*")))
10567    (set_attr "mode" "<MODE>")])
10568
10569 (define_insn "*<rotate_insn>qi3_1_slp"
10570   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm"))
10571         (any_rotate:QI (match_dup 0)
10572                        (match_operand:QI 1 "nonmemory_operand" "cI")))
10573    (clobber (reg:CC FLAGS_REG))]
10574   "(optimize_function_for_size_p (cfun)
10575     || !TARGET_PARTIAL_REG_STALL
10576     || (operands[1] == const1_rtx
10577         && TARGET_SHIFT1))"
10578 {
10579   if (operands[1] == const1_rtx
10580       && (TARGET_SHIFT1 || optimize_function_for_size_p (cfun)))
10581     return "<rotate>{b}\t%0";
10582   else
10583     return "<rotate>{b}\t{%1, %0|%0, %1}";
10584 }
10585   [(set_attr "type" "rotate1")
10586    (set (attr "length_immediate")
10587      (if_then_else
10588        (and (match_operand 1 "const1_operand")
10589             (ior (match_test "TARGET_SHIFT1")
10590                  (match_test "optimize_function_for_size_p (cfun)")))
10591        (const_string "0")
10592        (const_string "*")))
10593    (set_attr "mode" "QI")])
10594
10595 (define_split
10596  [(set (match_operand:HI 0 "register_operand")
10597        (any_rotate:HI (match_dup 0) (const_int 8)))
10598   (clobber (reg:CC FLAGS_REG))]
10599  "reload_completed
10600   && (TARGET_USE_XCHGB || optimize_function_for_size_p (cfun))"
10601  [(parallel [(set (strict_low_part (match_dup 0))
10602                   (bswap:HI (match_dup 0)))
10603              (clobber (reg:CC FLAGS_REG))])])
10604 \f
10605 ;; Bit set / bit test instructions
10606
10607 (define_expand "extv"
10608   [(set (match_operand:SI 0 "register_operand")
10609         (sign_extract:SI (match_operand:SI 1 "register_operand")
10610                          (match_operand:SI 2 "const8_operand")
10611                          (match_operand:SI 3 "const8_operand")))]
10612   ""
10613 {
10614   /* Handle extractions from %ah et al.  */
10615   if (INTVAL (operands[2]) != 8 || INTVAL (operands[3]) != 8)
10616     FAIL;
10617
10618   /* From mips.md: extract_bit_field doesn't verify that our source
10619      matches the predicate, so check it again here.  */
10620   if (! ext_register_operand (operands[1], VOIDmode))
10621     FAIL;
10622 })
10623
10624 (define_expand "extzv"
10625   [(set (match_operand:SI 0 "register_operand")
10626         (zero_extract:SI (match_operand 1 "ext_register_operand")
10627                          (match_operand:SI 2 "const8_operand")
10628                          (match_operand:SI 3 "const8_operand")))]
10629   ""
10630 {
10631   /* Handle extractions from %ah et al.  */
10632   if (INTVAL (operands[2]) != 8 || INTVAL (operands[3]) != 8)
10633     FAIL;
10634
10635   /* From mips.md: extract_bit_field doesn't verify that our source
10636      matches the predicate, so check it again here.  */
10637   if (! ext_register_operand (operands[1], VOIDmode))
10638     FAIL;
10639 })
10640
10641 (define_expand "insv"
10642   [(set (zero_extract (match_operand 0 "register_operand")
10643                       (match_operand 1 "const_int_operand")
10644                       (match_operand 2 "const_int_operand"))
10645         (match_operand 3 "register_operand"))]
10646   ""
10647 {
10648   rtx (*gen_mov_insv_1) (rtx, rtx);
10649
10650   if (ix86_expand_pinsr (operands))
10651     DONE;
10652
10653   /* Handle insertions to %ah et al.  */
10654   if (INTVAL (operands[1]) != 8 || INTVAL (operands[2]) != 8)
10655     FAIL;
10656
10657   /* From mips.md: insert_bit_field doesn't verify that our source
10658      matches the predicate, so check it again here.  */
10659   if (! ext_register_operand (operands[0], VOIDmode))
10660     FAIL;
10661
10662   gen_mov_insv_1 = (TARGET_64BIT
10663                     ? gen_movdi_insv_1 : gen_movsi_insv_1);
10664
10665   emit_insn (gen_mov_insv_1 (operands[0], operands[3]));
10666   DONE;
10667 })
10668
10669 ;; %%% bts, btr, btc, bt.
10670 ;; In general these instructions are *slow* when applied to memory,
10671 ;; since they enforce atomic operation.  When applied to registers,
10672 ;; it depends on the cpu implementation.  They're never faster than
10673 ;; the corresponding and/ior/xor operations, so with 32-bit there's
10674 ;; no point.  But in 64-bit, we can't hold the relevant immediates
10675 ;; within the instruction itself, so operating on bits in the high
10676 ;; 32-bits of a register becomes easier.
10677 ;;
10678 ;; These are slow on Nocona, but fast on Athlon64.  We do require the use
10679 ;; of btrq and btcq for corner cases of post-reload expansion of absdf and
10680 ;; negdf respectively, so they can never be disabled entirely.
10681
10682 (define_insn "*btsq"
10683   [(set (zero_extract:DI (match_operand:DI 0 "register_operand" "+r")
10684                          (const_int 1)
10685                          (match_operand:DI 1 "const_0_to_63_operand"))
10686         (const_int 1))
10687    (clobber (reg:CC FLAGS_REG))]
10688   "TARGET_64BIT && (TARGET_USE_BT || reload_completed)"
10689   "bts{q}\t{%1, %0|%0, %1}"
10690   [(set_attr "type" "alu1")
10691    (set_attr "prefix_0f" "1")
10692    (set_attr "mode" "DI")])
10693
10694 (define_insn "*btrq"
10695   [(set (zero_extract:DI (match_operand:DI 0 "register_operand" "+r")
10696                          (const_int 1)
10697                          (match_operand:DI 1 "const_0_to_63_operand"))
10698         (const_int 0))
10699    (clobber (reg:CC FLAGS_REG))]
10700   "TARGET_64BIT && (TARGET_USE_BT || reload_completed)"
10701   "btr{q}\t{%1, %0|%0, %1}"
10702   [(set_attr "type" "alu1")
10703    (set_attr "prefix_0f" "1")
10704    (set_attr "mode" "DI")])
10705
10706 (define_insn "*btcq"
10707   [(set (zero_extract:DI (match_operand:DI 0 "register_operand" "+r")
10708                          (const_int 1)
10709                          (match_operand:DI 1 "const_0_to_63_operand"))
10710         (not:DI (zero_extract:DI (match_dup 0) (const_int 1) (match_dup 1))))
10711    (clobber (reg:CC FLAGS_REG))]
10712   "TARGET_64BIT && (TARGET_USE_BT || reload_completed)"
10713   "btc{q}\t{%1, %0|%0, %1}"
10714   [(set_attr "type" "alu1")
10715    (set_attr "prefix_0f" "1")
10716    (set_attr "mode" "DI")])
10717
10718 ;; Allow Nocona to avoid these instructions if a register is available.
10719
10720 (define_peephole2
10721   [(match_scratch:DI 2 "r")
10722    (parallel [(set (zero_extract:DI
10723                      (match_operand:DI 0 "register_operand")
10724                      (const_int 1)
10725                      (match_operand:DI 1 "const_0_to_63_operand"))
10726                    (const_int 1))
10727               (clobber (reg:CC FLAGS_REG))])]
10728   "TARGET_64BIT && !TARGET_USE_BT"
10729   [(const_int 0)]
10730 {
10731   HOST_WIDE_INT i = INTVAL (operands[1]), hi, lo;
10732   rtx op1;
10733
10734   if (HOST_BITS_PER_WIDE_INT >= 64)
10735     lo = (HOST_WIDE_INT)1 << i, hi = 0;
10736   else if (i < HOST_BITS_PER_WIDE_INT)
10737     lo = (HOST_WIDE_INT)1 << i, hi = 0;
10738   else
10739     lo = 0, hi = (HOST_WIDE_INT)1 << (i - HOST_BITS_PER_WIDE_INT);
10740
10741   op1 = immed_double_const (lo, hi, DImode);
10742   if (i >= 31)
10743     {
10744       emit_move_insn (operands[2], op1);
10745       op1 = operands[2];
10746     }
10747
10748   emit_insn (gen_iordi3 (operands[0], operands[0], op1));
10749   DONE;
10750 })
10751
10752 (define_peephole2
10753   [(match_scratch:DI 2 "r")
10754    (parallel [(set (zero_extract:DI
10755                      (match_operand:DI 0 "register_operand")
10756                      (const_int 1)
10757                      (match_operand:DI 1 "const_0_to_63_operand"))
10758                    (const_int 0))
10759               (clobber (reg:CC FLAGS_REG))])]
10760   "TARGET_64BIT && !TARGET_USE_BT"
10761   [(const_int 0)]
10762 {
10763   HOST_WIDE_INT i = INTVAL (operands[1]), hi, lo;
10764   rtx op1;
10765
10766   if (HOST_BITS_PER_WIDE_INT >= 64)
10767     lo = (HOST_WIDE_INT)1 << i, hi = 0;
10768   else if (i < HOST_BITS_PER_WIDE_INT)
10769     lo = (HOST_WIDE_INT)1 << i, hi = 0;
10770   else
10771     lo = 0, hi = (HOST_WIDE_INT)1 << (i - HOST_BITS_PER_WIDE_INT);
10772
10773   op1 = immed_double_const (~lo, ~hi, DImode);
10774   if (i >= 32)
10775     {
10776       emit_move_insn (operands[2], op1);
10777       op1 = operands[2];
10778     }
10779
10780   emit_insn (gen_anddi3 (operands[0], operands[0], op1));
10781   DONE;
10782 })
10783
10784 (define_peephole2
10785   [(match_scratch:DI 2 "r")
10786    (parallel [(set (zero_extract:DI
10787                      (match_operand:DI 0 "register_operand")
10788                      (const_int 1)
10789                      (match_operand:DI 1 "const_0_to_63_operand"))
10790               (not:DI (zero_extract:DI
10791                         (match_dup 0) (const_int 1) (match_dup 1))))
10792               (clobber (reg:CC FLAGS_REG))])]
10793   "TARGET_64BIT && !TARGET_USE_BT"
10794   [(const_int 0)]
10795 {
10796   HOST_WIDE_INT i = INTVAL (operands[1]), hi, lo;
10797   rtx op1;
10798
10799   if (HOST_BITS_PER_WIDE_INT >= 64)
10800     lo = (HOST_WIDE_INT)1 << i, hi = 0;
10801   else if (i < HOST_BITS_PER_WIDE_INT)
10802     lo = (HOST_WIDE_INT)1 << i, hi = 0;
10803   else
10804     lo = 0, hi = (HOST_WIDE_INT)1 << (i - HOST_BITS_PER_WIDE_INT);
10805
10806   op1 = immed_double_const (lo, hi, DImode);
10807   if (i >= 31)
10808     {
10809       emit_move_insn (operands[2], op1);
10810       op1 = operands[2];
10811     }
10812
10813   emit_insn (gen_xordi3 (operands[0], operands[0], op1));
10814   DONE;
10815 })
10816
10817 (define_insn "*bt<mode>"
10818   [(set (reg:CCC FLAGS_REG)
10819         (compare:CCC
10820           (zero_extract:SWI48
10821             (match_operand:SWI48 0 "register_operand" "r")
10822             (const_int 1)
10823             (match_operand:SWI48 1 "x86_64_nonmemory_operand" "rN"))
10824           (const_int 0)))]
10825   "TARGET_USE_BT || optimize_function_for_size_p (cfun)"
10826   "bt{<imodesuffix>}\t{%1, %0|%0, %1}"
10827   [(set_attr "type" "alu1")
10828    (set_attr "prefix_0f" "1")
10829    (set_attr "mode" "<MODE>")])
10830 \f
10831 ;; Store-flag instructions.
10832
10833 ;; For all sCOND expanders, also expand the compare or test insn that
10834 ;; generates cc0.  Generate an equality comparison if `seq' or `sne'.
10835
10836 (define_insn_and_split "*setcc_di_1"
10837   [(set (match_operand:DI 0 "register_operand" "=q")
10838         (match_operator:DI 1 "ix86_comparison_operator"
10839           [(reg FLAGS_REG) (const_int 0)]))]
10840   "TARGET_64BIT && !TARGET_PARTIAL_REG_STALL"
10841   "#"
10842   "&& reload_completed"
10843   [(set (match_dup 2) (match_dup 1))
10844    (set (match_dup 0) (zero_extend:DI (match_dup 2)))]
10845 {
10846   PUT_MODE (operands[1], QImode);
10847   operands[2] = gen_lowpart (QImode, operands[0]);
10848 })
10849
10850 (define_insn_and_split "*setcc_si_1_and"
10851   [(set (match_operand:SI 0 "register_operand" "=q")
10852         (match_operator:SI 1 "ix86_comparison_operator"
10853           [(reg FLAGS_REG) (const_int 0)]))
10854    (clobber (reg:CC FLAGS_REG))]
10855   "!TARGET_PARTIAL_REG_STALL
10856    && TARGET_ZERO_EXTEND_WITH_AND && optimize_function_for_speed_p (cfun)"
10857   "#"
10858   "&& reload_completed"
10859   [(set (match_dup 2) (match_dup 1))
10860    (parallel [(set (match_dup 0) (zero_extend:SI (match_dup 2)))
10861               (clobber (reg:CC FLAGS_REG))])]
10862 {
10863   PUT_MODE (operands[1], QImode);
10864   operands[2] = gen_lowpart (QImode, operands[0]);
10865 })
10866
10867 (define_insn_and_split "*setcc_si_1_movzbl"
10868   [(set (match_operand:SI 0 "register_operand" "=q")
10869         (match_operator:SI 1 "ix86_comparison_operator"
10870           [(reg FLAGS_REG) (const_int 0)]))]
10871   "!TARGET_PARTIAL_REG_STALL
10872    && (!TARGET_ZERO_EXTEND_WITH_AND || optimize_function_for_size_p (cfun))"
10873   "#"
10874   "&& reload_completed"
10875   [(set (match_dup 2) (match_dup 1))
10876    (set (match_dup 0) (zero_extend:SI (match_dup 2)))]
10877 {
10878   PUT_MODE (operands[1], QImode);
10879   operands[2] = gen_lowpart (QImode, operands[0]);
10880 })
10881
10882 (define_insn "*setcc_qi"
10883   [(set (match_operand:QI 0 "nonimmediate_operand" "=qm")
10884         (match_operator:QI 1 "ix86_comparison_operator"
10885           [(reg FLAGS_REG) (const_int 0)]))]
10886   ""
10887   "set%C1\t%0"
10888   [(set_attr "type" "setcc")
10889    (set_attr "mode" "QI")])
10890
10891 (define_insn "*setcc_qi_slp"
10892   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand" "+qm"))
10893         (match_operator:QI 1 "ix86_comparison_operator"
10894           [(reg FLAGS_REG) (const_int 0)]))]
10895   ""
10896   "set%C1\t%0"
10897   [(set_attr "type" "setcc")
10898    (set_attr "mode" "QI")])
10899
10900 ;; In general it is not safe to assume too much about CCmode registers,
10901 ;; so simplify-rtx stops when it sees a second one.  Under certain
10902 ;; conditions this is safe on x86, so help combine not create
10903 ;;
10904 ;;      seta    %al
10905 ;;      testb   %al, %al
10906 ;;      sete    %al
10907
10908 (define_split
10909   [(set (match_operand:QI 0 "nonimmediate_operand")
10910         (ne:QI (match_operator 1 "ix86_comparison_operator"
10911                  [(reg FLAGS_REG) (const_int 0)])
10912             (const_int 0)))]
10913   ""
10914   [(set (match_dup 0) (match_dup 1))]
10915   "PUT_MODE (operands[1], QImode);")
10916
10917 (define_split
10918   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand"))
10919         (ne:QI (match_operator 1 "ix86_comparison_operator"
10920                  [(reg FLAGS_REG) (const_int 0)])
10921             (const_int 0)))]
10922   ""
10923   [(set (match_dup 0) (match_dup 1))]
10924   "PUT_MODE (operands[1], QImode);")
10925
10926 (define_split
10927   [(set (match_operand:QI 0 "nonimmediate_operand")
10928         (eq:QI (match_operator 1 "ix86_comparison_operator"
10929                  [(reg FLAGS_REG) (const_int 0)])
10930             (const_int 0)))]
10931   ""
10932   [(set (match_dup 0) (match_dup 1))]
10933 {
10934   rtx new_op1 = copy_rtx (operands[1]);
10935   operands[1] = new_op1;
10936   PUT_MODE (new_op1, QImode);
10937   PUT_CODE (new_op1, ix86_reverse_condition (GET_CODE (new_op1),
10938                                              GET_MODE (XEXP (new_op1, 0))));
10939
10940   /* Make sure that (a) the CCmode we have for the flags is strong
10941      enough for the reversed compare or (b) we have a valid FP compare.  */
10942   if (! ix86_comparison_operator (new_op1, VOIDmode))
10943     FAIL;
10944 })
10945
10946 (define_split
10947   [(set (strict_low_part (match_operand:QI 0 "nonimmediate_operand"))
10948         (eq:QI (match_operator 1 "ix86_comparison_operator"
10949                  [(reg FLAGS_REG) (const_int 0)])
10950             (const_int 0)))]
10951   ""
10952   [(set (match_dup 0) (match_dup 1))]
10953 {
10954   rtx new_op1 = copy_rtx (operands[1]);
10955   operands[1] = new_op1;
10956   PUT_MODE (new_op1, QImode);
10957   PUT_CODE (new_op1, ix86_reverse_condition (GET_CODE (new_op1),
10958                                              GET_MODE (XEXP (new_op1, 0))));
10959
10960   /* Make sure that (a) the CCmode we have for the flags is strong
10961      enough for the reversed compare or (b) we have a valid FP compare.  */
10962   if (! ix86_comparison_operator (new_op1, VOIDmode))
10963     FAIL;
10964 })
10965
10966 ;; The SSE store flag instructions saves 0 or 0xffffffff to the result.
10967 ;; subsequent logical operations are used to imitate conditional moves.
10968 ;; 0xffffffff is NaN, but not in normalized form, so we can't represent
10969 ;; it directly.
10970
10971 (define_insn "setcc_<mode>_sse"
10972   [(set (match_operand:MODEF 0 "register_operand" "=x,x")
10973         (match_operator:MODEF 3 "sse_comparison_operator"
10974           [(match_operand:MODEF 1 "register_operand" "0,x")
10975            (match_operand:MODEF 2 "nonimmediate_operand" "xm,xm")]))]
10976   "SSE_FLOAT_MODE_P (<MODE>mode)"
10977   "@
10978    cmp%D3<ssemodesuffix>\t{%2, %0|%0, %2}
10979    vcmp%D3<ssemodesuffix>\t{%2, %1, %0|%0, %1, %2}"
10980   [(set_attr "isa" "noavx,avx")
10981    (set_attr "type" "ssecmp")
10982    (set_attr "length_immediate" "1")
10983    (set_attr "prefix" "orig,vex")
10984    (set_attr "mode" "<MODE>")])
10985 \f
10986 ;; Basic conditional jump instructions.
10987 ;; We ignore the overflow flag for signed branch instructions.
10988
10989 (define_insn "*jcc_1_bnd"
10990   [(set (pc)
10991         (if_then_else (match_operator 1 "ix86_comparison_operator"
10992                                       [(reg FLAGS_REG) (const_int 0)])
10993                       (label_ref (match_operand 0))
10994                       (pc)))]
10995   "TARGET_MPX && ix86_bnd_prefixed_insn_p (insn)"
10996   "bnd %+j%C1\t%l0"
10997   [(set_attr "type" "ibr")
10998    (set_attr "modrm" "0")
10999    (set (attr "length")
11000            (if_then_else (and (ge (minus (match_dup 0) (pc))
11001                                   (const_int -126))
11002                               (lt (minus (match_dup 0) (pc))
11003                                   (const_int 128)))
11004              (const_int 3)
11005              (const_int 7)))])
11006
11007 (define_insn "*jcc_1"
11008   [(set (pc)
11009         (if_then_else (match_operator 1 "ix86_comparison_operator"
11010                                       [(reg FLAGS_REG) (const_int 0)])
11011                       (label_ref (match_operand 0))
11012                       (pc)))]
11013   ""
11014   "%+j%C1\t%l0"
11015   [(set_attr "type" "ibr")
11016    (set_attr "modrm" "0")
11017    (set (attr "length")
11018            (if_then_else (and (ge (minus (match_dup 0) (pc))
11019                                   (const_int -126))
11020                               (lt (minus (match_dup 0) (pc))
11021                                   (const_int 128)))
11022              (const_int 2)
11023              (const_int 6)))])
11024
11025 (define_insn "*jcc_2_bnd"
11026   [(set (pc)
11027         (if_then_else (match_operator 1 "ix86_comparison_operator"
11028                                       [(reg FLAGS_REG) (const_int 0)])
11029                       (pc)
11030                       (label_ref (match_operand 0))))]
11031   "TARGET_MPX && ix86_bnd_prefixed_insn_p (insn)"
11032   "bnd %+j%c1\t%l0"
11033   [(set_attr "type" "ibr")
11034    (set_attr "modrm" "0")
11035    (set (attr "length")
11036            (if_then_else (and (ge (minus (match_dup 0) (pc))
11037                                   (const_int -126))
11038                               (lt (minus (match_dup 0) (pc))
11039                                   (const_int 128)))
11040              (const_int 3)
11041              (const_int 7)))])
11042
11043 (define_insn "*jcc_2"
11044   [(set (pc)
11045         (if_then_else (match_operator 1 "ix86_comparison_operator"
11046                                       [(reg FLAGS_REG) (const_int 0)])
11047                       (pc)
11048                       (label_ref (match_operand 0))))]
11049   ""
11050   "%+j%c1\t%l0"
11051   [(set_attr "type" "ibr")
11052    (set_attr "modrm" "0")
11053    (set (attr "length")
11054            (if_then_else (and (ge (minus (match_dup 0) (pc))
11055                                   (const_int -126))
11056                               (lt (minus (match_dup 0) (pc))
11057                                   (const_int 128)))
11058              (const_int 2)
11059              (const_int 6)))])
11060
11061 ;; In general it is not safe to assume too much about CCmode registers,
11062 ;; so simplify-rtx stops when it sees a second one.  Under certain
11063 ;; conditions this is safe on x86, so help combine not create
11064 ;;
11065 ;;      seta    %al
11066 ;;      testb   %al, %al
11067 ;;      je      Lfoo
11068
11069 (define_split
11070   [(set (pc)
11071         (if_then_else (ne (match_operator 0 "ix86_comparison_operator"
11072                                       [(reg FLAGS_REG) (const_int 0)])
11073                           (const_int 0))
11074                       (label_ref (match_operand 1))
11075                       (pc)))]
11076   ""
11077   [(set (pc)
11078         (if_then_else (match_dup 0)
11079                       (label_ref (match_dup 1))
11080                       (pc)))]
11081   "PUT_MODE (operands[0], VOIDmode);")
11082
11083 (define_split
11084   [(set (pc)
11085         (if_then_else (eq (match_operator 0 "ix86_comparison_operator"
11086                                       [(reg FLAGS_REG) (const_int 0)])
11087                           (const_int 0))
11088                       (label_ref (match_operand 1))
11089                       (pc)))]
11090   ""
11091   [(set (pc)
11092         (if_then_else (match_dup 0)
11093                       (label_ref (match_dup 1))
11094                       (pc)))]
11095 {
11096   rtx new_op0 = copy_rtx (operands[0]);
11097   operands[0] = new_op0;
11098   PUT_MODE (new_op0, VOIDmode);
11099   PUT_CODE (new_op0, ix86_reverse_condition (GET_CODE (new_op0),
11100                                              GET_MODE (XEXP (new_op0, 0))));
11101
11102   /* Make sure that (a) the CCmode we have for the flags is strong
11103      enough for the reversed compare or (b) we have a valid FP compare.  */
11104   if (! ix86_comparison_operator (new_op0, VOIDmode))
11105     FAIL;
11106 })
11107
11108 ;; zero_extend in SImode is correct also for DImode, since this is what combine
11109 ;; pass generates from shift insn with QImode operand.  Actually, the mode
11110 ;; of operand 2 (bit offset operand) doesn't matter since bt insn takes
11111 ;; appropriate modulo of the bit offset value.
11112
11113 (define_insn_and_split "*jcc_bt<mode>"
11114   [(set (pc)
11115         (if_then_else (match_operator 0 "bt_comparison_operator"
11116                         [(zero_extract:SWI48
11117                            (match_operand:SWI48 1 "register_operand" "r")
11118                            (const_int 1)
11119                            (zero_extend:SI
11120                              (match_operand:QI 2 "register_operand" "r")))
11121                          (const_int 0)])
11122                       (label_ref (match_operand 3))
11123                       (pc)))
11124    (clobber (reg:CC FLAGS_REG))]
11125   "TARGET_USE_BT || optimize_function_for_size_p (cfun)"
11126   "#"
11127   "&& 1"
11128   [(set (reg:CCC FLAGS_REG)
11129         (compare:CCC
11130           (zero_extract:SWI48
11131             (match_dup 1)
11132             (const_int 1)
11133             (match_dup 2))
11134           (const_int 0)))
11135    (set (pc)
11136         (if_then_else (match_op_dup 0 [(reg:CCC FLAGS_REG) (const_int 0)])
11137                       (label_ref (match_dup 3))
11138                       (pc)))]
11139 {
11140   operands[2] = simplify_gen_subreg (<MODE>mode, operands[2], QImode, 0);
11141
11142   PUT_CODE (operands[0], reverse_condition (GET_CODE (operands[0])));
11143 })
11144
11145 ;; Like *jcc_bt<mode>, but expect a SImode operand 2 instead of QImode
11146 ;; zero extended to SImode.
11147 (define_insn_and_split "*jcc_bt<mode>_1"
11148   [(set (pc)
11149         (if_then_else (match_operator 0 "bt_comparison_operator"
11150                         [(zero_extract:SWI48
11151                            (match_operand:SWI48 1 "register_operand" "r")
11152                            (const_int 1)
11153                            (match_operand:SI 2 "register_operand" "r"))
11154                          (const_int 0)])
11155                       (label_ref (match_operand 3))
11156                       (pc)))
11157    (clobber (reg:CC FLAGS_REG))]
11158   "TARGET_USE_BT || optimize_function_for_size_p (cfun)"
11159   "#"
11160   "&& 1"
11161   [(set (reg:CCC FLAGS_REG)
11162         (compare:CCC
11163           (zero_extract:SWI48
11164             (match_dup 1)
11165             (const_int 1)
11166             (match_dup 2))
11167           (const_int 0)))
11168    (set (pc)
11169         (if_then_else (match_op_dup 0 [(reg:CCC FLAGS_REG) (const_int 0)])
11170                       (label_ref (match_dup 3))
11171                       (pc)))]
11172 {
11173   operands[2] = simplify_gen_subreg (<MODE>mode, operands[2], SImode, 0);
11174
11175   PUT_CODE (operands[0], reverse_condition (GET_CODE (operands[0])));
11176 })
11177
11178 ;; Avoid useless masking of bit offset operand.  "and" in SImode is correct
11179 ;; also for DImode, this is what combine produces.
11180 (define_insn_and_split "*jcc_bt<mode>_mask"
11181   [(set (pc)
11182         (if_then_else (match_operator 0 "bt_comparison_operator"
11183                         [(zero_extract:SWI48
11184                            (match_operand:SWI48 1 "register_operand" "r")
11185                            (const_int 1)
11186                            (and:SI
11187                              (match_operand:SI 2 "register_operand" "r")
11188                              (match_operand:SI 3 "const_int_operand" "n")))])
11189                       (label_ref (match_operand 4))
11190                       (pc)))
11191    (clobber (reg:CC FLAGS_REG))]
11192   "(TARGET_USE_BT || optimize_function_for_size_p (cfun))
11193    && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode)-1))
11194       == GET_MODE_BITSIZE (<MODE>mode)-1"
11195   "#"
11196   "&& 1"
11197   [(set (reg:CCC FLAGS_REG)
11198         (compare:CCC
11199           (zero_extract:SWI48
11200             (match_dup 1)
11201             (const_int 1)
11202             (match_dup 2))
11203           (const_int 0)))
11204    (set (pc)
11205         (if_then_else (match_op_dup 0 [(reg:CCC FLAGS_REG) (const_int 0)])
11206                       (label_ref (match_dup 4))
11207                       (pc)))]
11208 {
11209   operands[2] = simplify_gen_subreg (<MODE>mode, operands[2], SImode, 0);
11210
11211   PUT_CODE (operands[0], reverse_condition (GET_CODE (operands[0])));
11212 })
11213
11214 (define_insn_and_split "*jcc_btsi_1"
11215   [(set (pc)
11216         (if_then_else (match_operator 0 "bt_comparison_operator"
11217                         [(and:SI
11218                            (lshiftrt:SI
11219                              (match_operand:SI 1 "register_operand" "r")
11220                              (match_operand:QI 2 "register_operand" "r"))
11221                            (const_int 1))
11222                          (const_int 0)])
11223                       (label_ref (match_operand 3))
11224                       (pc)))
11225    (clobber (reg:CC FLAGS_REG))]
11226   "TARGET_USE_BT || optimize_function_for_size_p (cfun)"
11227   "#"
11228   "&& 1"
11229   [(set (reg:CCC FLAGS_REG)
11230         (compare:CCC
11231           (zero_extract:SI
11232             (match_dup 1)
11233             (const_int 1)
11234             (match_dup 2))
11235           (const_int 0)))
11236    (set (pc)
11237         (if_then_else (match_op_dup 0 [(reg:CCC FLAGS_REG) (const_int 0)])
11238                       (label_ref (match_dup 3))
11239                       (pc)))]
11240 {
11241   operands[2] = simplify_gen_subreg (SImode, operands[2], QImode, 0);
11242
11243   PUT_CODE (operands[0], reverse_condition (GET_CODE (operands[0])));
11244 })
11245
11246 ;; avoid useless masking of bit offset operand
11247 (define_insn_and_split "*jcc_btsi_mask_1"
11248   [(set (pc)
11249         (if_then_else
11250           (match_operator 0 "bt_comparison_operator"
11251             [(and:SI
11252                (lshiftrt:SI
11253                  (match_operand:SI 1 "register_operand" "r")
11254                  (subreg:QI
11255                    (and:SI
11256                      (match_operand:SI 2 "register_operand" "r")
11257                      (match_operand:SI 3 "const_int_operand" "n")) 0))
11258                (const_int 1))
11259              (const_int 0)])
11260           (label_ref (match_operand 4))
11261           (pc)))
11262    (clobber (reg:CC FLAGS_REG))]
11263   "(TARGET_USE_BT || optimize_function_for_size_p (cfun))
11264    && (INTVAL (operands[3]) & 0x1f) == 0x1f"
11265   "#"
11266   "&& 1"
11267   [(set (reg:CCC FLAGS_REG)
11268         (compare:CCC
11269           (zero_extract:SI
11270             (match_dup 1)
11271             (const_int 1)
11272             (match_dup 2))
11273           (const_int 0)))
11274    (set (pc)
11275         (if_then_else (match_op_dup 0 [(reg:CCC FLAGS_REG) (const_int 0)])
11276                       (label_ref (match_dup 4))
11277                       (pc)))]
11278   "PUT_CODE (operands[0], reverse_condition (GET_CODE (operands[0])));")
11279
11280 ;; Define combination compare-and-branch fp compare instructions to help
11281 ;; combine.
11282
11283 (define_insn "*jcc<mode>_0_i387"
11284   [(set (pc)
11285         (if_then_else (match_operator:CCFP 0 "ix86_fp_comparison_operator"
11286                         [(match_operand:X87MODEF 1 "register_operand" "f")
11287                          (match_operand:X87MODEF 2 "const0_operand")])
11288           (label_ref (match_operand 3))
11289           (pc)))
11290    (clobber (reg:CCFP FPSR_REG))
11291    (clobber (reg:CCFP FLAGS_REG))
11292    (clobber (match_scratch:HI 4 "=a"))]
11293   "TARGET_80387 && !TARGET_CMOVE"
11294   "#")
11295
11296 (define_insn "*jcc<mode>_0_r_i387"
11297   [(set (pc)
11298         (if_then_else (match_operator:CCFP 0 "ix86_fp_comparison_operator"
11299                         [(match_operand:X87MODEF 1 "register_operand" "f")
11300                          (match_operand:X87MODEF 2 "const0_operand")])
11301           (pc)
11302           (label_ref (match_operand 3))))
11303    (clobber (reg:CCFP FPSR_REG))
11304    (clobber (reg:CCFP FLAGS_REG))
11305    (clobber (match_scratch:HI 4 "=a"))]
11306   "TARGET_80387 && !TARGET_CMOVE"
11307   "#")
11308
11309 (define_insn "*jccxf_i387"
11310   [(set (pc)
11311         (if_then_else (match_operator:CCFP 0 "ix86_fp_comparison_operator"
11312                         [(match_operand:XF 1 "register_operand" "f")
11313                          (match_operand:XF 2 "register_operand" "f")])
11314           (label_ref (match_operand 3))
11315           (pc)))
11316    (clobber (reg:CCFP FPSR_REG))
11317    (clobber (reg:CCFP FLAGS_REG))
11318    (clobber (match_scratch:HI 4 "=a"))]
11319   "TARGET_80387 && !TARGET_CMOVE"
11320   "#")
11321
11322 (define_insn "*jccxf_r_i387"
11323   [(set (pc)
11324         (if_then_else (match_operator:CCFP 0 "ix86_fp_comparison_operator"
11325                         [(match_operand:XF 1 "register_operand" "f")
11326                          (match_operand:XF 2 "register_operand" "f")])
11327           (pc)
11328           (label_ref (match_operand 3))))
11329    (clobber (reg:CCFP FPSR_REG))
11330    (clobber (reg:CCFP FLAGS_REG))
11331    (clobber (match_scratch:HI 4 "=a"))]
11332   "TARGET_80387 && !TARGET_CMOVE"
11333   "#")
11334
11335 (define_insn "*jcc<mode>_i387"
11336   [(set (pc)
11337         (if_then_else (match_operator:CCFP 0 "ix86_fp_comparison_operator"
11338                         [(match_operand:MODEF 1 "register_operand" "f")
11339                          (match_operand:MODEF 2 "nonimmediate_operand" "fm")])
11340           (label_ref (match_operand 3))
11341           (pc)))
11342    (clobber (reg:CCFP FPSR_REG))
11343    (clobber (reg:CCFP FLAGS_REG))
11344    (clobber (match_scratch:HI 4 "=a"))]
11345   "TARGET_80387 && !TARGET_CMOVE"
11346   "#")
11347
11348 (define_insn "*jcc<mode>_r_i387"
11349   [(set (pc)
11350         (if_then_else (match_operator:CCFP 0 "ix86_fp_comparison_operator"
11351                         [(match_operand:MODEF 1 "register_operand" "f")
11352                          (match_operand:MODEF 2 "nonimmediate_operand" "fm")])
11353           (pc)
11354           (label_ref (match_operand 3))))
11355    (clobber (reg:CCFP FPSR_REG))
11356    (clobber (reg:CCFP FLAGS_REG))
11357    (clobber (match_scratch:HI 4 "=a"))]
11358   "TARGET_80387 && !TARGET_CMOVE"
11359   "#")
11360
11361 (define_insn "*jccu<mode>_i387"
11362   [(set (pc)
11363         (if_then_else (match_operator:CCFPU 0 "ix86_fp_comparison_operator"
11364                         [(match_operand:X87MODEF 1 "register_operand" "f")
11365                          (match_operand:X87MODEF 2 "register_operand" "f")])
11366           (label_ref (match_operand 3))
11367           (pc)))
11368    (clobber (reg:CCFP FPSR_REG))
11369    (clobber (reg:CCFP FLAGS_REG))
11370    (clobber (match_scratch:HI 4 "=a"))]
11371   "TARGET_80387 && !TARGET_CMOVE"
11372   "#")
11373
11374 (define_insn "*jccu<mode>_r_i387"
11375   [(set (pc)
11376         (if_then_else (match_operator:CCFPU 0 "ix86_fp_comparison_operator"
11377                         [(match_operand:X87MODEF 1 "register_operand" "f")
11378                          (match_operand:X87MODEF 2 "register_operand" "f")])
11379           (pc)
11380           (label_ref (match_operand 3))))
11381    (clobber (reg:CCFP FPSR_REG))
11382    (clobber (reg:CCFP FLAGS_REG))
11383    (clobber (match_scratch:HI 4 "=a"))]
11384   "TARGET_80387 && !TARGET_CMOVE"
11385   "#")
11386
11387 (define_split
11388   [(set (pc)
11389         (if_then_else (match_operator 0 "ix86_fp_comparison_operator"
11390                         [(match_operand:X87MODEF 1 "register_operand")
11391                          (match_operand:X87MODEF 2 "nonimmediate_operand")])
11392           (match_operand 3)
11393           (match_operand 4)))
11394    (clobber (reg:CCFP FPSR_REG))
11395    (clobber (reg:CCFP FLAGS_REG))]
11396   "TARGET_80387 && !TARGET_CMOVE
11397    && reload_completed"
11398   [(const_int 0)]
11399 {
11400   ix86_split_fp_branch (GET_CODE (operands[0]), operands[1], operands[2],
11401                         operands[3], operands[4], NULL_RTX);
11402   DONE;
11403 })
11404
11405 (define_split
11406   [(set (pc)
11407         (if_then_else (match_operator 0 "ix86_fp_comparison_operator"
11408                         [(match_operand:X87MODEF 1 "register_operand")
11409                          (match_operand:X87MODEF 2 "general_operand")])
11410           (match_operand 3)
11411           (match_operand 4)))
11412    (clobber (reg:CCFP FPSR_REG))
11413    (clobber (reg:CCFP FLAGS_REG))
11414    (clobber (match_scratch:HI 5))]
11415   "TARGET_80387 && !TARGET_CMOVE
11416    && reload_completed"
11417   [(const_int 0)]
11418 {
11419   ix86_split_fp_branch (GET_CODE (operands[0]), operands[1], operands[2],
11420                         operands[3], operands[4], operands[5]);
11421   DONE;
11422 })
11423
11424 ;; The order of operands in *jcc<fp>_<int>_i387 is forced by combine in
11425 ;; simplify_comparison () function. Float operator is treated as RTX_OBJ
11426 ;; with a precedence over other operators and is always put in the first
11427 ;; place. Swap condition and operands to match ficom instruction.
11428
11429 (define_insn "*jcc<X87MODEF:mode>_<SWI24:mode>_i387"
11430   [(set (pc)
11431         (if_then_else
11432           (match_operator:CCFP 0 "ix86_swapped_fp_comparison_operator"
11433             [(match_operator:X87MODEF 1 "float_operator"
11434               [(match_operand:SWI24 2 "nonimmediate_operand" "m")])
11435              (match_operand:X87MODEF 3 "register_operand" "f")])
11436           (label_ref (match_operand 4))
11437           (pc)))
11438    (clobber (reg:CCFP FPSR_REG))
11439    (clobber (reg:CCFP FLAGS_REG))
11440    (clobber (match_scratch:HI 5 "=a"))]
11441   "TARGET_80387 && !TARGET_CMOVE
11442    && (TARGET_USE_<SWI24:MODE>MODE_FIOP
11443        || optimize_function_for_size_p (cfun))"
11444   "#")
11445
11446 (define_insn "*jcc<X87MODEF:mode>_<SWI24:mode>_r_i387"
11447   [(set (pc)
11448         (if_then_else
11449           (match_operator:CCFP 0 "ix86_swapped_fp_comparison_operator"
11450             [(match_operator:X87MODEF 1 "float_operator"
11451               [(match_operand:SWI24 2 "nonimmediate_operand" "m")])
11452              (match_operand:X87MODEF 3 "register_operand" "f")])
11453           (pc)
11454           (label_ref (match_operand 4))))
11455    (clobber (reg:CCFP FPSR_REG))
11456    (clobber (reg:CCFP FLAGS_REG))
11457    (clobber (match_scratch:HI 5 "=a"))]
11458   "TARGET_80387 && !TARGET_CMOVE
11459    && (TARGET_USE_<SWI24:MODE>MODE_FIOP
11460        || optimize_function_for_size_p (cfun))"
11461   "#")
11462
11463 (define_split
11464   [(set (pc)
11465         (if_then_else
11466           (match_operator:CCFP 0 "ix86_swapped_fp_comparison_operator"
11467             [(match_operator:X87MODEF 1 "float_operator"
11468               [(match_operand:SWI24 2 "memory_operand")])
11469              (match_operand:X87MODEF 3 "register_operand")])
11470           (match_operand 4)
11471           (match_operand 5)))
11472    (clobber (reg:CCFP FPSR_REG))
11473    (clobber (reg:CCFP FLAGS_REG))
11474    (clobber (match_scratch:HI 6))]
11475   "TARGET_80387 && !TARGET_CMOVE
11476    && reload_completed"
11477   [(const_int 0)]
11478 {
11479   ix86_split_fp_branch (swap_condition (GET_CODE (operands[0])), operands[3],
11480                         gen_rtx_FLOAT (GET_MODE (operands[1]), operands[2]),
11481                         operands[4], operands[5], operands[6]);
11482   DONE;
11483 })
11484 \f
11485 ;; Unconditional and other jump instructions
11486
11487 (define_insn "jump_bnd"
11488   [(set (pc)
11489         (label_ref (match_operand 0)))]
11490   "TARGET_MPX && ix86_bnd_prefixed_insn_p (insn)"
11491   "bnd jmp\t%l0"
11492   [(set_attr "type" "ibr")
11493    (set (attr "length")
11494            (if_then_else (and (ge (minus (match_dup 0) (pc))
11495                                   (const_int -126))
11496                               (lt (minus (match_dup 0) (pc))
11497                                   (const_int 128)))
11498              (const_int 3)
11499              (const_int 6)))
11500    (set_attr "modrm" "0")])
11501
11502 (define_insn "jump"
11503   [(set (pc)
11504         (label_ref (match_operand 0)))]
11505   ""
11506   "jmp\t%l0"
11507   [(set_attr "type" "ibr")
11508    (set (attr "length")
11509            (if_then_else (and (ge (minus (match_dup 0) (pc))
11510                                   (const_int -126))
11511                               (lt (minus (match_dup 0) (pc))
11512                                   (const_int 128)))
11513              (const_int 2)
11514              (const_int 5)))
11515    (set_attr "modrm" "0")])
11516
11517 (define_expand "indirect_jump"
11518   [(set (pc) (match_operand 0 "indirect_branch_operand"))]
11519   ""
11520 {
11521   if (TARGET_X32)
11522     operands[0] = convert_memory_address (word_mode, operands[0]);
11523 })
11524
11525 (define_insn "*indirect_jump"
11526   [(set (pc) (match_operand:W 0 "indirect_branch_operand" "rBw"))]
11527   ""
11528   "%!jmp\t%A0"
11529   [(set_attr "type" "ibr")
11530    (set_attr "length_immediate" "0")])
11531
11532 (define_expand "tablejump"
11533   [(parallel [(set (pc) (match_operand 0 "indirect_branch_operand"))
11534               (use (label_ref (match_operand 1)))])]
11535   ""
11536 {
11537   /* In PIC mode, the table entries are stored GOT (32-bit) or PC (64-bit)
11538      relative.  Convert the relative address to an absolute address.  */
11539   if (flag_pic)
11540     {
11541       rtx op0, op1;
11542       enum rtx_code code;
11543
11544       /* We can't use @GOTOFF for text labels on VxWorks;
11545          see gotoff_operand.  */
11546       if (TARGET_64BIT || TARGET_VXWORKS_RTP)
11547         {
11548           code = PLUS;
11549           op0 = operands[0];
11550           op1 = gen_rtx_LABEL_REF (Pmode, operands[1]);
11551         }
11552       else if (TARGET_MACHO || HAVE_AS_GOTOFF_IN_DATA)
11553         {
11554           code = PLUS;
11555           op0 = operands[0];
11556           op1 = pic_offset_table_rtx;
11557         }
11558       else
11559         {
11560           code = MINUS;
11561           op0 = pic_offset_table_rtx;
11562           op1 = operands[0];
11563         }
11564
11565       operands[0] = expand_simple_binop (Pmode, code, op0, op1, NULL_RTX, 0,
11566                                          OPTAB_DIRECT);
11567     }
11568
11569   if (TARGET_X32)
11570     operands[0] = convert_memory_address (word_mode, operands[0]);
11571 })
11572
11573 (define_insn "*tablejump_1"
11574   [(set (pc) (match_operand:W 0 "indirect_branch_operand" "rBw"))
11575    (use (label_ref (match_operand 1)))]
11576   ""
11577   "%!jmp\t%A0"
11578   [(set_attr "type" "ibr")
11579    (set_attr "length_immediate" "0")])
11580 \f
11581 ;; Convert setcc + movzbl to xor + setcc if operands don't overlap.
11582
11583 (define_peephole2
11584   [(set (reg FLAGS_REG) (match_operand 0))
11585    (set (match_operand:QI 1 "register_operand")
11586         (match_operator:QI 2 "ix86_comparison_operator"
11587           [(reg FLAGS_REG) (const_int 0)]))
11588    (set (match_operand 3 "q_regs_operand")
11589         (zero_extend (match_dup 1)))]
11590   "(peep2_reg_dead_p (3, operands[1])
11591     || operands_match_p (operands[1], operands[3]))
11592    && ! reg_overlap_mentioned_p (operands[3], operands[0])"
11593   [(set (match_dup 4) (match_dup 0))
11594    (set (strict_low_part (match_dup 5))
11595         (match_dup 2))]
11596 {
11597   operands[4] = gen_rtx_REG (GET_MODE (operands[0]), FLAGS_REG);
11598   operands[5] = gen_lowpart (QImode, operands[3]);
11599   ix86_expand_clear (operands[3]);
11600 })
11601
11602 (define_peephole2
11603   [(parallel [(set (reg FLAGS_REG) (match_operand 0))
11604               (match_operand 4)])
11605    (set (match_operand:QI 1 "register_operand")
11606         (match_operator:QI 2 "ix86_comparison_operator"
11607           [(reg FLAGS_REG) (const_int 0)]))
11608    (set (match_operand 3 "q_regs_operand")
11609         (zero_extend (match_dup 1)))]
11610   "(peep2_reg_dead_p (3, operands[1])
11611     || operands_match_p (operands[1], operands[3]))
11612    && ! reg_overlap_mentioned_p (operands[3], operands[0])"
11613   [(parallel [(set (match_dup 5) (match_dup 0))
11614               (match_dup 4)])
11615    (set (strict_low_part (match_dup 6))
11616         (match_dup 2))]
11617 {
11618   operands[5] = gen_rtx_REG (GET_MODE (operands[0]), FLAGS_REG);
11619   operands[6] = gen_lowpart (QImode, operands[3]);
11620   ix86_expand_clear (operands[3]);
11621 })
11622
11623 ;; Similar, but match zero extend with andsi3.
11624
11625 (define_peephole2
11626   [(set (reg FLAGS_REG) (match_operand 0))
11627    (set (match_operand:QI 1 "register_operand")
11628         (match_operator:QI 2 "ix86_comparison_operator"
11629           [(reg FLAGS_REG) (const_int 0)]))
11630    (parallel [(set (match_operand:SI 3 "q_regs_operand")
11631                    (and:SI (match_dup 3) (const_int 255)))
11632               (clobber (reg:CC FLAGS_REG))])]
11633   "REGNO (operands[1]) == REGNO (operands[3])
11634    && ! reg_overlap_mentioned_p (operands[3], operands[0])"
11635   [(set (match_dup 4) (match_dup 0))
11636    (set (strict_low_part (match_dup 5))
11637         (match_dup 2))]
11638 {
11639   operands[4] = gen_rtx_REG (GET_MODE (operands[0]), FLAGS_REG);
11640   operands[5] = gen_lowpart (QImode, operands[3]);
11641   ix86_expand_clear (operands[3]);
11642 })
11643
11644 (define_peephole2
11645   [(parallel [(set (reg FLAGS_REG) (match_operand 0))
11646               (match_operand 4)])
11647    (set (match_operand:QI 1 "register_operand")
11648         (match_operator:QI 2 "ix86_comparison_operator"
11649           [(reg FLAGS_REG) (const_int 0)]))
11650    (parallel [(set (match_operand 3 "q_regs_operand")
11651                    (zero_extend (match_dup 1)))
11652               (clobber (reg:CC FLAGS_REG))])]
11653   "(peep2_reg_dead_p (3, operands[1])
11654     || operands_match_p (operands[1], operands[3]))
11655    && ! reg_overlap_mentioned_p (operands[3], operands[0])"
11656   [(parallel [(set (match_dup 5) (match_dup 0))
11657               (match_dup 4)])
11658    (set (strict_low_part (match_dup 6))
11659         (match_dup 2))]
11660 {
11661   operands[5] = gen_rtx_REG (GET_MODE (operands[0]), FLAGS_REG);
11662   operands[6] = gen_lowpart (QImode, operands[3]);
11663   ix86_expand_clear (operands[3]);
11664 })
11665 \f
11666 ;; Call instructions.
11667
11668 ;; The predicates normally associated with named expanders are not properly
11669 ;; checked for calls.  This is a bug in the generic code, but it isn't that
11670 ;; easy to fix.  Ignore it for now and be prepared to fix things up.
11671
11672 ;; P6 processors will jump to the address after the decrement when %esp
11673 ;; is used as a call operand, so they will execute return address as a code.
11674 ;; See Pentium Pro errata 70, Pentium 2 errata A33 and Pentium 3 errata E17.
11675
11676 ;; Register constraint for call instruction.
11677 (define_mode_attr c [(SI "l") (DI "r")])
11678
11679 ;; Call subroutine returning no value.
11680
11681 (define_expand "call"
11682   [(call (match_operand:QI 0)
11683          (match_operand 1))
11684    (use (match_operand 2))]
11685   ""
11686 {
11687   ix86_expand_call (NULL, operands[0], operands[1],
11688                     operands[2], NULL, false);
11689   DONE;
11690 })
11691
11692 (define_expand "sibcall"
11693   [(call (match_operand:QI 0)
11694          (match_operand 1))
11695    (use (match_operand 2))]
11696   ""
11697 {
11698   ix86_expand_call (NULL, operands[0], operands[1],
11699                     operands[2], NULL, true);
11700   DONE;
11701 })
11702
11703 (define_insn "*call"
11704   [(call (mem:QI (match_operand:W 0 "call_insn_operand" "<c>BwBz"))
11705          (match_operand 1))]
11706   "!SIBLING_CALL_P (insn)"
11707   "* return ix86_output_call_insn (insn, operands[0]);"
11708   [(set_attr "type" "call")])
11709
11710 (define_insn "*sibcall"
11711   [(call (mem:QI (match_operand:W 0 "sibcall_insn_operand" "UBsBz"))
11712          (match_operand 1))]
11713   "SIBLING_CALL_P (insn)"
11714   "* return ix86_output_call_insn (insn, operands[0]);"
11715   [(set_attr "type" "call")])
11716
11717 (define_insn "*sibcall_memory"
11718   [(call (mem:QI (match_operand:W 0 "memory_operand" "m"))
11719          (match_operand 1))
11720    (unspec [(const_int 0)] UNSPEC_PEEPSIB)]
11721   "!TARGET_X32"
11722   "* return ix86_output_call_insn (insn, operands[0]);"
11723   [(set_attr "type" "call")])
11724
11725 (define_peephole2
11726   [(set (match_operand:W 0 "register_operand")
11727         (match_operand:W 1 "memory_operand"))
11728    (call (mem:QI (match_dup 0))
11729          (match_operand 3))]
11730   "!TARGET_X32 && SIBLING_CALL_P (peep2_next_insn (1))
11731    && peep2_reg_dead_p (2, operands[0])"
11732   [(parallel [(call (mem:QI (match_dup 1))
11733                     (match_dup 3))
11734               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11735
11736 (define_peephole2
11737   [(set (match_operand:W 0 "register_operand")
11738         (match_operand:W 1 "memory_operand"))
11739    (unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11740    (call (mem:QI (match_dup 0))
11741          (match_operand 3))]
11742   "!TARGET_X32 && SIBLING_CALL_P (peep2_next_insn (2))
11743    && peep2_reg_dead_p (3, operands[0])"
11744   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11745    (parallel [(call (mem:QI (match_dup 1))
11746                     (match_dup 3))
11747               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11748
11749 (define_expand "call_pop"
11750   [(parallel [(call (match_operand:QI 0)
11751                     (match_operand:SI 1))
11752               (set (reg:SI SP_REG)
11753                    (plus:SI (reg:SI SP_REG)
11754                             (match_operand:SI 3)))])]
11755   "!TARGET_64BIT"
11756 {
11757   ix86_expand_call (NULL, operands[0], operands[1],
11758                     operands[2], operands[3], false);
11759   DONE;
11760 })
11761
11762 (define_insn "*call_pop"
11763   [(call (mem:QI (match_operand:SI 0 "call_insn_operand" "lmBz"))
11764          (match_operand 1))
11765    (set (reg:SI SP_REG)
11766         (plus:SI (reg:SI SP_REG)
11767                  (match_operand:SI 2 "immediate_operand" "i")))]
11768   "!TARGET_64BIT && !SIBLING_CALL_P (insn)"
11769   "* return ix86_output_call_insn (insn, operands[0]);"
11770   [(set_attr "type" "call")])
11771
11772 (define_insn "*sibcall_pop"
11773   [(call (mem:QI (match_operand:SI 0 "sibcall_insn_operand" "UBsBz"))
11774          (match_operand 1))
11775    (set (reg:SI SP_REG)
11776         (plus:SI (reg:SI SP_REG)
11777                  (match_operand:SI 2 "immediate_operand" "i")))]
11778   "!TARGET_64BIT && SIBLING_CALL_P (insn)"
11779   "* return ix86_output_call_insn (insn, operands[0]);"
11780   [(set_attr "type" "call")])
11781
11782 (define_insn "*sibcall_pop_memory"
11783   [(call (mem:QI (match_operand:SI 0 "memory_operand" "m"))
11784          (match_operand 1))
11785    (set (reg:SI SP_REG)
11786         (plus:SI (reg:SI SP_REG)
11787                  (match_operand:SI 2 "immediate_operand" "i")))
11788    (unspec [(const_int 0)] UNSPEC_PEEPSIB)]
11789   "!TARGET_64BIT"
11790   "* return ix86_output_call_insn (insn, operands[0]);"
11791   [(set_attr "type" "call")])
11792
11793 (define_peephole2
11794   [(set (match_operand:SI 0 "register_operand")
11795         (match_operand:SI 1 "memory_operand"))
11796    (parallel [(call (mem:QI (match_dup 0))
11797                     (match_operand 3))
11798               (set (reg:SI SP_REG)
11799                    (plus:SI (reg:SI SP_REG)
11800                             (match_operand:SI 4 "immediate_operand")))])]
11801   "!TARGET_64BIT && SIBLING_CALL_P (peep2_next_insn (1))
11802    && peep2_reg_dead_p (2, operands[0])"
11803   [(parallel [(call (mem:QI (match_dup 1))
11804                     (match_dup 3))
11805               (set (reg:SI SP_REG)
11806                    (plus:SI (reg:SI SP_REG)
11807                             (match_dup 4)))
11808               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11809
11810 (define_peephole2
11811   [(set (match_operand:SI 0 "register_operand")
11812         (match_operand:SI 1 "memory_operand"))
11813    (unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11814    (parallel [(call (mem:QI (match_dup 0))
11815                     (match_operand 3))
11816               (set (reg:SI SP_REG)
11817                    (plus:SI (reg:SI SP_REG)
11818                             (match_operand:SI 4 "immediate_operand")))])]
11819   "!TARGET_64BIT && SIBLING_CALL_P (peep2_next_insn (2))
11820    && peep2_reg_dead_p (3, operands[0])"
11821   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11822    (parallel [(call (mem:QI (match_dup 1))
11823                     (match_dup 3))
11824               (set (reg:SI SP_REG)
11825                    (plus:SI (reg:SI SP_REG)
11826                             (match_dup 4)))
11827               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11828
11829 ;; Combining simple memory jump instruction
11830
11831 (define_peephole2
11832   [(set (match_operand:W 0 "register_operand")
11833         (match_operand:W 1 "memory_operand"))
11834    (set (pc) (match_dup 0))]
11835   "!TARGET_X32 && peep2_reg_dead_p (2, operands[0])"
11836   [(set (pc) (match_dup 1))])
11837
11838 ;; Call subroutine, returning value in operand 0
11839
11840 (define_expand "call_value"
11841   [(set (match_operand 0)
11842         (call (match_operand:QI 1)
11843               (match_operand 2)))
11844    (use (match_operand 3))]
11845   ""
11846 {
11847   ix86_expand_call (operands[0], operands[1], operands[2],
11848                     operands[3], NULL, false);
11849   DONE;
11850 })
11851
11852 (define_expand "sibcall_value"
11853   [(set (match_operand 0)
11854         (call (match_operand:QI 1)
11855               (match_operand 2)))
11856    (use (match_operand 3))]
11857   ""
11858 {
11859   ix86_expand_call (operands[0], operands[1], operands[2],
11860                     operands[3], NULL, true);
11861   DONE;
11862 })
11863
11864 (define_insn "*call_value"
11865   [(set (match_operand 0)
11866         (call (mem:QI (match_operand:W 1 "call_insn_operand" "<c>BwBz"))
11867               (match_operand 2)))]
11868   "!SIBLING_CALL_P (insn)"
11869   "* return ix86_output_call_insn (insn, operands[1]);"
11870   [(set_attr "type" "callv")])
11871
11872 (define_insn "*sibcall_value"
11873   [(set (match_operand 0)
11874         (call (mem:QI (match_operand:W 1 "sibcall_insn_operand" "UBsBz"))
11875               (match_operand 2)))]
11876   "SIBLING_CALL_P (insn)"
11877   "* return ix86_output_call_insn (insn, operands[1]);"
11878   [(set_attr "type" "callv")])
11879
11880 (define_insn "*sibcall_value_memory"
11881   [(set (match_operand 0)
11882         (call (mem:QI (match_operand:W 1 "memory_operand" "m"))
11883               (match_operand 2)))
11884    (unspec [(const_int 0)] UNSPEC_PEEPSIB)]
11885   "!TARGET_X32"
11886   "* return ix86_output_call_insn (insn, operands[1]);"
11887   [(set_attr "type" "callv")])
11888
11889 (define_peephole2
11890   [(set (match_operand:W 0 "register_operand")
11891         (match_operand:W 1 "memory_operand"))
11892    (set (match_operand 2)
11893    (call (mem:QI (match_dup 0))
11894                  (match_operand 3)))]
11895   "!TARGET_X32 && SIBLING_CALL_P (peep2_next_insn (1))
11896    && peep2_reg_dead_p (2, operands[0])"
11897   [(parallel [(set (match_dup 2)
11898                    (call (mem:QI (match_dup 1))
11899                          (match_dup 3)))
11900               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11901
11902 (define_peephole2
11903   [(set (match_operand:W 0 "register_operand")
11904         (match_operand:W 1 "memory_operand"))
11905    (unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11906    (set (match_operand 2)
11907         (call (mem:QI (match_dup 0))
11908               (match_operand 3)))]
11909   "!TARGET_X32 && SIBLING_CALL_P (peep2_next_insn (2))
11910    && peep2_reg_dead_p (3, operands[0])"
11911   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11912    (parallel [(set (match_dup 2)
11913                    (call (mem:QI (match_dup 1))
11914                          (match_dup 3)))
11915               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11916
11917 (define_expand "call_value_pop"
11918   [(parallel [(set (match_operand 0)
11919                    (call (match_operand:QI 1)
11920                          (match_operand:SI 2)))
11921               (set (reg:SI SP_REG)
11922                    (plus:SI (reg:SI SP_REG)
11923                             (match_operand:SI 4)))])]
11924   "!TARGET_64BIT"
11925 {
11926   ix86_expand_call (operands[0], operands[1], operands[2],
11927                     operands[3], operands[4], false);
11928   DONE;
11929 })
11930
11931 (define_insn "*call_value_pop"
11932   [(set (match_operand 0)
11933         (call (mem:QI (match_operand:SI 1 "call_insn_operand" "lmBz"))
11934               (match_operand 2)))
11935    (set (reg:SI SP_REG)
11936         (plus:SI (reg:SI SP_REG)
11937                  (match_operand:SI 3 "immediate_operand" "i")))]
11938   "!TARGET_64BIT && !SIBLING_CALL_P (insn)"
11939   "* return ix86_output_call_insn (insn, operands[1]);"
11940   [(set_attr "type" "callv")])
11941
11942 (define_insn "*sibcall_value_pop"
11943   [(set (match_operand 0)
11944         (call (mem:QI (match_operand:SI 1 "sibcall_insn_operand" "UBsBz"))
11945               (match_operand 2)))
11946    (set (reg:SI SP_REG)
11947         (plus:SI (reg:SI SP_REG)
11948                  (match_operand:SI 3 "immediate_operand" "i")))]
11949   "!TARGET_64BIT && SIBLING_CALL_P (insn)"
11950   "* return ix86_output_call_insn (insn, operands[1]);"
11951   [(set_attr "type" "callv")])
11952
11953 (define_insn "*sibcall_value_pop_memory"
11954   [(set (match_operand 0)
11955         (call (mem:QI (match_operand:SI 1 "memory_operand" "m"))
11956               (match_operand 2)))
11957    (set (reg:SI SP_REG)
11958         (plus:SI (reg:SI SP_REG)
11959                  (match_operand:SI 3 "immediate_operand" "i")))
11960    (unspec [(const_int 0)] UNSPEC_PEEPSIB)]
11961   "!TARGET_64BIT"
11962   "* return ix86_output_call_insn (insn, operands[1]);"
11963   [(set_attr "type" "callv")])
11964
11965 (define_peephole2
11966   [(set (match_operand:SI 0 "register_operand")
11967         (match_operand:SI 1 "memory_operand"))
11968    (parallel [(set (match_operand 2)
11969                    (call (mem:QI (match_dup 0))
11970                          (match_operand 3)))
11971               (set (reg:SI SP_REG)
11972                    (plus:SI (reg:SI SP_REG)
11973                             (match_operand:SI 4 "immediate_operand")))])]
11974   "!TARGET_64BIT && SIBLING_CALL_P (peep2_next_insn (1))
11975    && peep2_reg_dead_p (2, operands[0])"
11976   [(parallel [(set (match_dup 2)
11977                    (call (mem:QI (match_dup 1))
11978                          (match_dup 3)))
11979               (set (reg:SI SP_REG)
11980                    (plus:SI (reg:SI SP_REG)
11981                             (match_dup 4)))
11982               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
11983
11984 (define_peephole2
11985   [(set (match_operand:SI 0 "register_operand")
11986         (match_operand:SI 1 "memory_operand"))
11987    (unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11988    (parallel [(set (match_operand 2)
11989                    (call (mem:QI (match_dup 0))
11990                          (match_operand 3)))
11991               (set (reg:SI SP_REG)
11992                    (plus:SI (reg:SI SP_REG)
11993                             (match_operand:SI 4 "immediate_operand")))])]
11994   "!TARGET_64BIT && SIBLING_CALL_P (peep2_next_insn (2))
11995    && peep2_reg_dead_p (3, operands[0])"
11996   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)
11997    (parallel [(set (match_dup 2)
11998                    (call (mem:QI (match_dup 1))
11999                          (match_dup 3)))
12000               (set (reg:SI SP_REG)
12001                    (plus:SI (reg:SI SP_REG)
12002                             (match_dup 4)))
12003               (unspec [(const_int 0)] UNSPEC_PEEPSIB)])])
12004
12005 ;; Call subroutine returning any type.
12006
12007 (define_expand "untyped_call"
12008   [(parallel [(call (match_operand 0)
12009                     (const_int 0))
12010               (match_operand 1)
12011               (match_operand 2)])]
12012   ""
12013 {
12014   int i;
12015
12016   /* In order to give reg-stack an easier job in validating two
12017      coprocessor registers as containing a possible return value,
12018      simply pretend the untyped call returns a complex long double
12019      value. 
12020
12021      We can't use SSE_REGPARM_MAX here since callee is unprototyped
12022      and should have the default ABI.  */
12023
12024   ix86_expand_call ((TARGET_FLOAT_RETURNS_IN_80387
12025                      ? gen_rtx_REG (XCmode, FIRST_FLOAT_REG) : NULL),
12026                     operands[0], const0_rtx,
12027                     GEN_INT ((TARGET_64BIT
12028                               ? (ix86_abi == SYSV_ABI
12029                                  ? X86_64_SSE_REGPARM_MAX
12030                                  : X86_64_MS_SSE_REGPARM_MAX)
12031                               : X86_32_SSE_REGPARM_MAX)
12032                              - 1),
12033                     NULL, false);
12034
12035   for (i = 0; i < XVECLEN (operands[2], 0); i++)
12036     {
12037       rtx set = XVECEXP (operands[2], 0, i);
12038       emit_move_insn (SET_DEST (set), SET_SRC (set));
12039     }
12040
12041   /* The optimizer does not know that the call sets the function value
12042      registers we stored in the result block.  We avoid problems by
12043      claiming that all hard registers are used and clobbered at this
12044      point.  */
12045   emit_insn (gen_blockage ());
12046
12047   DONE;
12048 })
12049 \f
12050 ;; Prologue and epilogue instructions
12051
12052 ;; UNSPEC_VOLATILE is considered to use and clobber all hard registers and
12053 ;; all of memory.  This blocks insns from being moved across this point.
12054
12055 (define_insn "blockage"
12056   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)]
12057   ""
12058   ""
12059   [(set_attr "length" "0")])
12060
12061 ;; Do not schedule instructions accessing memory across this point.
12062
12063 (define_expand "memory_blockage"
12064   [(set (match_dup 0)
12065         (unspec:BLK [(match_dup 0)] UNSPEC_MEMORY_BLOCKAGE))]
12066   ""
12067 {
12068   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
12069   MEM_VOLATILE_P (operands[0]) = 1;
12070 })
12071
12072 (define_insn "*memory_blockage"
12073   [(set (match_operand:BLK 0)
12074         (unspec:BLK [(match_dup 0)] UNSPEC_MEMORY_BLOCKAGE))]
12075   ""
12076   ""
12077   [(set_attr "length" "0")])
12078
12079 ;; As USE insns aren't meaningful after reload, this is used instead
12080 ;; to prevent deleting instructions setting registers for PIC code
12081 (define_insn "prologue_use"
12082   [(unspec_volatile [(match_operand 0)] UNSPECV_PROLOGUE_USE)]
12083   ""
12084   ""
12085   [(set_attr "length" "0")])
12086
12087 ;; Insn emitted into the body of a function to return from a function.
12088 ;; This is only done if the function's epilogue is known to be simple.
12089 ;; See comments for ix86_can_use_return_insn_p in i386.c.
12090
12091 (define_expand "return"
12092   [(simple_return)]
12093   "ix86_can_use_return_insn_p ()"
12094 {
12095   if (crtl->args.pops_args)
12096     {
12097       rtx popc = GEN_INT (crtl->args.pops_args);
12098       emit_jump_insn (gen_simple_return_pop_internal (popc));
12099       DONE;
12100     }
12101 })
12102
12103 ;; We need to disable this for TARGET_SEH, as otherwise
12104 ;; shrink-wrapped prologue gets enabled too.  This might exceed
12105 ;; the maximum size of prologue in unwind information.
12106
12107 (define_expand "simple_return"
12108   [(simple_return)]
12109   "!TARGET_SEH"
12110 {
12111   if (crtl->args.pops_args)
12112     {
12113       rtx popc = GEN_INT (crtl->args.pops_args);
12114       emit_jump_insn (gen_simple_return_pop_internal (popc));
12115       DONE;
12116     }
12117 })
12118
12119 (define_insn "simple_return_internal"
12120   [(simple_return)]
12121   "reload_completed"
12122   "%!ret"
12123   [(set_attr "length_nobnd" "1")
12124    (set_attr "atom_unit" "jeu")
12125    (set_attr "length_immediate" "0")
12126    (set_attr "modrm" "0")])
12127
12128 ;; Used by x86_machine_dependent_reorg to avoid penalty on single byte RET
12129 ;; instruction Athlon and K8 have.
12130
12131 (define_insn "simple_return_internal_long"
12132   [(simple_return)
12133    (unspec [(const_int 0)] UNSPEC_REP)]
12134   "reload_completed"
12135 {
12136   if (ix86_bnd_prefixed_insn_p (insn))
12137     return "%!ret";
12138
12139   return "rep%; ret";
12140 }
12141   [(set_attr "length" "2")
12142    (set_attr "atom_unit" "jeu")
12143    (set_attr "length_immediate" "0")
12144    (set_attr "prefix_rep" "1")
12145    (set_attr "modrm" "0")])
12146
12147 (define_insn "simple_return_pop_internal"
12148   [(simple_return)
12149    (use (match_operand:SI 0 "const_int_operand"))]
12150   "reload_completed"
12151   "%!ret\t%0"
12152   [(set_attr "length_nobnd" "3")
12153    (set_attr "atom_unit" "jeu")
12154    (set_attr "length_immediate" "2")
12155    (set_attr "modrm" "0")])
12156
12157 (define_insn "simple_return_indirect_internal"
12158   [(simple_return)
12159    (use (match_operand:SI 0 "register_operand" "r"))]
12160   "reload_completed"
12161   "%!jmp\t%A0"
12162   [(set_attr "type" "ibr")
12163    (set_attr "length_immediate" "0")])
12164
12165 (define_insn "nop"
12166   [(const_int 0)]
12167   ""
12168   "nop"
12169   [(set_attr "length" "1")
12170    (set_attr "length_immediate" "0")
12171    (set_attr "modrm" "0")])
12172
12173 ;; Generate nops.  Operand 0 is the number of nops, up to 8.
12174 (define_insn "nops"
12175   [(unspec_volatile [(match_operand 0 "const_int_operand")]
12176                     UNSPECV_NOPS)]
12177   "reload_completed"
12178 {
12179   int num = INTVAL (operands[0]);
12180
12181   gcc_assert (IN_RANGE (num, 1, 8));
12182
12183   while (num--)
12184     fputs ("\tnop\n", asm_out_file);
12185
12186   return "";
12187 }
12188   [(set (attr "length") (symbol_ref "INTVAL (operands[0])"))
12189    (set_attr "length_immediate" "0")
12190    (set_attr "modrm" "0")])
12191
12192 ;; Pad to 16-byte boundary, max skip in op0.  Used to avoid
12193 ;; branch prediction penalty for the third jump in a 16-byte
12194 ;; block on K8.
12195
12196 (define_insn "pad"
12197   [(unspec_volatile [(match_operand 0)] UNSPECV_ALIGN)]
12198   ""
12199 {
12200 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
12201   ASM_OUTPUT_MAX_SKIP_PAD (asm_out_file, 4, (int)INTVAL (operands[0]));
12202 #else
12203   /* It is tempting to use ASM_OUTPUT_ALIGN here, but we don't want to do that.
12204      The align insn is used to avoid 3 jump instructions in the row to improve
12205      branch prediction and the benefits hardly outweigh the cost of extra 8
12206      nops on the average inserted by full alignment pseudo operation.  */
12207 #endif
12208   return "";
12209 }
12210   [(set_attr "length" "16")])
12211
12212 (define_expand "prologue"
12213   [(const_int 0)]
12214   ""
12215   "ix86_expand_prologue (); DONE;")
12216
12217 (define_insn "set_got"
12218   [(set (match_operand:SI 0 "register_operand" "=r")
12219         (unspec:SI [(const_int 0)] UNSPEC_SET_GOT))
12220    (clobber (reg:CC FLAGS_REG))]
12221   "!TARGET_64BIT"
12222   "* return output_set_got (operands[0], NULL_RTX);"
12223   [(set_attr "type" "multi")
12224    (set_attr "length" "12")])
12225
12226 (define_insn "set_got_labelled"
12227   [(set (match_operand:SI 0 "register_operand" "=r")
12228         (unspec:SI [(label_ref (match_operand 1))]
12229          UNSPEC_SET_GOT))
12230    (clobber (reg:CC FLAGS_REG))]
12231   "!TARGET_64BIT"
12232   "* return output_set_got (operands[0], operands[1]);"
12233   [(set_attr "type" "multi")
12234    (set_attr "length" "12")])
12235
12236 (define_insn "set_got_rex64"
12237   [(set (match_operand:DI 0 "register_operand" "=r")
12238         (unspec:DI [(const_int 0)] UNSPEC_SET_GOT))]
12239   "TARGET_64BIT"
12240   "lea{q}\t{_GLOBAL_OFFSET_TABLE_(%%rip), %0|%0, _GLOBAL_OFFSET_TABLE_[rip]}"
12241   [(set_attr "type" "lea")
12242    (set_attr "length_address" "4")
12243    (set_attr "mode" "DI")])
12244
12245 (define_insn "set_rip_rex64"
12246   [(set (match_operand:DI 0 "register_operand" "=r")
12247         (unspec:DI [(label_ref (match_operand 1))] UNSPEC_SET_RIP))]
12248   "TARGET_64BIT"
12249   "lea{q}\t{%l1(%%rip), %0|%0, %l1[rip]}"
12250   [(set_attr "type" "lea")
12251    (set_attr "length_address" "4")
12252    (set_attr "mode" "DI")])
12253
12254 (define_insn "set_got_offset_rex64"
12255   [(set (match_operand:DI 0 "register_operand" "=r")
12256         (unspec:DI
12257           [(label_ref (match_operand 1))]
12258           UNSPEC_SET_GOT_OFFSET))]
12259   "TARGET_LP64"
12260   "movabs{q}\t{$_GLOBAL_OFFSET_TABLE_-%l1, %0|%0, OFFSET FLAT:_GLOBAL_OFFSET_TABLE_-%l1}"
12261   [(set_attr "type" "imov")
12262    (set_attr "length_immediate" "0")
12263    (set_attr "length_address" "8")
12264    (set_attr "mode" "DI")])
12265
12266 (define_expand "epilogue"
12267   [(const_int 0)]
12268   ""
12269   "ix86_expand_epilogue (1); DONE;")
12270
12271 (define_expand "sibcall_epilogue"
12272   [(const_int 0)]
12273   ""
12274   "ix86_expand_epilogue (0); DONE;")
12275
12276 (define_expand "eh_return"
12277   [(use (match_operand 0 "register_operand"))]
12278   ""
12279 {
12280   rtx tmp, sa = EH_RETURN_STACKADJ_RTX, ra = operands[0];
12281
12282   /* Tricky bit: we write the address of the handler to which we will
12283      be returning into someone else's stack frame, one word below the
12284      stack address we wish to restore.  */
12285   tmp = gen_rtx_PLUS (Pmode, arg_pointer_rtx, sa);
12286   tmp = plus_constant (Pmode, tmp, -UNITS_PER_WORD);
12287   tmp = gen_rtx_MEM (Pmode, tmp);
12288   emit_move_insn (tmp, ra);
12289
12290   emit_jump_insn (gen_eh_return_internal ());
12291   emit_barrier ();
12292   DONE;
12293 })
12294
12295 (define_insn_and_split "eh_return_internal"
12296   [(eh_return)]
12297   ""
12298   "#"
12299   "epilogue_completed"
12300   [(const_int 0)]
12301   "ix86_expand_epilogue (2); DONE;")
12302
12303 (define_insn "leave"
12304   [(set (reg:SI SP_REG) (plus:SI (reg:SI BP_REG) (const_int 4)))
12305    (set (reg:SI BP_REG) (mem:SI (reg:SI BP_REG)))
12306    (clobber (mem:BLK (scratch)))]
12307   "!TARGET_64BIT"
12308   "leave"
12309   [(set_attr "type" "leave")])
12310
12311 (define_insn "leave_rex64"
12312   [(set (reg:DI SP_REG) (plus:DI (reg:DI BP_REG) (const_int 8)))
12313    (set (reg:DI BP_REG) (mem:DI (reg:DI BP_REG)))
12314    (clobber (mem:BLK (scratch)))]
12315   "TARGET_64BIT"
12316   "leave"
12317   [(set_attr "type" "leave")])
12318 \f
12319 ;; Handle -fsplit-stack.
12320
12321 (define_expand "split_stack_prologue"
12322   [(const_int 0)]
12323   ""
12324 {
12325   ix86_expand_split_stack_prologue ();
12326   DONE;
12327 })
12328
12329 ;; In order to support the call/return predictor, we use a return
12330 ;; instruction which the middle-end doesn't see.
12331 (define_insn "split_stack_return"
12332   [(unspec_volatile [(match_operand:SI 0 "const_int_operand")]
12333                      UNSPECV_SPLIT_STACK_RETURN)]
12334   ""
12335 {
12336   if (operands[0] == const0_rtx)
12337     return "ret";
12338   else
12339     return "ret\t%0";
12340 }
12341   [(set_attr "atom_unit" "jeu")
12342    (set_attr "modrm" "0")
12343    (set (attr "length")
12344         (if_then_else (match_operand:SI 0 "const0_operand")
12345                       (const_int 1)
12346                       (const_int 3)))
12347    (set (attr "length_immediate")
12348         (if_then_else (match_operand:SI 0 "const0_operand")
12349                       (const_int 0)
12350                       (const_int 2)))])
12351
12352 ;; If there are operand 0 bytes available on the stack, jump to
12353 ;; operand 1.
12354
12355 (define_expand "split_stack_space_check"
12356   [(set (pc) (if_then_else
12357               (ltu (minus (reg SP_REG)
12358                           (match_operand 0 "register_operand"))
12359                    (unspec [(const_int 0)] UNSPEC_STACK_CHECK))
12360               (label_ref (match_operand 1))
12361               (pc)))]
12362   ""
12363 {
12364   rtx reg, size, limit;
12365
12366   reg = gen_reg_rtx (Pmode);
12367   size = force_reg (Pmode, operands[0]);
12368   emit_insn (gen_sub3_insn (reg, stack_pointer_rtx, size));
12369   limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
12370                           UNSPEC_STACK_CHECK);
12371   limit = gen_rtx_MEM (Pmode, gen_rtx_CONST (Pmode, limit));
12372   ix86_expand_branch (GEU, reg, limit, operands[1]);
12373
12374   DONE;
12375 })
12376 \f
12377 ;; Bit manipulation instructions.
12378
12379 (define_expand "ffs<mode>2"
12380   [(set (match_dup 2) (const_int -1))
12381    (parallel [(set (match_dup 3) (match_dup 4))
12382               (set (match_operand:SWI48 0 "register_operand")
12383                    (ctz:SWI48
12384                      (match_operand:SWI48 1 "nonimmediate_operand")))])
12385    (set (match_dup 0) (if_then_else:SWI48
12386                         (eq (match_dup 3) (const_int 0))
12387                         (match_dup 2)
12388                         (match_dup 0)))
12389    (parallel [(set (match_dup 0) (plus:SWI48 (match_dup 0) (const_int 1)))
12390               (clobber (reg:CC FLAGS_REG))])]
12391   ""
12392 {
12393   machine_mode flags_mode;
12394
12395   if (<MODE>mode == SImode && !TARGET_CMOVE)
12396     {
12397       emit_insn (gen_ffssi2_no_cmove (operands[0], operands [1]));
12398       DONE;
12399     }
12400
12401   flags_mode
12402     = (TARGET_BMI && !TARGET_AVOID_FALSE_DEP_FOR_BMI) ? CCCmode : CCZmode;
12403
12404   operands[2] = gen_reg_rtx (<MODE>mode);
12405   operands[3] = gen_rtx_REG (flags_mode, FLAGS_REG);
12406   operands[4] = gen_rtx_COMPARE (flags_mode, operands[1], const0_rtx);
12407 })
12408
12409 (define_insn_and_split "ffssi2_no_cmove"
12410   [(set (match_operand:SI 0 "register_operand" "=r")
12411         (ffs:SI (match_operand:SI 1 "nonimmediate_operand" "rm")))
12412    (clobber (match_scratch:SI 2 "=&q"))
12413    (clobber (reg:CC FLAGS_REG))]
12414   "!TARGET_CMOVE"
12415   "#"
12416   "&& reload_completed"
12417   [(parallel [(set (match_dup 4) (match_dup 5))
12418               (set (match_dup 0) (ctz:SI (match_dup 1)))])
12419    (set (strict_low_part (match_dup 3))
12420         (eq:QI (match_dup 4) (const_int 0)))
12421    (parallel [(set (match_dup 2) (neg:SI (match_dup 2)))
12422               (clobber (reg:CC FLAGS_REG))])
12423    (parallel [(set (match_dup 0) (ior:SI (match_dup 0) (match_dup 2)))
12424               (clobber (reg:CC FLAGS_REG))])
12425    (parallel [(set (match_dup 0) (plus:SI (match_dup 0) (const_int 1)))
12426               (clobber (reg:CC FLAGS_REG))])]
12427 {
12428   machine_mode flags_mode
12429     = (TARGET_BMI && !TARGET_AVOID_FALSE_DEP_FOR_BMI) ? CCCmode : CCZmode;
12430
12431   operands[3] = gen_lowpart (QImode, operands[2]);
12432   operands[4] = gen_rtx_REG (flags_mode, FLAGS_REG);
12433   operands[5] = gen_rtx_COMPARE (flags_mode, operands[1], const0_rtx);
12434
12435   ix86_expand_clear (operands[2]);
12436 })
12437
12438 (define_insn "*tzcnt<mode>_1"
12439   [(set (reg:CCC FLAGS_REG)
12440         (compare:CCC (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12441                      (const_int 0)))
12442    (set (match_operand:SWI48 0 "register_operand" "=r")
12443         (ctz:SWI48 (match_dup 1)))]
12444   "TARGET_BMI && !TARGET_AVOID_FALSE_DEP_FOR_BMI"
12445   "tzcnt{<imodesuffix>}\t{%1, %0|%0, %1}"
12446   [(set_attr "type" "alu1")
12447    (set_attr "prefix_0f" "1")
12448    (set_attr "prefix_rep" "1")
12449    (set_attr "btver2_decode" "double")
12450    (set_attr "mode" "<MODE>")])
12451
12452 (define_insn "*bsf<mode>_1"
12453   [(set (reg:CCZ FLAGS_REG)
12454         (compare:CCZ (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12455                      (const_int 0)))
12456    (set (match_operand:SWI48 0 "register_operand" "=r")
12457         (ctz:SWI48 (match_dup 1)))]
12458   ""
12459   "bsf{<imodesuffix>}\t{%1, %0|%0, %1}"
12460   [(set_attr "type" "alu1")
12461    (set_attr "prefix_0f" "1")
12462    (set_attr "btver2_decode" "double")
12463    (set_attr "mode" "<MODE>")])
12464
12465 (define_expand "ctz<mode>2"
12466   [(parallel
12467     [(set (match_operand:SWI248 0 "register_operand")
12468           (ctz:SWI248
12469             (match_operand:SWI248 1 "nonimmediate_operand")))
12470      (clobber (reg:CC FLAGS_REG))])])
12471
12472 ; False dependency happens when destination is only updated by tzcnt,
12473 ; lzcnt or popcnt.  There is no false dependency when destination is
12474 ; also used in source.
12475 (define_insn_and_split "*ctz<mode>2_falsedep_1"
12476   [(set (match_operand:SWI48 0 "register_operand" "=r")
12477         (ctz:SWI48
12478           (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12479    (clobber (reg:CC FLAGS_REG))]
12480   "(TARGET_BMI || TARGET_GENERIC)
12481    && TARGET_AVOID_FALSE_DEP_FOR_BMI && optimize_function_for_speed_p (cfun)"
12482   "#"
12483   "&& reload_completed"
12484   [(parallel
12485     [(set (match_dup 0)
12486           (ctz:SWI48 (match_dup 1)))
12487      (unspec [(match_dup 0)] UNSPEC_INSN_FALSE_DEP)
12488      (clobber (reg:CC FLAGS_REG))])]
12489 {
12490   if (!reg_mentioned_p (operands[0], operands[1]))
12491     ix86_expand_clear (operands[0]);
12492 })
12493
12494 (define_insn "*ctz<mode>2_falsedep"
12495   [(set (match_operand:SWI48 0 "register_operand" "=r")
12496         (ctz:SWI48
12497           (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12498    (unspec [(match_operand:SWI48 2 "register_operand" "0")]
12499            UNSPEC_INSN_FALSE_DEP)
12500    (clobber (reg:CC FLAGS_REG))]
12501   ""
12502 {
12503   if (TARGET_BMI)
12504     return "tzcnt{<imodesuffix>}\t{%1, %0|%0, %1}";
12505   else if (TARGET_GENERIC)
12506     /* tzcnt expands to 'rep bsf' and we can use it even if !TARGET_BMI.  */
12507     return "rep%; bsf{<imodesuffix>}\t{%1, %0|%0, %1}";
12508   else
12509     gcc_unreachable ();
12510 }
12511   [(set_attr "type" "alu1")
12512    (set_attr "prefix_0f" "1")
12513    (set_attr "prefix_rep" "1")
12514    (set_attr "mode" "<MODE>")])
12515
12516 (define_insn "*ctz<mode>2"
12517   [(set (match_operand:SWI248 0 "register_operand" "=r")
12518         (ctz:SWI248 (match_operand:SWI248 1 "nonimmediate_operand" "rm")))
12519    (clobber (reg:CC FLAGS_REG))]
12520   ""
12521 {
12522   if (TARGET_BMI)
12523     return "tzcnt{<imodesuffix>}\t{%1, %0|%0, %1}";
12524   else if (optimize_function_for_size_p (cfun))
12525     ;
12526   else if (TARGET_GENERIC)
12527     /* tzcnt expands to 'rep bsf' and we can use it even if !TARGET_BMI.  */
12528     return "rep%; bsf{<imodesuffix>}\t{%1, %0|%0, %1}";
12529
12530   return "bsf{<imodesuffix>}\t{%1, %0|%0, %1}";
12531 }
12532   [(set_attr "type" "alu1")
12533    (set_attr "prefix_0f" "1")
12534    (set (attr "prefix_rep")
12535      (if_then_else
12536        (ior (match_test "TARGET_BMI")
12537             (and (not (match_test "optimize_function_for_size_p (cfun)"))
12538                  (match_test "TARGET_GENERIC")))
12539        (const_string "1")
12540        (const_string "0")))
12541    (set_attr "mode" "<MODE>")])
12542
12543 (define_expand "clz<mode>2"
12544   [(parallel
12545      [(set (match_operand:SWI248 0 "register_operand")
12546            (minus:SWI248
12547              (match_dup 2)
12548              (clz:SWI248 (match_operand:SWI248 1 "nonimmediate_operand"))))
12549       (clobber (reg:CC FLAGS_REG))])
12550    (parallel
12551      [(set (match_dup 0) (xor:SWI248 (match_dup 0) (match_dup 2)))
12552       (clobber (reg:CC FLAGS_REG))])]
12553   ""
12554 {
12555   if (TARGET_LZCNT)
12556     {
12557       emit_insn (gen_clz<mode>2_lzcnt (operands[0], operands[1]));
12558       DONE;
12559     }
12560   operands[2] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode)-1);
12561 })
12562
12563 (define_expand "clz<mode>2_lzcnt"
12564   [(parallel
12565     [(set (match_operand:SWI248 0 "register_operand")
12566           (clz:SWI248
12567             (match_operand:SWI248 1 "nonimmediate_operand")))
12568      (clobber (reg:CC FLAGS_REG))])]
12569   "TARGET_LZCNT")
12570
12571 (define_insn_and_split "*clz<mode>2_lzcnt_falsedep_1"
12572   [(set (match_operand:SWI48 0 "register_operand" "=r")
12573         (clz:SWI48
12574           (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12575    (clobber (reg:CC FLAGS_REG))]
12576   "TARGET_LZCNT
12577    && TARGET_AVOID_FALSE_DEP_FOR_BMI && optimize_function_for_speed_p (cfun)"
12578   "#"
12579   "&& reload_completed"
12580   [(parallel
12581     [(set (match_dup 0)
12582           (clz:SWI48 (match_dup 1)))
12583      (unspec [(match_dup 0)] UNSPEC_INSN_FALSE_DEP)
12584      (clobber (reg:CC FLAGS_REG))])]
12585 {
12586   if (!reg_mentioned_p (operands[0], operands[1]))
12587     ix86_expand_clear (operands[0]);
12588 })
12589
12590 (define_insn "*clz<mode>2_lzcnt_falsedep"
12591   [(set (match_operand:SWI48 0 "register_operand" "=r")
12592         (clz:SWI48
12593           (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12594    (unspec [(match_operand:SWI48 2 "register_operand" "0")]
12595            UNSPEC_INSN_FALSE_DEP)
12596    (clobber (reg:CC FLAGS_REG))]
12597   "TARGET_LZCNT"
12598   "lzcnt{<imodesuffix>}\t{%1, %0|%0, %1}"
12599   [(set_attr "prefix_rep" "1")
12600    (set_attr "type" "bitmanip")
12601    (set_attr "mode" "<MODE>")])
12602
12603 (define_insn "*clz<mode>2_lzcnt"
12604   [(set (match_operand:SWI248 0 "register_operand" "=r")
12605         (clz:SWI248 (match_operand:SWI248 1 "nonimmediate_operand" "rm")))
12606    (clobber (reg:CC FLAGS_REG))]
12607   "TARGET_LZCNT"
12608   "lzcnt{<imodesuffix>}\t{%1, %0|%0, %1}"
12609   [(set_attr "prefix_rep" "1")
12610    (set_attr "type" "bitmanip")
12611    (set_attr "mode" "<MODE>")])
12612
12613 ;; BMI instructions.
12614 (define_insn "*bmi_andn_<mode>"
12615   [(set (match_operand:SWI48 0 "register_operand" "=r,r")
12616         (and:SWI48
12617           (not:SWI48
12618             (match_operand:SWI48 1 "register_operand" "r,r"))
12619             (match_operand:SWI48 2 "nonimmediate_operand" "r,m")))
12620    (clobber (reg:CC FLAGS_REG))]
12621   "TARGET_BMI"
12622   "andn\t{%2, %1, %0|%0, %1, %2}"
12623   [(set_attr "type" "bitmanip")
12624    (set_attr "btver2_decode" "direct, double")
12625    (set_attr "mode" "<MODE>")])
12626
12627 (define_insn "bmi_bextr_<mode>"
12628   [(set (match_operand:SWI48 0 "register_operand" "=r,r")
12629         (unspec:SWI48 [(match_operand:SWI48 1 "nonimmediate_operand" "r,m")
12630                        (match_operand:SWI48 2 "register_operand" "r,r")]
12631                        UNSPEC_BEXTR))
12632    (clobber (reg:CC FLAGS_REG))]
12633   "TARGET_BMI"
12634   "bextr\t{%2, %1, %0|%0, %1, %2}"
12635   [(set_attr "type" "bitmanip")
12636    (set_attr "btver2_decode" "direct, double")
12637    (set_attr "mode" "<MODE>")])
12638
12639 (define_insn "*bmi_blsi_<mode>"
12640   [(set (match_operand:SWI48 0 "register_operand" "=r")
12641         (and:SWI48
12642           (neg:SWI48
12643             (match_operand:SWI48 1 "nonimmediate_operand" "rm"))
12644           (match_dup 1)))
12645    (clobber (reg:CC FLAGS_REG))]
12646   "TARGET_BMI"
12647   "blsi\t{%1, %0|%0, %1}"
12648   [(set_attr "type" "bitmanip")
12649    (set_attr "btver2_decode" "double")
12650    (set_attr "mode" "<MODE>")])
12651
12652 (define_insn "*bmi_blsmsk_<mode>"
12653   [(set (match_operand:SWI48 0 "register_operand" "=r")
12654         (xor:SWI48
12655           (plus:SWI48
12656             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12657             (const_int -1))
12658           (match_dup 1)))
12659    (clobber (reg:CC FLAGS_REG))]
12660   "TARGET_BMI"
12661   "blsmsk\t{%1, %0|%0, %1}"
12662   [(set_attr "type" "bitmanip")
12663    (set_attr "btver2_decode" "double")
12664    (set_attr "mode" "<MODE>")])
12665
12666 (define_insn "*bmi_blsr_<mode>"
12667   [(set (match_operand:SWI48 0 "register_operand" "=r")
12668         (and:SWI48
12669           (plus:SWI48
12670             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12671             (const_int -1))
12672           (match_dup 1)))
12673    (clobber (reg:CC FLAGS_REG))]
12674    "TARGET_BMI"
12675    "blsr\t{%1, %0|%0, %1}"
12676   [(set_attr "type" "bitmanip")
12677    (set_attr "btver2_decode" "double")
12678    (set_attr "mode" "<MODE>")])
12679
12680 ;; BMI2 instructions.
12681 (define_insn "bmi2_bzhi_<mode>3"
12682   [(set (match_operand:SWI48 0 "register_operand" "=r")
12683         (and:SWI48 (lshiftrt:SWI48 (const_int -1)
12684                                    (match_operand:SWI48 2 "register_operand" "r"))
12685                    (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12686    (clobber (reg:CC FLAGS_REG))]
12687   "TARGET_BMI2"
12688   "bzhi\t{%2, %1, %0|%0, %1, %2}"
12689   [(set_attr "type" "bitmanip")
12690    (set_attr "prefix" "vex")
12691    (set_attr "mode" "<MODE>")])
12692
12693 (define_insn "bmi2_pdep_<mode>3"
12694   [(set (match_operand:SWI48 0 "register_operand" "=r")
12695         (unspec:SWI48 [(match_operand:SWI48 1 "register_operand" "r")
12696                        (match_operand:SWI48 2 "nonimmediate_operand" "rm")]
12697                        UNSPEC_PDEP))]
12698   "TARGET_BMI2"
12699   "pdep\t{%2, %1, %0|%0, %1, %2}"
12700   [(set_attr "type" "bitmanip")
12701    (set_attr "prefix" "vex")
12702    (set_attr "mode" "<MODE>")])
12703
12704 (define_insn "bmi2_pext_<mode>3"
12705   [(set (match_operand:SWI48 0 "register_operand" "=r")
12706         (unspec:SWI48 [(match_operand:SWI48 1 "register_operand" "r")
12707                        (match_operand:SWI48 2 "nonimmediate_operand" "rm")]
12708                        UNSPEC_PEXT))]
12709   "TARGET_BMI2"
12710   "pext\t{%2, %1, %0|%0, %1, %2}"
12711   [(set_attr "type" "bitmanip")
12712    (set_attr "prefix" "vex")
12713    (set_attr "mode" "<MODE>")])
12714
12715 ;; TBM instructions.
12716 (define_insn "tbm_bextri_<mode>"
12717   [(set (match_operand:SWI48 0 "register_operand" "=r")
12718         (zero_extract:SWI48
12719           (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12720           (match_operand:SWI48 2 "const_0_to_255_operand" "n")
12721           (match_operand:SWI48 3 "const_0_to_255_operand" "n")))
12722    (clobber (reg:CC FLAGS_REG))]
12723    "TARGET_TBM"
12724 {
12725   operands[2] = GEN_INT (INTVAL (operands[2]) << 8 | INTVAL (operands[3]));
12726   return "bextr\t{%2, %1, %0|%0, %1, %2}";
12727 }
12728   [(set_attr "type" "bitmanip")
12729    (set_attr "mode" "<MODE>")])
12730
12731 (define_insn "*tbm_blcfill_<mode>"
12732   [(set (match_operand:SWI48 0 "register_operand" "=r")
12733         (and:SWI48
12734           (plus:SWI48
12735             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12736             (const_int 1))
12737           (match_dup 1)))
12738    (clobber (reg:CC FLAGS_REG))]
12739    "TARGET_TBM"
12740    "blcfill\t{%1, %0|%0, %1}"
12741   [(set_attr "type" "bitmanip")
12742    (set_attr "mode" "<MODE>")])
12743
12744 (define_insn "*tbm_blci_<mode>"
12745   [(set (match_operand:SWI48 0 "register_operand" "=r")
12746         (ior:SWI48
12747           (not:SWI48
12748             (plus:SWI48
12749               (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12750               (const_int 1)))
12751           (match_dup 1)))
12752    (clobber (reg:CC FLAGS_REG))]
12753    "TARGET_TBM"
12754    "blci\t{%1, %0|%0, %1}"
12755   [(set_attr "type" "bitmanip")
12756    (set_attr "mode" "<MODE>")])
12757
12758 (define_insn "*tbm_blcic_<mode>"
12759   [(set (match_operand:SWI48 0 "register_operand" "=r")
12760         (and:SWI48
12761           (plus:SWI48
12762             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12763             (const_int 1))
12764           (not:SWI48
12765             (match_dup 1))))
12766    (clobber (reg:CC FLAGS_REG))]
12767    "TARGET_TBM"
12768    "blcic\t{%1, %0|%0, %1}"
12769   [(set_attr "type" "bitmanip")
12770    (set_attr "mode" "<MODE>")])
12771
12772 (define_insn "*tbm_blcmsk_<mode>"
12773   [(set (match_operand:SWI48 0 "register_operand" "=r")
12774         (xor:SWI48
12775           (plus:SWI48
12776             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12777             (const_int 1))
12778           (match_dup 1)))
12779    (clobber (reg:CC FLAGS_REG))]
12780    "TARGET_TBM"
12781    "blcmsk\t{%1, %0|%0, %1}"
12782   [(set_attr "type" "bitmanip")
12783    (set_attr "mode" "<MODE>")])
12784
12785 (define_insn "*tbm_blcs_<mode>"
12786   [(set (match_operand:SWI48 0 "register_operand" "=r")
12787         (ior:SWI48
12788           (plus:SWI48
12789             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12790             (const_int 1))
12791           (match_dup 1)))
12792    (clobber (reg:CC FLAGS_REG))]
12793    "TARGET_TBM"
12794    "blcs\t{%1, %0|%0, %1}"
12795   [(set_attr "type" "bitmanip")
12796    (set_attr "mode" "<MODE>")])
12797
12798 (define_insn "*tbm_blsfill_<mode>"
12799   [(set (match_operand:SWI48 0 "register_operand" "=r")
12800         (ior:SWI48
12801           (plus:SWI48
12802             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12803             (const_int -1))
12804           (match_dup 1)))
12805    (clobber (reg:CC FLAGS_REG))]
12806    "TARGET_TBM"
12807    "blsfill\t{%1, %0|%0, %1}"
12808   [(set_attr "type" "bitmanip")
12809    (set_attr "mode" "<MODE>")])
12810
12811 (define_insn "*tbm_blsic_<mode>"
12812   [(set (match_operand:SWI48 0 "register_operand" "=r")
12813         (ior:SWI48
12814           (plus:SWI48
12815             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12816             (const_int -1))
12817           (not:SWI48
12818             (match_dup 1))))
12819    (clobber (reg:CC FLAGS_REG))]
12820    "TARGET_TBM"
12821    "blsic\t{%1, %0|%0, %1}"
12822   [(set_attr "type" "bitmanip")
12823    (set_attr "mode" "<MODE>")])
12824
12825 (define_insn "*tbm_t1mskc_<mode>"
12826   [(set (match_operand:SWI48 0 "register_operand" "=r")
12827         (ior:SWI48
12828           (plus:SWI48
12829             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12830             (const_int 1))
12831           (not:SWI48
12832             (match_dup 1))))
12833    (clobber (reg:CC FLAGS_REG))]
12834    "TARGET_TBM"
12835    "t1mskc\t{%1, %0|%0, %1}"
12836   [(set_attr "type" "bitmanip")
12837    (set_attr "mode" "<MODE>")])
12838
12839 (define_insn "*tbm_tzmsk_<mode>"
12840   [(set (match_operand:SWI48 0 "register_operand" "=r")
12841         (and:SWI48
12842           (plus:SWI48
12843             (match_operand:SWI48 1 "nonimmediate_operand" "rm")
12844             (const_int -1))
12845           (not:SWI48
12846             (match_dup 1))))
12847    (clobber (reg:CC FLAGS_REG))]
12848    "TARGET_TBM"
12849    "tzmsk\t{%1, %0|%0, %1}"
12850   [(set_attr "type" "bitmanip")
12851    (set_attr "mode" "<MODE>")])
12852
12853 (define_insn "bsr_rex64"
12854   [(set (match_operand:DI 0 "register_operand" "=r")
12855         (minus:DI (const_int 63)
12856                   (clz:DI (match_operand:DI 1 "nonimmediate_operand" "rm"))))
12857    (clobber (reg:CC FLAGS_REG))]
12858   "TARGET_64BIT"
12859   "bsr{q}\t{%1, %0|%0, %1}"
12860   [(set_attr "type" "alu1")
12861    (set_attr "prefix_0f" "1")
12862    (set_attr "mode" "DI")])
12863
12864 (define_insn "bsr"
12865   [(set (match_operand:SI 0 "register_operand" "=r")
12866         (minus:SI (const_int 31)
12867                   (clz:SI (match_operand:SI 1 "nonimmediate_operand" "rm"))))
12868    (clobber (reg:CC FLAGS_REG))]
12869   ""
12870   "bsr{l}\t{%1, %0|%0, %1}"
12871   [(set_attr "type" "alu1")
12872    (set_attr "prefix_0f" "1")
12873    (set_attr "mode" "SI")])
12874
12875 (define_insn "*bsrhi"
12876   [(set (match_operand:HI 0 "register_operand" "=r")
12877         (minus:HI (const_int 15)
12878                   (clz:HI (match_operand:HI 1 "nonimmediate_operand" "rm"))))
12879    (clobber (reg:CC FLAGS_REG))]
12880   ""
12881   "bsr{w}\t{%1, %0|%0, %1}"
12882   [(set_attr "type" "alu1")
12883    (set_attr "prefix_0f" "1")
12884    (set_attr "mode" "HI")])
12885
12886 (define_expand "popcount<mode>2"
12887   [(parallel
12888     [(set (match_operand:SWI248 0 "register_operand")
12889           (popcount:SWI248
12890             (match_operand:SWI248 1 "nonimmediate_operand")))
12891      (clobber (reg:CC FLAGS_REG))])]
12892   "TARGET_POPCNT")
12893
12894 (define_insn_and_split "*popcount<mode>2_falsedep_1"
12895   [(set (match_operand:SWI48 0 "register_operand" "=r")
12896         (popcount:SWI48
12897           (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12898    (clobber (reg:CC FLAGS_REG))]
12899   "TARGET_POPCNT
12900    && TARGET_AVOID_FALSE_DEP_FOR_BMI && optimize_function_for_speed_p (cfun)"
12901   "#"
12902   "&& reload_completed"
12903   [(parallel
12904     [(set (match_dup 0)
12905           (popcount:SWI48 (match_dup 1)))
12906      (unspec [(match_dup 0)] UNSPEC_INSN_FALSE_DEP)
12907      (clobber (reg:CC FLAGS_REG))])]
12908 {
12909   if (!reg_mentioned_p (operands[0], operands[1]))
12910     ix86_expand_clear (operands[0]);
12911 })
12912
12913 (define_insn "*popcount<mode>2_falsedep"
12914   [(set (match_operand:SWI48 0 "register_operand" "=r")
12915         (popcount:SWI48
12916           (match_operand:SWI48 1 "nonimmediate_operand" "rm")))
12917    (unspec [(match_operand:SWI48 2 "register_operand" "0")]
12918            UNSPEC_INSN_FALSE_DEP)
12919    (clobber (reg:CC FLAGS_REG))]
12920   "TARGET_POPCNT"
12921 {
12922 #if TARGET_MACHO
12923   return "popcnt\t{%1, %0|%0, %1}";
12924 #else
12925   return "popcnt{<imodesuffix>}\t{%1, %0|%0, %1}";
12926 #endif
12927 }
12928   [(set_attr "prefix_rep" "1")
12929    (set_attr "type" "bitmanip")
12930    (set_attr "mode" "<MODE>")])
12931
12932 (define_insn "*popcount<mode>2"
12933   [(set (match_operand:SWI248 0 "register_operand" "=r")
12934         (popcount:SWI248
12935           (match_operand:SWI248 1 "nonimmediate_operand" "rm")))
12936    (clobber (reg:CC FLAGS_REG))]
12937   "TARGET_POPCNT"
12938 {
12939 #if TARGET_MACHO
12940   return "popcnt\t{%1, %0|%0, %1}";
12941 #else
12942   return "popcnt{<imodesuffix>}\t{%1, %0|%0, %1}";
12943 #endif
12944 }
12945   [(set_attr "prefix_rep" "1")
12946    (set_attr "type" "bitmanip")
12947    (set_attr "mode" "<MODE>")])
12948
12949 (define_expand "bswapdi2"
12950   [(set (match_operand:DI 0 "register_operand")
12951         (bswap:DI (match_operand:DI 1 "nonimmediate_operand")))]
12952   "TARGET_64BIT"
12953 {
12954   if (!TARGET_MOVBE)
12955     operands[1] = force_reg (DImode, operands[1]);
12956 })
12957
12958 (define_expand "bswapsi2"
12959   [(set (match_operand:SI 0 "register_operand")
12960         (bswap:SI (match_operand:SI 1 "nonimmediate_operand")))]
12961   ""
12962 {
12963   if (TARGET_MOVBE)
12964     ;
12965   else if (TARGET_BSWAP)
12966     operands[1] = force_reg (SImode, operands[1]);
12967   else
12968     {
12969       rtx x = operands[0];
12970
12971       emit_move_insn (x, operands[1]);
12972       emit_insn (gen_bswaphi_lowpart (gen_lowpart (HImode, x)));
12973       emit_insn (gen_rotlsi3 (x, x, GEN_INT (16)));
12974       emit_insn (gen_bswaphi_lowpart (gen_lowpart (HImode, x)));
12975       DONE;
12976     }
12977 })
12978
12979 (define_insn "*bswap<mode>2_movbe"
12980   [(set (match_operand:SWI48 0 "nonimmediate_operand" "=r,r,m")
12981         (bswap:SWI48 (match_operand:SWI48 1 "nonimmediate_operand" "0,m,r")))]
12982   "TARGET_MOVBE
12983    && !(MEM_P (operands[0]) && MEM_P (operands[1]))"
12984   "@
12985     bswap\t%0
12986     movbe\t{%1, %0|%0, %1}
12987     movbe\t{%1, %0|%0, %1}"
12988   [(set_attr "type" "bitmanip,imov,imov")
12989    (set_attr "modrm" "0,1,1")
12990    (set_attr "prefix_0f" "*,1,1")
12991    (set_attr "prefix_extra" "*,1,1")
12992    (set_attr "mode" "<MODE>")])
12993
12994 (define_insn "*bswap<mode>2"
12995   [(set (match_operand:SWI48 0 "register_operand" "=r")
12996         (bswap:SWI48 (match_operand:SWI48 1 "register_operand" "0")))]
12997   "TARGET_BSWAP"
12998   "bswap\t%0"
12999   [(set_attr "type" "bitmanip")
13000    (set_attr "modrm" "0")
13001    (set_attr "mode" "<MODE>")])
13002
13003 (define_insn "*bswaphi_lowpart_1"
13004   [(set (strict_low_part (match_operand:HI 0 "register_operand" "+Q,r"))
13005         (bswap:HI (match_dup 0)))
13006    (clobber (reg:CC FLAGS_REG))]
13007   "TARGET_USE_XCHGB || optimize_function_for_size_p (cfun)"
13008   "@
13009     xchg{b}\t{%h0, %b0|%b0, %h0}
13010     rol{w}\t{$8, %0|%0, 8}"
13011   [(set_attr "length" "2,4")
13012    (set_attr "mode" "QI,HI")])
13013
13014 (define_insn "bswaphi_lowpart"
13015   [(set (strict_low_part (match_operand:HI 0 "register_operand" "+r"))
13016         (bswap:HI (match_dup 0)))
13017    (clobber (reg:CC FLAGS_REG))]
13018   ""
13019   "rol{w}\t{$8, %0|%0, 8}"
13020   [(set_attr "length" "4")
13021    (set_attr "mode" "HI")])
13022
13023 (define_expand "paritydi2"
13024   [(set (match_operand:DI 0 "register_operand")
13025         (parity:DI (match_operand:DI 1 "register_operand")))]
13026   "! TARGET_POPCNT"
13027 {
13028   rtx scratch = gen_reg_rtx (QImode);
13029   rtx cond;
13030
13031   emit_insn (gen_paritydi2_cmp (NULL_RTX, NULL_RTX,
13032                                 NULL_RTX, operands[1]));
13033
13034   cond = gen_rtx_fmt_ee (ORDERED, QImode,
13035                          gen_rtx_REG (CCmode, FLAGS_REG),
13036                          const0_rtx);
13037   emit_insn (gen_rtx_SET (VOIDmode, scratch, cond));
13038
13039   if (TARGET_64BIT)
13040     emit_insn (gen_zero_extendqidi2 (operands[0], scratch));
13041   else
13042     {
13043       rtx tmp = gen_reg_rtx (SImode);
13044
13045       emit_insn (gen_zero_extendqisi2 (tmp, scratch));
13046       emit_insn (gen_zero_extendsidi2 (operands[0], tmp));
13047     }
13048   DONE;
13049 })
13050
13051 (define_expand "paritysi2"
13052   [(set (match_operand:SI 0 "register_operand")
13053         (parity:SI (match_operand:SI 1 "register_operand")))]
13054   "! TARGET_POPCNT"
13055 {
13056   rtx scratch = gen_reg_rtx (QImode);
13057   rtx cond;
13058
13059   emit_insn (gen_paritysi2_cmp (NULL_RTX, NULL_RTX, operands[1]));
13060
13061   cond = gen_rtx_fmt_ee (ORDERED, QImode,
13062                          gen_rtx_REG (CCmode, FLAGS_REG),
13063                          const0_rtx);
13064   emit_insn (gen_rtx_SET (VOIDmode, scratch, cond));
13065
13066   emit_insn (gen_zero_extendqisi2 (operands[0], scratch));
13067   DONE;
13068 })
13069
13070 (define_insn_and_split "paritydi2_cmp"
13071   [(set (reg:CC FLAGS_REG)
13072         (unspec:CC [(match_operand:DI 3 "register_operand" "0")]
13073                    UNSPEC_PARITY))
13074    (clobber (match_scratch:DI 0 "=r"))
13075    (clobber (match_scratch:SI 1 "=&r"))
13076    (clobber (match_scratch:HI 2 "=Q"))]
13077   "! TARGET_POPCNT"
13078   "#"
13079   "&& reload_completed"
13080   [(parallel
13081      [(set (match_dup 1)
13082            (xor:SI (match_dup 1) (match_dup 4)))
13083       (clobber (reg:CC FLAGS_REG))])
13084    (parallel
13085      [(set (reg:CC FLAGS_REG)
13086            (unspec:CC [(match_dup 1)] UNSPEC_PARITY))
13087       (clobber (match_dup 1))
13088       (clobber (match_dup 2))])]
13089 {
13090   operands[4] = gen_lowpart (SImode, operands[3]);
13091
13092   if (TARGET_64BIT)
13093     {
13094       emit_move_insn (operands[1], gen_lowpart (SImode, operands[3]));
13095       emit_insn (gen_lshrdi3 (operands[3], operands[3], GEN_INT (32)));
13096     }
13097   else
13098     operands[1] = gen_highpart (SImode, operands[3]);
13099 })
13100
13101 (define_insn_and_split "paritysi2_cmp"
13102   [(set (reg:CC FLAGS_REG)
13103         (unspec:CC [(match_operand:SI 2 "register_operand" "0")]
13104                    UNSPEC_PARITY))
13105    (clobber (match_scratch:SI 0 "=r"))
13106    (clobber (match_scratch:HI 1 "=&Q"))]
13107   "! TARGET_POPCNT"
13108   "#"
13109   "&& reload_completed"
13110   [(parallel
13111      [(set (match_dup 1)
13112            (xor:HI (match_dup 1) (match_dup 3)))
13113       (clobber (reg:CC FLAGS_REG))])
13114    (parallel
13115      [(set (reg:CC FLAGS_REG)
13116            (unspec:CC [(match_dup 1)] UNSPEC_PARITY))
13117       (clobber (match_dup 1))])]
13118 {
13119   operands[3] = gen_lowpart (HImode, operands[2]);
13120
13121   emit_move_insn (operands[1], gen_lowpart (HImode, operands[2]));
13122   emit_insn (gen_lshrsi3 (operands[2], operands[2], GEN_INT (16)));
13123 })
13124
13125 (define_insn "*parityhi2_cmp"
13126   [(set (reg:CC FLAGS_REG)
13127         (unspec:CC [(match_operand:HI 1 "register_operand" "0")]
13128                    UNSPEC_PARITY))
13129    (clobber (match_scratch:HI 0 "=Q"))]
13130   "! TARGET_POPCNT"
13131   "xor{b}\t{%h0, %b0|%b0, %h0}"
13132   [(set_attr "length" "2")
13133    (set_attr "mode" "HI")])
13134
13135 \f
13136 ;; Thread-local storage patterns for ELF.
13137 ;;
13138 ;; Note that these code sequences must appear exactly as shown
13139 ;; in order to allow linker relaxation.
13140
13141 (define_insn "*tls_global_dynamic_32_gnu"
13142   [(set (match_operand:SI 0 "register_operand" "=a")
13143         (unspec:SI
13144          [(match_operand:SI 1 "register_operand" "b")
13145           (match_operand 2 "tls_symbolic_operand")
13146           (match_operand 3 "constant_call_address_operand" "Bz")
13147           (reg:SI SP_REG)]
13148          UNSPEC_TLS_GD))
13149    (clobber (match_scratch:SI 4 "=d"))
13150    (clobber (match_scratch:SI 5 "=c"))
13151    (clobber (reg:CC FLAGS_REG))]
13152   "!TARGET_64BIT && TARGET_GNU_TLS"
13153 {
13154   output_asm_insn
13155     ("lea{l}\t{%E2@tlsgd(,%1,1), %0|%0, %E2@tlsgd[%1*1]}", operands);
13156   if (TARGET_SUN_TLS)
13157 #ifdef HAVE_AS_IX86_TLSGDPLT
13158     return "call\t%a2@tlsgdplt";
13159 #else
13160     return "call\t%p3@plt";
13161 #endif
13162   return "call\t%P3";
13163 }
13164   [(set_attr "type" "multi")
13165    (set_attr "length" "12")])
13166
13167 (define_expand "tls_global_dynamic_32"
13168   [(parallel
13169     [(set (match_operand:SI 0 "register_operand")
13170           (unspec:SI [(match_operand:SI 2 "register_operand")
13171                       (match_operand 1 "tls_symbolic_operand")
13172                       (match_operand 3 "constant_call_address_operand")
13173                       (reg:SI SP_REG)]
13174                      UNSPEC_TLS_GD))
13175      (clobber (match_scratch:SI 4))
13176      (clobber (match_scratch:SI 5))
13177      (clobber (reg:CC FLAGS_REG))])]
13178   ""
13179   "ix86_tls_descriptor_calls_expanded_in_cfun = true;")
13180
13181 (define_insn "*tls_global_dynamic_64_<mode>"
13182   [(set (match_operand:P 0 "register_operand" "=a")
13183         (call:P
13184          (mem:QI (match_operand 2 "constant_call_address_operand" "Bz"))
13185          (match_operand 3)))
13186    (unspec:P [(match_operand 1 "tls_symbolic_operand")]
13187              UNSPEC_TLS_GD)]
13188   "TARGET_64BIT"
13189 {
13190   if (!TARGET_X32)
13191     fputs (ASM_BYTE "0x66\n", asm_out_file);
13192   output_asm_insn
13193     ("lea{q}\t{%E1@tlsgd(%%rip), %%rdi|rdi, %E1@tlsgd[rip]}", operands);
13194   fputs (ASM_SHORT "0x6666\n", asm_out_file);
13195   fputs ("\trex64\n", asm_out_file);
13196   if (TARGET_SUN_TLS)
13197     return "call\t%p2@plt";
13198   return "call\t%P2";
13199 }
13200   [(set_attr "type" "multi")
13201    (set (attr "length")
13202         (symbol_ref "TARGET_X32 ? 15 : 16"))])
13203
13204 (define_insn "*tls_global_dynamic_64_largepic"
13205   [(set (match_operand:DI 0 "register_operand" "=a")
13206         (call:DI
13207          (mem:QI (plus:DI (match_operand:DI 2 "register_operand" "b")
13208                           (match_operand:DI 3 "immediate_operand" "i")))
13209          (match_operand 4)))
13210    (unspec:DI [(match_operand 1 "tls_symbolic_operand")]
13211              UNSPEC_TLS_GD)]
13212   "TARGET_64BIT && ix86_cmodel == CM_LARGE_PIC && !TARGET_PECOFF
13213    && GET_CODE (operands[3]) == CONST
13214    && GET_CODE (XEXP (operands[3], 0)) == UNSPEC
13215    && XINT (XEXP (operands[3], 0), 1) == UNSPEC_PLTOFF"
13216 {
13217   output_asm_insn
13218     ("lea{q}\t{%E1@tlsgd(%%rip), %%rdi|rdi, %E1@tlsgd[rip]}", operands);
13219   output_asm_insn ("movabs{q}\t{%3, %%rax|rax, %3}", operands);
13220   output_asm_insn ("add{q}\t{%2, %%rax|rax, %2}", operands);
13221   return "call\t{*%%rax|rax}";
13222 }
13223   [(set_attr "type" "multi")
13224    (set_attr "length" "22")])
13225
13226 (define_expand "tls_global_dynamic_64_<mode>"
13227   [(parallel
13228     [(set (match_operand:P 0 "register_operand")
13229           (call:P
13230            (mem:QI (match_operand 2))
13231            (const_int 0)))
13232      (unspec:P [(match_operand 1 "tls_symbolic_operand")]
13233                UNSPEC_TLS_GD)])]
13234   "TARGET_64BIT"
13235   "ix86_tls_descriptor_calls_expanded_in_cfun = true;")
13236
13237 (define_insn "*tls_local_dynamic_base_32_gnu"
13238   [(set (match_operand:SI 0 "register_operand" "=a")
13239         (unspec:SI
13240          [(match_operand:SI 1 "register_operand" "b")
13241           (match_operand 2 "constant_call_address_operand" "Bz")
13242           (reg:SI SP_REG)]
13243          UNSPEC_TLS_LD_BASE))
13244    (clobber (match_scratch:SI 3 "=d"))
13245    (clobber (match_scratch:SI 4 "=c"))
13246    (clobber (reg:CC FLAGS_REG))]
13247   "!TARGET_64BIT && TARGET_GNU_TLS"
13248 {
13249   output_asm_insn
13250     ("lea{l}\t{%&@tlsldm(%1), %0|%0, %&@tlsldm[%1]}", operands);
13251   if (TARGET_SUN_TLS)
13252     {
13253       if (HAVE_AS_IX86_TLSLDMPLT)
13254         return "call\t%&@tlsldmplt";
13255       else
13256         return "call\t%p2@plt";
13257     }
13258   return "call\t%P2";
13259 }
13260   [(set_attr "type" "multi")
13261    (set_attr "length" "11")])
13262
13263 (define_expand "tls_local_dynamic_base_32"
13264   [(parallel
13265      [(set (match_operand:SI 0 "register_operand")
13266            (unspec:SI
13267             [(match_operand:SI 1 "register_operand")
13268              (match_operand 2 "constant_call_address_operand")
13269              (reg:SI SP_REG)]
13270             UNSPEC_TLS_LD_BASE))
13271       (clobber (match_scratch:SI 3))
13272       (clobber (match_scratch:SI 4))
13273       (clobber (reg:CC FLAGS_REG))])]
13274   ""
13275   "ix86_tls_descriptor_calls_expanded_in_cfun = true;")
13276
13277 (define_insn "*tls_local_dynamic_base_64_<mode>"
13278   [(set (match_operand:P 0 "register_operand" "=a")
13279         (call:P
13280          (mem:QI (match_operand 1 "constant_call_address_operand" "Bz"))
13281          (match_operand 2)))
13282    (unspec:P [(const_int 0)] UNSPEC_TLS_LD_BASE)]
13283   "TARGET_64BIT"
13284 {
13285   output_asm_insn
13286     ("lea{q}\t{%&@tlsld(%%rip), %%rdi|rdi, %&@tlsld[rip]}", operands);
13287   if (TARGET_SUN_TLS)
13288     return "call\t%p1@plt";
13289   return "call\t%P1";
13290 }
13291   [(set_attr "type" "multi")
13292    (set_attr "length" "12")])
13293
13294 (define_insn "*tls_local_dynamic_base_64_largepic"
13295   [(set (match_operand:DI 0 "register_operand" "=a")
13296         (call:DI
13297          (mem:QI (plus:DI (match_operand:DI 1 "register_operand" "b")
13298                           (match_operand:DI 2 "immediate_operand" "i")))
13299          (match_operand 3)))
13300    (unspec:DI [(const_int 0)] UNSPEC_TLS_LD_BASE)]
13301   "TARGET_64BIT && ix86_cmodel == CM_LARGE_PIC && !TARGET_PECOFF
13302    && GET_CODE (operands[2]) == CONST
13303    && GET_CODE (XEXP (operands[2], 0)) == UNSPEC
13304    && XINT (XEXP (operands[2], 0), 1) == UNSPEC_PLTOFF"
13305 {
13306   output_asm_insn
13307     ("lea{q}\t{%&@tlsld(%%rip), %%rdi|rdi, %&@tlsld[rip]}", operands);
13308   output_asm_insn ("movabs{q}\t{%2, %%rax|rax, %2}", operands);
13309   output_asm_insn ("add{q}\t{%1, %%rax|rax, %1}", operands);
13310   return "call\t{*%%rax|rax}";
13311 }
13312   [(set_attr "type" "multi")
13313    (set_attr "length" "22")])
13314
13315 (define_expand "tls_local_dynamic_base_64_<mode>"
13316   [(parallel
13317      [(set (match_operand:P 0 "register_operand")
13318            (call:P
13319             (mem:QI (match_operand 1))
13320             (const_int 0)))
13321       (unspec:P [(const_int 0)] UNSPEC_TLS_LD_BASE)])]
13322   "TARGET_64BIT"
13323   "ix86_tls_descriptor_calls_expanded_in_cfun = true;")
13324
13325 ;; Local dynamic of a single variable is a lose.  Show combine how
13326 ;; to convert that back to global dynamic.
13327
13328 (define_insn_and_split "*tls_local_dynamic_32_once"
13329   [(set (match_operand:SI 0 "register_operand" "=a")
13330         (plus:SI
13331          (unspec:SI [(match_operand:SI 1 "register_operand" "b")
13332                      (match_operand 2 "constant_call_address_operand" "Bz")
13333                      (reg:SI SP_REG)]
13334                     UNSPEC_TLS_LD_BASE)
13335          (const:SI (unspec:SI
13336                     [(match_operand 3 "tls_symbolic_operand")]
13337                     UNSPEC_DTPOFF))))
13338    (clobber (match_scratch:SI 4 "=d"))
13339    (clobber (match_scratch:SI 5 "=c"))
13340    (clobber (reg:CC FLAGS_REG))]
13341   ""
13342   "#"
13343   ""
13344   [(parallel
13345      [(set (match_dup 0)
13346            (unspec:SI [(match_dup 1) (match_dup 3) (match_dup 2)
13347                        (reg:SI SP_REG)]
13348                       UNSPEC_TLS_GD))
13349       (clobber (match_dup 4))
13350       (clobber (match_dup 5))
13351       (clobber (reg:CC FLAGS_REG))])])
13352
13353 ;; Segment register for the thread base ptr load
13354 (define_mode_attr tp_seg [(SI "gs") (DI "fs")])
13355
13356 ;; Load and add the thread base pointer from %<tp_seg>:0.
13357 (define_insn "*load_tp_x32"
13358   [(set (match_operand:SI 0 "register_operand" "=r")
13359         (unspec:SI [(const_int 0)] UNSPEC_TP))]
13360   "TARGET_X32"
13361   "mov{l}\t{%%fs:0, %0|%0, DWORD PTR fs:0}"
13362   [(set_attr "type" "imov")
13363    (set_attr "modrm" "0")
13364    (set_attr "length" "7")
13365    (set_attr "memory" "load")
13366    (set_attr "imm_disp" "false")])
13367
13368 (define_insn "*load_tp_x32_zext"
13369   [(set (match_operand:DI 0 "register_operand" "=r")
13370         (zero_extend:DI (unspec:SI [(const_int 0)] UNSPEC_TP)))]
13371   "TARGET_X32"
13372   "mov{l}\t{%%fs:0, %k0|%k0, DWORD PTR fs:0}"
13373   [(set_attr "type" "imov")
13374    (set_attr "modrm" "0")
13375    (set_attr "length" "7")
13376    (set_attr "memory" "load")
13377    (set_attr "imm_disp" "false")])
13378
13379 (define_insn "*load_tp_<mode>"
13380   [(set (match_operand:P 0 "register_operand" "=r")
13381         (unspec:P [(const_int 0)] UNSPEC_TP))]
13382   "!TARGET_X32"
13383   "mov{<imodesuffix>}\t{%%<tp_seg>:0, %0|%0, <iptrsize> PTR <tp_seg>:0}"
13384   [(set_attr "type" "imov")
13385    (set_attr "modrm" "0")
13386    (set_attr "length" "7")
13387    (set_attr "memory" "load")
13388    (set_attr "imm_disp" "false")])
13389
13390 (define_insn "*add_tp_x32"
13391   [(set (match_operand:SI 0 "register_operand" "=r")
13392         (plus:SI (unspec:SI [(const_int 0)] UNSPEC_TP)
13393                  (match_operand:SI 1 "register_operand" "0")))
13394    (clobber (reg:CC FLAGS_REG))]
13395   "TARGET_X32"
13396   "add{l}\t{%%fs:0, %0|%0, DWORD PTR fs:0}"
13397   [(set_attr "type" "alu")
13398    (set_attr "modrm" "0")
13399    (set_attr "length" "7")
13400    (set_attr "memory" "load")
13401    (set_attr "imm_disp" "false")])
13402
13403 (define_insn "*add_tp_x32_zext"
13404   [(set (match_operand:DI 0 "register_operand" "=r")
13405         (zero_extend:DI
13406           (plus:SI (unspec:SI [(const_int 0)] UNSPEC_TP)
13407                    (match_operand:SI 1 "register_operand" "0"))))
13408    (clobber (reg:CC FLAGS_REG))]
13409   "TARGET_X32"
13410   "add{l}\t{%%fs:0, %k0|%k0, DWORD PTR fs:0}"
13411   [(set_attr "type" "alu")
13412    (set_attr "modrm" "0")
13413    (set_attr "length" "7")
13414    (set_attr "memory" "load")
13415    (set_attr "imm_disp" "false")])
13416
13417 (define_insn "*add_tp_<mode>"
13418   [(set (match_operand:P 0 "register_operand" "=r")
13419         (plus:P (unspec:P [(const_int 0)] UNSPEC_TP)
13420                 (match_operand:P 1 "register_operand" "0")))
13421    (clobber (reg:CC FLAGS_REG))]
13422   "!TARGET_X32"
13423   "add{<imodesuffix>}\t{%%<tp_seg>:0, %0|%0, <iptrsize> PTR <tp_seg>:0}"
13424   [(set_attr "type" "alu")
13425    (set_attr "modrm" "0")
13426    (set_attr "length" "7")
13427    (set_attr "memory" "load")
13428    (set_attr "imm_disp" "false")])
13429
13430 ;; The Sun linker took the AMD64 TLS spec literally and can only handle
13431 ;; %rax as destination of the initial executable code sequence.
13432 (define_insn "tls_initial_exec_64_sun"
13433   [(set (match_operand:DI 0 "register_operand" "=a")
13434         (unspec:DI
13435          [(match_operand 1 "tls_symbolic_operand")]
13436          UNSPEC_TLS_IE_SUN))
13437    (clobber (reg:CC FLAGS_REG))]
13438   "TARGET_64BIT && TARGET_SUN_TLS"
13439 {
13440   output_asm_insn
13441     ("mov{q}\t{%%fs:0, %0|%0, QWORD PTR fs:0}", operands);
13442   return "add{q}\t{%a1@gottpoff(%%rip), %0|%0, %a1@gottpoff[rip]}";
13443 }
13444   [(set_attr "type" "multi")])
13445
13446 ;; GNU2 TLS patterns can be split.
13447
13448 (define_expand "tls_dynamic_gnu2_32"
13449   [(set (match_dup 3)
13450         (plus:SI (match_operand:SI 2 "register_operand")
13451                  (const:SI
13452                   (unspec:SI [(match_operand 1 "tls_symbolic_operand")]
13453                              UNSPEC_TLSDESC))))
13454    (parallel
13455     [(set (match_operand:SI 0 "register_operand")
13456           (unspec:SI [(match_dup 1) (match_dup 3)
13457                       (match_dup 2) (reg:SI SP_REG)]
13458                       UNSPEC_TLSDESC))
13459      (clobber (reg:CC FLAGS_REG))])]
13460   "!TARGET_64BIT && TARGET_GNU2_TLS"
13461 {
13462   operands[3] = can_create_pseudo_p () ? gen_reg_rtx (Pmode) : operands[0];
13463   ix86_tls_descriptor_calls_expanded_in_cfun = true;
13464 })
13465
13466 (define_insn "*tls_dynamic_gnu2_lea_32"
13467   [(set (match_operand:SI 0 "register_operand" "=r")
13468         (plus:SI (match_operand:SI 1 "register_operand" "b")
13469                  (const:SI
13470                   (unspec:SI [(match_operand 2 "tls_symbolic_operand")]
13471                               UNSPEC_TLSDESC))))]
13472   "!TARGET_64BIT && TARGET_GNU2_TLS"
13473   "lea{l}\t{%E2@TLSDESC(%1), %0|%0, %E2@TLSDESC[%1]}"
13474   [(set_attr "type" "lea")
13475    (set_attr "mode" "SI")
13476    (set_attr "length" "6")
13477    (set_attr "length_address" "4")])
13478
13479 (define_insn "*tls_dynamic_gnu2_call_32"
13480   [(set (match_operand:SI 0 "register_operand" "=a")
13481         (unspec:SI [(match_operand 1 "tls_symbolic_operand")
13482                     (match_operand:SI 2 "register_operand" "0")
13483                     ;; we have to make sure %ebx still points to the GOT
13484                     (match_operand:SI 3 "register_operand" "b")
13485                     (reg:SI SP_REG)]
13486                    UNSPEC_TLSDESC))
13487    (clobber (reg:CC FLAGS_REG))]
13488   "!TARGET_64BIT && TARGET_GNU2_TLS"
13489   "call\t{*%a1@TLSCALL(%2)|[DWORD PTR [%2+%a1@TLSCALL]]}"
13490   [(set_attr "type" "call")
13491    (set_attr "length" "2")
13492    (set_attr "length_address" "0")])
13493
13494 (define_insn_and_split "*tls_dynamic_gnu2_combine_32"
13495   [(set (match_operand:SI 0 "register_operand" "=&a")
13496         (plus:SI
13497          (unspec:SI [(match_operand 3 "tls_modbase_operand")
13498                      (match_operand:SI 4)
13499                      (match_operand:SI 2 "register_operand" "b")
13500                      (reg:SI SP_REG)]
13501                     UNSPEC_TLSDESC)
13502          (const:SI (unspec:SI
13503                     [(match_operand 1 "tls_symbolic_operand")]
13504                     UNSPEC_DTPOFF))))
13505    (clobber (reg:CC FLAGS_REG))]
13506   "!TARGET_64BIT && TARGET_GNU2_TLS"
13507   "#"
13508   ""
13509   [(set (match_dup 0) (match_dup 5))]
13510 {
13511   operands[5] = can_create_pseudo_p () ? gen_reg_rtx (Pmode) : operands[0];
13512   emit_insn (gen_tls_dynamic_gnu2_32 (operands[5], operands[1], operands[2]));
13513 })
13514
13515 (define_expand "tls_dynamic_gnu2_64"
13516   [(set (match_dup 2)
13517         (unspec:DI [(match_operand 1 "tls_symbolic_operand")]
13518                    UNSPEC_TLSDESC))
13519    (parallel
13520     [(set (match_operand:DI 0 "register_operand")
13521           (unspec:DI [(match_dup 1) (match_dup 2) (reg:DI SP_REG)]
13522                      UNSPEC_TLSDESC))
13523      (clobber (reg:CC FLAGS_REG))])]
13524   "TARGET_64BIT && TARGET_GNU2_TLS"
13525 {
13526   operands[2] = can_create_pseudo_p () ? gen_reg_rtx (Pmode) : operands[0];
13527   ix86_tls_descriptor_calls_expanded_in_cfun = true;
13528 })
13529
13530 (define_insn "*tls_dynamic_gnu2_lea_64"
13531   [(set (match_operand:DI 0 "register_operand" "=r")
13532         (unspec:DI [(match_operand 1 "tls_symbolic_operand")]
13533                    UNSPEC_TLSDESC))]
13534   "TARGET_64BIT && TARGET_GNU2_TLS"
13535   "lea{q}\t{%E1@TLSDESC(%%rip), %0|%0, %E1@TLSDESC[rip]}"
13536   [(set_attr "type" "lea")
13537    (set_attr "mode" "DI")
13538    (set_attr "length" "7")
13539    (set_attr "length_address" "4")])
13540
13541 (define_insn "*tls_dynamic_gnu2_call_64"
13542   [(set (match_operand:DI 0 "register_operand" "=a")
13543         (unspec:DI [(match_operand 1 "tls_symbolic_operand")
13544                     (match_operand:DI 2 "register_operand" "0")
13545                     (reg:DI SP_REG)]
13546                    UNSPEC_TLSDESC))
13547    (clobber (reg:CC FLAGS_REG))]
13548   "TARGET_64BIT && TARGET_GNU2_TLS"
13549   "call\t{*%a1@TLSCALL(%2)|[QWORD PTR [%2+%a1@TLSCALL]]}"
13550   [(set_attr "type" "call")
13551    (set_attr "length" "2")
13552    (set_attr "length_address" "0")])
13553
13554 (define_insn_and_split "*tls_dynamic_gnu2_combine_64"
13555   [(set (match_operand:DI 0 "register_operand" "=&a")
13556         (plus:DI
13557          (unspec:DI [(match_operand 2 "tls_modbase_operand")
13558                      (match_operand:DI 3)
13559                      (reg:DI SP_REG)]
13560                     UNSPEC_TLSDESC)
13561          (const:DI (unspec:DI
13562                     [(match_operand 1 "tls_symbolic_operand")]
13563                     UNSPEC_DTPOFF))))
13564    (clobber (reg:CC FLAGS_REG))]
13565   "TARGET_64BIT && TARGET_GNU2_TLS"
13566   "#"
13567   ""
13568   [(set (match_dup 0) (match_dup 4))]
13569 {
13570   operands[4] = can_create_pseudo_p () ? gen_reg_rtx (Pmode) : operands[0];
13571   emit_insn (gen_tls_dynamic_gnu2_64 (operands[4], operands[1]));
13572 })
13573 \f
13574 ;; These patterns match the binary 387 instructions for addM3, subM3,
13575 ;; mulM3 and divM3.  There are three patterns for each of DFmode and
13576 ;; SFmode.  The first is the normal insn, the second the same insn but
13577 ;; with one operand a conversion, and the third the same insn but with
13578 ;; the other operand a conversion.  The conversion may be SFmode or
13579 ;; SImode if the target mode DFmode, but only SImode if the target mode
13580 ;; is SFmode.
13581
13582 ;; Gcc is slightly more smart about handling normal two address instructions
13583 ;; so use special patterns for add and mull.
13584
13585 (define_insn "*fop_<mode>_comm_mixed"
13586   [(set (match_operand:MODEF 0 "register_operand" "=f,x,x")
13587         (match_operator:MODEF 3 "binary_fp_operator"
13588           [(match_operand:MODEF 1 "nonimmediate_operand" "%0,0,x")
13589            (match_operand:MODEF 2 "nonimmediate_operand" "fm,xm,xm")]))]
13590   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_MIX_SSE_I387
13591    && COMMUTATIVE_ARITH_P (operands[3])
13592    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
13593   "* return output_387_binary_op (insn, operands);"
13594   [(set (attr "type")
13595         (if_then_else (eq_attr "alternative" "1,2")
13596            (if_then_else (match_operand:MODEF 3 "mult_operator")
13597               (const_string "ssemul")
13598               (const_string "sseadd"))
13599            (if_then_else (match_operand:MODEF 3 "mult_operator")
13600               (const_string "fmul")
13601               (const_string "fop"))))
13602    (set_attr "isa" "*,noavx,avx")
13603    (set_attr "prefix" "orig,orig,vex")
13604    (set_attr "mode" "<MODE>")])
13605
13606 (define_insn "*fop_<mode>_comm_sse"
13607   [(set (match_operand:MODEF 0 "register_operand" "=x,v")
13608         (match_operator:MODEF 3 "binary_fp_operator"
13609           [(match_operand:MODEF 1 "nonimmediate_operand" "%0,v")
13610            (match_operand:MODEF 2 "nonimmediate_operand" "xm,vm")]))]
13611   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
13612    && COMMUTATIVE_ARITH_P (operands[3])
13613    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
13614   "* return output_387_binary_op (insn, operands);"
13615   [(set (attr "type")
13616         (if_then_else (match_operand:MODEF 3 "mult_operator")
13617            (const_string "ssemul")
13618            (const_string "sseadd")))
13619    (set_attr "isa" "noavx,avx")
13620    (set_attr "prefix" "orig,vex")
13621    (set_attr "mode" "<MODE>")])
13622
13623 (define_insn "*fop_<mode>_comm_i387"
13624   [(set (match_operand:MODEF 0 "register_operand" "=f")
13625         (match_operator:MODEF 3 "binary_fp_operator"
13626           [(match_operand:MODEF 1 "nonimmediate_operand" "%0")
13627            (match_operand:MODEF 2 "nonimmediate_operand" "fm")]))]
13628   "TARGET_80387 && X87_ENABLE_ARITH (<MODE>mode)
13629    && COMMUTATIVE_ARITH_P (operands[3])
13630    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
13631   "* return output_387_binary_op (insn, operands);"
13632   [(set (attr "type")
13633         (if_then_else (match_operand:MODEF 3 "mult_operator")
13634            (const_string "fmul")
13635            (const_string "fop")))
13636    (set_attr "mode" "<MODE>")])
13637
13638 (define_insn "*fop_<mode>_1_mixed"
13639   [(set (match_operand:MODEF 0 "register_operand" "=f,f,x,x")
13640         (match_operator:MODEF 3 "binary_fp_operator"
13641           [(match_operand:MODEF 1 "nonimmediate_operand" "0,fm,0,x")
13642            (match_operand:MODEF 2 "nonimmediate_operand" "fm,0,xm,xm")]))]
13643   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_MIX_SSE_I387
13644    && !COMMUTATIVE_ARITH_P (operands[3])
13645    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
13646   "* return output_387_binary_op (insn, operands);"
13647   [(set (attr "type")
13648         (cond [(and (eq_attr "alternative" "2,3")
13649                     (match_operand:MODEF 3 "mult_operator"))
13650                  (const_string "ssemul")
13651                (and (eq_attr "alternative" "2,3")
13652                     (match_operand:MODEF 3 "div_operator"))
13653                  (const_string "ssediv")
13654                (eq_attr "alternative" "2,3")
13655                  (const_string "sseadd")
13656                (match_operand:MODEF 3 "mult_operator")
13657                  (const_string "fmul")
13658                (match_operand:MODEF 3 "div_operator")
13659                  (const_string "fdiv")
13660               ]
13661               (const_string "fop")))
13662    (set_attr "isa" "*,*,noavx,avx")
13663    (set_attr "prefix" "orig,orig,orig,vex")
13664    (set_attr "mode" "<MODE>")])
13665
13666 (define_insn "*rcpsf2_sse"
13667   [(set (match_operand:SF 0 "register_operand" "=x")
13668         (unspec:SF [(match_operand:SF 1 "nonimmediate_operand" "xm")]
13669                    UNSPEC_RCP))]
13670   "TARGET_SSE_MATH"
13671   "%vrcpss\t{%1, %d0|%d0, %1}"
13672   [(set_attr "type" "sse")
13673    (set_attr "atom_sse_attr" "rcp")
13674    (set_attr "btver2_sse_attr" "rcp")
13675    (set_attr "prefix" "maybe_vex")
13676    (set_attr "mode" "SF")])
13677
13678 (define_insn "*fop_<mode>_1_sse"
13679   [(set (match_operand:MODEF 0 "register_operand" "=x,x")
13680         (match_operator:MODEF 3 "binary_fp_operator"
13681           [(match_operand:MODEF 1 "register_operand" "0,x")
13682            (match_operand:MODEF 2 "nonimmediate_operand" "xm,xm")]))]
13683   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
13684    && !COMMUTATIVE_ARITH_P (operands[3])"
13685   "* return output_387_binary_op (insn, operands);"
13686   [(set (attr "type")
13687         (cond [(match_operand:MODEF 3 "mult_operator")
13688                  (const_string "ssemul")
13689                (match_operand:MODEF 3 "div_operator")
13690                  (const_string "ssediv")
13691               ]
13692               (const_string "sseadd")))
13693    (set_attr "isa" "noavx,avx")
13694    (set_attr "prefix" "orig,vex")
13695    (set_attr "mode" "<MODE>")])
13696
13697 ;; This pattern is not fully shadowed by the pattern above.
13698 (define_insn "*fop_<mode>_1_i387"
13699   [(set (match_operand:MODEF 0 "register_operand" "=f,f")
13700         (match_operator:MODEF 3 "binary_fp_operator"
13701           [(match_operand:MODEF 1 "nonimmediate_operand" "0,fm")
13702            (match_operand:MODEF 2 "nonimmediate_operand" "fm,0")]))]
13703   "TARGET_80387 && X87_ENABLE_ARITH (<MODE>mode)
13704    && !(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
13705    && !COMMUTATIVE_ARITH_P (operands[3])
13706    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
13707   "* return output_387_binary_op (insn, operands);"
13708   [(set (attr "type")
13709         (cond [(match_operand:MODEF 3 "mult_operator")
13710                  (const_string "fmul")
13711                (match_operand:MODEF 3 "div_operator")
13712                  (const_string "fdiv")
13713               ]
13714               (const_string "fop")))
13715    (set_attr "mode" "<MODE>")])
13716
13717 ;; ??? Add SSE splitters for these!
13718 (define_insn "*fop_<MODEF:mode>_2_i387"
13719   [(set (match_operand:MODEF 0 "register_operand" "=f")
13720         (match_operator:MODEF 3 "binary_fp_operator"
13721           [(float:MODEF
13722              (match_operand:SWI24 1 "nonimmediate_operand" "m"))
13723            (match_operand:MODEF 2 "register_operand" "0")]))]
13724   "TARGET_80387 && X87_ENABLE_FLOAT (<MODEF:MODE>mode, <SWI24:MODE>mode)
13725    && !(SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH)
13726    && (TARGET_USE_<SWI24:MODE>MODE_FIOP
13727        || optimize_function_for_size_p (cfun))"
13728   { return output_387_binary_op (insn, operands); }
13729   [(set (attr "type")
13730         (cond [(match_operand:MODEF 3 "mult_operator")
13731                  (const_string "fmul")
13732                (match_operand:MODEF 3 "div_operator")
13733                  (const_string "fdiv")
13734               ]
13735               (const_string "fop")))
13736    (set_attr "fp_int_src" "true")
13737    (set_attr "mode" "<SWI24:MODE>")])
13738
13739 (define_insn "*fop_<MODEF:mode>_3_i387"
13740   [(set (match_operand:MODEF 0 "register_operand" "=f")
13741         (match_operator:MODEF 3 "binary_fp_operator"
13742           [(match_operand:MODEF 1 "register_operand" "0")
13743            (float:MODEF
13744              (match_operand:SWI24 2 "nonimmediate_operand" "m"))]))]
13745   "TARGET_80387 && X87_ENABLE_FLOAT (<MODEF:MODE>mode, <SWI24:MODE>mode)
13746    && !(SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH)
13747    && (TARGET_USE_<SWI24:MODE>MODE_FIOP
13748        || optimize_function_for_size_p (cfun))"
13749   { return output_387_binary_op (insn, operands); }
13750   [(set (attr "type")
13751         (cond [(match_operand:MODEF 3 "mult_operator")
13752                  (const_string "fmul")
13753                (match_operand:MODEF 3 "div_operator")
13754                  (const_string "fdiv")
13755               ]
13756               (const_string "fop")))
13757    (set_attr "fp_int_src" "true")
13758    (set_attr "mode" "<MODE>")])
13759
13760 (define_insn "*fop_df_4_i387"
13761   [(set (match_operand:DF 0 "register_operand" "=f,f")
13762         (match_operator:DF 3 "binary_fp_operator"
13763            [(float_extend:DF
13764              (match_operand:SF 1 "nonimmediate_operand" "fm,0"))
13765             (match_operand:DF 2 "register_operand" "0,f")]))]
13766   "TARGET_80387 && X87_ENABLE_ARITH (DFmode)
13767    && !(TARGET_SSE2 && TARGET_SSE_MATH)
13768    && !(MEM_P (operands[1]) && MEM_P (operands[2]))"
13769   "* return output_387_binary_op (insn, operands);"
13770   [(set (attr "type")
13771         (cond [(match_operand:DF 3 "mult_operator")
13772                  (const_string "fmul")
13773                (match_operand:DF 3 "div_operator")
13774                  (const_string "fdiv")
13775               ]
13776               (const_string "fop")))
13777    (set_attr "mode" "SF")])
13778
13779 (define_insn "*fop_df_5_i387"
13780   [(set (match_operand:DF 0 "register_operand" "=f,f")
13781         (match_operator:DF 3 "binary_fp_operator"
13782           [(match_operand:DF 1 "register_operand" "0,f")
13783            (float_extend:DF
13784             (match_operand:SF 2 "nonimmediate_operand" "fm,0"))]))]
13785   "TARGET_80387 && X87_ENABLE_ARITH (DFmode)
13786    && !(TARGET_SSE2 && TARGET_SSE_MATH)"
13787   "* return output_387_binary_op (insn, operands);"
13788   [(set (attr "type")
13789         (cond [(match_operand:DF 3 "mult_operator")
13790                  (const_string "fmul")
13791                (match_operand:DF 3 "div_operator")
13792                  (const_string "fdiv")
13793               ]
13794               (const_string "fop")))
13795    (set_attr "mode" "SF")])
13796
13797 (define_insn "*fop_df_6_i387"
13798   [(set (match_operand:DF 0 "register_operand" "=f,f")
13799         (match_operator:DF 3 "binary_fp_operator"
13800           [(float_extend:DF
13801             (match_operand:SF 1 "register_operand" "0,f"))
13802            (float_extend:DF
13803             (match_operand:SF 2 "nonimmediate_operand" "fm,0"))]))]
13804   "TARGET_80387 && X87_ENABLE_ARITH (DFmode)
13805    && !(TARGET_SSE2 && TARGET_SSE_MATH)"
13806   "* return output_387_binary_op (insn, operands);"
13807   [(set (attr "type")
13808         (cond [(match_operand:DF 3 "mult_operator")
13809                  (const_string "fmul")
13810                (match_operand:DF 3 "div_operator")
13811                  (const_string "fdiv")
13812               ]
13813               (const_string "fop")))
13814    (set_attr "mode" "SF")])
13815
13816 (define_insn "*fop_xf_comm_i387"
13817   [(set (match_operand:XF 0 "register_operand" "=f")
13818         (match_operator:XF 3 "binary_fp_operator"
13819                         [(match_operand:XF 1 "register_operand" "%0")
13820                          (match_operand:XF 2 "register_operand" "f")]))]
13821   "TARGET_80387
13822    && COMMUTATIVE_ARITH_P (operands[3])"
13823   "* return output_387_binary_op (insn, operands);"
13824   [(set (attr "type")
13825         (if_then_else (match_operand:XF 3 "mult_operator")
13826            (const_string "fmul")
13827            (const_string "fop")))
13828    (set_attr "mode" "XF")])
13829
13830 (define_insn "*fop_xf_1_i387"
13831   [(set (match_operand:XF 0 "register_operand" "=f,f")
13832         (match_operator:XF 3 "binary_fp_operator"
13833                         [(match_operand:XF 1 "register_operand" "0,f")
13834                          (match_operand:XF 2 "register_operand" "f,0")]))]
13835   "TARGET_80387
13836    && !COMMUTATIVE_ARITH_P (operands[3])"
13837   "* return output_387_binary_op (insn, operands);"
13838   [(set (attr "type")
13839         (cond [(match_operand:XF 3 "mult_operator")
13840                  (const_string "fmul")
13841                (match_operand:XF 3 "div_operator")
13842                  (const_string "fdiv")
13843               ]
13844               (const_string "fop")))
13845    (set_attr "mode" "XF")])
13846
13847 (define_insn "*fop_xf_2_i387"
13848   [(set (match_operand:XF 0 "register_operand" "=f")
13849         (match_operator:XF 3 "binary_fp_operator"
13850           [(float:XF
13851              (match_operand:SWI24 1 "nonimmediate_operand" "m"))
13852            (match_operand:XF 2 "register_operand" "0")]))]
13853   "TARGET_80387
13854    && (TARGET_USE_<MODE>MODE_FIOP || optimize_function_for_size_p (cfun))"
13855   { return output_387_binary_op (insn, operands); }
13856   [(set (attr "type")
13857         (cond [(match_operand:XF 3 "mult_operator")
13858                  (const_string "fmul")
13859                (match_operand:XF 3 "div_operator")
13860                  (const_string "fdiv")
13861               ]
13862               (const_string "fop")))
13863    (set_attr "fp_int_src" "true")
13864    (set_attr "mode" "<MODE>")])
13865
13866 (define_insn "*fop_xf_3_i387"
13867   [(set (match_operand:XF 0 "register_operand" "=f")
13868         (match_operator:XF 3 "binary_fp_operator"
13869           [(match_operand:XF 1 "register_operand" "0")
13870            (float:XF
13871              (match_operand:SWI24 2 "nonimmediate_operand" "m"))]))]
13872   "TARGET_80387
13873    && (TARGET_USE_<MODE>MODE_FIOP || optimize_function_for_size_p (cfun))"
13874   { return output_387_binary_op (insn, operands); }
13875   [(set (attr "type")
13876         (cond [(match_operand:XF 3 "mult_operator")
13877                  (const_string "fmul")
13878                (match_operand:XF 3 "div_operator")
13879                  (const_string "fdiv")
13880               ]
13881               (const_string "fop")))
13882    (set_attr "fp_int_src" "true")
13883    (set_attr "mode" "<MODE>")])
13884
13885 (define_insn "*fop_xf_4_i387"
13886   [(set (match_operand:XF 0 "register_operand" "=f,f")
13887         (match_operator:XF 3 "binary_fp_operator"
13888            [(float_extend:XF
13889               (match_operand:MODEF 1 "nonimmediate_operand" "fm,0"))
13890             (match_operand:XF 2 "register_operand" "0,f")]))]
13891   "TARGET_80387"
13892   "* return output_387_binary_op (insn, operands);"
13893   [(set (attr "type")
13894         (cond [(match_operand:XF 3 "mult_operator")
13895                  (const_string "fmul")
13896                (match_operand:XF 3 "div_operator")
13897                  (const_string "fdiv")
13898               ]
13899               (const_string "fop")))
13900    (set_attr "mode" "<MODE>")])
13901
13902 (define_insn "*fop_xf_5_i387"
13903   [(set (match_operand:XF 0 "register_operand" "=f,f")
13904         (match_operator:XF 3 "binary_fp_operator"
13905           [(match_operand:XF 1 "register_operand" "0,f")
13906            (float_extend:XF
13907              (match_operand:MODEF 2 "nonimmediate_operand" "fm,0"))]))]
13908   "TARGET_80387"
13909   "* return output_387_binary_op (insn, operands);"
13910   [(set (attr "type")
13911         (cond [(match_operand:XF 3 "mult_operator")
13912                  (const_string "fmul")
13913                (match_operand:XF 3 "div_operator")
13914                  (const_string "fdiv")
13915               ]
13916               (const_string "fop")))
13917    (set_attr "mode" "<MODE>")])
13918
13919 (define_insn "*fop_xf_6_i387"
13920   [(set (match_operand:XF 0 "register_operand" "=f,f")
13921         (match_operator:XF 3 "binary_fp_operator"
13922           [(float_extend:XF
13923              (match_operand:MODEF 1 "register_operand" "0,f"))
13924            (float_extend:XF
13925              (match_operand:MODEF 2 "nonimmediate_operand" "fm,0"))]))]
13926   "TARGET_80387"
13927   "* return output_387_binary_op (insn, operands);"
13928   [(set (attr "type")
13929         (cond [(match_operand:XF 3 "mult_operator")
13930                  (const_string "fmul")
13931                (match_operand:XF 3 "div_operator")
13932                  (const_string "fdiv")
13933               ]
13934               (const_string "fop")))
13935    (set_attr "mode" "<MODE>")])
13936 \f
13937 ;; FPU special functions.
13938
13939 ;; This pattern implements a no-op XFmode truncation for
13940 ;; all fancy i386 XFmode math functions.
13941
13942 (define_insn "truncxf<mode>2_i387_noop_unspec"
13943   [(set (match_operand:MODEF 0 "register_operand" "=f")
13944         (unspec:MODEF [(match_operand:XF 1 "register_operand" "f")]
13945         UNSPEC_TRUNC_NOOP))]
13946   "TARGET_USE_FANCY_MATH_387"
13947   "* return output_387_reg_move (insn, operands);"
13948   [(set_attr "type" "fmov")
13949    (set_attr "mode" "<MODE>")])
13950
13951 (define_insn "sqrtxf2"
13952   [(set (match_operand:XF 0 "register_operand" "=f")
13953         (sqrt:XF (match_operand:XF 1 "register_operand" "0")))]
13954   "TARGET_USE_FANCY_MATH_387"
13955   "fsqrt"
13956   [(set_attr "type" "fpspc")
13957    (set_attr "mode" "XF")
13958    (set_attr "athlon_decode" "direct")
13959    (set_attr "amdfam10_decode" "direct")
13960    (set_attr "bdver1_decode" "direct")])
13961
13962 (define_insn "sqrt_extend<mode>xf2_i387"
13963   [(set (match_operand:XF 0 "register_operand" "=f")
13964         (sqrt:XF
13965           (float_extend:XF
13966             (match_operand:MODEF 1 "register_operand" "0"))))]
13967   "TARGET_USE_FANCY_MATH_387"
13968   "fsqrt"
13969   [(set_attr "type" "fpspc")
13970    (set_attr "mode" "XF")
13971    (set_attr "athlon_decode" "direct")
13972    (set_attr "amdfam10_decode" "direct")
13973    (set_attr "bdver1_decode" "direct")])
13974
13975 (define_insn "*rsqrtsf2_sse"
13976   [(set (match_operand:SF 0 "register_operand" "=x")
13977         (unspec:SF [(match_operand:SF 1 "nonimmediate_operand" "xm")]
13978                    UNSPEC_RSQRT))]
13979   "TARGET_SSE_MATH"
13980   "%vrsqrtss\t{%1, %d0|%d0, %1}"
13981   [(set_attr "type" "sse")
13982    (set_attr "atom_sse_attr" "rcp")
13983    (set_attr "btver2_sse_attr" "rcp")
13984    (set_attr "prefix" "maybe_vex")
13985    (set_attr "mode" "SF")])
13986
13987 (define_expand "rsqrtsf2"
13988   [(set (match_operand:SF 0 "register_operand")
13989         (unspec:SF [(match_operand:SF 1 "nonimmediate_operand")]
13990                    UNSPEC_RSQRT))]
13991   "TARGET_SSE_MATH"
13992 {
13993   ix86_emit_swsqrtsf (operands[0], operands[1], SFmode, 1);
13994   DONE;
13995 })
13996
13997 (define_insn "*sqrt<mode>2_sse"
13998   [(set (match_operand:MODEF 0 "register_operand" "=x")
13999         (sqrt:MODEF
14000           (match_operand:MODEF 1 "nonimmediate_operand" "xm")))]
14001   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH"
14002   "%vsqrt<ssemodesuffix>\t{%1, %d0|%d0, %1}"
14003   [(set_attr "type" "sse")
14004    (set_attr "atom_sse_attr" "sqrt")
14005    (set_attr "btver2_sse_attr" "sqrt")
14006    (set_attr "prefix" "maybe_vex")
14007    (set_attr "mode" "<MODE>")
14008    (set_attr "athlon_decode" "*")
14009    (set_attr "amdfam10_decode" "*")
14010    (set_attr "bdver1_decode" "*")])
14011
14012 (define_expand "sqrt<mode>2"
14013   [(set (match_operand:MODEF 0 "register_operand")
14014         (sqrt:MODEF
14015           (match_operand:MODEF 1 "nonimmediate_operand")))]
14016   "(TARGET_USE_FANCY_MATH_387 && X87_ENABLE_ARITH (<MODE>mode))
14017    || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
14018 {
14019   if (<MODE>mode == SFmode
14020       && TARGET_SSE_MATH
14021       && TARGET_RECIP_SQRT
14022       && !optimize_function_for_size_p (cfun)
14023       && flag_finite_math_only && !flag_trapping_math
14024       && flag_unsafe_math_optimizations)
14025     {
14026       ix86_emit_swsqrtsf (operands[0], operands[1], SFmode, 0);
14027       DONE;
14028     }
14029
14030   if (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH))
14031     {
14032       rtx op0 = gen_reg_rtx (XFmode);
14033       rtx op1 = force_reg (<MODE>mode, operands[1]);
14034
14035       emit_insn (gen_sqrt_extend<mode>xf2_i387 (op0, op1));
14036       emit_insn (gen_truncxf<mode>2_i387_noop_unspec (operands[0], op0));
14037       DONE;
14038    }
14039 })
14040
14041 (define_insn "fpremxf4_i387"
14042   [(set (match_operand:XF 0 "register_operand" "=f")
14043         (unspec:XF [(match_operand:XF 2 "register_operand" "0")
14044                     (match_operand:XF 3 "register_operand" "1")]
14045                    UNSPEC_FPREM_F))
14046    (set (match_operand:XF 1 "register_operand" "=u")
14047         (unspec:XF [(match_dup 2) (match_dup 3)]
14048                    UNSPEC_FPREM_U))
14049    (set (reg:CCFP FPSR_REG)
14050         (unspec:CCFP [(match_dup 2) (match_dup 3)]
14051                      UNSPEC_C2_FLAG))]
14052   "TARGET_USE_FANCY_MATH_387
14053    && flag_finite_math_only"
14054   "fprem"
14055   [(set_attr "type" "fpspc")
14056    (set_attr "mode" "XF")])
14057
14058 (define_expand "fmodxf3"
14059   [(use (match_operand:XF 0 "register_operand"))
14060    (use (match_operand:XF 1 "general_operand"))
14061    (use (match_operand:XF 2 "general_operand"))]
14062   "TARGET_USE_FANCY_MATH_387
14063    && flag_finite_math_only"
14064 {
14065   rtx_code_label *label = gen_label_rtx ();
14066
14067   rtx op1 = gen_reg_rtx (XFmode);
14068   rtx op2 = gen_reg_rtx (XFmode);
14069
14070   emit_move_insn (op2, operands[2]);
14071   emit_move_insn (op1, operands[1]);
14072
14073   emit_label (label);
14074   emit_insn (gen_fpremxf4_i387 (op1, op2, op1, op2));
14075   ix86_emit_fp_unordered_jump (label);
14076   LABEL_NUSES (label) = 1;
14077
14078   emit_move_insn (operands[0], op1);
14079   DONE;
14080 })
14081
14082 (define_expand "fmod<mode>3"
14083   [(use (match_operand:MODEF 0 "register_operand"))
14084    (use (match_operand:MODEF 1 "general_operand"))
14085    (use (match_operand:MODEF 2 "general_operand"))]
14086   "TARGET_USE_FANCY_MATH_387
14087    && flag_finite_math_only"
14088 {
14089   rtx (*gen_truncxf) (rtx, rtx);
14090
14091   rtx_code_label *label = gen_label_rtx ();
14092
14093   rtx op1 = gen_reg_rtx (XFmode);
14094   rtx op2 = gen_reg_rtx (XFmode);
14095
14096   emit_insn (gen_extend<mode>xf2 (op2, operands[2]));
14097   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14098
14099   emit_label (label);
14100   emit_insn (gen_fpremxf4_i387 (op1, op2, op1, op2));
14101   ix86_emit_fp_unordered_jump (label);
14102   LABEL_NUSES (label) = 1;
14103
14104   /* Truncate the result properly for strict SSE math.  */
14105   if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
14106       && !TARGET_MIX_SSE_I387)
14107     gen_truncxf = gen_truncxf<mode>2;
14108   else
14109     gen_truncxf = gen_truncxf<mode>2_i387_noop_unspec;
14110
14111   emit_insn (gen_truncxf (operands[0], op1));
14112   DONE;
14113 })
14114
14115 (define_insn "fprem1xf4_i387"
14116   [(set (match_operand:XF 0 "register_operand" "=f")
14117         (unspec:XF [(match_operand:XF 2 "register_operand" "0")
14118                     (match_operand:XF 3 "register_operand" "1")]
14119                    UNSPEC_FPREM1_F))
14120    (set (match_operand:XF 1 "register_operand" "=u")
14121         (unspec:XF [(match_dup 2) (match_dup 3)]
14122                    UNSPEC_FPREM1_U))
14123    (set (reg:CCFP FPSR_REG)
14124         (unspec:CCFP [(match_dup 2) (match_dup 3)]
14125                      UNSPEC_C2_FLAG))]
14126   "TARGET_USE_FANCY_MATH_387
14127    && flag_finite_math_only"
14128   "fprem1"
14129   [(set_attr "type" "fpspc")
14130    (set_attr "mode" "XF")])
14131
14132 (define_expand "remainderxf3"
14133   [(use (match_operand:XF 0 "register_operand"))
14134    (use (match_operand:XF 1 "general_operand"))
14135    (use (match_operand:XF 2 "general_operand"))]
14136   "TARGET_USE_FANCY_MATH_387
14137    && flag_finite_math_only"
14138 {
14139   rtx_code_label *label = gen_label_rtx ();
14140
14141   rtx op1 = gen_reg_rtx (XFmode);
14142   rtx op2 = gen_reg_rtx (XFmode);
14143
14144   emit_move_insn (op2, operands[2]);
14145   emit_move_insn (op1, operands[1]);
14146
14147   emit_label (label);
14148   emit_insn (gen_fprem1xf4_i387 (op1, op2, op1, op2));
14149   ix86_emit_fp_unordered_jump (label);
14150   LABEL_NUSES (label) = 1;
14151
14152   emit_move_insn (operands[0], op1);
14153   DONE;
14154 })
14155
14156 (define_expand "remainder<mode>3"
14157   [(use (match_operand:MODEF 0 "register_operand"))
14158    (use (match_operand:MODEF 1 "general_operand"))
14159    (use (match_operand:MODEF 2 "general_operand"))]
14160   "TARGET_USE_FANCY_MATH_387
14161    && flag_finite_math_only"
14162 {
14163   rtx (*gen_truncxf) (rtx, rtx);
14164
14165   rtx_code_label *label = gen_label_rtx ();
14166
14167   rtx op1 = gen_reg_rtx (XFmode);
14168   rtx op2 = gen_reg_rtx (XFmode);
14169
14170   emit_insn (gen_extend<mode>xf2 (op2, operands[2]));
14171   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14172
14173   emit_label (label);
14174
14175   emit_insn (gen_fprem1xf4_i387 (op1, op2, op1, op2));
14176   ix86_emit_fp_unordered_jump (label);
14177   LABEL_NUSES (label) = 1;
14178
14179   /* Truncate the result properly for strict SSE math.  */
14180   if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
14181       && !TARGET_MIX_SSE_I387)
14182     gen_truncxf = gen_truncxf<mode>2;
14183   else
14184     gen_truncxf = gen_truncxf<mode>2_i387_noop_unspec;
14185
14186   emit_insn (gen_truncxf (operands[0], op1));
14187   DONE;
14188 })
14189
14190 (define_int_iterator SINCOS
14191         [UNSPEC_SIN
14192          UNSPEC_COS])
14193
14194 (define_int_attr sincos
14195         [(UNSPEC_SIN "sin")
14196          (UNSPEC_COS "cos")])
14197
14198 (define_insn "*<sincos>xf2_i387"
14199   [(set (match_operand:XF 0 "register_operand" "=f")
14200         (unspec:XF [(match_operand:XF 1 "register_operand" "0")]
14201                    SINCOS))]
14202   "TARGET_USE_FANCY_MATH_387
14203    && flag_unsafe_math_optimizations"
14204   "f<sincos>"
14205   [(set_attr "type" "fpspc")
14206    (set_attr "mode" "XF")])
14207
14208 (define_insn "*<sincos>_extend<mode>xf2_i387"
14209   [(set (match_operand:XF 0 "register_operand" "=f")
14210         (unspec:XF [(float_extend:XF
14211                       (match_operand:MODEF 1 "register_operand" "0"))]
14212                    SINCOS))]
14213   "TARGET_USE_FANCY_MATH_387
14214    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14215        || TARGET_MIX_SSE_I387)
14216    && flag_unsafe_math_optimizations"
14217   "f<sincos>"
14218   [(set_attr "type" "fpspc")
14219    (set_attr "mode" "XF")])
14220
14221 ;; When sincos pattern is defined, sin and cos builtin functions will be
14222 ;; expanded to sincos pattern with one of its outputs left unused.
14223 ;; CSE pass will figure out if two sincos patterns can be combined,
14224 ;; otherwise sincos pattern will be split back to sin or cos pattern,
14225 ;; depending on the unused output.
14226
14227 (define_insn "sincosxf3"
14228   [(set (match_operand:XF 0 "register_operand" "=f")
14229         (unspec:XF [(match_operand:XF 2 "register_operand" "0")]
14230                    UNSPEC_SINCOS_COS))
14231    (set (match_operand:XF 1 "register_operand" "=u")
14232         (unspec:XF [(match_dup 2)] UNSPEC_SINCOS_SIN))]
14233   "TARGET_USE_FANCY_MATH_387
14234    && flag_unsafe_math_optimizations"
14235   "fsincos"
14236   [(set_attr "type" "fpspc")
14237    (set_attr "mode" "XF")])
14238
14239 (define_split
14240   [(set (match_operand:XF 0 "register_operand")
14241         (unspec:XF [(match_operand:XF 2 "register_operand")]
14242                    UNSPEC_SINCOS_COS))
14243    (set (match_operand:XF 1 "register_operand")
14244         (unspec:XF [(match_dup 2)] UNSPEC_SINCOS_SIN))]
14245   "find_regno_note (insn, REG_UNUSED, REGNO (operands[0]))
14246    && can_create_pseudo_p ()"
14247   [(set (match_dup 1) (unspec:XF [(match_dup 2)] UNSPEC_SIN))])
14248
14249 (define_split
14250   [(set (match_operand:XF 0 "register_operand")
14251         (unspec:XF [(match_operand:XF 2 "register_operand")]
14252                    UNSPEC_SINCOS_COS))
14253    (set (match_operand:XF 1 "register_operand")
14254         (unspec:XF [(match_dup 2)] UNSPEC_SINCOS_SIN))]
14255   "find_regno_note (insn, REG_UNUSED, REGNO (operands[1]))
14256    && can_create_pseudo_p ()"
14257   [(set (match_dup 0) (unspec:XF [(match_dup 2)] UNSPEC_COS))])
14258
14259 (define_insn "sincos_extend<mode>xf3_i387"
14260   [(set (match_operand:XF 0 "register_operand" "=f")
14261         (unspec:XF [(float_extend:XF
14262                       (match_operand:MODEF 2 "register_operand" "0"))]
14263                    UNSPEC_SINCOS_COS))
14264    (set (match_operand:XF 1 "register_operand" "=u")
14265         (unspec:XF [(float_extend:XF (match_dup 2))] UNSPEC_SINCOS_SIN))]
14266   "TARGET_USE_FANCY_MATH_387
14267    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14268        || TARGET_MIX_SSE_I387)
14269    && flag_unsafe_math_optimizations"
14270   "fsincos"
14271   [(set_attr "type" "fpspc")
14272    (set_attr "mode" "XF")])
14273
14274 (define_split
14275   [(set (match_operand:XF 0 "register_operand")
14276         (unspec:XF [(float_extend:XF
14277                       (match_operand:MODEF 2 "register_operand"))]
14278                    UNSPEC_SINCOS_COS))
14279    (set (match_operand:XF 1 "register_operand")
14280         (unspec:XF [(float_extend:XF (match_dup 2))] UNSPEC_SINCOS_SIN))]
14281   "find_regno_note (insn, REG_UNUSED, REGNO (operands[0]))
14282    && can_create_pseudo_p ()"
14283   [(set (match_dup 1)
14284         (unspec:XF [(float_extend:XF (match_dup 2))] UNSPEC_SIN))])
14285
14286 (define_split
14287   [(set (match_operand:XF 0 "register_operand")
14288         (unspec:XF [(float_extend:XF
14289                       (match_operand:MODEF 2 "register_operand"))]
14290                    UNSPEC_SINCOS_COS))
14291    (set (match_operand:XF 1 "register_operand")
14292         (unspec:XF [(float_extend:XF (match_dup 2))] UNSPEC_SINCOS_SIN))]
14293   "find_regno_note (insn, REG_UNUSED, REGNO (operands[1]))
14294    && can_create_pseudo_p ()"
14295   [(set (match_dup 0)
14296         (unspec:XF [(float_extend:XF (match_dup 2))] UNSPEC_COS))])
14297
14298 (define_expand "sincos<mode>3"
14299   [(use (match_operand:MODEF 0 "register_operand"))
14300    (use (match_operand:MODEF 1 "register_operand"))
14301    (use (match_operand:MODEF 2 "register_operand"))]
14302   "TARGET_USE_FANCY_MATH_387
14303    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14304        || TARGET_MIX_SSE_I387)
14305    && flag_unsafe_math_optimizations"
14306 {
14307   rtx op0 = gen_reg_rtx (XFmode);
14308   rtx op1 = gen_reg_rtx (XFmode);
14309
14310   emit_insn (gen_sincos_extend<mode>xf3_i387 (op0, op1, operands[2]));
14311   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14312   emit_insn (gen_truncxf<mode>2_i387_noop (operands[1], op1));
14313   DONE;
14314 })
14315
14316 (define_insn "fptanxf4_i387"
14317   [(set (match_operand:XF 0 "register_operand" "=f")
14318         (match_operand:XF 3 "const_double_operand" "F"))
14319    (set (match_operand:XF 1 "register_operand" "=u")
14320         (unspec:XF [(match_operand:XF 2 "register_operand" "0")]
14321                    UNSPEC_TAN))]
14322   "TARGET_USE_FANCY_MATH_387
14323    && flag_unsafe_math_optimizations
14324    && standard_80387_constant_p (operands[3]) == 2"
14325   "fptan"
14326   [(set_attr "type" "fpspc")
14327    (set_attr "mode" "XF")])
14328
14329 (define_insn "fptan_extend<mode>xf4_i387"
14330   [(set (match_operand:MODEF 0 "register_operand" "=f")
14331         (match_operand:MODEF 3 "const_double_operand" "F"))
14332    (set (match_operand:XF 1 "register_operand" "=u")
14333         (unspec:XF [(float_extend:XF
14334                       (match_operand:MODEF 2 "register_operand" "0"))]
14335                    UNSPEC_TAN))]
14336   "TARGET_USE_FANCY_MATH_387
14337    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14338        || TARGET_MIX_SSE_I387)
14339    && flag_unsafe_math_optimizations
14340    && standard_80387_constant_p (operands[3]) == 2"
14341   "fptan"
14342   [(set_attr "type" "fpspc")
14343    (set_attr "mode" "XF")])
14344
14345 (define_expand "tanxf2"
14346   [(use (match_operand:XF 0 "register_operand"))
14347    (use (match_operand:XF 1 "register_operand"))]
14348   "TARGET_USE_FANCY_MATH_387
14349    && flag_unsafe_math_optimizations"
14350 {
14351   rtx one = gen_reg_rtx (XFmode);
14352   rtx op2 = CONST1_RTX (XFmode); /* fld1 */
14353
14354   emit_insn (gen_fptanxf4_i387 (one, operands[0], operands[1], op2));
14355   DONE;
14356 })
14357
14358 (define_expand "tan<mode>2"
14359   [(use (match_operand:MODEF 0 "register_operand"))
14360    (use (match_operand:MODEF 1 "register_operand"))]
14361   "TARGET_USE_FANCY_MATH_387
14362    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14363        || TARGET_MIX_SSE_I387)
14364    && flag_unsafe_math_optimizations"
14365 {
14366   rtx op0 = gen_reg_rtx (XFmode);
14367
14368   rtx one = gen_reg_rtx (<MODE>mode);
14369   rtx op2 = CONST1_RTX (<MODE>mode); /* fld1 */
14370
14371   emit_insn (gen_fptan_extend<mode>xf4_i387 (one, op0,
14372                                              operands[1], op2));
14373   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14374   DONE;
14375 })
14376
14377 (define_insn "*fpatanxf3_i387"
14378   [(set (match_operand:XF 0 "register_operand" "=f")
14379         (unspec:XF [(match_operand:XF 1 "register_operand" "0")
14380                     (match_operand:XF 2 "register_operand" "u")]
14381                    UNSPEC_FPATAN))
14382    (clobber (match_scratch:XF 3 "=2"))]
14383   "TARGET_USE_FANCY_MATH_387
14384    && flag_unsafe_math_optimizations"
14385   "fpatan"
14386   [(set_attr "type" "fpspc")
14387    (set_attr "mode" "XF")])
14388
14389 (define_insn "fpatan_extend<mode>xf3_i387"
14390   [(set (match_operand:XF 0 "register_operand" "=f")
14391         (unspec:XF [(float_extend:XF
14392                       (match_operand:MODEF 1 "register_operand" "0"))
14393                     (float_extend:XF
14394                       (match_operand:MODEF 2 "register_operand" "u"))]
14395                    UNSPEC_FPATAN))
14396    (clobber (match_scratch:XF 3 "=2"))]
14397   "TARGET_USE_FANCY_MATH_387
14398    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14399        || TARGET_MIX_SSE_I387)
14400    && flag_unsafe_math_optimizations"
14401   "fpatan"
14402   [(set_attr "type" "fpspc")
14403    (set_attr "mode" "XF")])
14404
14405 (define_expand "atan2xf3"
14406   [(parallel [(set (match_operand:XF 0 "register_operand")
14407                    (unspec:XF [(match_operand:XF 2 "register_operand")
14408                                (match_operand:XF 1 "register_operand")]
14409                               UNSPEC_FPATAN))
14410               (clobber (match_scratch:XF 3))])]
14411   "TARGET_USE_FANCY_MATH_387
14412    && flag_unsafe_math_optimizations")
14413
14414 (define_expand "atan2<mode>3"
14415   [(use (match_operand:MODEF 0 "register_operand"))
14416    (use (match_operand:MODEF 1 "register_operand"))
14417    (use (match_operand:MODEF 2 "register_operand"))]
14418   "TARGET_USE_FANCY_MATH_387
14419    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14420        || TARGET_MIX_SSE_I387)
14421    && flag_unsafe_math_optimizations"
14422 {
14423   rtx op0 = gen_reg_rtx (XFmode);
14424
14425   emit_insn (gen_fpatan_extend<mode>xf3_i387 (op0, operands[2], operands[1]));
14426   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14427   DONE;
14428 })
14429
14430 (define_expand "atanxf2"
14431   [(parallel [(set (match_operand:XF 0 "register_operand")
14432                    (unspec:XF [(match_dup 2)
14433                                (match_operand:XF 1 "register_operand")]
14434                               UNSPEC_FPATAN))
14435               (clobber (match_scratch:XF 3))])]
14436   "TARGET_USE_FANCY_MATH_387
14437    && flag_unsafe_math_optimizations"
14438 {
14439   operands[2] = gen_reg_rtx (XFmode);
14440   emit_move_insn (operands[2], CONST1_RTX (XFmode));  /* fld1 */
14441 })
14442
14443 (define_expand "atan<mode>2"
14444   [(use (match_operand:MODEF 0 "register_operand"))
14445    (use (match_operand:MODEF 1 "register_operand"))]
14446   "TARGET_USE_FANCY_MATH_387
14447    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14448        || TARGET_MIX_SSE_I387)
14449    && flag_unsafe_math_optimizations"
14450 {
14451   rtx op0 = gen_reg_rtx (XFmode);
14452
14453   rtx op2 = gen_reg_rtx (<MODE>mode);
14454   emit_move_insn (op2, CONST1_RTX (<MODE>mode));  /* fld1 */
14455
14456   emit_insn (gen_fpatan_extend<mode>xf3_i387 (op0, op2, operands[1]));
14457   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14458   DONE;
14459 })
14460
14461 (define_expand "asinxf2"
14462   [(set (match_dup 2)
14463         (mult:XF (match_operand:XF 1 "register_operand")
14464                  (match_dup 1)))
14465    (set (match_dup 4) (minus:XF (match_dup 3) (match_dup 2)))
14466    (set (match_dup 5) (sqrt:XF (match_dup 4)))
14467    (parallel [(set (match_operand:XF 0 "register_operand")
14468                    (unspec:XF [(match_dup 5) (match_dup 1)]
14469                               UNSPEC_FPATAN))
14470               (clobber (match_scratch:XF 6))])]
14471   "TARGET_USE_FANCY_MATH_387
14472    && flag_unsafe_math_optimizations"
14473 {
14474   int i;
14475
14476   if (optimize_insn_for_size_p ())
14477     FAIL;
14478
14479   for (i = 2; i < 6; i++)
14480     operands[i] = gen_reg_rtx (XFmode);
14481
14482   emit_move_insn (operands[3], CONST1_RTX (XFmode));  /* fld1 */
14483 })
14484
14485 (define_expand "asin<mode>2"
14486   [(use (match_operand:MODEF 0 "register_operand"))
14487    (use (match_operand:MODEF 1 "general_operand"))]
14488  "TARGET_USE_FANCY_MATH_387
14489    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14490        || TARGET_MIX_SSE_I387)
14491    && flag_unsafe_math_optimizations"
14492 {
14493   rtx op0 = gen_reg_rtx (XFmode);
14494   rtx op1 = gen_reg_rtx (XFmode);
14495
14496   if (optimize_insn_for_size_p ())
14497     FAIL;
14498
14499   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14500   emit_insn (gen_asinxf2 (op0, op1));
14501   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14502   DONE;
14503 })
14504
14505 (define_expand "acosxf2"
14506   [(set (match_dup 2)
14507         (mult:XF (match_operand:XF 1 "register_operand")
14508                  (match_dup 1)))
14509    (set (match_dup 4) (minus:XF (match_dup 3) (match_dup 2)))
14510    (set (match_dup 5) (sqrt:XF (match_dup 4)))
14511    (parallel [(set (match_operand:XF 0 "register_operand")
14512                    (unspec:XF [(match_dup 1) (match_dup 5)]
14513                               UNSPEC_FPATAN))
14514               (clobber (match_scratch:XF 6))])]
14515   "TARGET_USE_FANCY_MATH_387
14516    && flag_unsafe_math_optimizations"
14517 {
14518   int i;
14519
14520   if (optimize_insn_for_size_p ())
14521     FAIL;
14522
14523   for (i = 2; i < 6; i++)
14524     operands[i] = gen_reg_rtx (XFmode);
14525
14526   emit_move_insn (operands[3], CONST1_RTX (XFmode));  /* fld1 */
14527 })
14528
14529 (define_expand "acos<mode>2"
14530   [(use (match_operand:MODEF 0 "register_operand"))
14531    (use (match_operand:MODEF 1 "general_operand"))]
14532  "TARGET_USE_FANCY_MATH_387
14533    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14534        || TARGET_MIX_SSE_I387)
14535    && flag_unsafe_math_optimizations"
14536 {
14537   rtx op0 = gen_reg_rtx (XFmode);
14538   rtx op1 = gen_reg_rtx (XFmode);
14539
14540   if (optimize_insn_for_size_p ())
14541     FAIL;
14542
14543   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14544   emit_insn (gen_acosxf2 (op0, op1));
14545   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14546   DONE;
14547 })
14548
14549 (define_insn "fyl2xxf3_i387"
14550   [(set (match_operand:XF 0 "register_operand" "=f")
14551         (unspec:XF [(match_operand:XF 1 "register_operand" "0")
14552                     (match_operand:XF 2 "register_operand" "u")]
14553                    UNSPEC_FYL2X))
14554    (clobber (match_scratch:XF 3 "=2"))]
14555   "TARGET_USE_FANCY_MATH_387
14556    && flag_unsafe_math_optimizations"
14557   "fyl2x"
14558   [(set_attr "type" "fpspc")
14559    (set_attr "mode" "XF")])
14560
14561 (define_insn "fyl2x_extend<mode>xf3_i387"
14562   [(set (match_operand:XF 0 "register_operand" "=f")
14563         (unspec:XF [(float_extend:XF
14564                       (match_operand:MODEF 1 "register_operand" "0"))
14565                     (match_operand:XF 2 "register_operand" "u")]
14566                    UNSPEC_FYL2X))
14567    (clobber (match_scratch:XF 3 "=2"))]
14568   "TARGET_USE_FANCY_MATH_387
14569    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14570        || TARGET_MIX_SSE_I387)
14571    && flag_unsafe_math_optimizations"
14572   "fyl2x"
14573   [(set_attr "type" "fpspc")
14574    (set_attr "mode" "XF")])
14575
14576 (define_expand "logxf2"
14577   [(parallel [(set (match_operand:XF 0 "register_operand")
14578                    (unspec:XF [(match_operand:XF 1 "register_operand")
14579                                (match_dup 2)] UNSPEC_FYL2X))
14580               (clobber (match_scratch:XF 3))])]
14581   "TARGET_USE_FANCY_MATH_387
14582    && flag_unsafe_math_optimizations"
14583 {
14584   operands[2] = gen_reg_rtx (XFmode);
14585   emit_move_insn (operands[2], standard_80387_constant_rtx (4)); /* fldln2 */
14586 })
14587
14588 (define_expand "log<mode>2"
14589   [(use (match_operand:MODEF 0 "register_operand"))
14590    (use (match_operand:MODEF 1 "register_operand"))]
14591   "TARGET_USE_FANCY_MATH_387
14592    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14593        || TARGET_MIX_SSE_I387)
14594    && flag_unsafe_math_optimizations"
14595 {
14596   rtx op0 = gen_reg_rtx (XFmode);
14597
14598   rtx op2 = gen_reg_rtx (XFmode);
14599   emit_move_insn (op2, standard_80387_constant_rtx (4)); /* fldln2 */
14600
14601   emit_insn (gen_fyl2x_extend<mode>xf3_i387 (op0, operands[1], op2));
14602   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14603   DONE;
14604 })
14605
14606 (define_expand "log10xf2"
14607   [(parallel [(set (match_operand:XF 0 "register_operand")
14608                    (unspec:XF [(match_operand:XF 1 "register_operand")
14609                                (match_dup 2)] UNSPEC_FYL2X))
14610               (clobber (match_scratch:XF 3))])]
14611   "TARGET_USE_FANCY_MATH_387
14612    && flag_unsafe_math_optimizations"
14613 {
14614   operands[2] = gen_reg_rtx (XFmode);
14615   emit_move_insn (operands[2], standard_80387_constant_rtx (3)); /* fldlg2 */
14616 })
14617
14618 (define_expand "log10<mode>2"
14619   [(use (match_operand:MODEF 0 "register_operand"))
14620    (use (match_operand:MODEF 1 "register_operand"))]
14621   "TARGET_USE_FANCY_MATH_387
14622    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14623        || TARGET_MIX_SSE_I387)
14624    && flag_unsafe_math_optimizations"
14625 {
14626   rtx op0 = gen_reg_rtx (XFmode);
14627
14628   rtx op2 = gen_reg_rtx (XFmode);
14629   emit_move_insn (op2, standard_80387_constant_rtx (3)); /* fldlg2 */
14630
14631   emit_insn (gen_fyl2x_extend<mode>xf3_i387 (op0, operands[1], op2));
14632   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14633   DONE;
14634 })
14635
14636 (define_expand "log2xf2"
14637   [(parallel [(set (match_operand:XF 0 "register_operand")
14638                    (unspec:XF [(match_operand:XF 1 "register_operand")
14639                                (match_dup 2)] UNSPEC_FYL2X))
14640               (clobber (match_scratch:XF 3))])]
14641   "TARGET_USE_FANCY_MATH_387
14642    && flag_unsafe_math_optimizations"
14643 {
14644   operands[2] = gen_reg_rtx (XFmode);
14645   emit_move_insn (operands[2], CONST1_RTX (XFmode)); /* fld1 */
14646 })
14647
14648 (define_expand "log2<mode>2"
14649   [(use (match_operand:MODEF 0 "register_operand"))
14650    (use (match_operand:MODEF 1 "register_operand"))]
14651   "TARGET_USE_FANCY_MATH_387
14652    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14653        || TARGET_MIX_SSE_I387)
14654    && flag_unsafe_math_optimizations"
14655 {
14656   rtx op0 = gen_reg_rtx (XFmode);
14657
14658   rtx op2 = gen_reg_rtx (XFmode);
14659   emit_move_insn (op2, CONST1_RTX (XFmode)); /* fld1 */
14660
14661   emit_insn (gen_fyl2x_extend<mode>xf3_i387 (op0, operands[1], op2));
14662   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14663   DONE;
14664 })
14665
14666 (define_insn "fyl2xp1xf3_i387"
14667   [(set (match_operand:XF 0 "register_operand" "=f")
14668         (unspec:XF [(match_operand:XF 1 "register_operand" "0")
14669                     (match_operand:XF 2 "register_operand" "u")]
14670                    UNSPEC_FYL2XP1))
14671    (clobber (match_scratch:XF 3 "=2"))]
14672   "TARGET_USE_FANCY_MATH_387
14673    && flag_unsafe_math_optimizations"
14674   "fyl2xp1"
14675   [(set_attr "type" "fpspc")
14676    (set_attr "mode" "XF")])
14677
14678 (define_insn "fyl2xp1_extend<mode>xf3_i387"
14679   [(set (match_operand:XF 0 "register_operand" "=f")
14680         (unspec:XF [(float_extend:XF
14681                       (match_operand:MODEF 1 "register_operand" "0"))
14682                     (match_operand:XF 2 "register_operand" "u")]
14683                    UNSPEC_FYL2XP1))
14684    (clobber (match_scratch:XF 3 "=2"))]
14685   "TARGET_USE_FANCY_MATH_387
14686    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14687        || TARGET_MIX_SSE_I387)
14688    && flag_unsafe_math_optimizations"
14689   "fyl2xp1"
14690   [(set_attr "type" "fpspc")
14691    (set_attr "mode" "XF")])
14692
14693 (define_expand "log1pxf2"
14694   [(use (match_operand:XF 0 "register_operand"))
14695    (use (match_operand:XF 1 "register_operand"))]
14696   "TARGET_USE_FANCY_MATH_387
14697    && flag_unsafe_math_optimizations"
14698 {
14699   if (optimize_insn_for_size_p ())
14700     FAIL;
14701
14702   ix86_emit_i387_log1p (operands[0], operands[1]);
14703   DONE;
14704 })
14705
14706 (define_expand "log1p<mode>2"
14707   [(use (match_operand:MODEF 0 "register_operand"))
14708    (use (match_operand:MODEF 1 "register_operand"))]
14709   "TARGET_USE_FANCY_MATH_387
14710    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14711        || TARGET_MIX_SSE_I387)
14712    && flag_unsafe_math_optimizations"
14713 {
14714   rtx op0;
14715
14716   if (optimize_insn_for_size_p ())
14717     FAIL;
14718
14719   op0 = gen_reg_rtx (XFmode);
14720
14721   operands[1] = gen_rtx_FLOAT_EXTEND (XFmode, operands[1]);
14722
14723   ix86_emit_i387_log1p (op0, operands[1]);
14724   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14725   DONE;
14726 })
14727
14728 (define_insn "fxtractxf3_i387"
14729   [(set (match_operand:XF 0 "register_operand" "=f")
14730         (unspec:XF [(match_operand:XF 2 "register_operand" "0")]
14731                    UNSPEC_XTRACT_FRACT))
14732    (set (match_operand:XF 1 "register_operand" "=u")
14733         (unspec:XF [(match_dup 2)] UNSPEC_XTRACT_EXP))]
14734   "TARGET_USE_FANCY_MATH_387
14735    && flag_unsafe_math_optimizations"
14736   "fxtract"
14737   [(set_attr "type" "fpspc")
14738    (set_attr "mode" "XF")])
14739
14740 (define_insn "fxtract_extend<mode>xf3_i387"
14741   [(set (match_operand:XF 0 "register_operand" "=f")
14742         (unspec:XF [(float_extend:XF
14743                       (match_operand:MODEF 2 "register_operand" "0"))]
14744                    UNSPEC_XTRACT_FRACT))
14745    (set (match_operand:XF 1 "register_operand" "=u")
14746         (unspec:XF [(float_extend:XF (match_dup 2))] UNSPEC_XTRACT_EXP))]
14747   "TARGET_USE_FANCY_MATH_387
14748    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14749        || TARGET_MIX_SSE_I387)
14750    && flag_unsafe_math_optimizations"
14751   "fxtract"
14752   [(set_attr "type" "fpspc")
14753    (set_attr "mode" "XF")])
14754
14755 (define_expand "logbxf2"
14756   [(parallel [(set (match_dup 2)
14757                    (unspec:XF [(match_operand:XF 1 "register_operand")]
14758                               UNSPEC_XTRACT_FRACT))
14759               (set (match_operand:XF 0 "register_operand")
14760                    (unspec:XF [(match_dup 1)] UNSPEC_XTRACT_EXP))])]
14761   "TARGET_USE_FANCY_MATH_387
14762    && flag_unsafe_math_optimizations"
14763   "operands[2] = gen_reg_rtx (XFmode);")
14764
14765 (define_expand "logb<mode>2"
14766   [(use (match_operand:MODEF 0 "register_operand"))
14767    (use (match_operand:MODEF 1 "register_operand"))]
14768   "TARGET_USE_FANCY_MATH_387
14769    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14770        || TARGET_MIX_SSE_I387)
14771    && flag_unsafe_math_optimizations"
14772 {
14773   rtx op0 = gen_reg_rtx (XFmode);
14774   rtx op1 = gen_reg_rtx (XFmode);
14775
14776   emit_insn (gen_fxtract_extend<mode>xf3_i387 (op0, op1, operands[1]));
14777   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op1));
14778   DONE;
14779 })
14780
14781 (define_expand "ilogbxf2"
14782   [(use (match_operand:SI 0 "register_operand"))
14783    (use (match_operand:XF 1 "register_operand"))]
14784   "TARGET_USE_FANCY_MATH_387
14785    && flag_unsafe_math_optimizations"
14786 {
14787   rtx op0, op1;
14788
14789   if (optimize_insn_for_size_p ())
14790     FAIL;
14791
14792   op0 = gen_reg_rtx (XFmode);
14793   op1 = gen_reg_rtx (XFmode);
14794
14795   emit_insn (gen_fxtractxf3_i387 (op0, op1, operands[1]));
14796   emit_insn (gen_fix_truncxfsi2 (operands[0], op1));
14797   DONE;
14798 })
14799
14800 (define_expand "ilogb<mode>2"
14801   [(use (match_operand:SI 0 "register_operand"))
14802    (use (match_operand:MODEF 1 "register_operand"))]
14803   "TARGET_USE_FANCY_MATH_387
14804    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14805        || TARGET_MIX_SSE_I387)
14806    && flag_unsafe_math_optimizations"
14807 {
14808   rtx op0, op1;
14809
14810   if (optimize_insn_for_size_p ())
14811     FAIL;
14812
14813   op0 = gen_reg_rtx (XFmode);
14814   op1 = gen_reg_rtx (XFmode);
14815
14816   emit_insn (gen_fxtract_extend<mode>xf3_i387 (op0, op1, operands[1]));
14817   emit_insn (gen_fix_truncxfsi2 (operands[0], op1));
14818   DONE;
14819 })
14820
14821 (define_insn "*f2xm1xf2_i387"
14822   [(set (match_operand:XF 0 "register_operand" "=f")
14823         (unspec:XF [(match_operand:XF 1 "register_operand" "0")]
14824                    UNSPEC_F2XM1))]
14825   "TARGET_USE_FANCY_MATH_387
14826    && flag_unsafe_math_optimizations"
14827   "f2xm1"
14828   [(set_attr "type" "fpspc")
14829    (set_attr "mode" "XF")])
14830
14831 (define_insn "fscalexf4_i387"
14832   [(set (match_operand:XF 0 "register_operand" "=f")
14833         (unspec:XF [(match_operand:XF 2 "register_operand" "0")
14834                     (match_operand:XF 3 "register_operand" "1")]
14835                    UNSPEC_FSCALE_FRACT))
14836    (set (match_operand:XF 1 "register_operand" "=u")
14837         (unspec:XF [(match_dup 2) (match_dup 3)]
14838                    UNSPEC_FSCALE_EXP))]
14839   "TARGET_USE_FANCY_MATH_387
14840    && flag_unsafe_math_optimizations"
14841   "fscale"
14842   [(set_attr "type" "fpspc")
14843    (set_attr "mode" "XF")])
14844
14845 (define_expand "expNcorexf3"
14846   [(set (match_dup 3) (mult:XF (match_operand:XF 1 "register_operand")
14847                                (match_operand:XF 2 "register_operand")))
14848    (set (match_dup 4) (unspec:XF [(match_dup 3)] UNSPEC_FRNDINT))
14849    (set (match_dup 5) (minus:XF (match_dup 3) (match_dup 4)))
14850    (set (match_dup 6) (unspec:XF [(match_dup 5)] UNSPEC_F2XM1))
14851    (set (match_dup 8) (plus:XF (match_dup 6) (match_dup 7)))
14852    (parallel [(set (match_operand:XF 0 "register_operand")
14853                    (unspec:XF [(match_dup 8) (match_dup 4)]
14854                               UNSPEC_FSCALE_FRACT))
14855               (set (match_dup 9)
14856                    (unspec:XF [(match_dup 8) (match_dup 4)]
14857                               UNSPEC_FSCALE_EXP))])]
14858   "TARGET_USE_FANCY_MATH_387
14859    && flag_unsafe_math_optimizations"
14860 {
14861   int i;
14862
14863   if (optimize_insn_for_size_p ())
14864     FAIL;
14865
14866   for (i = 3; i < 10; i++)
14867     operands[i] = gen_reg_rtx (XFmode);
14868
14869   emit_move_insn (operands[7], CONST1_RTX (XFmode));  /* fld1 */
14870 })
14871
14872 (define_expand "expxf2"
14873   [(use (match_operand:XF 0 "register_operand"))
14874    (use (match_operand:XF 1 "register_operand"))]
14875   "TARGET_USE_FANCY_MATH_387
14876    && flag_unsafe_math_optimizations"
14877 {
14878   rtx op2;
14879
14880   if (optimize_insn_for_size_p ())
14881     FAIL;
14882
14883   op2 = gen_reg_rtx (XFmode);
14884   emit_move_insn (op2, standard_80387_constant_rtx (5)); /* fldl2e */
14885
14886   emit_insn (gen_expNcorexf3 (operands[0], operands[1], op2));
14887   DONE;
14888 })
14889
14890 (define_expand "exp<mode>2"
14891   [(use (match_operand:MODEF 0 "register_operand"))
14892    (use (match_operand:MODEF 1 "general_operand"))]
14893  "TARGET_USE_FANCY_MATH_387
14894    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14895        || TARGET_MIX_SSE_I387)
14896    && flag_unsafe_math_optimizations"
14897 {
14898   rtx op0, op1;
14899
14900   if (optimize_insn_for_size_p ())
14901     FAIL;
14902
14903   op0 = gen_reg_rtx (XFmode);
14904   op1 = gen_reg_rtx (XFmode);
14905
14906   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14907   emit_insn (gen_expxf2 (op0, op1));
14908   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14909   DONE;
14910 })
14911
14912 (define_expand "exp10xf2"
14913   [(use (match_operand:XF 0 "register_operand"))
14914    (use (match_operand:XF 1 "register_operand"))]
14915   "TARGET_USE_FANCY_MATH_387
14916    && flag_unsafe_math_optimizations"
14917 {
14918   rtx op2;
14919
14920   if (optimize_insn_for_size_p ())
14921     FAIL;
14922
14923   op2 = gen_reg_rtx (XFmode);
14924   emit_move_insn (op2, standard_80387_constant_rtx (6)); /* fldl2t */
14925
14926   emit_insn (gen_expNcorexf3 (operands[0], operands[1], op2));
14927   DONE;
14928 })
14929
14930 (define_expand "exp10<mode>2"
14931   [(use (match_operand:MODEF 0 "register_operand"))
14932    (use (match_operand:MODEF 1 "general_operand"))]
14933  "TARGET_USE_FANCY_MATH_387
14934    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14935        || TARGET_MIX_SSE_I387)
14936    && flag_unsafe_math_optimizations"
14937 {
14938   rtx op0, op1;
14939
14940   if (optimize_insn_for_size_p ())
14941     FAIL;
14942
14943   op0 = gen_reg_rtx (XFmode);
14944   op1 = gen_reg_rtx (XFmode);
14945
14946   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14947   emit_insn (gen_exp10xf2 (op0, op1));
14948   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14949   DONE;
14950 })
14951
14952 (define_expand "exp2xf2"
14953   [(use (match_operand:XF 0 "register_operand"))
14954    (use (match_operand:XF 1 "register_operand"))]
14955   "TARGET_USE_FANCY_MATH_387
14956    && flag_unsafe_math_optimizations"
14957 {
14958   rtx op2;
14959
14960   if (optimize_insn_for_size_p ())
14961     FAIL;
14962
14963   op2 = gen_reg_rtx (XFmode);
14964   emit_move_insn (op2, CONST1_RTX (XFmode));  /* fld1 */
14965
14966   emit_insn (gen_expNcorexf3 (operands[0], operands[1], op2));
14967   DONE;
14968 })
14969
14970 (define_expand "exp2<mode>2"
14971   [(use (match_operand:MODEF 0 "register_operand"))
14972    (use (match_operand:MODEF 1 "general_operand"))]
14973  "TARGET_USE_FANCY_MATH_387
14974    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
14975        || TARGET_MIX_SSE_I387)
14976    && flag_unsafe_math_optimizations"
14977 {
14978   rtx op0, op1;
14979
14980   if (optimize_insn_for_size_p ())
14981     FAIL;
14982
14983   op0 = gen_reg_rtx (XFmode);
14984   op1 = gen_reg_rtx (XFmode);
14985
14986   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
14987   emit_insn (gen_exp2xf2 (op0, op1));
14988   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
14989   DONE;
14990 })
14991
14992 (define_expand "expm1xf2"
14993   [(set (match_dup 3) (mult:XF (match_operand:XF 1 "register_operand")
14994                                (match_dup 2)))
14995    (set (match_dup 4) (unspec:XF [(match_dup 3)] UNSPEC_FRNDINT))
14996    (set (match_dup 5) (minus:XF (match_dup 3) (match_dup 4)))
14997    (set (match_dup 9) (float_extend:XF (match_dup 13)))
14998    (set (match_dup 6) (unspec:XF [(match_dup 5)] UNSPEC_F2XM1))
14999    (parallel [(set (match_dup 7)
15000                    (unspec:XF [(match_dup 6) (match_dup 4)]
15001                               UNSPEC_FSCALE_FRACT))
15002               (set (match_dup 8)
15003                    (unspec:XF [(match_dup 6) (match_dup 4)]
15004                               UNSPEC_FSCALE_EXP))])
15005    (parallel [(set (match_dup 10)
15006                    (unspec:XF [(match_dup 9) (match_dup 8)]
15007                               UNSPEC_FSCALE_FRACT))
15008               (set (match_dup 11)
15009                    (unspec:XF [(match_dup 9) (match_dup 8)]
15010                               UNSPEC_FSCALE_EXP))])
15011    (set (match_dup 12) (minus:XF (match_dup 10)
15012                                  (float_extend:XF (match_dup 13))))
15013    (set (match_operand:XF 0 "register_operand")
15014         (plus:XF (match_dup 12) (match_dup 7)))]
15015   "TARGET_USE_FANCY_MATH_387
15016    && flag_unsafe_math_optimizations"
15017 {
15018   int i;
15019
15020   if (optimize_insn_for_size_p ())
15021     FAIL;
15022
15023   for (i = 2; i < 13; i++)
15024     operands[i] = gen_reg_rtx (XFmode);
15025
15026   operands[13]
15027     = validize_mem (force_const_mem (SFmode, CONST1_RTX (SFmode))); /* fld1 */
15028
15029   emit_move_insn (operands[2], standard_80387_constant_rtx (5)); /* fldl2e */
15030 })
15031
15032 (define_expand "expm1<mode>2"
15033   [(use (match_operand:MODEF 0 "register_operand"))
15034    (use (match_operand:MODEF 1 "general_operand"))]
15035  "TARGET_USE_FANCY_MATH_387
15036    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15037        || TARGET_MIX_SSE_I387)
15038    && flag_unsafe_math_optimizations"
15039 {
15040   rtx op0, op1;
15041
15042   if (optimize_insn_for_size_p ())
15043     FAIL;
15044
15045   op0 = gen_reg_rtx (XFmode);
15046   op1 = gen_reg_rtx (XFmode);
15047
15048   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
15049   emit_insn (gen_expm1xf2 (op0, op1));
15050   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15051   DONE;
15052 })
15053
15054 (define_expand "ldexpxf3"
15055   [(match_operand:XF 0 "register_operand")
15056    (match_operand:XF 1 "register_operand")
15057    (match_operand:SI 2 "register_operand")]
15058   "TARGET_USE_FANCY_MATH_387
15059    && flag_unsafe_math_optimizations"
15060 {
15061   rtx tmp1, tmp2;
15062   if (optimize_insn_for_size_p ())
15063     FAIL;
15064
15065   tmp1 = gen_reg_rtx (XFmode);
15066   tmp2 = gen_reg_rtx (XFmode);
15067
15068   emit_insn (gen_floatsixf2 (tmp1, operands[2]));
15069   emit_insn (gen_fscalexf4_i387 (operands[0], tmp2,
15070                                  operands[1], tmp1));
15071   DONE;
15072 })
15073
15074 (define_expand "ldexp<mode>3"
15075   [(use (match_operand:MODEF 0 "register_operand"))
15076    (use (match_operand:MODEF 1 "general_operand"))
15077    (use (match_operand:SI 2 "register_operand"))]
15078  "TARGET_USE_FANCY_MATH_387
15079    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15080        || TARGET_MIX_SSE_I387)
15081    && flag_unsafe_math_optimizations"
15082 {
15083   rtx op0, op1;
15084
15085   if (optimize_insn_for_size_p ())
15086     FAIL;
15087
15088   op0 = gen_reg_rtx (XFmode);
15089   op1 = gen_reg_rtx (XFmode);
15090
15091   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
15092   emit_insn (gen_ldexpxf3 (op0, op1, operands[2]));
15093   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15094   DONE;
15095 })
15096
15097 (define_expand "scalbxf3"
15098   [(parallel [(set (match_operand:XF 0 " register_operand")
15099                    (unspec:XF [(match_operand:XF 1 "register_operand")
15100                                (match_operand:XF 2 "register_operand")]
15101                               UNSPEC_FSCALE_FRACT))
15102               (set (match_dup 3)
15103                    (unspec:XF [(match_dup 1) (match_dup 2)]
15104                               UNSPEC_FSCALE_EXP))])]
15105   "TARGET_USE_FANCY_MATH_387
15106    && flag_unsafe_math_optimizations"
15107 {
15108   if (optimize_insn_for_size_p ())
15109     FAIL;
15110
15111   operands[3] = gen_reg_rtx (XFmode);
15112 })
15113
15114 (define_expand "scalb<mode>3"
15115   [(use (match_operand:MODEF 0 "register_operand"))
15116    (use (match_operand:MODEF 1 "general_operand"))
15117    (use (match_operand:MODEF 2 "general_operand"))]
15118  "TARGET_USE_FANCY_MATH_387
15119    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15120        || TARGET_MIX_SSE_I387)
15121    && flag_unsafe_math_optimizations"
15122 {
15123   rtx op0, op1, op2;
15124
15125   if (optimize_insn_for_size_p ())
15126     FAIL;
15127
15128   op0 = gen_reg_rtx (XFmode);
15129   op1 = gen_reg_rtx (XFmode);
15130   op2 = gen_reg_rtx (XFmode);
15131
15132   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
15133   emit_insn (gen_extend<mode>xf2 (op2, operands[2]));
15134   emit_insn (gen_scalbxf3 (op0, op1, op2));
15135   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15136   DONE;
15137 })
15138
15139 (define_expand "significandxf2"
15140   [(parallel [(set (match_operand:XF 0 "register_operand")
15141                    (unspec:XF [(match_operand:XF 1 "register_operand")]
15142                               UNSPEC_XTRACT_FRACT))
15143               (set (match_dup 2)
15144                    (unspec:XF [(match_dup 1)] UNSPEC_XTRACT_EXP))])]
15145   "TARGET_USE_FANCY_MATH_387
15146    && flag_unsafe_math_optimizations"
15147   "operands[2] = gen_reg_rtx (XFmode);")
15148
15149 (define_expand "significand<mode>2"
15150   [(use (match_operand:MODEF 0 "register_operand"))
15151    (use (match_operand:MODEF 1 "register_operand"))]
15152   "TARGET_USE_FANCY_MATH_387
15153    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15154        || TARGET_MIX_SSE_I387)
15155    && flag_unsafe_math_optimizations"
15156 {
15157   rtx op0 = gen_reg_rtx (XFmode);
15158   rtx op1 = gen_reg_rtx (XFmode);
15159
15160   emit_insn (gen_fxtract_extend<mode>xf3_i387 (op0, op1, operands[1]));
15161   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15162   DONE;
15163 })
15164 \f
15165
15166 (define_insn "sse4_1_round<mode>2"
15167   [(set (match_operand:MODEF 0 "register_operand" "=x")
15168         (unspec:MODEF [(match_operand:MODEF 1 "register_operand" "x")
15169                        (match_operand:SI 2 "const_0_to_15_operand" "n")]
15170                       UNSPEC_ROUND))]
15171   "TARGET_ROUND"
15172   "%vround<ssemodesuffix>\t{%2, %1, %d0|%d0, %1, %2}"
15173   [(set_attr "type" "ssecvt")
15174    (set_attr "prefix_extra" "1")
15175    (set_attr "prefix" "maybe_vex")
15176    (set_attr "mode" "<MODE>")])
15177
15178 (define_insn "rintxf2"
15179   [(set (match_operand:XF 0 "register_operand" "=f")
15180         (unspec:XF [(match_operand:XF 1 "register_operand" "0")]
15181                    UNSPEC_FRNDINT))]
15182   "TARGET_USE_FANCY_MATH_387
15183    && flag_unsafe_math_optimizations"
15184   "frndint"
15185   [(set_attr "type" "fpspc")
15186    (set_attr "mode" "XF")])
15187
15188 (define_expand "rint<mode>2"
15189   [(use (match_operand:MODEF 0 "register_operand"))
15190    (use (match_operand:MODEF 1 "register_operand"))]
15191   "(TARGET_USE_FANCY_MATH_387
15192     && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15193         || TARGET_MIX_SSE_I387)
15194     && flag_unsafe_math_optimizations)
15195    || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
15196        && !flag_trapping_math)"
15197 {
15198   if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
15199       && !flag_trapping_math)
15200     {
15201       if (TARGET_ROUND)
15202         emit_insn (gen_sse4_1_round<mode>2
15203                    (operands[0], operands[1], GEN_INT (ROUND_MXCSR)));
15204       else if (optimize_insn_for_size_p ())
15205         FAIL;
15206       else
15207         ix86_expand_rint (operands[0], operands[1]);
15208     }
15209   else
15210     {
15211       rtx op0 = gen_reg_rtx (XFmode);
15212       rtx op1 = gen_reg_rtx (XFmode);
15213
15214       emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
15215       emit_insn (gen_rintxf2 (op0, op1));
15216
15217       emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15218     }
15219   DONE;
15220 })
15221
15222 (define_expand "round<mode>2"
15223   [(match_operand:X87MODEF 0 "register_operand")
15224    (match_operand:X87MODEF 1 "nonimmediate_operand")]
15225   "(TARGET_USE_FANCY_MATH_387
15226     && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15227         || TARGET_MIX_SSE_I387)
15228     && flag_unsafe_math_optimizations)
15229    || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
15230        && !flag_trapping_math && !flag_rounding_math)"
15231 {
15232   if (optimize_insn_for_size_p ())
15233     FAIL;
15234
15235   if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
15236       && !flag_trapping_math && !flag_rounding_math)
15237     {
15238       if (TARGET_ROUND)
15239         {
15240           operands[1] = force_reg (<MODE>mode, operands[1]);
15241           ix86_expand_round_sse4 (operands[0], operands[1]);
15242         }
15243       else if (TARGET_64BIT || (<MODE>mode != DFmode))
15244         ix86_expand_round (operands[0], operands[1]);
15245       else
15246         ix86_expand_rounddf_32 (operands[0], operands[1]);
15247     }
15248   else
15249     {
15250       operands[1] = force_reg (<MODE>mode, operands[1]);
15251       ix86_emit_i387_round (operands[0], operands[1]);
15252     }
15253   DONE;
15254 })
15255
15256 (define_insn_and_split "*fistdi2_1"
15257   [(set (match_operand:DI 0 "nonimmediate_operand")
15258         (unspec:DI [(match_operand:XF 1 "register_operand")]
15259                    UNSPEC_FIST))]
15260   "TARGET_USE_FANCY_MATH_387
15261    && can_create_pseudo_p ()"
15262   "#"
15263   "&& 1"
15264   [(const_int 0)]
15265 {
15266   if (memory_operand (operands[0], VOIDmode))
15267     emit_insn (gen_fistdi2 (operands[0], operands[1]));
15268   else
15269     {
15270       operands[2] = assign_386_stack_local (DImode, SLOT_TEMP);
15271       emit_insn (gen_fistdi2_with_temp (operands[0], operands[1],
15272                                          operands[2]));
15273     }
15274   DONE;
15275 }
15276   [(set_attr "type" "fpspc")
15277    (set_attr "mode" "DI")])
15278
15279 (define_insn "fistdi2"
15280   [(set (match_operand:DI 0 "memory_operand" "=m")
15281         (unspec:DI [(match_operand:XF 1 "register_operand" "f")]
15282                    UNSPEC_FIST))
15283    (clobber (match_scratch:XF 2 "=&1f"))]
15284   "TARGET_USE_FANCY_MATH_387"
15285   "* return output_fix_trunc (insn, operands, false);"
15286   [(set_attr "type" "fpspc")
15287    (set_attr "mode" "DI")])
15288
15289 (define_insn "fistdi2_with_temp"
15290   [(set (match_operand:DI 0 "nonimmediate_operand" "=m,?r")
15291         (unspec:DI [(match_operand:XF 1 "register_operand" "f,f")]
15292                    UNSPEC_FIST))
15293    (clobber (match_operand:DI 2 "memory_operand" "=X,m"))
15294    (clobber (match_scratch:XF 3 "=&1f,&1f"))]
15295   "TARGET_USE_FANCY_MATH_387"
15296   "#"
15297   [(set_attr "type" "fpspc")
15298    (set_attr "mode" "DI")])
15299
15300 (define_split
15301   [(set (match_operand:DI 0 "register_operand")
15302         (unspec:DI [(match_operand:XF 1 "register_operand")]
15303                    UNSPEC_FIST))
15304    (clobber (match_operand:DI 2 "memory_operand"))
15305    (clobber (match_scratch 3))]
15306   "reload_completed"
15307   [(parallel [(set (match_dup 2) (unspec:DI [(match_dup 1)] UNSPEC_FIST))
15308               (clobber (match_dup 3))])
15309    (set (match_dup 0) (match_dup 2))])
15310
15311 (define_split
15312   [(set (match_operand:DI 0 "memory_operand")
15313         (unspec:DI [(match_operand:XF 1 "register_operand")]
15314                    UNSPEC_FIST))
15315    (clobber (match_operand:DI 2 "memory_operand"))
15316    (clobber (match_scratch 3))]
15317   "reload_completed"
15318   [(parallel [(set (match_dup 0) (unspec:DI [(match_dup 1)] UNSPEC_FIST))
15319               (clobber (match_dup 3))])])
15320
15321 (define_insn_and_split "*fist<mode>2_1"
15322   [(set (match_operand:SWI24 0 "register_operand")
15323         (unspec:SWI24 [(match_operand:XF 1 "register_operand")]
15324                       UNSPEC_FIST))]
15325   "TARGET_USE_FANCY_MATH_387
15326    && can_create_pseudo_p ()"
15327   "#"
15328   "&& 1"
15329   [(const_int 0)]
15330 {
15331   operands[2] = assign_386_stack_local (<MODE>mode, SLOT_TEMP);
15332   emit_insn (gen_fist<mode>2_with_temp (operands[0], operands[1],
15333                                         operands[2]));
15334   DONE;
15335 }
15336   [(set_attr "type" "fpspc")
15337    (set_attr "mode" "<MODE>")])
15338
15339 (define_insn "fist<mode>2"
15340   [(set (match_operand:SWI24 0 "memory_operand" "=m")
15341         (unspec:SWI24 [(match_operand:XF 1 "register_operand" "f")]
15342                       UNSPEC_FIST))]
15343   "TARGET_USE_FANCY_MATH_387"
15344   "* return output_fix_trunc (insn, operands, false);"
15345   [(set_attr "type" "fpspc")
15346    (set_attr "mode" "<MODE>")])
15347
15348 (define_insn "fist<mode>2_with_temp"
15349   [(set (match_operand:SWI24 0 "register_operand" "=r")
15350         (unspec:SWI24 [(match_operand:XF 1 "register_operand" "f")]
15351                       UNSPEC_FIST))
15352    (clobber (match_operand:SWI24 2 "memory_operand" "=m"))]
15353   "TARGET_USE_FANCY_MATH_387"
15354   "#"
15355   [(set_attr "type" "fpspc")
15356    (set_attr "mode" "<MODE>")])
15357
15358 (define_split
15359   [(set (match_operand:SWI24 0 "register_operand")
15360         (unspec:SWI24 [(match_operand:XF 1 "register_operand")]
15361                       UNSPEC_FIST))
15362    (clobber (match_operand:SWI24 2 "memory_operand"))]
15363   "reload_completed"
15364   [(set (match_dup 2) (unspec:SWI24 [(match_dup 1)] UNSPEC_FIST))
15365    (set (match_dup 0) (match_dup 2))])
15366
15367 (define_split
15368   [(set (match_operand:SWI24 0 "memory_operand")
15369         (unspec:SWI24 [(match_operand:XF 1 "register_operand")]
15370                       UNSPEC_FIST))
15371    (clobber (match_operand:SWI24 2 "memory_operand"))]
15372   "reload_completed"
15373   [(set (match_dup 0) (unspec:SWI24 [(match_dup 1)] UNSPEC_FIST))])
15374
15375 (define_expand "lrintxf<mode>2"
15376   [(set (match_operand:SWI248x 0 "nonimmediate_operand")
15377      (unspec:SWI248x [(match_operand:XF 1 "register_operand")]
15378                      UNSPEC_FIST))]
15379   "TARGET_USE_FANCY_MATH_387")
15380
15381 (define_expand "lrint<MODEF:mode><SWI48:mode>2"
15382   [(set (match_operand:SWI48 0 "nonimmediate_operand")
15383      (unspec:SWI48 [(match_operand:MODEF 1 "register_operand")]
15384                    UNSPEC_FIX_NOTRUNC))]
15385   "SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH")
15386
15387 (define_expand "lround<X87MODEF:mode><SWI248x:mode>2"
15388   [(match_operand:SWI248x 0 "nonimmediate_operand")
15389    (match_operand:X87MODEF 1 "register_operand")]
15390   "(TARGET_USE_FANCY_MATH_387
15391     && (!(SSE_FLOAT_MODE_P (<X87MODEF:MODE>mode) && TARGET_SSE_MATH)
15392         || TARGET_MIX_SSE_I387)
15393     && flag_unsafe_math_optimizations)
15394    || (SSE_FLOAT_MODE_P (<X87MODEF:MODE>mode) && TARGET_SSE_MATH
15395        && <SWI248x:MODE>mode != HImode 
15396        && ((<SWI248x:MODE>mode != DImode) || TARGET_64BIT)
15397        && !flag_trapping_math && !flag_rounding_math)"
15398 {
15399   if (optimize_insn_for_size_p ())
15400     FAIL;
15401
15402   if (SSE_FLOAT_MODE_P (<X87MODEF:MODE>mode) && TARGET_SSE_MATH
15403       && <SWI248x:MODE>mode != HImode
15404       && ((<SWI248x:MODE>mode != DImode) || TARGET_64BIT)
15405       && !flag_trapping_math && !flag_rounding_math)
15406     ix86_expand_lround (operands[0], operands[1]);
15407   else
15408     ix86_emit_i387_round (operands[0], operands[1]);
15409   DONE;
15410 })
15411
15412 (define_int_iterator FRNDINT_ROUNDING
15413         [UNSPEC_FRNDINT_FLOOR
15414          UNSPEC_FRNDINT_CEIL
15415          UNSPEC_FRNDINT_TRUNC])
15416
15417 (define_int_iterator FIST_ROUNDING
15418         [UNSPEC_FIST_FLOOR
15419          UNSPEC_FIST_CEIL])
15420
15421 ;; Base name for define_insn
15422 (define_int_attr rounding_insn
15423         [(UNSPEC_FRNDINT_FLOOR "floor")
15424          (UNSPEC_FRNDINT_CEIL "ceil")
15425          (UNSPEC_FRNDINT_TRUNC "btrunc")
15426          (UNSPEC_FIST_FLOOR "floor")
15427          (UNSPEC_FIST_CEIL "ceil")])
15428
15429 (define_int_attr rounding
15430         [(UNSPEC_FRNDINT_FLOOR "floor")
15431          (UNSPEC_FRNDINT_CEIL "ceil")
15432          (UNSPEC_FRNDINT_TRUNC "trunc")
15433          (UNSPEC_FIST_FLOOR "floor")
15434          (UNSPEC_FIST_CEIL "ceil")])
15435
15436 (define_int_attr ROUNDING
15437         [(UNSPEC_FRNDINT_FLOOR "FLOOR")
15438          (UNSPEC_FRNDINT_CEIL "CEIL")
15439          (UNSPEC_FRNDINT_TRUNC "TRUNC")
15440          (UNSPEC_FIST_FLOOR "FLOOR")
15441          (UNSPEC_FIST_CEIL "CEIL")])
15442
15443 ;; Rounding mode control word calculation could clobber FLAGS_REG.
15444 (define_insn_and_split "frndintxf2_<rounding>"
15445   [(set (match_operand:XF 0 "register_operand")
15446         (unspec:XF [(match_operand:XF 1 "register_operand")]
15447                    FRNDINT_ROUNDING))
15448    (clobber (reg:CC FLAGS_REG))]
15449   "TARGET_USE_FANCY_MATH_387
15450    && flag_unsafe_math_optimizations
15451    && can_create_pseudo_p ()"
15452   "#"
15453   "&& 1"
15454   [(const_int 0)]
15455 {
15456   ix86_optimize_mode_switching[I387_<ROUNDING>] = 1;
15457
15458   operands[2] = assign_386_stack_local (HImode, SLOT_CW_STORED);
15459   operands[3] = assign_386_stack_local (HImode, SLOT_CW_<ROUNDING>);
15460
15461   emit_insn (gen_frndintxf2_<rounding>_i387 (operands[0], operands[1],
15462                                              operands[2], operands[3]));
15463   DONE;
15464 }
15465   [(set_attr "type" "frndint")
15466    (set_attr "i387_cw" "<rounding>")
15467    (set_attr "mode" "XF")])
15468
15469 (define_insn "frndintxf2_<rounding>_i387"
15470   [(set (match_operand:XF 0 "register_operand" "=f")
15471         (unspec:XF [(match_operand:XF 1 "register_operand" "0")]
15472                    FRNDINT_ROUNDING))
15473    (use (match_operand:HI 2 "memory_operand" "m"))
15474    (use (match_operand:HI 3 "memory_operand" "m"))]
15475   "TARGET_USE_FANCY_MATH_387
15476    && flag_unsafe_math_optimizations"
15477   "fldcw\t%3\n\tfrndint\n\tfldcw\t%2"
15478   [(set_attr "type" "frndint")
15479    (set_attr "i387_cw" "<rounding>")
15480    (set_attr "mode" "XF")])
15481
15482 (define_expand "<rounding_insn>xf2"
15483   [(parallel [(set (match_operand:XF 0 "register_operand")
15484                    (unspec:XF [(match_operand:XF 1 "register_operand")]
15485                               FRNDINT_ROUNDING))
15486               (clobber (reg:CC FLAGS_REG))])]
15487   "TARGET_USE_FANCY_MATH_387
15488    && flag_unsafe_math_optimizations
15489    && !optimize_insn_for_size_p ()")
15490
15491 (define_expand "<rounding_insn><mode>2"
15492   [(parallel [(set (match_operand:MODEF 0 "register_operand")
15493                    (unspec:MODEF [(match_operand:MODEF 1 "register_operand")]
15494                                  FRNDINT_ROUNDING))
15495               (clobber (reg:CC FLAGS_REG))])]
15496   "(TARGET_USE_FANCY_MATH_387
15497     && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15498         || TARGET_MIX_SSE_I387)
15499     && flag_unsafe_math_optimizations)
15500    || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
15501        && !flag_trapping_math)"
15502 {
15503   if (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH
15504       && !flag_trapping_math)
15505     {
15506       if (TARGET_ROUND)
15507         emit_insn (gen_sse4_1_round<mode>2
15508                    (operands[0], operands[1], GEN_INT (ROUND_<ROUNDING>)));
15509       else if (optimize_insn_for_size_p ())
15510         FAIL;
15511       else if (TARGET_64BIT || (<MODE>mode != DFmode))
15512         {
15513           if (ROUND_<ROUNDING> == ROUND_FLOOR)
15514             ix86_expand_floorceil (operands[0], operands[1], true);
15515           else if (ROUND_<ROUNDING> == ROUND_CEIL)
15516             ix86_expand_floorceil (operands[0], operands[1], false);
15517           else if (ROUND_<ROUNDING> == ROUND_TRUNC)
15518             ix86_expand_trunc (operands[0], operands[1]);
15519           else
15520             gcc_unreachable ();
15521         }
15522       else
15523         {
15524           if (ROUND_<ROUNDING> == ROUND_FLOOR)
15525             ix86_expand_floorceildf_32 (operands[0], operands[1], true);
15526           else if (ROUND_<ROUNDING> == ROUND_CEIL)
15527             ix86_expand_floorceildf_32 (operands[0], operands[1], false);
15528           else if (ROUND_<ROUNDING> == ROUND_TRUNC)
15529             ix86_expand_truncdf_32 (operands[0], operands[1]);
15530           else
15531             gcc_unreachable ();
15532         }
15533     }
15534   else
15535     {
15536       rtx op0, op1;
15537
15538       if (optimize_insn_for_size_p ())
15539         FAIL;
15540
15541       op0 = gen_reg_rtx (XFmode);
15542       op1 = gen_reg_rtx (XFmode);
15543       emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
15544       emit_insn (gen_frndintxf2_<rounding> (op0, op1));
15545
15546       emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15547     }
15548   DONE;
15549 })
15550
15551 ;; Rounding mode control word calculation could clobber FLAGS_REG.
15552 (define_insn_and_split "frndintxf2_mask_pm"
15553   [(set (match_operand:XF 0 "register_operand")
15554         (unspec:XF [(match_operand:XF 1 "register_operand")]
15555                    UNSPEC_FRNDINT_MASK_PM))
15556    (clobber (reg:CC FLAGS_REG))]
15557   "TARGET_USE_FANCY_MATH_387
15558    && flag_unsafe_math_optimizations
15559    && can_create_pseudo_p ()"
15560   "#"
15561   "&& 1"
15562   [(const_int 0)]
15563 {
15564   ix86_optimize_mode_switching[I387_MASK_PM] = 1;
15565
15566   operands[2] = assign_386_stack_local (HImode, SLOT_CW_STORED);
15567   operands[3] = assign_386_stack_local (HImode, SLOT_CW_MASK_PM);
15568
15569   emit_insn (gen_frndintxf2_mask_pm_i387 (operands[0], operands[1],
15570                                           operands[2], operands[3]));
15571   DONE;
15572 }
15573   [(set_attr "type" "frndint")
15574    (set_attr "i387_cw" "mask_pm")
15575    (set_attr "mode" "XF")])
15576
15577 (define_insn "frndintxf2_mask_pm_i387"
15578   [(set (match_operand:XF 0 "register_operand" "=f")
15579         (unspec:XF [(match_operand:XF 1 "register_operand" "0")]
15580                    UNSPEC_FRNDINT_MASK_PM))
15581    (use (match_operand:HI 2 "memory_operand" "m"))
15582    (use (match_operand:HI 3 "memory_operand" "m"))]
15583   "TARGET_USE_FANCY_MATH_387
15584    && flag_unsafe_math_optimizations"
15585   "fldcw\t%3\n\tfrndint\n\tfclex\n\tfldcw\t%2"
15586   [(set_attr "type" "frndint")
15587    (set_attr "i387_cw" "mask_pm")
15588    (set_attr "mode" "XF")])
15589
15590 (define_expand "nearbyintxf2"
15591   [(parallel [(set (match_operand:XF 0 "register_operand")
15592                    (unspec:XF [(match_operand:XF 1 "register_operand")]
15593                               UNSPEC_FRNDINT_MASK_PM))
15594               (clobber (reg:CC FLAGS_REG))])]
15595   "TARGET_USE_FANCY_MATH_387
15596    && flag_unsafe_math_optimizations")
15597
15598 (define_expand "nearbyint<mode>2"
15599   [(use (match_operand:MODEF 0 "register_operand"))
15600    (use (match_operand:MODEF 1 "register_operand"))]
15601   "TARGET_USE_FANCY_MATH_387
15602    && (!(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)
15603        || TARGET_MIX_SSE_I387)
15604    && flag_unsafe_math_optimizations"
15605 {
15606   rtx op0 = gen_reg_rtx (XFmode);
15607   rtx op1 = gen_reg_rtx (XFmode);
15608
15609   emit_insn (gen_extend<mode>xf2 (op1, operands[1]));
15610   emit_insn (gen_frndintxf2_mask_pm (op0, op1));
15611
15612   emit_insn (gen_truncxf<mode>2_i387_noop (operands[0], op0));
15613   DONE;
15614 })
15615
15616 ;; Rounding mode control word calculation could clobber FLAGS_REG.
15617 (define_insn_and_split "*fist<mode>2_<rounding>_1"
15618   [(set (match_operand:SWI248x 0 "nonimmediate_operand")
15619         (unspec:SWI248x [(match_operand:XF 1 "register_operand")]
15620                         FIST_ROUNDING))
15621    (clobber (reg:CC FLAGS_REG))]
15622   "TARGET_USE_FANCY_MATH_387
15623    && flag_unsafe_math_optimizations
15624    && can_create_pseudo_p ()"
15625   "#"
15626   "&& 1"
15627   [(const_int 0)]
15628 {
15629   ix86_optimize_mode_switching[I387_<ROUNDING>] = 1;
15630
15631   operands[2] = assign_386_stack_local (HImode, SLOT_CW_STORED);
15632   operands[3] = assign_386_stack_local (HImode, SLOT_CW_<ROUNDING>);
15633   if (memory_operand (operands[0], VOIDmode))
15634     emit_insn (gen_fist<mode>2_<rounding> (operands[0], operands[1],
15635                                            operands[2], operands[3]));
15636   else
15637     {
15638       operands[4] = assign_386_stack_local (<MODE>mode, SLOT_TEMP);
15639       emit_insn (gen_fist<mode>2_<rounding>_with_temp
15640                   (operands[0], operands[1], operands[2],
15641                    operands[3], operands[4]));
15642     }
15643   DONE;
15644 }
15645   [(set_attr "type" "fistp")
15646    (set_attr "i387_cw" "<rounding>")
15647    (set_attr "mode" "<MODE>")])
15648
15649 (define_insn "fistdi2_<rounding>"
15650   [(set (match_operand:DI 0 "memory_operand" "=m")
15651         (unspec:DI [(match_operand:XF 1 "register_operand" "f")]
15652                    FIST_ROUNDING))
15653    (use (match_operand:HI 2 "memory_operand" "m"))
15654    (use (match_operand:HI 3 "memory_operand" "m"))
15655    (clobber (match_scratch:XF 4 "=&1f"))]
15656   "TARGET_USE_FANCY_MATH_387
15657    && flag_unsafe_math_optimizations"
15658   "* return output_fix_trunc (insn, operands, false);"
15659   [(set_attr "type" "fistp")
15660    (set_attr "i387_cw" "<rounding>")
15661    (set_attr "mode" "DI")])
15662
15663 (define_insn "fistdi2_<rounding>_with_temp"
15664   [(set (match_operand:DI 0 "nonimmediate_operand" "=m,?r")
15665         (unspec:DI [(match_operand:XF 1 "register_operand" "f,f")]
15666                    FIST_ROUNDING))
15667    (use (match_operand:HI 2 "memory_operand" "m,m"))
15668    (use (match_operand:HI 3 "memory_operand" "m,m"))
15669    (clobber (match_operand:DI 4 "memory_operand" "=X,m"))
15670    (clobber (match_scratch:XF 5 "=&1f,&1f"))]
15671   "TARGET_USE_FANCY_MATH_387
15672    && flag_unsafe_math_optimizations"
15673   "#"
15674   [(set_attr "type" "fistp")
15675    (set_attr "i387_cw" "<rounding>")
15676    (set_attr "mode" "DI")])
15677
15678 (define_split
15679   [(set (match_operand:DI 0 "register_operand")
15680         (unspec:DI [(match_operand:XF 1 "register_operand")]
15681                    FIST_ROUNDING))
15682    (use (match_operand:HI 2 "memory_operand"))
15683    (use (match_operand:HI 3 "memory_operand"))
15684    (clobber (match_operand:DI 4 "memory_operand"))
15685    (clobber (match_scratch 5))]
15686   "reload_completed"
15687   [(parallel [(set (match_dup 4)
15688                    (unspec:DI [(match_dup 1)] FIST_ROUNDING))
15689               (use (match_dup 2))
15690               (use (match_dup 3))
15691               (clobber (match_dup 5))])
15692    (set (match_dup 0) (match_dup 4))])
15693
15694 (define_split
15695   [(set (match_operand:DI 0 "memory_operand")
15696         (unspec:DI [(match_operand:XF 1 "register_operand")]
15697                    FIST_ROUNDING))
15698    (use (match_operand:HI 2 "memory_operand"))
15699    (use (match_operand:HI 3 "memory_operand"))
15700    (clobber (match_operand:DI 4 "memory_operand"))
15701    (clobber (match_scratch 5))]
15702   "reload_completed"
15703   [(parallel [(set (match_dup 0)
15704                    (unspec:DI [(match_dup 1)] FIST_ROUNDING))
15705               (use (match_dup 2))
15706               (use (match_dup 3))
15707               (clobber (match_dup 5))])])
15708
15709 (define_insn "fist<mode>2_<rounding>"
15710   [(set (match_operand:SWI24 0 "memory_operand" "=m")
15711         (unspec:SWI24 [(match_operand:XF 1 "register_operand" "f")]
15712                       FIST_ROUNDING))
15713    (use (match_operand:HI 2 "memory_operand" "m"))
15714    (use (match_operand:HI 3 "memory_operand" "m"))]
15715   "TARGET_USE_FANCY_MATH_387
15716    && flag_unsafe_math_optimizations"
15717   "* return output_fix_trunc (insn, operands, false);"
15718   [(set_attr "type" "fistp")
15719    (set_attr "i387_cw" "<rounding>")
15720    (set_attr "mode" "<MODE>")])
15721
15722 (define_insn "fist<mode>2_<rounding>_with_temp"
15723   [(set (match_operand:SWI24 0 "nonimmediate_operand" "=m,?r")
15724         (unspec:SWI24 [(match_operand:XF 1 "register_operand" "f,f")]
15725                       FIST_ROUNDING))
15726    (use (match_operand:HI 2 "memory_operand" "m,m"))
15727    (use (match_operand:HI 3 "memory_operand" "m,m"))
15728    (clobber (match_operand:SWI24 4 "memory_operand" "=X,m"))]
15729   "TARGET_USE_FANCY_MATH_387
15730    && flag_unsafe_math_optimizations"
15731   "#"
15732   [(set_attr "type" "fistp")
15733    (set_attr "i387_cw" "<rounding>")
15734    (set_attr "mode" "<MODE>")])
15735
15736 (define_split
15737   [(set (match_operand:SWI24 0 "register_operand")
15738         (unspec:SWI24 [(match_operand:XF 1 "register_operand")]
15739                       FIST_ROUNDING))
15740    (use (match_operand:HI 2 "memory_operand"))
15741    (use (match_operand:HI 3 "memory_operand"))
15742    (clobber (match_operand:SWI24 4 "memory_operand"))]
15743   "reload_completed"
15744   [(parallel [(set (match_dup 4)
15745                    (unspec:SWI24 [(match_dup 1)] FIST_ROUNDING))
15746               (use (match_dup 2))
15747               (use (match_dup 3))])
15748    (set (match_dup 0) (match_dup 4))])
15749
15750 (define_split
15751   [(set (match_operand:SWI24 0 "memory_operand")
15752         (unspec:SWI24 [(match_operand:XF 1 "register_operand")]
15753                       FIST_ROUNDING))
15754    (use (match_operand:HI 2 "memory_operand"))
15755    (use (match_operand:HI 3 "memory_operand"))
15756    (clobber (match_operand:SWI24 4 "memory_operand"))]
15757   "reload_completed"
15758   [(parallel [(set (match_dup 0)
15759                    (unspec:SWI24 [(match_dup 1)] FIST_ROUNDING))
15760               (use (match_dup 2))
15761               (use (match_dup 3))])])
15762
15763 (define_expand "l<rounding_insn>xf<mode>2"
15764   [(parallel [(set (match_operand:SWI248x 0 "nonimmediate_operand")
15765                    (unspec:SWI248x [(match_operand:XF 1 "register_operand")]
15766                                    FIST_ROUNDING))
15767               (clobber (reg:CC FLAGS_REG))])]
15768   "TARGET_USE_FANCY_MATH_387
15769    && (!TARGET_SSE_MATH || TARGET_MIX_SSE_I387)
15770    && flag_unsafe_math_optimizations")
15771
15772 (define_expand "l<rounding_insn><MODEF:mode><SWI48:mode>2"
15773   [(parallel [(set (match_operand:SWI48 0 "nonimmediate_operand")
15774                    (unspec:SWI48 [(match_operand:MODEF 1 "register_operand")]
15775                                  FIST_ROUNDING))
15776               (clobber (reg:CC FLAGS_REG))])]
15777   "SSE_FLOAT_MODE_P (<MODEF:MODE>mode) && TARGET_SSE_MATH
15778    && !flag_trapping_math"
15779 {
15780   if (TARGET_64BIT && optimize_insn_for_size_p ())
15781     FAIL;
15782
15783   if (ROUND_<ROUNDING> == ROUND_FLOOR)
15784     ix86_expand_lfloorceil (operands[0], operands[1], true);
15785   else if (ROUND_<ROUNDING> == ROUND_CEIL)
15786     ix86_expand_lfloorceil (operands[0], operands[1], false);
15787   else
15788     gcc_unreachable ();
15789
15790   DONE;
15791 })
15792
15793 (define_insn "fxam<mode>2_i387"
15794   [(set (match_operand:HI 0 "register_operand" "=a")
15795         (unspec:HI
15796           [(match_operand:X87MODEF 1 "register_operand" "f")]
15797           UNSPEC_FXAM))]
15798   "TARGET_USE_FANCY_MATH_387"
15799   "fxam\n\tfnstsw\t%0"
15800   [(set_attr "type" "multi")
15801    (set_attr "length" "4")
15802    (set_attr "unit" "i387")
15803    (set_attr "mode" "<MODE>")])
15804
15805 (define_insn_and_split "fxam<mode>2_i387_with_temp"
15806   [(set (match_operand:HI 0 "register_operand")
15807         (unspec:HI
15808           [(match_operand:MODEF 1 "memory_operand")]
15809           UNSPEC_FXAM_MEM))]
15810   "TARGET_USE_FANCY_MATH_387
15811    && can_create_pseudo_p ()"
15812   "#"
15813   "&& 1"
15814   [(set (match_dup 2)(match_dup 1))
15815    (set (match_dup 0)
15816         (unspec:HI [(match_dup 2)] UNSPEC_FXAM))]
15817 {
15818   operands[2] = gen_reg_rtx (<MODE>mode);
15819
15820   MEM_VOLATILE_P (operands[1]) = 1;
15821 }
15822   [(set_attr "type" "multi")
15823    (set_attr "unit" "i387")
15824    (set_attr "mode" "<MODE>")])
15825
15826 (define_expand "isinfxf2"
15827   [(use (match_operand:SI 0 "register_operand"))
15828    (use (match_operand:XF 1 "register_operand"))]
15829   "TARGET_USE_FANCY_MATH_387
15830    && ix86_libc_has_function (function_c99_misc)"
15831 {
15832   rtx mask = GEN_INT (0x45);
15833   rtx val = GEN_INT (0x05);
15834
15835   rtx cond;
15836
15837   rtx scratch = gen_reg_rtx (HImode);
15838   rtx res = gen_reg_rtx (QImode);
15839
15840   emit_insn (gen_fxamxf2_i387 (scratch, operands[1]));
15841
15842   emit_insn (gen_andqi_ext_0 (scratch, scratch, mask));
15843   emit_insn (gen_cmpqi_ext_3 (scratch, val));
15844   cond = gen_rtx_fmt_ee (EQ, QImode,
15845                          gen_rtx_REG (CCmode, FLAGS_REG),
15846                          const0_rtx);
15847   emit_insn (gen_rtx_SET (VOIDmode, res, cond));
15848   emit_insn (gen_zero_extendqisi2 (operands[0], res));
15849   DONE;
15850 })
15851
15852 (define_expand "isinf<mode>2"
15853   [(use (match_operand:SI 0 "register_operand"))
15854    (use (match_operand:MODEF 1 "nonimmediate_operand"))]
15855   "TARGET_USE_FANCY_MATH_387
15856    && ix86_libc_has_function (function_c99_misc)
15857    && !(SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
15858 {
15859   rtx mask = GEN_INT (0x45);
15860   rtx val = GEN_INT (0x05);
15861
15862   rtx cond;
15863
15864   rtx scratch = gen_reg_rtx (HImode);
15865   rtx res = gen_reg_rtx (QImode);
15866
15867   /* Remove excess precision by forcing value through memory. */
15868   if (memory_operand (operands[1], VOIDmode))
15869     emit_insn (gen_fxam<mode>2_i387_with_temp (scratch, operands[1]));
15870   else
15871     {
15872       rtx temp = assign_386_stack_local (<MODE>mode, SLOT_TEMP);
15873
15874       emit_move_insn (temp, operands[1]);
15875       emit_insn (gen_fxam<mode>2_i387_with_temp (scratch, temp));
15876     }
15877
15878   emit_insn (gen_andqi_ext_0 (scratch, scratch, mask));
15879   emit_insn (gen_cmpqi_ext_3 (scratch, val));
15880   cond = gen_rtx_fmt_ee (EQ, QImode,
15881                          gen_rtx_REG (CCmode, FLAGS_REG),
15882                          const0_rtx);
15883   emit_insn (gen_rtx_SET (VOIDmode, res, cond));
15884   emit_insn (gen_zero_extendqisi2 (operands[0], res));
15885   DONE;
15886 })
15887
15888 (define_expand "signbitxf2"
15889   [(use (match_operand:SI 0 "register_operand"))
15890    (use (match_operand:XF 1 "register_operand"))]
15891   "TARGET_USE_FANCY_MATH_387"
15892 {
15893   rtx scratch = gen_reg_rtx (HImode);
15894
15895   emit_insn (gen_fxamxf2_i387 (scratch, operands[1]));
15896   emit_insn (gen_andsi3 (operands[0],
15897              gen_lowpart (SImode, scratch), GEN_INT (0x200)));
15898   DONE;
15899 })
15900
15901 (define_insn "movmsk_df"
15902   [(set (match_operand:SI 0 "register_operand" "=r")
15903         (unspec:SI
15904           [(match_operand:DF 1 "register_operand" "x")]
15905           UNSPEC_MOVMSK))]
15906   "SSE_FLOAT_MODE_P (DFmode) && TARGET_SSE_MATH"
15907   "%vmovmskpd\t{%1, %0|%0, %1}"
15908   [(set_attr "type" "ssemov")
15909    (set_attr "prefix" "maybe_vex")
15910    (set_attr "mode" "DF")])
15911
15912 ;; Use movmskpd in SSE mode to avoid store forwarding stall
15913 ;; for 32bit targets and movq+shrq sequence for 64bit targets.
15914 (define_expand "signbitdf2"
15915   [(use (match_operand:SI 0 "register_operand"))
15916    (use (match_operand:DF 1 "register_operand"))]
15917   "TARGET_USE_FANCY_MATH_387
15918    || (SSE_FLOAT_MODE_P (DFmode) && TARGET_SSE_MATH)"
15919 {
15920   if (SSE_FLOAT_MODE_P (DFmode) && TARGET_SSE_MATH)
15921     {
15922       emit_insn (gen_movmsk_df (operands[0], operands[1]));
15923       emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
15924     }
15925   else
15926     {
15927       rtx scratch = gen_reg_rtx (HImode);
15928
15929       emit_insn (gen_fxamdf2_i387 (scratch, operands[1]));
15930       emit_insn (gen_andsi3 (operands[0],
15931                  gen_lowpart (SImode, scratch), GEN_INT (0x200)));
15932     }
15933   DONE;
15934 })
15935
15936 (define_expand "signbitsf2"
15937   [(use (match_operand:SI 0 "register_operand"))
15938    (use (match_operand:SF 1 "register_operand"))]
15939   "TARGET_USE_FANCY_MATH_387
15940    && !(SSE_FLOAT_MODE_P (SFmode) && TARGET_SSE_MATH)"
15941 {
15942   rtx scratch = gen_reg_rtx (HImode);
15943
15944   emit_insn (gen_fxamsf2_i387 (scratch, operands[1]));
15945   emit_insn (gen_andsi3 (operands[0],
15946              gen_lowpart (SImode, scratch), GEN_INT (0x200)));
15947   DONE;
15948 })
15949 \f
15950 ;; Block operation instructions
15951
15952 (define_insn "cld"
15953   [(unspec_volatile [(const_int 0)] UNSPECV_CLD)]
15954   ""
15955   "cld"
15956   [(set_attr "length" "1")
15957    (set_attr "length_immediate" "0")
15958    (set_attr "modrm" "0")])
15959
15960 (define_expand "movmem<mode>"
15961   [(use (match_operand:BLK 0 "memory_operand"))
15962    (use (match_operand:BLK 1 "memory_operand"))
15963    (use (match_operand:SWI48 2 "nonmemory_operand"))
15964    (use (match_operand:SWI48 3 "const_int_operand"))
15965    (use (match_operand:SI 4 "const_int_operand"))
15966    (use (match_operand:SI 5 "const_int_operand"))
15967    (use (match_operand:SI 6 ""))
15968    (use (match_operand:SI 7 ""))
15969    (use (match_operand:SI 8 ""))]
15970   ""
15971 {
15972  if (ix86_expand_set_or_movmem (operands[0], operands[1],
15973                                 operands[2], NULL, operands[3],
15974                                 operands[4], operands[5],
15975                                 operands[6], operands[7],
15976                                 operands[8], false))
15977    DONE;
15978  else
15979    FAIL;
15980 })
15981
15982 ;; Most CPUs don't like single string operations
15983 ;; Handle this case here to simplify previous expander.
15984
15985 (define_expand "strmov"
15986   [(set (match_dup 4) (match_operand 3 "memory_operand"))
15987    (set (match_operand 1 "memory_operand") (match_dup 4))
15988    (parallel [(set (match_operand 0 "register_operand") (match_dup 5))
15989               (clobber (reg:CC FLAGS_REG))])
15990    (parallel [(set (match_operand 2 "register_operand") (match_dup 6))
15991               (clobber (reg:CC FLAGS_REG))])]
15992   ""
15993 {
15994   rtx adjust = GEN_INT (GET_MODE_SIZE (GET_MODE (operands[1])));
15995
15996   /* If .md ever supports :P for Pmode, these can be directly
15997      in the pattern above.  */
15998   operands[5] = gen_rtx_PLUS (Pmode, operands[0], adjust);
15999   operands[6] = gen_rtx_PLUS (Pmode, operands[2], adjust);
16000
16001   /* Can't use this if the user has appropriated esi or edi.  */
16002   if ((TARGET_SINGLE_STRINGOP || optimize_insn_for_size_p ())
16003       && !(fixed_regs[SI_REG] || fixed_regs[DI_REG]))
16004     {
16005       emit_insn (gen_strmov_singleop (operands[0], operands[1],
16006                                       operands[2], operands[3],
16007                                       operands[5], operands[6]));
16008       DONE;
16009     }
16010
16011   operands[4] = gen_reg_rtx (GET_MODE (operands[1]));
16012 })
16013
16014 (define_expand "strmov_singleop"
16015   [(parallel [(set (match_operand 1 "memory_operand")
16016                    (match_operand 3 "memory_operand"))
16017               (set (match_operand 0 "register_operand")
16018                    (match_operand 4))
16019               (set (match_operand 2 "register_operand")
16020                    (match_operand 5))])]
16021   ""
16022   "ix86_current_function_needs_cld = 1;")
16023
16024 (define_insn "*strmovdi_rex_1"
16025   [(set (mem:DI (match_operand:P 2 "register_operand" "0"))
16026         (mem:DI (match_operand:P 3 "register_operand" "1")))
16027    (set (match_operand:P 0 "register_operand" "=D")
16028         (plus:P (match_dup 2)
16029                 (const_int 8)))
16030    (set (match_operand:P 1 "register_operand" "=S")
16031         (plus:P (match_dup 3)
16032                 (const_int 8)))]
16033   "TARGET_64BIT
16034    && !(fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16035   "%^movsq"
16036   [(set_attr "type" "str")
16037    (set_attr "memory" "both")
16038    (set_attr "mode" "DI")])
16039
16040 (define_insn "*strmovsi_1"
16041   [(set (mem:SI (match_operand:P 2 "register_operand" "0"))
16042         (mem:SI (match_operand:P 3 "register_operand" "1")))
16043    (set (match_operand:P 0 "register_operand" "=D")
16044         (plus:P (match_dup 2)
16045                 (const_int 4)))
16046    (set (match_operand:P 1 "register_operand" "=S")
16047         (plus:P (match_dup 3)
16048                 (const_int 4)))]
16049   "!(fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16050   "%^movs{l|d}"
16051   [(set_attr "type" "str")
16052    (set_attr "memory" "both")
16053    (set_attr "mode" "SI")])
16054
16055 (define_insn "*strmovhi_1"
16056   [(set (mem:HI (match_operand:P 2 "register_operand" "0"))
16057         (mem:HI (match_operand:P 3 "register_operand" "1")))
16058    (set (match_operand:P 0 "register_operand" "=D")
16059         (plus:P (match_dup 2)
16060                 (const_int 2)))
16061    (set (match_operand:P 1 "register_operand" "=S")
16062         (plus:P (match_dup 3)
16063                 (const_int 2)))]
16064   "!(fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16065   "%^movsw"
16066   [(set_attr "type" "str")
16067    (set_attr "memory" "both")
16068    (set_attr "mode" "HI")])
16069
16070 (define_insn "*strmovqi_1"
16071   [(set (mem:QI (match_operand:P 2 "register_operand" "0"))
16072         (mem:QI (match_operand:P 3 "register_operand" "1")))
16073    (set (match_operand:P 0 "register_operand" "=D")
16074         (plus:P (match_dup 2)
16075                 (const_int 1)))
16076    (set (match_operand:P 1 "register_operand" "=S")
16077         (plus:P (match_dup 3)
16078                 (const_int 1)))]
16079   "!(fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16080   "%^movsb"
16081   [(set_attr "type" "str")
16082    (set_attr "memory" "both")
16083    (set (attr "prefix_rex")
16084         (if_then_else
16085           (match_test "<P:MODE>mode == DImode")
16086           (const_string "0")
16087           (const_string "*")))
16088    (set_attr "mode" "QI")])
16089
16090 (define_expand "rep_mov"
16091   [(parallel [(set (match_operand 4 "register_operand") (const_int 0))
16092               (set (match_operand 0 "register_operand")
16093                    (match_operand 5))
16094               (set (match_operand 2 "register_operand")
16095                    (match_operand 6))
16096               (set (match_operand 1 "memory_operand")
16097                    (match_operand 3 "memory_operand"))
16098               (use (match_dup 4))])]
16099   ""
16100   "ix86_current_function_needs_cld = 1;")
16101
16102 (define_insn "*rep_movdi_rex64"
16103   [(set (match_operand:P 2 "register_operand" "=c") (const_int 0))
16104    (set (match_operand:P 0 "register_operand" "=D")
16105         (plus:P (ashift:P (match_operand:P 5 "register_operand" "2")
16106                           (const_int 3))
16107                 (match_operand:P 3 "register_operand" "0")))
16108    (set (match_operand:P 1 "register_operand" "=S")
16109         (plus:P (ashift:P (match_dup 5) (const_int 3))
16110                 (match_operand:P 4 "register_operand" "1")))
16111    (set (mem:BLK (match_dup 3))
16112         (mem:BLK (match_dup 4)))
16113    (use (match_dup 5))]
16114   "TARGET_64BIT
16115    && !(fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16116   "%^rep{%;} movsq"
16117   [(set_attr "type" "str")
16118    (set_attr "prefix_rep" "1")
16119    (set_attr "memory" "both")
16120    (set_attr "mode" "DI")])
16121
16122 (define_insn "*rep_movsi"
16123   [(set (match_operand:P 2 "register_operand" "=c") (const_int 0))
16124    (set (match_operand:P 0 "register_operand" "=D")
16125         (plus:P (ashift:P (match_operand:P 5 "register_operand" "2")
16126                           (const_int 2))
16127                  (match_operand:P 3 "register_operand" "0")))
16128    (set (match_operand:P 1 "register_operand" "=S")
16129         (plus:P (ashift:P (match_dup 5) (const_int 2))
16130                 (match_operand:P 4 "register_operand" "1")))
16131    (set (mem:BLK (match_dup 3))
16132         (mem:BLK (match_dup 4)))
16133    (use (match_dup 5))]
16134   "!(fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16135   "%^rep{%;} movs{l|d}"
16136   [(set_attr "type" "str")
16137    (set_attr "prefix_rep" "1")
16138    (set_attr "memory" "both")
16139    (set_attr "mode" "SI")])
16140
16141 (define_insn "*rep_movqi"
16142   [(set (match_operand:P 2 "register_operand" "=c") (const_int 0))
16143    (set (match_operand:P 0 "register_operand" "=D")
16144         (plus:P (match_operand:P 3 "register_operand" "0")
16145                 (match_operand:P 5 "register_operand" "2")))
16146    (set (match_operand:P 1 "register_operand" "=S")
16147         (plus:P (match_operand:P 4 "register_operand" "1") (match_dup 5)))
16148    (set (mem:BLK (match_dup 3))
16149         (mem:BLK (match_dup 4)))
16150    (use (match_dup 5))]
16151   "!(fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16152   "%^rep{%;} movsb"
16153   [(set_attr "type" "str")
16154    (set_attr "prefix_rep" "1")
16155    (set_attr "memory" "both")
16156    (set_attr "mode" "QI")])
16157
16158 (define_expand "setmem<mode>"
16159    [(use (match_operand:BLK 0 "memory_operand"))
16160     (use (match_operand:SWI48 1 "nonmemory_operand"))
16161     (use (match_operand:QI 2 "nonmemory_operand"))
16162     (use (match_operand 3 "const_int_operand"))
16163     (use (match_operand:SI 4 "const_int_operand"))
16164     (use (match_operand:SI 5 "const_int_operand"))
16165     (use (match_operand:SI 6 ""))
16166     (use (match_operand:SI 7 ""))
16167     (use (match_operand:SI 8 ""))]
16168   ""
16169 {
16170  if (ix86_expand_set_or_movmem (operands[0], NULL,
16171                                 operands[1], operands[2],
16172                                 operands[3], operands[4],
16173                                 operands[5], operands[6],
16174                                 operands[7], operands[8], true))
16175    DONE;
16176  else
16177    FAIL;
16178 })
16179
16180 ;; Most CPUs don't like single string operations
16181 ;; Handle this case here to simplify previous expander.
16182
16183 (define_expand "strset"
16184   [(set (match_operand 1 "memory_operand")
16185         (match_operand 2 "register_operand"))
16186    (parallel [(set (match_operand 0 "register_operand")
16187                    (match_dup 3))
16188               (clobber (reg:CC FLAGS_REG))])]
16189   ""
16190 {
16191   if (GET_MODE (operands[1]) != GET_MODE (operands[2]))
16192     operands[1] = adjust_address_nv (operands[1], GET_MODE (operands[2]), 0);
16193
16194   /* If .md ever supports :P for Pmode, this can be directly
16195      in the pattern above.  */
16196   operands[3] = gen_rtx_PLUS (Pmode, operands[0],
16197                               GEN_INT (GET_MODE_SIZE (GET_MODE
16198                                                       (operands[2]))));
16199   /* Can't use this if the user has appropriated eax or edi.  */
16200   if ((TARGET_SINGLE_STRINGOP || optimize_insn_for_size_p ())
16201       && !(fixed_regs[AX_REG] || fixed_regs[DI_REG]))
16202     {
16203       emit_insn (gen_strset_singleop (operands[0], operands[1], operands[2],
16204                                       operands[3]));
16205       DONE;
16206     }
16207 })
16208
16209 (define_expand "strset_singleop"
16210   [(parallel [(set (match_operand 1 "memory_operand")
16211                    (match_operand 2 "register_operand"))
16212               (set (match_operand 0 "register_operand")
16213                    (match_operand 3))
16214               (unspec [(const_int 0)] UNSPEC_STOS)])]
16215   ""
16216   "ix86_current_function_needs_cld = 1;")
16217
16218 (define_insn "*strsetdi_rex_1"
16219   [(set (mem:DI (match_operand:P 1 "register_operand" "0"))
16220         (match_operand:DI 2 "register_operand" "a"))
16221    (set (match_operand:P 0 "register_operand" "=D")
16222         (plus:P (match_dup 1)
16223                 (const_int 8)))
16224    (unspec [(const_int 0)] UNSPEC_STOS)]
16225   "TARGET_64BIT
16226    && !(fixed_regs[AX_REG] || fixed_regs[DI_REG])"
16227   "%^stosq"
16228   [(set_attr "type" "str")
16229    (set_attr "memory" "store")
16230    (set_attr "mode" "DI")])
16231
16232 (define_insn "*strsetsi_1"
16233   [(set (mem:SI (match_operand:P 1 "register_operand" "0"))
16234         (match_operand:SI 2 "register_operand" "a"))
16235    (set (match_operand:P 0 "register_operand" "=D")
16236         (plus:P (match_dup 1)
16237                 (const_int 4)))
16238    (unspec [(const_int 0)] UNSPEC_STOS)]
16239   "!(fixed_regs[AX_REG] || fixed_regs[DI_REG])"
16240   "%^stos{l|d}"
16241   [(set_attr "type" "str")
16242    (set_attr "memory" "store")
16243    (set_attr "mode" "SI")])
16244
16245 (define_insn "*strsethi_1"
16246   [(set (mem:HI (match_operand:P 1 "register_operand" "0"))
16247         (match_operand:HI 2 "register_operand" "a"))
16248    (set (match_operand:P 0 "register_operand" "=D")
16249         (plus:P (match_dup 1)
16250                 (const_int 2)))
16251    (unspec [(const_int 0)] UNSPEC_STOS)]
16252   "!(fixed_regs[AX_REG] || fixed_regs[DI_REG])"
16253   "%^stosw"
16254   [(set_attr "type" "str")
16255    (set_attr "memory" "store")
16256    (set_attr "mode" "HI")])
16257
16258 (define_insn "*strsetqi_1"
16259   [(set (mem:QI (match_operand:P 1 "register_operand" "0"))
16260         (match_operand:QI 2 "register_operand" "a"))
16261    (set (match_operand:P 0 "register_operand" "=D")
16262         (plus:P (match_dup 1)
16263                 (const_int 1)))
16264    (unspec [(const_int 0)] UNSPEC_STOS)]
16265   "!(fixed_regs[AX_REG] || fixed_regs[DI_REG])"
16266   "%^stosb"
16267   [(set_attr "type" "str")
16268    (set_attr "memory" "store")
16269    (set (attr "prefix_rex")
16270         (if_then_else
16271           (match_test "<P:MODE>mode == DImode")
16272           (const_string "0")
16273           (const_string "*")))
16274    (set_attr "mode" "QI")])
16275
16276 (define_expand "rep_stos"
16277   [(parallel [(set (match_operand 1 "register_operand") (const_int 0))
16278               (set (match_operand 0 "register_operand")
16279                    (match_operand 4))
16280               (set (match_operand 2 "memory_operand") (const_int 0))
16281               (use (match_operand 3 "register_operand"))
16282               (use (match_dup 1))])]
16283   ""
16284   "ix86_current_function_needs_cld = 1;")
16285
16286 (define_insn "*rep_stosdi_rex64"
16287   [(set (match_operand:P 1 "register_operand" "=c") (const_int 0))
16288    (set (match_operand:P 0 "register_operand" "=D")
16289         (plus:P (ashift:P (match_operand:P 4 "register_operand" "1")
16290                           (const_int 3))
16291                  (match_operand:P 3 "register_operand" "0")))
16292    (set (mem:BLK (match_dup 3))
16293         (const_int 0))
16294    (use (match_operand:DI 2 "register_operand" "a"))
16295    (use (match_dup 4))]
16296   "TARGET_64BIT
16297    && !(fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])"
16298   "%^rep{%;} stosq"
16299   [(set_attr "type" "str")
16300    (set_attr "prefix_rep" "1")
16301    (set_attr "memory" "store")
16302    (set_attr "mode" "DI")])
16303
16304 (define_insn "*rep_stossi"
16305   [(set (match_operand:P 1 "register_operand" "=c") (const_int 0))
16306    (set (match_operand:P 0 "register_operand" "=D")
16307         (plus:P (ashift:P (match_operand:P 4 "register_operand" "1")
16308                           (const_int 2))
16309                  (match_operand:P 3 "register_operand" "0")))
16310    (set (mem:BLK (match_dup 3))
16311         (const_int 0))
16312    (use (match_operand:SI 2 "register_operand" "a"))
16313    (use (match_dup 4))]
16314   "!(fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])"
16315   "%^rep{%;} stos{l|d}"
16316   [(set_attr "type" "str")
16317    (set_attr "prefix_rep" "1")
16318    (set_attr "memory" "store")
16319    (set_attr "mode" "SI")])
16320
16321 (define_insn "*rep_stosqi"
16322   [(set (match_operand:P 1 "register_operand" "=c") (const_int 0))
16323    (set (match_operand:P 0 "register_operand" "=D")
16324         (plus:P (match_operand:P 3 "register_operand" "0")
16325                 (match_operand:P 4 "register_operand" "1")))
16326    (set (mem:BLK (match_dup 3))
16327         (const_int 0))
16328    (use (match_operand:QI 2 "register_operand" "a"))
16329    (use (match_dup 4))]
16330   "!(fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])"
16331   "%^rep{%;} stosb"
16332   [(set_attr "type" "str")
16333    (set_attr "prefix_rep" "1")
16334    (set_attr "memory" "store")
16335    (set (attr "prefix_rex")
16336         (if_then_else
16337           (match_test "<P:MODE>mode == DImode")
16338           (const_string "0")
16339           (const_string "*")))
16340    (set_attr "mode" "QI")])
16341
16342 (define_expand "cmpstrnsi"
16343   [(set (match_operand:SI 0 "register_operand")
16344         (compare:SI (match_operand:BLK 1 "general_operand")
16345                     (match_operand:BLK 2 "general_operand")))
16346    (use (match_operand 3 "general_operand"))
16347    (use (match_operand 4 "immediate_operand"))]
16348   ""
16349 {
16350   rtx addr1, addr2, out, outlow, count, countreg, align;
16351
16352   if (optimize_insn_for_size_p () && !TARGET_INLINE_ALL_STRINGOPS)
16353     FAIL;
16354
16355   /* Can't use this if the user has appropriated ecx, esi or edi.  */
16356   if (fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])
16357     FAIL;
16358
16359   out = operands[0];
16360   if (!REG_P (out))
16361     out = gen_reg_rtx (SImode);
16362
16363   addr1 = copy_addr_to_reg (XEXP (operands[1], 0));
16364   addr2 = copy_addr_to_reg (XEXP (operands[2], 0));
16365   if (addr1 != XEXP (operands[1], 0))
16366     operands[1] = replace_equiv_address_nv (operands[1], addr1);
16367   if (addr2 != XEXP (operands[2], 0))
16368     operands[2] = replace_equiv_address_nv (operands[2], addr2);
16369
16370   count = operands[3];
16371   countreg = ix86_zero_extend_to_Pmode (count);
16372
16373   /* %%% Iff we are testing strict equality, we can use known alignment
16374      to good advantage.  This may be possible with combine, particularly
16375      once cc0 is dead.  */
16376   align = operands[4];
16377
16378   if (CONST_INT_P (count))
16379     {
16380       if (INTVAL (count) == 0)
16381         {
16382           emit_move_insn (operands[0], const0_rtx);
16383           DONE;
16384         }
16385       emit_insn (gen_cmpstrnqi_nz_1 (addr1, addr2, countreg, align,
16386                                      operands[1], operands[2]));
16387     }
16388   else
16389     {
16390       rtx (*gen_cmp) (rtx, rtx);
16391
16392       gen_cmp = (TARGET_64BIT
16393                  ? gen_cmpdi_1 : gen_cmpsi_1);
16394
16395       emit_insn (gen_cmp (countreg, countreg));
16396       emit_insn (gen_cmpstrnqi_1 (addr1, addr2, countreg, align,
16397                                   operands[1], operands[2]));
16398     }
16399
16400   outlow = gen_lowpart (QImode, out);
16401   emit_insn (gen_cmpintqi (outlow));
16402   emit_move_insn (out, gen_rtx_SIGN_EXTEND (SImode, outlow));
16403
16404   if (operands[0] != out)
16405     emit_move_insn (operands[0], out);
16406
16407   DONE;
16408 })
16409
16410 ;; Produce a tri-state integer (-1, 0, 1) from condition codes.
16411
16412 (define_expand "cmpintqi"
16413   [(set (match_dup 1)
16414         (gtu:QI (reg:CC FLAGS_REG) (const_int 0)))
16415    (set (match_dup 2)
16416         (ltu:QI (reg:CC FLAGS_REG) (const_int 0)))
16417    (parallel [(set (match_operand:QI 0 "register_operand")
16418                    (minus:QI (match_dup 1)
16419                              (match_dup 2)))
16420               (clobber (reg:CC FLAGS_REG))])]
16421   ""
16422 {
16423   operands[1] = gen_reg_rtx (QImode);
16424   operands[2] = gen_reg_rtx (QImode);
16425 })
16426
16427 ;; memcmp recognizers.  The `cmpsb' opcode does nothing if the count is
16428 ;; zero.  Emit extra code to make sure that a zero-length compare is EQ.
16429
16430 (define_expand "cmpstrnqi_nz_1"
16431   [(parallel [(set (reg:CC FLAGS_REG)
16432                    (compare:CC (match_operand 4 "memory_operand")
16433                                (match_operand 5 "memory_operand")))
16434               (use (match_operand 2 "register_operand"))
16435               (use (match_operand:SI 3 "immediate_operand"))
16436               (clobber (match_operand 0 "register_operand"))
16437               (clobber (match_operand 1 "register_operand"))
16438               (clobber (match_dup 2))])]
16439   ""
16440   "ix86_current_function_needs_cld = 1;")
16441
16442 (define_insn "*cmpstrnqi_nz_1"
16443   [(set (reg:CC FLAGS_REG)
16444         (compare:CC (mem:BLK (match_operand:P 4 "register_operand" "0"))
16445                     (mem:BLK (match_operand:P 5 "register_operand" "1"))))
16446    (use (match_operand:P 6 "register_operand" "2"))
16447    (use (match_operand:SI 3 "immediate_operand" "i"))
16448    (clobber (match_operand:P 0 "register_operand" "=S"))
16449    (clobber (match_operand:P 1 "register_operand" "=D"))
16450    (clobber (match_operand:P 2 "register_operand" "=c"))]
16451   "!(fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16452   "%^repz{%;} cmpsb"
16453   [(set_attr "type" "str")
16454    (set_attr "mode" "QI")
16455    (set (attr "prefix_rex")
16456         (if_then_else
16457           (match_test "<P:MODE>mode == DImode")
16458           (const_string "0")
16459           (const_string "*")))
16460    (set_attr "prefix_rep" "1")])
16461
16462 ;; The same, but the count is not known to not be zero.
16463
16464 (define_expand "cmpstrnqi_1"
16465   [(parallel [(set (reg:CC FLAGS_REG)
16466                 (if_then_else:CC (ne (match_operand 2 "register_operand")
16467                                      (const_int 0))
16468                   (compare:CC (match_operand 4 "memory_operand")
16469                               (match_operand 5 "memory_operand"))
16470                   (const_int 0)))
16471               (use (match_operand:SI 3 "immediate_operand"))
16472               (use (reg:CC FLAGS_REG))
16473               (clobber (match_operand 0 "register_operand"))
16474               (clobber (match_operand 1 "register_operand"))
16475               (clobber (match_dup 2))])]
16476   ""
16477   "ix86_current_function_needs_cld = 1;")
16478
16479 (define_insn "*cmpstrnqi_1"
16480   [(set (reg:CC FLAGS_REG)
16481         (if_then_else:CC (ne (match_operand:P 6 "register_operand" "2")
16482                              (const_int 0))
16483           (compare:CC (mem:BLK (match_operand:P 4 "register_operand" "0"))
16484                       (mem:BLK (match_operand:P 5 "register_operand" "1")))
16485           (const_int 0)))
16486    (use (match_operand:SI 3 "immediate_operand" "i"))
16487    (use (reg:CC FLAGS_REG))
16488    (clobber (match_operand:P 0 "register_operand" "=S"))
16489    (clobber (match_operand:P 1 "register_operand" "=D"))
16490    (clobber (match_operand:P 2 "register_operand" "=c"))]
16491   "!(fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])"
16492   "%^repz{%;} cmpsb"
16493   [(set_attr "type" "str")
16494    (set_attr "mode" "QI")
16495    (set (attr "prefix_rex")
16496         (if_then_else
16497           (match_test "<P:MODE>mode == DImode")
16498           (const_string "0")
16499           (const_string "*")))
16500    (set_attr "prefix_rep" "1")])
16501
16502 (define_expand "strlen<mode>"
16503   [(set (match_operand:P 0 "register_operand")
16504         (unspec:P [(match_operand:BLK 1 "general_operand")
16505                    (match_operand:QI 2 "immediate_operand")
16506                    (match_operand 3 "immediate_operand")]
16507                   UNSPEC_SCAS))]
16508   ""
16509 {
16510  if (ix86_expand_strlen (operands[0], operands[1], operands[2], operands[3]))
16511    DONE;
16512  else
16513    FAIL;
16514 })
16515
16516 (define_expand "strlenqi_1"
16517   [(parallel [(set (match_operand 0 "register_operand")
16518                    (match_operand 2))
16519               (clobber (match_operand 1 "register_operand"))
16520               (clobber (reg:CC FLAGS_REG))])]
16521   ""
16522   "ix86_current_function_needs_cld = 1;")
16523
16524 (define_insn "*strlenqi_1"
16525   [(set (match_operand:P 0 "register_operand" "=&c")
16526         (unspec:P [(mem:BLK (match_operand:P 5 "register_operand" "1"))
16527                    (match_operand:QI 2 "register_operand" "a")
16528                    (match_operand:P 3 "immediate_operand" "i")
16529                    (match_operand:P 4 "register_operand" "0")] UNSPEC_SCAS))
16530    (clobber (match_operand:P 1 "register_operand" "=D"))
16531    (clobber (reg:CC FLAGS_REG))]
16532   "!(fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])"
16533   "%^repnz{%;} scasb"
16534   [(set_attr "type" "str")
16535    (set_attr "mode" "QI")
16536    (set (attr "prefix_rex")
16537         (if_then_else
16538           (match_test "<P:MODE>mode == DImode")
16539           (const_string "0")
16540           (const_string "*")))
16541    (set_attr "prefix_rep" "1")])
16542
16543 ;; Peephole optimizations to clean up after cmpstrn*.  This should be
16544 ;; handled in combine, but it is not currently up to the task.
16545 ;; When used for their truth value, the cmpstrn* expanders generate
16546 ;; code like this:
16547 ;;
16548 ;;   repz cmpsb
16549 ;;   seta       %al
16550 ;;   setb       %dl
16551 ;;   cmpb       %al, %dl
16552 ;;   jcc        label
16553 ;;
16554 ;; The intermediate three instructions are unnecessary.
16555
16556 ;; This one handles cmpstrn*_nz_1...
16557 (define_peephole2
16558   [(parallel[
16559      (set (reg:CC FLAGS_REG)
16560           (compare:CC (mem:BLK (match_operand 4 "register_operand"))
16561                       (mem:BLK (match_operand 5 "register_operand"))))
16562      (use (match_operand 6 "register_operand"))
16563      (use (match_operand:SI 3 "immediate_operand"))
16564      (clobber (match_operand 0 "register_operand"))
16565      (clobber (match_operand 1 "register_operand"))
16566      (clobber (match_operand 2 "register_operand"))])
16567    (set (match_operand:QI 7 "register_operand")
16568         (gtu:QI (reg:CC FLAGS_REG) (const_int 0)))
16569    (set (match_operand:QI 8 "register_operand")
16570         (ltu:QI (reg:CC FLAGS_REG) (const_int 0)))
16571    (set (reg FLAGS_REG)
16572         (compare (match_dup 7) (match_dup 8)))
16573   ]
16574   "peep2_reg_dead_p (4, operands[7]) && peep2_reg_dead_p (4, operands[8])"
16575   [(parallel[
16576      (set (reg:CC FLAGS_REG)
16577           (compare:CC (mem:BLK (match_dup 4))
16578                       (mem:BLK (match_dup 5))))
16579      (use (match_dup 6))
16580      (use (match_dup 3))
16581      (clobber (match_dup 0))
16582      (clobber (match_dup 1))
16583      (clobber (match_dup 2))])])
16584
16585 ;; ...and this one handles cmpstrn*_1.
16586 (define_peephole2
16587   [(parallel[
16588      (set (reg:CC FLAGS_REG)
16589           (if_then_else:CC (ne (match_operand 6 "register_operand")
16590                                (const_int 0))
16591             (compare:CC (mem:BLK (match_operand 4 "register_operand"))
16592                         (mem:BLK (match_operand 5 "register_operand")))
16593             (const_int 0)))
16594      (use (match_operand:SI 3 "immediate_operand"))
16595      (use (reg:CC FLAGS_REG))
16596      (clobber (match_operand 0 "register_operand"))
16597      (clobber (match_operand 1 "register_operand"))
16598      (clobber (match_operand 2 "register_operand"))])
16599    (set (match_operand:QI 7 "register_operand")
16600         (gtu:QI (reg:CC FLAGS_REG) (const_int 0)))
16601    (set (match_operand:QI 8 "register_operand")
16602         (ltu:QI (reg:CC FLAGS_REG) (const_int 0)))
16603    (set (reg FLAGS_REG)
16604         (compare (match_dup 7) (match_dup 8)))
16605   ]
16606   "peep2_reg_dead_p (4, operands[7]) && peep2_reg_dead_p (4, operands[8])"
16607   [(parallel[
16608      (set (reg:CC FLAGS_REG)
16609           (if_then_else:CC (ne (match_dup 6)
16610                                (const_int 0))
16611             (compare:CC (mem:BLK (match_dup 4))
16612                         (mem:BLK (match_dup 5)))
16613             (const_int 0)))
16614      (use (match_dup 3))
16615      (use (reg:CC FLAGS_REG))
16616      (clobber (match_dup 0))
16617      (clobber (match_dup 1))
16618      (clobber (match_dup 2))])])
16619 \f
16620 ;; Conditional move instructions.
16621
16622 (define_expand "mov<mode>cc"
16623   [(set (match_operand:SWIM 0 "register_operand")
16624         (if_then_else:SWIM (match_operand 1 "comparison_operator")
16625                            (match_operand:SWIM 2 "<general_operand>")
16626                            (match_operand:SWIM 3 "<general_operand>")))]
16627   ""
16628   "if (ix86_expand_int_movcc (operands)) DONE; else FAIL;")
16629
16630 ;; Data flow gets confused by our desire for `sbbl reg,reg', and clearing
16631 ;; the register first winds up with `sbbl $0,reg', which is also weird.
16632 ;; So just document what we're doing explicitly.
16633
16634 (define_expand "x86_mov<mode>cc_0_m1"
16635   [(parallel
16636     [(set (match_operand:SWI48 0 "register_operand")
16637           (if_then_else:SWI48
16638             (match_operator:SWI48 2 "ix86_carry_flag_operator"
16639              [(match_operand 1 "flags_reg_operand")
16640               (const_int 0)])
16641             (const_int -1)
16642             (const_int 0)))
16643      (clobber (reg:CC FLAGS_REG))])])
16644
16645 (define_insn "*x86_mov<mode>cc_0_m1"
16646   [(set (match_operand:SWI48 0 "register_operand" "=r")
16647         (if_then_else:SWI48 (match_operator 1 "ix86_carry_flag_operator"
16648                              [(reg FLAGS_REG) (const_int 0)])
16649           (const_int -1)
16650           (const_int 0)))
16651    (clobber (reg:CC FLAGS_REG))]
16652   ""
16653   "sbb{<imodesuffix>}\t%0, %0"
16654   ; Since we don't have the proper number of operands for an alu insn,
16655   ; fill in all the blanks.
16656   [(set_attr "type" "alu")
16657    (set_attr "use_carry" "1")
16658    (set_attr "pent_pair" "pu")
16659    (set_attr "memory" "none")
16660    (set_attr "imm_disp" "false")
16661    (set_attr "mode" "<MODE>")
16662    (set_attr "length_immediate" "0")])
16663
16664 (define_insn "*x86_mov<mode>cc_0_m1_se"
16665   [(set (match_operand:SWI48 0 "register_operand" "=r")
16666         (sign_extract:SWI48 (match_operator 1 "ix86_carry_flag_operator"
16667                              [(reg FLAGS_REG) (const_int 0)])
16668                             (const_int 1)
16669                             (const_int 0)))
16670    (clobber (reg:CC FLAGS_REG))]
16671   ""
16672   "sbb{<imodesuffix>}\t%0, %0"
16673   [(set_attr "type" "alu")
16674    (set_attr "use_carry" "1")
16675    (set_attr "pent_pair" "pu")
16676    (set_attr "memory" "none")
16677    (set_attr "imm_disp" "false")
16678    (set_attr "mode" "<MODE>")
16679    (set_attr "length_immediate" "0")])
16680
16681 (define_insn "*x86_mov<mode>cc_0_m1_neg"
16682   [(set (match_operand:SWI48 0 "register_operand" "=r")
16683         (neg:SWI48 (match_operator 1 "ix86_carry_flag_operator"
16684                     [(reg FLAGS_REG) (const_int 0)])))
16685    (clobber (reg:CC FLAGS_REG))]
16686   ""
16687   "sbb{<imodesuffix>}\t%0, %0"
16688   [(set_attr "type" "alu")
16689    (set_attr "use_carry" "1")
16690    (set_attr "pent_pair" "pu")
16691    (set_attr "memory" "none")
16692    (set_attr "imm_disp" "false")
16693    (set_attr "mode" "<MODE>")
16694    (set_attr "length_immediate" "0")])
16695
16696 (define_insn "*mov<mode>cc_noc"
16697   [(set (match_operand:SWI248 0 "register_operand" "=r,r")
16698         (if_then_else:SWI248 (match_operator 1 "ix86_comparison_operator"
16699                                [(reg FLAGS_REG) (const_int 0)])
16700           (match_operand:SWI248 2 "nonimmediate_operand" "rm,0")
16701           (match_operand:SWI248 3 "nonimmediate_operand" "0,rm")))]
16702   "TARGET_CMOVE && !(MEM_P (operands[2]) && MEM_P (operands[3]))"
16703   "@
16704    cmov%O2%C1\t{%2, %0|%0, %2}
16705    cmov%O2%c1\t{%3, %0|%0, %3}"
16706   [(set_attr "type" "icmov")
16707    (set_attr "mode" "<MODE>")])
16708
16709 ;; Don't do conditional moves with memory inputs.  This splitter helps
16710 ;; register starved x86_32 by forcing inputs into registers before reload.
16711 (define_split
16712   [(set (match_operand:SWI248 0 "register_operand")
16713         (if_then_else:SWI248 (match_operator 1 "ix86_comparison_operator"
16714                                [(reg FLAGS_REG) (const_int 0)])
16715           (match_operand:SWI248 2 "nonimmediate_operand")
16716           (match_operand:SWI248 3 "nonimmediate_operand")))]
16717   "!TARGET_64BIT && TARGET_CMOVE
16718    && TARGET_AVOID_MEM_OPND_FOR_CMOVE
16719    && (MEM_P (operands[2]) || MEM_P (operands[3]))
16720    && can_create_pseudo_p ()
16721    && optimize_insn_for_speed_p ()"
16722   [(set (match_dup 0)
16723         (if_then_else:SWI248 (match_dup 1) (match_dup 2) (match_dup 3)))]
16724 {
16725   if (MEM_P (operands[2]))
16726     operands[2] = force_reg (<MODE>mode, operands[2]);
16727   if (MEM_P (operands[3]))
16728     operands[3] = force_reg (<MODE>mode, operands[3]);
16729 })
16730
16731 (define_insn "*movqicc_noc"
16732   [(set (match_operand:QI 0 "register_operand" "=r,r")
16733         (if_then_else:QI (match_operator 1 "ix86_comparison_operator"
16734                            [(reg FLAGS_REG) (const_int 0)])
16735                       (match_operand:QI 2 "register_operand" "r,0")
16736                       (match_operand:QI 3 "register_operand" "0,r")))]
16737   "TARGET_CMOVE && !TARGET_PARTIAL_REG_STALL"
16738   "#"
16739   [(set_attr "type" "icmov")
16740    (set_attr "mode" "QI")])
16741
16742 (define_split
16743   [(set (match_operand:SWI12 0 "register_operand")
16744         (if_then_else:SWI12 (match_operator 1 "ix86_comparison_operator"
16745                               [(reg FLAGS_REG) (const_int 0)])
16746                       (match_operand:SWI12 2 "register_operand")
16747                       (match_operand:SWI12 3 "register_operand")))]
16748   "TARGET_CMOVE && !TARGET_PARTIAL_REG_STALL
16749    && reload_completed"
16750   [(set (match_dup 0)
16751         (if_then_else:SI (match_dup 1) (match_dup 2) (match_dup 3)))]
16752 {
16753   operands[0] = gen_lowpart (SImode, operands[0]);
16754   operands[2] = gen_lowpart (SImode, operands[2]);
16755   operands[3] = gen_lowpart (SImode, operands[3]);
16756 })
16757
16758 ;; Don't do conditional moves with memory inputs
16759 (define_peephole2
16760   [(match_scratch:SWI248 2 "r")
16761    (set (match_operand:SWI248 0 "register_operand")
16762         (if_then_else:SWI248 (match_operator 1 "ix86_comparison_operator"
16763                                [(reg FLAGS_REG) (const_int 0)])
16764           (match_dup 0)
16765           (match_operand:SWI248 3 "memory_operand")))]
16766   "TARGET_CMOVE && TARGET_AVOID_MEM_OPND_FOR_CMOVE
16767    && optimize_insn_for_speed_p ()"
16768   [(set (match_dup 2) (match_dup 3))
16769    (set (match_dup 0)
16770         (if_then_else:SWI248 (match_dup 1) (match_dup 0) (match_dup 2)))])
16771
16772 (define_peephole2
16773   [(match_scratch:SWI248 2 "r")
16774    (set (match_operand:SWI248 0 "register_operand")
16775         (if_then_else:SWI248 (match_operator 1 "ix86_comparison_operator"
16776                                [(reg FLAGS_REG) (const_int 0)])
16777           (match_operand:SWI248 3 "memory_operand")
16778           (match_dup 0)))]
16779   "TARGET_CMOVE && TARGET_AVOID_MEM_OPND_FOR_CMOVE
16780    && optimize_insn_for_speed_p ()"
16781   [(set (match_dup 2) (match_dup 3))
16782    (set (match_dup 0)
16783         (if_then_else:SWI248 (match_dup 1) (match_dup 2) (match_dup 0)))])
16784
16785 (define_expand "mov<mode>cc"
16786   [(set (match_operand:X87MODEF 0 "register_operand")
16787         (if_then_else:X87MODEF
16788           (match_operand 1 "comparison_operator")
16789           (match_operand:X87MODEF 2 "register_operand")
16790           (match_operand:X87MODEF 3 "register_operand")))]
16791   "(TARGET_80387 && TARGET_CMOVE)
16792    || (SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH)"
16793   "if (ix86_expand_fp_movcc (operands)) DONE; else FAIL;")
16794
16795 (define_insn "*movxfcc_1"
16796   [(set (match_operand:XF 0 "register_operand" "=f,f")
16797         (if_then_else:XF (match_operator 1 "fcmov_comparison_operator"
16798                                 [(reg FLAGS_REG) (const_int 0)])
16799                       (match_operand:XF 2 "register_operand" "f,0")
16800                       (match_operand:XF 3 "register_operand" "0,f")))]
16801   "TARGET_80387 && TARGET_CMOVE"
16802   "@
16803    fcmov%F1\t{%2, %0|%0, %2}
16804    fcmov%f1\t{%3, %0|%0, %3}"
16805   [(set_attr "type" "fcmov")
16806    (set_attr "mode" "XF")])
16807
16808 (define_insn "*movdfcc_1"
16809   [(set (match_operand:DF 0 "register_operand" "=f,f,&r,&r,r ,r")
16810         (if_then_else:DF (match_operator 1 "fcmov_comparison_operator"
16811                                 [(reg FLAGS_REG) (const_int 0)])
16812                       (match_operand:DF 2 "nonimmediate_operand"
16813                                                "f ,0,rm,0 ,rm,0")
16814                       (match_operand:DF 3 "nonimmediate_operand"
16815                                                "0 ,f,0 ,rm,0, rm")))]
16816   "TARGET_80387 && TARGET_CMOVE
16817    && !(MEM_P (operands[2]) && MEM_P (operands[3]))"
16818   "@
16819    fcmov%F1\t{%2, %0|%0, %2}
16820    fcmov%f1\t{%3, %0|%0, %3}
16821    #
16822    #
16823    cmov%O2%C1\t{%2, %0|%0, %2}
16824    cmov%O2%c1\t{%3, %0|%0, %3}"
16825   [(set_attr "isa" "*,*,nox64,nox64,x64,x64")
16826    (set_attr "type" "fcmov,fcmov,multi,multi,icmov,icmov")
16827    (set_attr "mode" "DF,DF,DI,DI,DI,DI")])
16828
16829 (define_split
16830   [(set (match_operand:DF 0 "register_and_not_any_fp_reg_operand")
16831         (if_then_else:DF (match_operator 1 "fcmov_comparison_operator"
16832                                 [(reg FLAGS_REG) (const_int 0)])
16833                       (match_operand:DF 2 "nonimmediate_operand")
16834                       (match_operand:DF 3 "nonimmediate_operand")))]
16835   "!TARGET_64BIT && reload_completed"
16836   [(set (match_dup 2)
16837         (if_then_else:SI (match_dup 1) (match_dup 4) (match_dup 5)))
16838    (set (match_dup 3)
16839         (if_then_else:SI (match_dup 1) (match_dup 6) (match_dup 7)))]
16840 {
16841   split_double_mode (DImode, &operands[2], 2, &operands[4], &operands[6]);
16842   split_double_mode (DImode, &operands[0], 1, &operands[2], &operands[3]);
16843 })
16844
16845 (define_insn "*movsfcc_1_387"
16846   [(set (match_operand:SF 0 "register_operand" "=f,f,r,r")
16847         (if_then_else:SF (match_operator 1 "fcmov_comparison_operator"
16848                                 [(reg FLAGS_REG) (const_int 0)])
16849                       (match_operand:SF 2 "nonimmediate_operand" "f,0,rm,0")
16850                       (match_operand:SF 3 "nonimmediate_operand" "0,f,0,rm")))]
16851   "TARGET_80387 && TARGET_CMOVE
16852    && !(MEM_P (operands[2]) && MEM_P (operands[3]))"
16853   "@
16854    fcmov%F1\t{%2, %0|%0, %2}
16855    fcmov%f1\t{%3, %0|%0, %3}
16856    cmov%O2%C1\t{%2, %0|%0, %2}
16857    cmov%O2%c1\t{%3, %0|%0, %3}"
16858   [(set_attr "type" "fcmov,fcmov,icmov,icmov")
16859    (set_attr "mode" "SF,SF,SI,SI")])
16860
16861 ;; Don't do conditional moves with memory inputs.  This splitter helps
16862 ;; register starved x86_32 by forcing inputs into registers before reload.
16863 (define_split
16864   [(set (match_operand:MODEF 0 "register_operand")
16865         (if_then_else:MODEF (match_operator 1 "ix86_comparison_operator"
16866                               [(reg FLAGS_REG) (const_int 0)])
16867           (match_operand:MODEF 2 "nonimmediate_operand")
16868           (match_operand:MODEF 3 "nonimmediate_operand")))]
16869   "!TARGET_64BIT && TARGET_80387 && TARGET_CMOVE
16870    && TARGET_AVOID_MEM_OPND_FOR_CMOVE
16871    && (MEM_P (operands[2]) || MEM_P (operands[3]))
16872    && can_create_pseudo_p ()
16873    && optimize_insn_for_speed_p ()"
16874   [(set (match_dup 0)
16875         (if_then_else:MODEF (match_dup 1) (match_dup 2) (match_dup 3)))]
16876 {
16877   if (MEM_P (operands[2]))
16878     operands[2] = force_reg (<MODE>mode, operands[2]);
16879   if (MEM_P (operands[3]))
16880     operands[3] = force_reg (<MODE>mode, operands[3]);
16881 })
16882
16883 ;; Don't do conditional moves with memory inputs
16884 (define_peephole2
16885   [(match_scratch:MODEF 2 "r")
16886    (set (match_operand:MODEF 0 "register_and_not_any_fp_reg_operand")
16887         (if_then_else:MODEF (match_operator 1 "fcmov_comparison_operator"
16888                               [(reg FLAGS_REG) (const_int 0)])
16889           (match_dup 0)
16890           (match_operand:MODEF 3 "memory_operand")))]
16891   "(<MODE>mode != DFmode || TARGET_64BIT)
16892    && TARGET_80387 && TARGET_CMOVE
16893    && TARGET_AVOID_MEM_OPND_FOR_CMOVE
16894    && optimize_insn_for_speed_p ()"
16895   [(set (match_dup 2) (match_dup 3))
16896    (set (match_dup 0)
16897         (if_then_else:MODEF (match_dup 1) (match_dup 0) (match_dup 2)))])
16898
16899 (define_peephole2
16900   [(match_scratch:MODEF 2 "r")
16901    (set (match_operand:MODEF 0 "register_and_not_any_fp_reg_operand")
16902         (if_then_else:MODEF (match_operator 1 "fcmov_comparison_operator"
16903                               [(reg FLAGS_REG) (const_int 0)])
16904           (match_operand:MODEF 3 "memory_operand")
16905           (match_dup 0)))]
16906   "(<MODE>mode != DFmode || TARGET_64BIT)
16907    && TARGET_80387 && TARGET_CMOVE
16908    && TARGET_AVOID_MEM_OPND_FOR_CMOVE
16909    && optimize_insn_for_speed_p ()"
16910   [(set (match_dup 2) (match_dup 3))
16911    (set (match_dup 0)
16912         (if_then_else:MODEF (match_dup 1) (match_dup 2) (match_dup 0)))])
16913
16914 ;; All moves in XOP pcmov instructions are 128 bits and hence we restrict
16915 ;; the scalar versions to have only XMM registers as operands.
16916
16917 ;; XOP conditional move
16918 (define_insn "*xop_pcmov_<mode>"
16919   [(set (match_operand:MODEF 0 "register_operand" "=x")
16920         (if_then_else:MODEF
16921           (match_operand:MODEF 1 "register_operand" "x")
16922           (match_operand:MODEF 2 "register_operand" "x")
16923           (match_operand:MODEF 3 "register_operand" "x")))]
16924   "TARGET_XOP"
16925   "vpcmov\t{%1, %3, %2, %0|%0, %2, %3, %1}"
16926   [(set_attr "type" "sse4arg")])
16927
16928 ;; These versions of the min/max patterns are intentionally ignorant of
16929 ;; their behavior wrt -0.0 and NaN (via the commutative operand mark).
16930 ;; Since both the tree-level MAX_EXPR and the rtl-level SMAX operator
16931 ;; are undefined in this condition, we're certain this is correct.
16932
16933 (define_insn "<code><mode>3"
16934   [(set (match_operand:MODEF 0 "register_operand" "=x,v")
16935         (smaxmin:MODEF
16936           (match_operand:MODEF 1 "nonimmediate_operand" "%0,v")
16937           (match_operand:MODEF 2 "nonimmediate_operand" "xm,vm")))]
16938   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH"
16939   "@
16940    <maxmin_float><ssemodesuffix>\t{%2, %0|%0, %2}
16941    v<maxmin_float><ssemodesuffix>\t{%2, %1, %0|%0, %1, %2}"
16942   [(set_attr "isa" "noavx,avx")
16943    (set_attr "prefix" "orig,vex")
16944    (set_attr "type" "sseadd")
16945    (set_attr "mode" "<MODE>")])
16946
16947 ;; These versions of the min/max patterns implement exactly the operations
16948 ;;   min = (op1 < op2 ? op1 : op2)
16949 ;;   max = (!(op1 < op2) ? op1 : op2)
16950 ;; Their operands are not commutative, and thus they may be used in the
16951 ;; presence of -0.0 and NaN.
16952
16953 (define_int_iterator IEEE_MAXMIN
16954         [UNSPEC_IEEE_MAX
16955          UNSPEC_IEEE_MIN])
16956
16957 (define_int_attr ieee_maxmin
16958         [(UNSPEC_IEEE_MAX "max")
16959          (UNSPEC_IEEE_MIN "min")])
16960
16961 (define_insn "*ieee_s<ieee_maxmin><mode>3"
16962   [(set (match_operand:MODEF 0 "register_operand" "=x,x")
16963         (unspec:MODEF
16964           [(match_operand:MODEF 1 "register_operand" "0,x")
16965            (match_operand:MODEF 2 "nonimmediate_operand" "xm,xm")]
16966           IEEE_MAXMIN))]
16967   "SSE_FLOAT_MODE_P (<MODE>mode) && TARGET_SSE_MATH"
16968   "@
16969    <ieee_maxmin><ssemodesuffix>\t{%2, %0|%0, %2}
16970    v<ieee_maxmin><ssemodesuffix>\t{%2, %1, %0|%0, %1, %2}"
16971   [(set_attr "isa" "noavx,avx")
16972    (set_attr "prefix" "orig,vex")
16973    (set_attr "type" "sseadd")
16974    (set_attr "mode" "<MODE>")])
16975
16976 ;; Make two stack loads independent:
16977 ;;   fld aa              fld aa
16978 ;;   fld %st(0)     ->   fld bb
16979 ;;   fmul bb             fmul %st(1), %st
16980 ;;
16981 ;; Actually we only match the last two instructions for simplicity.
16982 (define_peephole2
16983   [(set (match_operand 0 "fp_register_operand")
16984         (match_operand 1 "fp_register_operand"))
16985    (set (match_dup 0)
16986         (match_operator 2 "binary_fp_operator"
16987            [(match_dup 0)
16988             (match_operand 3 "memory_operand")]))]
16989   "REGNO (operands[0]) != REGNO (operands[1])"
16990   [(set (match_dup 0) (match_dup 3))
16991    (set (match_dup 0) (match_dup 4))]
16992
16993   ;; The % modifier is not operational anymore in peephole2's, so we have to
16994   ;; swap the operands manually in the case of addition and multiplication.
16995 {
16996   rtx op0, op1;
16997
16998   if (COMMUTATIVE_ARITH_P (operands[2]))
16999     op0 = operands[0], op1 = operands[1];
17000   else
17001     op0 = operands[1], op1 = operands[0];
17002
17003   operands[4] = gen_rtx_fmt_ee (GET_CODE (operands[2]),
17004                                 GET_MODE (operands[2]),
17005                                 op0, op1);
17006 })
17007
17008 ;; Conditional addition patterns
17009 (define_expand "add<mode>cc"
17010   [(match_operand:SWI 0 "register_operand")
17011    (match_operand 1 "ordered_comparison_operator")
17012    (match_operand:SWI 2 "register_operand")
17013    (match_operand:SWI 3 "const_int_operand")]
17014   ""
17015   "if (ix86_expand_int_addcc (operands)) DONE; else FAIL;")
17016 \f
17017 ;; Misc patterns (?)
17018
17019 ;; This pattern exists to put a dependency on all ebp-based memory accesses.
17020 ;; Otherwise there will be nothing to keep
17021 ;;
17022 ;; [(set (reg ebp) (reg esp))]
17023 ;; [(set (reg esp) (plus (reg esp) (const_int -160000)))
17024 ;;  (clobber (eflags)]
17025 ;; [(set (mem (plus (reg ebp) (const_int -160000))) (const_int 0))]
17026 ;;
17027 ;; in proper program order.
17028
17029 (define_insn "pro_epilogue_adjust_stack_<mode>_add"
17030   [(set (match_operand:P 0 "register_operand" "=r,r")
17031         (plus:P (match_operand:P 1 "register_operand" "0,r")
17032                 (match_operand:P 2 "<nonmemory_operand>" "r<i>,l<i>")))
17033    (clobber (reg:CC FLAGS_REG))
17034    (clobber (mem:BLK (scratch)))]
17035   ""
17036 {
17037   switch (get_attr_type (insn))
17038     {
17039     case TYPE_IMOV:
17040       return "mov{<imodesuffix>}\t{%1, %0|%0, %1}";
17041
17042     case TYPE_ALU:
17043       gcc_assert (rtx_equal_p (operands[0], operands[1]));
17044       if (x86_maybe_negate_const_int (&operands[2], <MODE>mode))
17045         return "sub{<imodesuffix>}\t{%2, %0|%0, %2}";
17046
17047       return "add{<imodesuffix>}\t{%2, %0|%0, %2}";
17048
17049     default:
17050       operands[2] = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
17051       return "lea{<imodesuffix>}\t{%E2, %0|%0, %E2}";
17052     }
17053 }
17054   [(set (attr "type")
17055         (cond [(and (eq_attr "alternative" "0")
17056                     (not (match_test "TARGET_OPT_AGU")))
17057                  (const_string "alu")
17058                (match_operand:<MODE> 2 "const0_operand")
17059                  (const_string "imov")
17060               ]
17061               (const_string "lea")))
17062    (set (attr "length_immediate")
17063         (cond [(eq_attr "type" "imov")
17064                  (const_string "0")
17065                (and (eq_attr "type" "alu")
17066                     (match_operand 2 "const128_operand"))
17067                  (const_string "1")
17068               ]
17069               (const_string "*")))
17070    (set_attr "mode" "<MODE>")])
17071
17072 (define_insn "pro_epilogue_adjust_stack_<mode>_sub"
17073   [(set (match_operand:P 0 "register_operand" "=r")
17074         (minus:P (match_operand:P 1 "register_operand" "0")
17075                  (match_operand:P 2 "register_operand" "r")))
17076    (clobber (reg:CC FLAGS_REG))
17077    (clobber (mem:BLK (scratch)))]
17078   ""
17079   "sub{<imodesuffix>}\t{%2, %0|%0, %2}"
17080   [(set_attr "type" "alu")
17081    (set_attr "mode" "<MODE>")])
17082
17083 (define_insn "allocate_stack_worker_probe_<mode>"
17084   [(set (match_operand:P 0 "register_operand" "=a")
17085         (unspec_volatile:P [(match_operand:P 1 "register_operand" "0")]
17086                             UNSPECV_STACK_PROBE))
17087    (clobber (reg:CC FLAGS_REG))]
17088   "ix86_target_stack_probe ()"
17089   "call\t___chkstk_ms"
17090   [(set_attr "type" "multi")
17091    (set_attr "length" "5")])
17092
17093 (define_expand "allocate_stack"
17094   [(match_operand 0 "register_operand")
17095    (match_operand 1 "general_operand")]
17096   "ix86_target_stack_probe ()"
17097 {
17098   rtx x;
17099
17100 #ifndef CHECK_STACK_LIMIT
17101 #define CHECK_STACK_LIMIT 0
17102 #endif
17103
17104   if (CHECK_STACK_LIMIT && CONST_INT_P (operands[1])
17105       && INTVAL (operands[1]) < CHECK_STACK_LIMIT)
17106     x = operands[1];
17107   else
17108     {
17109       rtx (*insn) (rtx, rtx);
17110
17111       x = copy_to_mode_reg (Pmode, operands[1]);
17112
17113       insn = (TARGET_64BIT
17114               ? gen_allocate_stack_worker_probe_di
17115               : gen_allocate_stack_worker_probe_si);
17116
17117       emit_insn (insn (x, x));
17118     }
17119
17120   x = expand_simple_binop (Pmode, MINUS, stack_pointer_rtx, x,
17121                            stack_pointer_rtx, 0, OPTAB_DIRECT);
17122
17123   if (x != stack_pointer_rtx)
17124     emit_move_insn (stack_pointer_rtx, x);
17125
17126   emit_move_insn (operands[0], virtual_stack_dynamic_rtx);
17127   DONE;
17128 })
17129
17130 ;; Use IOR for stack probes, this is shorter.
17131 (define_expand "probe_stack"
17132   [(match_operand 0 "memory_operand")]
17133   ""
17134 {
17135   rtx (*gen_ior3) (rtx, rtx, rtx);
17136
17137   gen_ior3 = (GET_MODE (operands[0]) == DImode
17138               ? gen_iordi3 : gen_iorsi3);
17139
17140   emit_insn (gen_ior3 (operands[0], operands[0], const0_rtx));
17141   DONE;
17142 })
17143
17144 (define_insn "adjust_stack_and_probe<mode>"
17145   [(set (match_operand:P 0 "register_operand" "=r")
17146         (unspec_volatile:P [(match_operand:P 1 "register_operand" "0")]
17147                             UNSPECV_PROBE_STACK_RANGE))
17148    (set (reg:P SP_REG)
17149         (minus:P (reg:P SP_REG) (match_operand:P 2 "const_int_operand" "n")))
17150    (clobber (reg:CC FLAGS_REG))
17151    (clobber (mem:BLK (scratch)))]
17152   ""
17153   "* return output_adjust_stack_and_probe (operands[0]);"
17154   [(set_attr "type" "multi")])
17155
17156 (define_insn "probe_stack_range<mode>"
17157   [(set (match_operand:P 0 "register_operand" "=r")
17158         (unspec_volatile:P [(match_operand:P 1 "register_operand" "0")
17159                             (match_operand:P 2 "const_int_operand" "n")]
17160                             UNSPECV_PROBE_STACK_RANGE))
17161    (clobber (reg:CC FLAGS_REG))]
17162   ""
17163   "* return output_probe_stack_range (operands[0], operands[2]);"
17164   [(set_attr "type" "multi")])
17165
17166 (define_expand "builtin_setjmp_receiver"
17167   [(label_ref (match_operand 0))]
17168   "!TARGET_64BIT && flag_pic"
17169 {
17170 #if TARGET_MACHO
17171   if (TARGET_MACHO)
17172     {
17173       rtx xops[3];
17174       rtx_code_label *label_rtx = gen_label_rtx ();
17175       emit_insn (gen_set_got_labelled (pic_offset_table_rtx, label_rtx));
17176       xops[0] = xops[1] = pic_offset_table_rtx;
17177       xops[2] = machopic_gen_offset (gen_rtx_LABEL_REF (SImode, label_rtx));
17178       ix86_expand_binary_operator (MINUS, SImode, xops);
17179     }
17180   else
17181 #endif
17182     emit_insn (gen_set_got (pic_offset_table_rtx));
17183   DONE;
17184 })
17185
17186 ;; Avoid redundant prefixes by splitting HImode arithmetic to SImode.
17187 ;; Do not split instructions with mask registers.
17188 (define_split
17189   [(set (match_operand 0 "general_reg_operand")
17190         (match_operator 3 "promotable_binary_operator"
17191            [(match_operand 1 "general_reg_operand")
17192             (match_operand 2 "aligned_operand")]))
17193    (clobber (reg:CC FLAGS_REG))]
17194   "! TARGET_PARTIAL_REG_STALL && reload_completed
17195    && ((GET_MODE (operands[0]) == HImode
17196         && ((optimize_function_for_speed_p (cfun) && !TARGET_FAST_PREFIX)
17197             /* ??? next two lines just !satisfies_constraint_K (...) */
17198             || !CONST_INT_P (operands[2])
17199             || satisfies_constraint_K (operands[2])))
17200        || (GET_MODE (operands[0]) == QImode
17201            && (TARGET_PROMOTE_QImode || optimize_function_for_size_p (cfun))))"
17202   [(parallel [(set (match_dup 0)
17203                    (match_op_dup 3 [(match_dup 1) (match_dup 2)]))
17204               (clobber (reg:CC FLAGS_REG))])]
17205 {
17206   operands[0] = gen_lowpart (SImode, operands[0]);
17207   operands[1] = gen_lowpart (SImode, operands[1]);
17208   if (GET_CODE (operands[3]) != ASHIFT)
17209     operands[2] = gen_lowpart (SImode, operands[2]);
17210   PUT_MODE (operands[3], SImode);
17211 })
17212
17213 ; Promote the QImode tests, as i386 has encoding of the AND
17214 ; instruction with 32-bit sign-extended immediate and thus the
17215 ; instruction size is unchanged, except in the %eax case for
17216 ; which it is increased by one byte, hence the ! optimize_size.
17217 (define_split
17218   [(set (match_operand 0 "flags_reg_operand")
17219         (match_operator 2 "compare_operator"
17220           [(and (match_operand 3 "aligned_operand")
17221                 (match_operand 4 "const_int_operand"))
17222            (const_int 0)]))
17223    (set (match_operand 1 "register_operand")
17224         (and (match_dup 3) (match_dup 4)))]
17225   "! TARGET_PARTIAL_REG_STALL && reload_completed
17226    && optimize_insn_for_speed_p ()
17227    && ((GET_MODE (operands[1]) == HImode && ! TARGET_FAST_PREFIX)
17228        || (GET_MODE (operands[1]) == QImode && TARGET_PROMOTE_QImode))
17229    /* Ensure that the operand will remain sign-extended immediate.  */
17230    && ix86_match_ccmode (insn, INTVAL (operands[4]) >= 0 ? CCNOmode : CCZmode)"
17231   [(parallel [(set (match_dup 0)
17232                    (match_op_dup 2 [(and:SI (match_dup 3) (match_dup 4))
17233                                     (const_int 0)]))
17234               (set (match_dup 1)
17235                    (and:SI (match_dup 3) (match_dup 4)))])]
17236 {
17237   operands[4]
17238     = gen_int_mode (INTVAL (operands[4])
17239                     & GET_MODE_MASK (GET_MODE (operands[1])), SImode);
17240   operands[1] = gen_lowpart (SImode, operands[1]);
17241   operands[3] = gen_lowpart (SImode, operands[3]);
17242 })
17243
17244 ; Don't promote the QImode tests, as i386 doesn't have encoding of
17245 ; the TEST instruction with 32-bit sign-extended immediate and thus
17246 ; the instruction size would at least double, which is not what we
17247 ; want even with ! optimize_size.
17248 (define_split
17249   [(set (match_operand 0 "flags_reg_operand")
17250         (match_operator 1 "compare_operator"
17251           [(and (match_operand:HI 2 "aligned_operand")
17252                 (match_operand:HI 3 "const_int_operand"))
17253            (const_int 0)]))]
17254   "! TARGET_PARTIAL_REG_STALL && reload_completed
17255    && ! TARGET_FAST_PREFIX
17256    && optimize_insn_for_speed_p ()
17257    /* Ensure that the operand will remain sign-extended immediate.  */
17258    && ix86_match_ccmode (insn, INTVAL (operands[3]) >= 0 ? CCNOmode : CCZmode)"
17259   [(set (match_dup 0)
17260         (match_op_dup 1 [(and:SI (match_dup 2) (match_dup 3))
17261                          (const_int 0)]))]
17262 {
17263   operands[3]
17264     = gen_int_mode (INTVAL (operands[3])
17265                     & GET_MODE_MASK (GET_MODE (operands[2])), SImode);
17266   operands[2] = gen_lowpart (SImode, operands[2]);
17267 })
17268
17269 (define_split
17270   [(set (match_operand 0 "register_operand")
17271         (neg (match_operand 1 "register_operand")))
17272    (clobber (reg:CC FLAGS_REG))]
17273   "! TARGET_PARTIAL_REG_STALL && reload_completed
17274    && (GET_MODE (operands[0]) == HImode
17275        || (GET_MODE (operands[0]) == QImode
17276            && (TARGET_PROMOTE_QImode
17277                || optimize_insn_for_size_p ())))"
17278   [(parallel [(set (match_dup 0)
17279                    (neg:SI (match_dup 1)))
17280               (clobber (reg:CC FLAGS_REG))])]
17281 {
17282   operands[0] = gen_lowpart (SImode, operands[0]);
17283   operands[1] = gen_lowpart (SImode, operands[1]);
17284 })
17285
17286 ;; Do not split instructions with mask regs.
17287 (define_split
17288   [(set (match_operand 0 "general_reg_operand")
17289         (not (match_operand 1 "general_reg_operand")))]
17290   "! TARGET_PARTIAL_REG_STALL && reload_completed
17291    && (GET_MODE (operands[0]) == HImode
17292        || (GET_MODE (operands[0]) == QImode
17293            && (TARGET_PROMOTE_QImode
17294                || optimize_insn_for_size_p ())))"
17295   [(set (match_dup 0)
17296         (not:SI (match_dup 1)))]
17297 {
17298   operands[0] = gen_lowpart (SImode, operands[0]);
17299   operands[1] = gen_lowpart (SImode, operands[1]);
17300 })
17301 \f
17302 ;; RTL Peephole optimizations, run before sched2.  These primarily look to
17303 ;; transform a complex memory operation into two memory to register operations.
17304
17305 ;; Don't push memory operands
17306 (define_peephole2
17307   [(set (match_operand:SWI 0 "push_operand")
17308         (match_operand:SWI 1 "memory_operand"))
17309    (match_scratch:SWI 2 "<r>")]
17310   "!(TARGET_PUSH_MEMORY || optimize_insn_for_size_p ())
17311    && !RTX_FRAME_RELATED_P (peep2_next_insn (0))"
17312   [(set (match_dup 2) (match_dup 1))
17313    (set (match_dup 0) (match_dup 2))])
17314
17315 ;; We need to handle SFmode only, because DFmode and XFmode are split to
17316 ;; SImode pushes.
17317 (define_peephole2
17318   [(set (match_operand:SF 0 "push_operand")
17319         (match_operand:SF 1 "memory_operand"))
17320    (match_scratch:SF 2 "r")]
17321   "!(TARGET_PUSH_MEMORY || optimize_insn_for_size_p ())
17322    && !RTX_FRAME_RELATED_P (peep2_next_insn (0))"
17323   [(set (match_dup 2) (match_dup 1))
17324    (set (match_dup 0) (match_dup 2))])
17325
17326 ;; Don't move an immediate directly to memory when the instruction
17327 ;; gets too big, or if LCP stalls are a problem for 16-bit moves.
17328 (define_peephole2
17329   [(match_scratch:SWI124 1 "<r>")
17330    (set (match_operand:SWI124 0 "memory_operand")
17331         (const_int 0))]
17332   "optimize_insn_for_speed_p ()
17333    && ((<MODE>mode == HImode
17334        && TARGET_LCP_STALL)
17335        || (!TARGET_USE_MOV0
17336           && TARGET_SPLIT_LONG_MOVES
17337           && get_attr_length (insn) >= ix86_cur_cost ()->large_insn))
17338    && peep2_regno_dead_p (0, FLAGS_REG)"
17339   [(parallel [(set (match_dup 2) (const_int 0))
17340               (clobber (reg:CC FLAGS_REG))])
17341    (set (match_dup 0) (match_dup 1))]
17342   "operands[2] = gen_lowpart (SImode, operands[1]);")
17343
17344 (define_peephole2
17345   [(match_scratch:SWI124 2 "<r>")
17346    (set (match_operand:SWI124 0 "memory_operand")
17347         (match_operand:SWI124 1 "immediate_operand"))]
17348   "optimize_insn_for_speed_p ()
17349    && ((<MODE>mode == HImode
17350        && TARGET_LCP_STALL)
17351        || (TARGET_SPLIT_LONG_MOVES
17352           && get_attr_length (insn) >= ix86_cur_cost ()->large_insn))"
17353   [(set (match_dup 2) (match_dup 1))
17354    (set (match_dup 0) (match_dup 2))])
17355
17356 ;; Don't compare memory with zero, load and use a test instead.
17357 (define_peephole2
17358   [(set (match_operand 0 "flags_reg_operand")
17359         (match_operator 1 "compare_operator"
17360           [(match_operand:SI 2 "memory_operand")
17361            (const_int 0)]))
17362    (match_scratch:SI 3 "r")]
17363   "optimize_insn_for_speed_p () && ix86_match_ccmode (insn, CCNOmode)"
17364   [(set (match_dup 3) (match_dup 2))
17365    (set (match_dup 0) (match_op_dup 1 [(match_dup 3) (const_int 0)]))])
17366
17367 ;; NOT is not pairable on Pentium, while XOR is, but one byte longer.
17368 ;; Don't split NOTs with a displacement operand, because resulting XOR
17369 ;; will not be pairable anyway.
17370 ;;
17371 ;; On AMD K6, NOT is vector decoded with memory operand that cannot be
17372 ;; represented using a modRM byte.  The XOR replacement is long decoded,
17373 ;; so this split helps here as well.
17374 ;;
17375 ;; Note: Can't do this as a regular split because we can't get proper
17376 ;; lifetime information then.
17377
17378 (define_peephole2
17379   [(set (match_operand:SWI124 0 "nonimmediate_operand")
17380         (not:SWI124 (match_operand:SWI124 1 "nonimmediate_operand")))]
17381   "optimize_insn_for_speed_p ()
17382    && ((TARGET_NOT_UNPAIRABLE
17383         && (!MEM_P (operands[0])
17384             || !memory_displacement_operand (operands[0], <MODE>mode)))
17385        || (TARGET_NOT_VECTORMODE
17386            && long_memory_operand (operands[0], <MODE>mode)))
17387    && peep2_regno_dead_p (0, FLAGS_REG)"
17388   [(parallel [(set (match_dup 0)
17389                    (xor:SWI124 (match_dup 1) (const_int -1)))
17390               (clobber (reg:CC FLAGS_REG))])])
17391
17392 ;; Non pairable "test imm, reg" instructions can be translated to
17393 ;; "and imm, reg" if reg dies.  The "and" form is also shorter (one
17394 ;; byte opcode instead of two, have a short form for byte operands),
17395 ;; so do it for other CPUs as well.  Given that the value was dead,
17396 ;; this should not create any new dependencies.  Pass on the sub-word
17397 ;; versions if we're concerned about partial register stalls.
17398
17399 (define_peephole2
17400   [(set (match_operand 0 "flags_reg_operand")
17401         (match_operator 1 "compare_operator"
17402           [(and:SI (match_operand:SI 2 "register_operand")
17403                    (match_operand:SI 3 "immediate_operand"))
17404            (const_int 0)]))]
17405   "ix86_match_ccmode (insn, CCNOmode)
17406    && (true_regnum (operands[2]) != AX_REG
17407        || satisfies_constraint_K (operands[3]))
17408    && peep2_reg_dead_p (1, operands[2])"
17409   [(parallel
17410      [(set (match_dup 0)
17411            (match_op_dup 1 [(and:SI (match_dup 2) (match_dup 3))
17412                             (const_int 0)]))
17413       (set (match_dup 2)
17414            (and:SI (match_dup 2) (match_dup 3)))])])
17415
17416 ;; We don't need to handle HImode case, because it will be promoted to SImode
17417 ;; on ! TARGET_PARTIAL_REG_STALL
17418
17419 (define_peephole2
17420   [(set (match_operand 0 "flags_reg_operand")
17421         (match_operator 1 "compare_operator"
17422           [(and:QI (match_operand:QI 2 "register_operand")
17423                    (match_operand:QI 3 "immediate_operand"))
17424            (const_int 0)]))]
17425   "! TARGET_PARTIAL_REG_STALL
17426    && ix86_match_ccmode (insn, CCNOmode)
17427    && true_regnum (operands[2]) != AX_REG
17428    && peep2_reg_dead_p (1, operands[2])"
17429   [(parallel
17430      [(set (match_dup 0)
17431            (match_op_dup 1 [(and:QI (match_dup 2) (match_dup 3))
17432                             (const_int 0)]))
17433       (set (match_dup 2)
17434            (and:QI (match_dup 2) (match_dup 3)))])])
17435
17436 (define_peephole2
17437   [(set (match_operand 0 "flags_reg_operand")
17438         (match_operator 1 "compare_operator"
17439           [(and:SI
17440              (zero_extract:SI
17441                (match_operand 2 "ext_register_operand")
17442                (const_int 8)
17443                (const_int 8))
17444              (match_operand 3 "const_int_operand"))
17445            (const_int 0)]))]
17446   "! TARGET_PARTIAL_REG_STALL
17447    && ix86_match_ccmode (insn, CCNOmode)
17448    && true_regnum (operands[2]) != AX_REG
17449    && peep2_reg_dead_p (1, operands[2])"
17450   [(parallel [(set (match_dup 0)
17451                    (match_op_dup 1
17452                      [(and:SI
17453                         (zero_extract:SI
17454                           (match_dup 2)
17455                           (const_int 8)
17456                           (const_int 8))
17457                         (match_dup 3))
17458                       (const_int 0)]))
17459               (set (zero_extract:SI (match_dup 2)
17460                                     (const_int 8)
17461                                     (const_int 8))
17462                    (and:SI
17463                      (zero_extract:SI
17464                        (match_dup 2)
17465                        (const_int 8)
17466                        (const_int 8))
17467                      (match_dup 3)))])])
17468
17469 ;; Don't do logical operations with memory inputs.
17470 (define_peephole2
17471   [(match_scratch:SI 2 "r")
17472    (parallel [(set (match_operand:SI 0 "register_operand")
17473                    (match_operator:SI 3 "arith_or_logical_operator"
17474                      [(match_dup 0)
17475                       (match_operand:SI 1 "memory_operand")]))
17476               (clobber (reg:CC FLAGS_REG))])]
17477   "!(TARGET_READ_MODIFY || optimize_insn_for_size_p ())"
17478   [(set (match_dup 2) (match_dup 1))
17479    (parallel [(set (match_dup 0)
17480                    (match_op_dup 3 [(match_dup 0) (match_dup 2)]))
17481               (clobber (reg:CC FLAGS_REG))])])
17482
17483 (define_peephole2
17484   [(match_scratch:SI 2 "r")
17485    (parallel [(set (match_operand:SI 0 "register_operand")
17486                    (match_operator:SI 3 "arith_or_logical_operator"
17487                      [(match_operand:SI 1 "memory_operand")
17488                       (match_dup 0)]))
17489               (clobber (reg:CC FLAGS_REG))])]
17490   "!(TARGET_READ_MODIFY || optimize_insn_for_size_p ())"
17491   [(set (match_dup 2) (match_dup 1))
17492    (parallel [(set (match_dup 0)
17493                    (match_op_dup 3 [(match_dup 2) (match_dup 0)]))
17494               (clobber (reg:CC FLAGS_REG))])])
17495
17496 ;; Prefer Load+RegOp to Mov+MemOp.  Watch out for cases when the memory address
17497 ;; refers to the destination of the load!
17498
17499 (define_peephole2
17500   [(set (match_operand:SI 0 "register_operand")
17501         (match_operand:SI 1 "register_operand"))
17502    (parallel [(set (match_dup 0)
17503                    (match_operator:SI 3 "commutative_operator"
17504                      [(match_dup 0)
17505                       (match_operand:SI 2 "memory_operand")]))
17506               (clobber (reg:CC FLAGS_REG))])]
17507   "REGNO (operands[0]) != REGNO (operands[1])
17508    && GENERAL_REGNO_P (REGNO (operands[0]))
17509    && GENERAL_REGNO_P (REGNO (operands[1]))"
17510   [(set (match_dup 0) (match_dup 4))
17511    (parallel [(set (match_dup 0)
17512                    (match_op_dup 3 [(match_dup 0) (match_dup 1)]))
17513               (clobber (reg:CC FLAGS_REG))])]
17514   "operands[4] = replace_rtx (operands[2], operands[0], operands[1]);")
17515
17516 (define_peephole2
17517   [(set (match_operand 0 "register_operand")
17518         (match_operand 1 "register_operand"))
17519    (set (match_dup 0)
17520                    (match_operator 3 "commutative_operator"
17521                      [(match_dup 0)
17522                       (match_operand 2 "memory_operand")]))]
17523   "REGNO (operands[0]) != REGNO (operands[1])
17524    && ((MMX_REG_P (operands[0]) && MMX_REG_P (operands[1])) 
17525        || (SSE_REG_P (operands[0]) && SSE_REG_P (operands[1])))"
17526   [(set (match_dup 0) (match_dup 2))
17527    (set (match_dup 0)
17528         (match_op_dup 3 [(match_dup 0) (match_dup 1)]))])
17529
17530 ; Don't do logical operations with memory outputs
17531 ;
17532 ; These two don't make sense for PPro/PII -- we're expanding a 4-uop
17533 ; instruction into two 1-uop insns plus a 2-uop insn.  That last has
17534 ; the same decoder scheduling characteristics as the original.
17535
17536 (define_peephole2
17537   [(match_scratch:SI 2 "r")
17538    (parallel [(set (match_operand:SI 0 "memory_operand")
17539                    (match_operator:SI 3 "arith_or_logical_operator"
17540                      [(match_dup 0)
17541                       (match_operand:SI 1 "nonmemory_operand")]))
17542               (clobber (reg:CC FLAGS_REG))])]
17543   "!(TARGET_READ_MODIFY_WRITE || optimize_insn_for_size_p ())
17544    /* Do not split stack checking probes.  */
17545    && GET_CODE (operands[3]) != IOR && operands[1] != const0_rtx"
17546   [(set (match_dup 2) (match_dup 0))
17547    (parallel [(set (match_dup 2)
17548                    (match_op_dup 3 [(match_dup 2) (match_dup 1)]))
17549               (clobber (reg:CC FLAGS_REG))])
17550    (set (match_dup 0) (match_dup 2))])
17551
17552 (define_peephole2
17553   [(match_scratch:SI 2 "r")
17554    (parallel [(set (match_operand:SI 0 "memory_operand")
17555                    (match_operator:SI 3 "arith_or_logical_operator"
17556                      [(match_operand:SI 1 "nonmemory_operand")
17557                       (match_dup 0)]))
17558               (clobber (reg:CC FLAGS_REG))])]
17559   "!(TARGET_READ_MODIFY_WRITE || optimize_insn_for_size_p ())
17560    /* Do not split stack checking probes.  */
17561    && GET_CODE (operands[3]) != IOR && operands[1] != const0_rtx"
17562   [(set (match_dup 2) (match_dup 0))
17563    (parallel [(set (match_dup 2)
17564                    (match_op_dup 3 [(match_dup 1) (match_dup 2)]))
17565               (clobber (reg:CC FLAGS_REG))])
17566    (set (match_dup 0) (match_dup 2))])
17567
17568 ;; Attempt to use arith or logical operations with memory outputs with
17569 ;; setting of flags.
17570 (define_peephole2
17571   [(set (match_operand:SWI 0 "register_operand")
17572         (match_operand:SWI 1 "memory_operand"))
17573    (parallel [(set (match_dup 0)
17574                    (match_operator:SWI 3 "plusminuslogic_operator"
17575                      [(match_dup 0)
17576                       (match_operand:SWI 2 "<nonmemory_operand>")]))
17577               (clobber (reg:CC FLAGS_REG))])
17578    (set (match_dup 1) (match_dup 0))
17579    (set (reg FLAGS_REG) (compare (match_dup 0) (const_int 0)))]
17580   "(TARGET_READ_MODIFY_WRITE || optimize_insn_for_size_p ())
17581    && peep2_reg_dead_p (4, operands[0])
17582    && !reg_overlap_mentioned_p (operands[0], operands[1])
17583    && !reg_overlap_mentioned_p (operands[0], operands[2])
17584    && (<MODE>mode != QImode
17585        || immediate_operand (operands[2], QImode)
17586        || q_regs_operand (operands[2], QImode))
17587    && ix86_match_ccmode (peep2_next_insn (3),
17588                          (GET_CODE (operands[3]) == PLUS
17589                           || GET_CODE (operands[3]) == MINUS)
17590                          ? CCGOCmode : CCNOmode)"
17591   [(parallel [(set (match_dup 4) (match_dup 5))
17592               (set (match_dup 1) (match_op_dup 3 [(match_dup 1)
17593                                                   (match_dup 2)]))])]
17594 {
17595   operands[4] = SET_DEST (PATTERN (peep2_next_insn (3)));
17596   operands[5] = gen_rtx_fmt_ee (GET_CODE (operands[3]), <MODE>mode,
17597                                 copy_rtx (operands[1]),
17598                                 copy_rtx (operands[2]));
17599   operands[5] = gen_rtx_COMPARE (GET_MODE (operands[4]),
17600                                  operands[5], const0_rtx);
17601 })
17602
17603 (define_peephole2
17604   [(parallel [(set (match_operand:SWI 0 "register_operand")
17605                    (match_operator:SWI 2 "plusminuslogic_operator"
17606                      [(match_dup 0)
17607                       (match_operand:SWI 1 "memory_operand")]))
17608               (clobber (reg:CC FLAGS_REG))])
17609    (set (match_dup 1) (match_dup 0))
17610    (set (reg FLAGS_REG) (compare (match_dup 0) (const_int 0)))]
17611   "(TARGET_READ_MODIFY_WRITE || optimize_insn_for_size_p ())
17612    && GET_CODE (operands[2]) != MINUS
17613    && peep2_reg_dead_p (3, operands[0])
17614    && !reg_overlap_mentioned_p (operands[0], operands[1])
17615    && ix86_match_ccmode (peep2_next_insn (2),
17616                          GET_CODE (operands[2]) == PLUS
17617                          ? CCGOCmode : CCNOmode)"
17618   [(parallel [(set (match_dup 3) (match_dup 4))
17619               (set (match_dup 1) (match_op_dup 2 [(match_dup 1)
17620                                                   (match_dup 0)]))])]
17621 {
17622   operands[3] = SET_DEST (PATTERN (peep2_next_insn (2)));
17623   operands[4] = gen_rtx_fmt_ee (GET_CODE (operands[2]), <MODE>mode,
17624                                 copy_rtx (operands[1]),
17625                                 copy_rtx (operands[0]));
17626   operands[4] = gen_rtx_COMPARE (GET_MODE (operands[3]),
17627                                  operands[4], const0_rtx);
17628 })
17629
17630 (define_peephole2
17631   [(set (match_operand:SWI12 0 "register_operand")
17632         (match_operand:SWI12 1 "memory_operand"))
17633    (parallel [(set (match_operand:SI 4 "register_operand")
17634                    (match_operator:SI 3 "plusminuslogic_operator"
17635                      [(match_dup 4)
17636                       (match_operand:SI 2 "nonmemory_operand")]))
17637               (clobber (reg:CC FLAGS_REG))])
17638    (set (match_dup 1) (match_dup 0))
17639    (set (reg FLAGS_REG) (compare (match_dup 0) (const_int 0)))]
17640   "(TARGET_READ_MODIFY_WRITE || optimize_insn_for_size_p ())
17641    && REG_P (operands[0]) && REG_P (operands[4])
17642    && REGNO (operands[0]) == REGNO (operands[4])
17643    && peep2_reg_dead_p (4, operands[0])
17644    && (<MODE>mode != QImode
17645        || immediate_operand (operands[2], SImode)
17646        || q_regs_operand (operands[2], SImode))
17647    && !reg_overlap_mentioned_p (operands[0], operands[1])
17648    && !reg_overlap_mentioned_p (operands[0], operands[2])
17649    && ix86_match_ccmode (peep2_next_insn (3),
17650                          (GET_CODE (operands[3]) == PLUS
17651                           || GET_CODE (operands[3]) == MINUS)
17652                          ? CCGOCmode : CCNOmode)"
17653   [(parallel [(set (match_dup 4) (match_dup 5))
17654               (set (match_dup 1) (match_dup 6))])]
17655 {
17656   operands[2] = gen_lowpart (<MODE>mode, operands[2]);
17657   operands[4] = SET_DEST (PATTERN (peep2_next_insn (3)));
17658   operands[5] = gen_rtx_fmt_ee (GET_CODE (operands[3]), <MODE>mode,
17659                                 copy_rtx (operands[1]), operands[2]);
17660   operands[5] = gen_rtx_COMPARE (GET_MODE (operands[4]),
17661                                  operands[5], const0_rtx);
17662   operands[6] = gen_rtx_fmt_ee (GET_CODE (operands[3]), <MODE>mode,
17663                                 copy_rtx (operands[1]),
17664                                 copy_rtx (operands[2]));
17665 })
17666
17667 ;; Attempt to always use XOR for zeroing registers.
17668 (define_peephole2
17669   [(set (match_operand 0 "register_operand")
17670         (match_operand 1 "const0_operand"))]
17671   "GET_MODE_SIZE (GET_MODE (operands[0])) <= UNITS_PER_WORD
17672    && (! TARGET_USE_MOV0 || optimize_insn_for_size_p ())
17673    && GENERAL_REG_P (operands[0])
17674    && peep2_regno_dead_p (0, FLAGS_REG)"
17675   [(parallel [(set (match_dup 0) (const_int 0))
17676               (clobber (reg:CC FLAGS_REG))])]
17677   "operands[0] = gen_lowpart (word_mode, operands[0]);")
17678
17679 (define_peephole2
17680   [(set (strict_low_part (match_operand 0 "register_operand"))
17681         (const_int 0))]
17682   "(GET_MODE (operands[0]) == QImode
17683     || GET_MODE (operands[0]) == HImode)
17684    && (! TARGET_USE_MOV0 || optimize_insn_for_size_p ())
17685    && peep2_regno_dead_p (0, FLAGS_REG)"
17686   [(parallel [(set (strict_low_part (match_dup 0)) (const_int 0))
17687               (clobber (reg:CC FLAGS_REG))])])
17688
17689 ;; For HI, SI and DI modes, or $-1,reg is smaller than mov $-1,reg.
17690 (define_peephole2
17691   [(set (match_operand:SWI248 0 "register_operand")
17692         (const_int -1))]
17693   "(optimize_insn_for_size_p () || TARGET_MOVE_M1_VIA_OR)
17694    && peep2_regno_dead_p (0, FLAGS_REG)"
17695   [(parallel [(set (match_dup 0) (const_int -1))
17696               (clobber (reg:CC FLAGS_REG))])]
17697 {
17698   if (<MODE_SIZE> < GET_MODE_SIZE (SImode))
17699     operands[0] = gen_lowpart (SImode, operands[0]);
17700 })
17701
17702 ;; Attempt to convert simple lea to add/shift.
17703 ;; These can be created by move expanders.
17704 ;; Disable PLUS peepholes on TARGET_OPT_AGU, since all
17705 ;; relevant lea instructions were already split.
17706
17707 (define_peephole2
17708   [(set (match_operand:SWI48 0 "register_operand")
17709         (plus:SWI48 (match_dup 0)
17710                     (match_operand:SWI48 1 "<nonmemory_operand>")))]
17711   "!TARGET_OPT_AGU
17712    && peep2_regno_dead_p (0, FLAGS_REG)"
17713   [(parallel [(set (match_dup 0) (plus:SWI48 (match_dup 0) (match_dup 1)))
17714               (clobber (reg:CC FLAGS_REG))])])
17715
17716 (define_peephole2
17717   [(set (match_operand:SWI48 0 "register_operand")
17718         (plus:SWI48 (match_operand:SWI48 1 "<nonmemory_operand>")
17719                     (match_dup 0)))]
17720   "!TARGET_OPT_AGU
17721    && peep2_regno_dead_p (0, FLAGS_REG)"
17722   [(parallel [(set (match_dup 0) (plus:SWI48 (match_dup 0) (match_dup 1)))
17723               (clobber (reg:CC FLAGS_REG))])])
17724
17725 (define_peephole2
17726   [(set (match_operand:DI 0 "register_operand")
17727         (zero_extend:DI
17728           (plus:SI (match_operand:SI 1 "register_operand")
17729                    (match_operand:SI 2 "nonmemory_operand"))))]
17730   "TARGET_64BIT && !TARGET_OPT_AGU
17731    && REGNO (operands[0]) == REGNO (operands[1])
17732    && peep2_regno_dead_p (0, FLAGS_REG)"
17733   [(parallel [(set (match_dup 0)
17734                    (zero_extend:DI (plus:SI (match_dup 1) (match_dup 2))))
17735               (clobber (reg:CC FLAGS_REG))])])
17736
17737 (define_peephole2
17738   [(set (match_operand:DI 0 "register_operand")
17739         (zero_extend:DI
17740           (plus:SI (match_operand:SI 1 "nonmemory_operand")
17741                    (match_operand:SI 2 "register_operand"))))]
17742   "TARGET_64BIT && !TARGET_OPT_AGU
17743    && REGNO (operands[0]) == REGNO (operands[2])
17744    && peep2_regno_dead_p (0, FLAGS_REG)"
17745   [(parallel [(set (match_dup 0)
17746                    (zero_extend:DI (plus:SI (match_dup 2) (match_dup 1))))
17747               (clobber (reg:CC FLAGS_REG))])])
17748
17749 (define_peephole2
17750   [(set (match_operand:SWI48 0 "register_operand")
17751         (mult:SWI48 (match_dup 0)
17752                     (match_operand:SWI48 1 "const_int_operand")))]
17753   "exact_log2 (INTVAL (operands[1])) >= 0
17754    && peep2_regno_dead_p (0, FLAGS_REG)"
17755   [(parallel [(set (match_dup 0) (ashift:SWI48 (match_dup 0) (match_dup 1)))
17756               (clobber (reg:CC FLAGS_REG))])]
17757   "operands[1] = GEN_INT (exact_log2 (INTVAL (operands[1])));")
17758
17759 (define_peephole2
17760   [(set (match_operand:DI 0 "register_operand")
17761         (zero_extend:DI
17762           (mult:SI (match_operand:SI 1 "register_operand")
17763                    (match_operand:SI 2 "const_int_operand"))))]
17764   "TARGET_64BIT
17765    && exact_log2 (INTVAL (operands[2])) >= 0
17766    && REGNO (operands[0]) == REGNO (operands[1])
17767    && peep2_regno_dead_p (0, FLAGS_REG)"
17768   [(parallel [(set (match_dup 0)
17769                    (zero_extend:DI (ashift:SI (match_dup 1) (match_dup 2))))
17770               (clobber (reg:CC FLAGS_REG))])]
17771   "operands[2] = GEN_INT (exact_log2 (INTVAL (operands[2])));")
17772
17773 ;; The ESP adjustments can be done by the push and pop instructions.  Resulting
17774 ;; code is shorter, since push is only 1 byte, while add imm, %esp is 3 bytes.
17775 ;; On many CPUs it is also faster, since special hardware to avoid esp
17776 ;; dependencies is present.
17777
17778 ;; While some of these conversions may be done using splitters, we use
17779 ;; peepholes in order to allow combine_stack_adjustments pass to see
17780 ;; nonobfuscated RTL.
17781
17782 ;; Convert prologue esp subtractions to push.
17783 ;; We need register to push.  In order to keep verify_flow_info happy we have
17784 ;; two choices
17785 ;; - use scratch and clobber it in order to avoid dependencies
17786 ;; - use already live register
17787 ;; We can't use the second way right now, since there is no reliable way how to
17788 ;; verify that given register is live.  First choice will also most likely in
17789 ;; fewer dependencies.  On the place of esp adjustments it is very likely that
17790 ;; call clobbered registers are dead.  We may want to use base pointer as an
17791 ;; alternative when no register is available later.
17792
17793 (define_peephole2
17794   [(match_scratch:W 1 "r")
17795    (parallel [(set (reg:P SP_REG)
17796                    (plus:P (reg:P SP_REG)
17797                            (match_operand:P 0 "const_int_operand")))
17798               (clobber (reg:CC FLAGS_REG))
17799               (clobber (mem:BLK (scratch)))])]
17800   "(TARGET_SINGLE_PUSH || optimize_insn_for_size_p ())
17801    && INTVAL (operands[0]) == -GET_MODE_SIZE (word_mode)"
17802   [(clobber (match_dup 1))
17803    (parallel [(set (mem:W (pre_dec:P (reg:P SP_REG))) (match_dup 1))
17804               (clobber (mem:BLK (scratch)))])])
17805
17806 (define_peephole2
17807   [(match_scratch:W 1 "r")
17808    (parallel [(set (reg:P SP_REG)
17809                    (plus:P (reg:P SP_REG)
17810                            (match_operand:P 0 "const_int_operand")))
17811               (clobber (reg:CC FLAGS_REG))
17812               (clobber (mem:BLK (scratch)))])]
17813   "(TARGET_DOUBLE_PUSH || optimize_insn_for_size_p ())
17814    && INTVAL (operands[0]) == -2*GET_MODE_SIZE (word_mode)"
17815   [(clobber (match_dup 1))
17816    (set (mem:W (pre_dec:P (reg:P SP_REG))) (match_dup 1))
17817    (parallel [(set (mem:W (pre_dec:P (reg:P SP_REG))) (match_dup 1))
17818               (clobber (mem:BLK (scratch)))])])
17819
17820 ;; Convert esp subtractions to push.
17821 (define_peephole2
17822   [(match_scratch:W 1 "r")
17823    (parallel [(set (reg:P SP_REG)
17824                    (plus:P (reg:P SP_REG)
17825                            (match_operand:P 0 "const_int_operand")))
17826               (clobber (reg:CC FLAGS_REG))])]
17827   "(TARGET_SINGLE_PUSH || optimize_insn_for_size_p ())
17828    && INTVAL (operands[0]) == -GET_MODE_SIZE (word_mode)"
17829   [(clobber (match_dup 1))
17830    (set (mem:W (pre_dec:P (reg:P SP_REG))) (match_dup 1))])
17831
17832 (define_peephole2
17833   [(match_scratch:W 1 "r")
17834    (parallel [(set (reg:P SP_REG)
17835                    (plus:P (reg:P SP_REG)
17836                            (match_operand:P 0 "const_int_operand")))
17837               (clobber (reg:CC FLAGS_REG))])]
17838   "(TARGET_DOUBLE_PUSH || optimize_insn_for_size_p ())
17839    && INTVAL (operands[0]) == -2*GET_MODE_SIZE (word_mode)"
17840   [(clobber (match_dup 1))
17841    (set (mem:W (pre_dec:P (reg:P SP_REG))) (match_dup 1))
17842    (set (mem:W (pre_dec:P (reg:P SP_REG))) (match_dup 1))])
17843
17844 ;; Convert epilogue deallocator to pop.
17845 (define_peephole2
17846   [(match_scratch:W 1 "r")
17847    (parallel [(set (reg:P SP_REG)
17848                    (plus:P (reg:P SP_REG)
17849                            (match_operand:P 0 "const_int_operand")))
17850               (clobber (reg:CC FLAGS_REG))
17851               (clobber (mem:BLK (scratch)))])]
17852   "(TARGET_SINGLE_POP || optimize_insn_for_size_p ())
17853    && INTVAL (operands[0]) == GET_MODE_SIZE (word_mode)"
17854   [(parallel [(set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))
17855               (clobber (mem:BLK (scratch)))])])
17856
17857 ;; Two pops case is tricky, since pop causes dependency
17858 ;; on destination register.  We use two registers if available.
17859 (define_peephole2
17860   [(match_scratch:W 1 "r")
17861    (match_scratch:W 2 "r")
17862    (parallel [(set (reg:P SP_REG)
17863                    (plus:P (reg:P SP_REG)
17864                            (match_operand:P 0 "const_int_operand")))
17865               (clobber (reg:CC FLAGS_REG))
17866               (clobber (mem:BLK (scratch)))])]
17867   "(TARGET_DOUBLE_POP || optimize_insn_for_size_p ())
17868    && INTVAL (operands[0]) == 2*GET_MODE_SIZE (word_mode)"
17869   [(parallel [(set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))
17870               (clobber (mem:BLK (scratch)))])
17871    (set (match_dup 2) (mem:W (post_inc:P (reg:P SP_REG))))])
17872
17873 (define_peephole2
17874   [(match_scratch:W 1 "r")
17875    (parallel [(set (reg:P SP_REG)
17876                    (plus:P (reg:P SP_REG)
17877                            (match_operand:P 0 "const_int_operand")))
17878               (clobber (reg:CC FLAGS_REG))
17879               (clobber (mem:BLK (scratch)))])]
17880   "optimize_insn_for_size_p ()
17881    && INTVAL (operands[0]) == 2*GET_MODE_SIZE (word_mode)"
17882   [(parallel [(set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))
17883               (clobber (mem:BLK (scratch)))])
17884    (set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))])
17885
17886 ;; Convert esp additions to pop.
17887 (define_peephole2
17888   [(match_scratch:W 1 "r")
17889    (parallel [(set (reg:P SP_REG)
17890                    (plus:P (reg:P SP_REG)
17891                            (match_operand:P 0 "const_int_operand")))
17892               (clobber (reg:CC FLAGS_REG))])]
17893   "INTVAL (operands[0]) == GET_MODE_SIZE (word_mode)"
17894   [(set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))])
17895
17896 ;; Two pops case is tricky, since pop causes dependency
17897 ;; on destination register.  We use two registers if available.
17898 (define_peephole2
17899   [(match_scratch:W 1 "r")
17900    (match_scratch:W 2 "r")
17901    (parallel [(set (reg:P SP_REG)
17902                    (plus:P (reg:P SP_REG)
17903                            (match_operand:P 0 "const_int_operand")))
17904               (clobber (reg:CC FLAGS_REG))])]
17905   "INTVAL (operands[0]) == 2*GET_MODE_SIZE (word_mode)"
17906   [(set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))
17907    (set (match_dup 2) (mem:W (post_inc:P (reg:P SP_REG))))])
17908
17909 (define_peephole2
17910   [(match_scratch:W 1 "r")
17911    (parallel [(set (reg:P SP_REG)
17912                    (plus:P (reg:P SP_REG)
17913                            (match_operand:P 0 "const_int_operand")))
17914               (clobber (reg:CC FLAGS_REG))])]
17915   "optimize_insn_for_size_p ()
17916    && INTVAL (operands[0]) == 2*GET_MODE_SIZE (word_mode)"
17917   [(set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))
17918    (set (match_dup 1) (mem:W (post_inc:P (reg:P SP_REG))))])
17919 \f
17920 ;; Convert compares with 1 to shorter inc/dec operations when CF is not
17921 ;; required and register dies.  Similarly for 128 to -128.
17922 (define_peephole2
17923   [(set (match_operand 0 "flags_reg_operand")
17924         (match_operator 1 "compare_operator"
17925           [(match_operand 2 "register_operand")
17926            (match_operand 3 "const_int_operand")]))]
17927   "(((!TARGET_FUSE_CMP_AND_BRANCH || optimize_insn_for_size_p ())
17928      && incdec_operand (operands[3], GET_MODE (operands[3])))
17929     || (!TARGET_FUSE_CMP_AND_BRANCH
17930         && INTVAL (operands[3]) == 128))
17931    && ix86_match_ccmode (insn, CCGCmode)
17932    && peep2_reg_dead_p (1, operands[2])"
17933   [(parallel [(set (match_dup 0)
17934                    (match_op_dup 1 [(match_dup 2) (match_dup 3)]))
17935               (clobber (match_dup 2))])])
17936 \f
17937 ;; Convert imul by three, five and nine into lea
17938 (define_peephole2
17939   [(parallel
17940     [(set (match_operand:SWI48 0 "register_operand")
17941           (mult:SWI48 (match_operand:SWI48 1 "register_operand")
17942                       (match_operand:SWI48 2 "const359_operand")))
17943      (clobber (reg:CC FLAGS_REG))])]
17944   "!TARGET_PARTIAL_REG_STALL
17945    || <MODE>mode == SImode
17946    || optimize_function_for_size_p (cfun)"
17947   [(set (match_dup 0)
17948         (plus:SWI48 (mult:SWI48 (match_dup 1) (match_dup 2))
17949                     (match_dup 1)))]
17950   "operands[2] = GEN_INT (INTVAL (operands[2]) - 1);")
17951
17952 (define_peephole2
17953   [(parallel
17954     [(set (match_operand:SWI48 0 "register_operand")
17955           (mult:SWI48 (match_operand:SWI48 1 "nonimmediate_operand")
17956                       (match_operand:SWI48 2 "const359_operand")))
17957      (clobber (reg:CC FLAGS_REG))])]
17958   "optimize_insn_for_speed_p ()
17959    && (!TARGET_PARTIAL_REG_STALL || <MODE>mode == SImode)"
17960   [(set (match_dup 0) (match_dup 1))
17961    (set (match_dup 0)
17962         (plus:SWI48 (mult:SWI48 (match_dup 0) (match_dup 2))
17963                     (match_dup 0)))]
17964   "operands[2] = GEN_INT (INTVAL (operands[2]) - 1);")
17965
17966 ;; imul $32bit_imm, mem, reg is vector decoded, while
17967 ;; imul $32bit_imm, reg, reg is direct decoded.
17968 (define_peephole2
17969   [(match_scratch:SWI48 3 "r")
17970    (parallel [(set (match_operand:SWI48 0 "register_operand")
17971                    (mult:SWI48 (match_operand:SWI48 1 "memory_operand")
17972                                (match_operand:SWI48 2 "immediate_operand")))
17973               (clobber (reg:CC FLAGS_REG))])]
17974   "TARGET_SLOW_IMUL_IMM32_MEM && optimize_insn_for_speed_p ()
17975    && !satisfies_constraint_K (operands[2])"
17976   [(set (match_dup 3) (match_dup 1))
17977    (parallel [(set (match_dup 0) (mult:SWI48 (match_dup 3) (match_dup 2)))
17978               (clobber (reg:CC FLAGS_REG))])])
17979
17980 (define_peephole2
17981   [(match_scratch:SI 3 "r")
17982    (parallel [(set (match_operand:DI 0 "register_operand")
17983                    (zero_extend:DI
17984                      (mult:SI (match_operand:SI 1 "memory_operand")
17985                               (match_operand:SI 2 "immediate_operand"))))
17986               (clobber (reg:CC FLAGS_REG))])]
17987   "TARGET_64BIT
17988    && TARGET_SLOW_IMUL_IMM32_MEM && optimize_insn_for_speed_p ()
17989    && !satisfies_constraint_K (operands[2])"
17990   [(set (match_dup 3) (match_dup 1))
17991    (parallel [(set (match_dup 0)
17992                    (zero_extend:DI (mult:SI (match_dup 3) (match_dup 2))))
17993               (clobber (reg:CC FLAGS_REG))])])
17994
17995 ;; imul $8/16bit_imm, regmem, reg is vector decoded.
17996 ;; Convert it into imul reg, reg
17997 ;; It would be better to force assembler to encode instruction using long
17998 ;; immediate, but there is apparently no way to do so.
17999 (define_peephole2
18000   [(parallel [(set (match_operand:SWI248 0 "register_operand")
18001                    (mult:SWI248
18002                     (match_operand:SWI248 1 "nonimmediate_operand")
18003                     (match_operand:SWI248 2 "const_int_operand")))
18004               (clobber (reg:CC FLAGS_REG))])
18005    (match_scratch:SWI248 3 "r")]
18006   "TARGET_SLOW_IMUL_IMM8 && optimize_insn_for_speed_p ()
18007    && satisfies_constraint_K (operands[2])"
18008   [(set (match_dup 3) (match_dup 2))
18009    (parallel [(set (match_dup 0) (mult:SWI248 (match_dup 0) (match_dup 3)))
18010               (clobber (reg:CC FLAGS_REG))])]
18011 {
18012   if (!rtx_equal_p (operands[0], operands[1]))
18013     emit_move_insn (operands[0], operands[1]);
18014 })
18015
18016 ;; After splitting up read-modify operations, array accesses with memory
18017 ;; operands might end up in form:
18018 ;;  sall    $2, %eax
18019 ;;  movl    4(%esp), %edx
18020 ;;  addl    %edx, %eax
18021 ;; instead of pre-splitting:
18022 ;;  sall    $2, %eax
18023 ;;  addl    4(%esp), %eax
18024 ;; Turn it into:
18025 ;;  movl    4(%esp), %edx
18026 ;;  leal    (%edx,%eax,4), %eax
18027
18028 (define_peephole2
18029   [(match_scratch:W 5 "r")
18030    (parallel [(set (match_operand 0 "register_operand")
18031                    (ashift (match_operand 1 "register_operand")
18032                            (match_operand 2 "const_int_operand")))
18033                (clobber (reg:CC FLAGS_REG))])
18034    (parallel [(set (match_operand 3 "register_operand")
18035                    (plus (match_dup 0)
18036                          (match_operand 4 "x86_64_general_operand")))
18037                    (clobber (reg:CC FLAGS_REG))])]
18038   "IN_RANGE (INTVAL (operands[2]), 1, 3)
18039    /* Validate MODE for lea.  */
18040    && ((!TARGET_PARTIAL_REG_STALL
18041         && (GET_MODE (operands[0]) == QImode
18042             || GET_MODE (operands[0]) == HImode))
18043        || GET_MODE (operands[0]) == SImode
18044        || (TARGET_64BIT && GET_MODE (operands[0]) == DImode))
18045    && (rtx_equal_p (operands[0], operands[3])
18046        || peep2_reg_dead_p (2, operands[0]))
18047    /* We reorder load and the shift.  */
18048    && !reg_overlap_mentioned_p (operands[0], operands[4])"
18049   [(set (match_dup 5) (match_dup 4))
18050    (set (match_dup 0) (match_dup 1))]
18051 {
18052   machine_mode op1mode = GET_MODE (operands[1]);
18053   machine_mode mode = op1mode == DImode ? DImode : SImode;
18054   int scale = 1 << INTVAL (operands[2]);
18055   rtx index = gen_lowpart (word_mode, operands[1]);
18056   rtx base = gen_lowpart (word_mode, operands[5]);
18057   rtx dest = gen_lowpart (mode, operands[3]);
18058
18059   operands[1] = gen_rtx_PLUS (word_mode, base,
18060                               gen_rtx_MULT (word_mode, index, GEN_INT (scale)));
18061   operands[5] = base;
18062   if (mode != word_mode)
18063     operands[1] = gen_rtx_SUBREG (mode, operands[1], 0);
18064   if (op1mode != word_mode)
18065     operands[5] = gen_rtx_SUBREG (op1mode, operands[5], 0);
18066   operands[0] = dest;
18067 })
18068 \f
18069 ;; We used to use "int $5", in honor of #BR which maps to interrupt vector 5.
18070 ;; That, however, is usually mapped by the OS to SIGSEGV, which is often
18071 ;; caught for use by garbage collectors and the like.  Using an insn that
18072 ;; maps to SIGILL makes it more likely the program will rightfully die.
18073 ;; Keeping with tradition, "6" is in honor of #UD.
18074 (define_insn "trap"
18075   [(trap_if (const_int 1) (const_int 6))]
18076   ""
18077 {
18078 #ifdef HAVE_AS_IX86_UD2
18079   return "ud2";
18080 #else
18081   return ASM_SHORT "0x0b0f";
18082 #endif
18083 }
18084   [(set_attr "length" "2")])
18085
18086 (define_expand "prefetch"
18087   [(prefetch (match_operand 0 "address_operand")
18088              (match_operand:SI 1 "const_int_operand")
18089              (match_operand:SI 2 "const_int_operand"))]
18090   "TARGET_PREFETCH_SSE || TARGET_PRFCHW || TARGET_PREFETCHWT1"
18091 {
18092   bool write = INTVAL (operands[1]) != 0;
18093   int locality = INTVAL (operands[2]);
18094
18095   gcc_assert (IN_RANGE (locality, 0, 3));
18096
18097   /* Use 3dNOW prefetch in case we are asking for write prefetch not
18098      supported by SSE counterpart or the SSE prefetch is not available
18099      (K6 machines).  Otherwise use SSE prefetch as it allows specifying
18100      of locality.  */
18101   if (TARGET_PREFETCHWT1 && write && locality <= 2)
18102     operands[2] = const2_rtx;
18103   else if (TARGET_PRFCHW && (write || !TARGET_PREFETCH_SSE))
18104     operands[2] = GEN_INT (3);
18105   else
18106     operands[1] = const0_rtx;
18107 })
18108
18109 (define_insn "*prefetch_sse"
18110   [(prefetch (match_operand 0 "address_operand" "p")
18111              (const_int 0)
18112              (match_operand:SI 1 "const_int_operand"))]
18113   "TARGET_PREFETCH_SSE"
18114 {
18115   static const char * const patterns[4] = {
18116    "prefetchnta\t%a0", "prefetcht2\t%a0", "prefetcht1\t%a0", "prefetcht0\t%a0"
18117   };
18118
18119   int locality = INTVAL (operands[1]);
18120   gcc_assert (IN_RANGE (locality, 0, 3));
18121
18122   return patterns[locality];
18123 }
18124   [(set_attr "type" "sse")
18125    (set_attr "atom_sse_attr" "prefetch")
18126    (set (attr "length_address")
18127         (symbol_ref "memory_address_length (operands[0], false)"))
18128    (set_attr "memory" "none")])
18129
18130 (define_insn "*prefetch_3dnow"
18131   [(prefetch (match_operand 0 "address_operand" "p")
18132              (match_operand:SI 1 "const_int_operand" "n")
18133              (const_int 3))]
18134   "TARGET_PRFCHW"
18135 {
18136   if (INTVAL (operands[1]) == 0)
18137     return "prefetch\t%a0";
18138   else
18139     return "prefetchw\t%a0";
18140 }
18141   [(set_attr "type" "mmx")
18142    (set (attr "length_address")
18143         (symbol_ref "memory_address_length (operands[0], false)"))
18144    (set_attr "memory" "none")])
18145
18146 (define_insn "*prefetch_prefetchwt1"
18147   [(prefetch (match_operand 0 "address_operand" "p")
18148              (const_int 1)
18149              (const_int 2))]
18150   "TARGET_PREFETCHWT1"
18151   "prefetchwt1\t%a0";
18152   [(set_attr "type" "sse")
18153    (set (attr "length_address")
18154         (symbol_ref "memory_address_length (operands[0], false)"))
18155    (set_attr "memory" "none")])
18156
18157 (define_expand "stack_protect_set"
18158   [(match_operand 0 "memory_operand")
18159    (match_operand 1 "memory_operand")]
18160   "TARGET_SSP_TLS_GUARD"
18161 {
18162   rtx (*insn)(rtx, rtx);
18163
18164 #ifdef TARGET_THREAD_SSP_OFFSET
18165   operands[1] = GEN_INT (TARGET_THREAD_SSP_OFFSET);
18166   insn = (TARGET_LP64
18167           ? gen_stack_tls_protect_set_di
18168           : gen_stack_tls_protect_set_si);
18169 #else
18170   insn = (TARGET_LP64
18171           ? gen_stack_protect_set_di
18172           : gen_stack_protect_set_si);
18173 #endif
18174
18175   emit_insn (insn (operands[0], operands[1]));
18176   DONE;
18177 })
18178
18179 (define_insn "stack_protect_set_<mode>"
18180   [(set (match_operand:PTR 0 "memory_operand" "=m")
18181         (unspec:PTR [(match_operand:PTR 1 "memory_operand" "m")]
18182                     UNSPEC_SP_SET))
18183    (set (match_scratch:PTR 2 "=&r") (const_int 0))
18184    (clobber (reg:CC FLAGS_REG))]
18185   "TARGET_SSP_TLS_GUARD"
18186   "mov{<imodesuffix>}\t{%1, %2|%2, %1}\;mov{<imodesuffix>}\t{%2, %0|%0, %2}\;xor{l}\t%k2, %k2"
18187   [(set_attr "type" "multi")])
18188
18189 (define_insn "stack_tls_protect_set_<mode>"
18190   [(set (match_operand:PTR 0 "memory_operand" "=m")
18191         (unspec:PTR [(match_operand:PTR 1 "const_int_operand" "i")]
18192                     UNSPEC_SP_TLS_SET))
18193    (set (match_scratch:PTR 2 "=&r") (const_int 0))
18194    (clobber (reg:CC FLAGS_REG))]
18195   ""
18196   "mov{<imodesuffix>}\t{%@:%P1, %2|%2, <iptrsize> PTR %@:%P1}\;mov{<imodesuffix>}\t{%2, %0|%0, %2}\;xor{l}\t%k2, %k2"
18197   [(set_attr "type" "multi")])
18198
18199 (define_expand "stack_protect_test"
18200   [(match_operand 0 "memory_operand")
18201    (match_operand 1 "memory_operand")
18202    (match_operand 2)]
18203   "TARGET_SSP_TLS_GUARD"
18204 {
18205   rtx flags = gen_rtx_REG (CCZmode, FLAGS_REG);
18206
18207   rtx (*insn)(rtx, rtx, rtx);
18208
18209 #ifdef TARGET_THREAD_SSP_OFFSET
18210   operands[1] = GEN_INT (TARGET_THREAD_SSP_OFFSET);
18211   insn = (TARGET_LP64
18212           ? gen_stack_tls_protect_test_di
18213           : gen_stack_tls_protect_test_si);
18214 #else
18215   insn = (TARGET_LP64
18216           ? gen_stack_protect_test_di
18217           : gen_stack_protect_test_si);
18218 #endif
18219
18220   emit_insn (insn (flags, operands[0], operands[1]));
18221
18222   emit_jump_insn (gen_cbranchcc4 (gen_rtx_EQ (VOIDmode, flags, const0_rtx),
18223                                   flags, const0_rtx, operands[2]));
18224   DONE;
18225 })
18226
18227 (define_insn "stack_protect_test_<mode>"
18228   [(set (match_operand:CCZ 0 "flags_reg_operand")
18229         (unspec:CCZ [(match_operand:PTR 1 "memory_operand" "m")
18230                      (match_operand:PTR 2 "memory_operand" "m")]
18231                     UNSPEC_SP_TEST))
18232    (clobber (match_scratch:PTR 3 "=&r"))]
18233   "TARGET_SSP_TLS_GUARD"
18234   "mov{<imodesuffix>}\t{%1, %3|%3, %1}\;xor{<imodesuffix>}\t{%2, %3|%3, %2}"
18235   [(set_attr "type" "multi")])
18236
18237 (define_insn "stack_tls_protect_test_<mode>"
18238   [(set (match_operand:CCZ 0 "flags_reg_operand")
18239         (unspec:CCZ [(match_operand:PTR 1 "memory_operand" "m")
18240                      (match_operand:PTR 2 "const_int_operand" "i")]
18241                     UNSPEC_SP_TLS_TEST))
18242    (clobber (match_scratch:PTR 3 "=r"))]
18243   ""
18244   "mov{<imodesuffix>}\t{%1, %3|%3, %1}\;xor{<imodesuffix>}\t{%@:%P2, %3|%3, <iptrsize> PTR %@:%P2}"
18245   [(set_attr "type" "multi")])
18246
18247 (define_insn "sse4_2_crc32<mode>"
18248   [(set (match_operand:SI 0 "register_operand" "=r")
18249         (unspec:SI
18250           [(match_operand:SI 1 "register_operand" "0")
18251            (match_operand:SWI124 2 "nonimmediate_operand" "<r>m")]
18252           UNSPEC_CRC32))]
18253   "TARGET_SSE4_2 || TARGET_CRC32"
18254   "crc32{<imodesuffix>}\t{%2, %0|%0, %2}"
18255   [(set_attr "type" "sselog1")
18256    (set_attr "prefix_rep" "1")
18257    (set_attr "prefix_extra" "1")
18258    (set (attr "prefix_data16")
18259      (if_then_else (match_operand:HI 2)
18260        (const_string "1")
18261        (const_string "*")))
18262    (set (attr "prefix_rex")
18263      (if_then_else (match_operand:QI 2 "ext_QIreg_operand")
18264        (const_string "1")
18265        (const_string "*")))
18266    (set_attr "mode" "SI")])
18267
18268 (define_insn "sse4_2_crc32di"
18269   [(set (match_operand:DI 0 "register_operand" "=r")
18270         (unspec:DI
18271           [(match_operand:DI 1 "register_operand" "0")
18272            (match_operand:DI 2 "nonimmediate_operand" "rm")]
18273           UNSPEC_CRC32))]
18274   "TARGET_64BIT && (TARGET_SSE4_2 || TARGET_CRC32)"
18275   "crc32{q}\t{%2, %0|%0, %2}"
18276   [(set_attr "type" "sselog1")
18277    (set_attr "prefix_rep" "1")
18278    (set_attr "prefix_extra" "1")
18279    (set_attr "mode" "DI")])
18280
18281 (define_insn "rdpmc"
18282   [(set (match_operand:DI 0 "register_operand" "=A")
18283         (unspec_volatile:DI [(match_operand:SI 1 "register_operand" "c")]
18284                             UNSPECV_RDPMC))]
18285   "!TARGET_64BIT"
18286   "rdpmc"
18287   [(set_attr "type" "other")
18288    (set_attr "length" "2")])
18289
18290 (define_insn "rdpmc_rex64"
18291   [(set (match_operand:DI 0 "register_operand" "=a")
18292         (unspec_volatile:DI [(match_operand:SI 2 "register_operand" "c")]
18293                             UNSPECV_RDPMC))
18294    (set (match_operand:DI 1 "register_operand" "=d")
18295         (unspec_volatile:DI [(match_dup 2)] UNSPECV_RDPMC))]
18296   "TARGET_64BIT"
18297   "rdpmc"
18298   [(set_attr "type" "other")
18299    (set_attr "length" "2")])
18300
18301 (define_insn "rdtsc"
18302   [(set (match_operand:DI 0 "register_operand" "=A")
18303         (unspec_volatile:DI [(const_int 0)] UNSPECV_RDTSC))]
18304   "!TARGET_64BIT"
18305   "rdtsc"
18306   [(set_attr "type" "other")
18307    (set_attr "length" "2")])
18308
18309 (define_insn "rdtsc_rex64"
18310   [(set (match_operand:DI 0 "register_operand" "=a")
18311         (unspec_volatile:DI [(const_int 0)] UNSPECV_RDTSC))
18312    (set (match_operand:DI 1 "register_operand" "=d")
18313         (unspec_volatile:DI [(const_int 0)] UNSPECV_RDTSC))]
18314   "TARGET_64BIT"
18315   "rdtsc"
18316   [(set_attr "type" "other")
18317    (set_attr "length" "2")])
18318
18319 (define_insn "rdtscp"
18320   [(set (match_operand:DI 0 "register_operand" "=A")
18321         (unspec_volatile:DI [(const_int 0)] UNSPECV_RDTSCP))
18322    (set (match_operand:SI 1 "register_operand" "=c")
18323         (unspec_volatile:SI [(const_int 0)] UNSPECV_RDTSCP))]
18324   "!TARGET_64BIT"
18325   "rdtscp"
18326   [(set_attr "type" "other")
18327    (set_attr "length" "3")])
18328
18329 (define_insn "rdtscp_rex64"
18330   [(set (match_operand:DI 0 "register_operand" "=a")
18331         (unspec_volatile:DI [(const_int 0)] UNSPECV_RDTSCP))
18332    (set (match_operand:DI 1 "register_operand" "=d")
18333         (unspec_volatile:DI [(const_int 0)] UNSPECV_RDTSCP))
18334    (set (match_operand:SI 2 "register_operand" "=c")
18335         (unspec_volatile:SI [(const_int 0)] UNSPECV_RDTSCP))]
18336   "TARGET_64BIT"
18337   "rdtscp"
18338   [(set_attr "type" "other")
18339    (set_attr "length" "3")])
18340
18341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18342 ;;
18343 ;; FXSR, XSAVE and XSAVEOPT instructions
18344 ;;
18345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18346
18347 (define_insn "fxsave"
18348   [(set (match_operand:BLK 0 "memory_operand" "=m")
18349         (unspec_volatile:BLK [(const_int 0)] UNSPECV_FXSAVE))]
18350   "TARGET_FXSR"
18351   "fxsave\t%0"
18352   [(set_attr "type" "other")
18353    (set_attr "memory" "store")
18354    (set (attr "length")
18355         (symbol_ref "ix86_attr_length_address_default (insn) + 3"))])
18356
18357 (define_insn "fxsave64"
18358   [(set (match_operand:BLK 0 "memory_operand" "=m")
18359         (unspec_volatile:BLK [(const_int 0)] UNSPECV_FXSAVE64))]
18360   "TARGET_64BIT && TARGET_FXSR"
18361   "fxsave64\t%0"
18362   [(set_attr "type" "other")
18363    (set_attr "memory" "store")
18364    (set (attr "length")
18365         (symbol_ref "ix86_attr_length_address_default (insn) + 4"))])
18366
18367 (define_insn "fxrstor"
18368   [(unspec_volatile [(match_operand:BLK 0 "memory_operand" "m")]
18369                     UNSPECV_FXRSTOR)]
18370   "TARGET_FXSR"
18371   "fxrstor\t%0"
18372   [(set_attr "type" "other")
18373    (set_attr "memory" "load")
18374    (set (attr "length")
18375         (symbol_ref "ix86_attr_length_address_default (insn) + 3"))])
18376
18377 (define_insn "fxrstor64"
18378   [(unspec_volatile [(match_operand:BLK 0 "memory_operand" "m")]
18379                     UNSPECV_FXRSTOR64)]
18380   "TARGET_64BIT && TARGET_FXSR"
18381   "fxrstor64\t%0"
18382   [(set_attr "type" "other")
18383    (set_attr "memory" "load")
18384    (set (attr "length")
18385         (symbol_ref "ix86_attr_length_address_default (insn) + 4"))])
18386
18387 (define_int_iterator ANY_XSAVE
18388         [UNSPECV_XSAVE
18389          (UNSPECV_XSAVEOPT "TARGET_XSAVEOPT")
18390          (UNSPECV_XSAVEC "TARGET_XSAVEC")
18391          (UNSPECV_XSAVES "TARGET_XSAVES")])
18392
18393 (define_int_iterator ANY_XSAVE64
18394         [UNSPECV_XSAVE64
18395          (UNSPECV_XSAVEOPT64 "TARGET_XSAVEOPT")
18396          (UNSPECV_XSAVEC64 "TARGET_XSAVEC")
18397          (UNSPECV_XSAVES64 "TARGET_XSAVES")])
18398
18399 (define_int_attr xsave
18400         [(UNSPECV_XSAVE "xsave")
18401          (UNSPECV_XSAVE64 "xsave64")
18402          (UNSPECV_XSAVEOPT "xsaveopt")
18403          (UNSPECV_XSAVEOPT64 "xsaveopt64")
18404          (UNSPECV_XSAVEC "xsavec")
18405          (UNSPECV_XSAVEC64 "xsavec64")
18406          (UNSPECV_XSAVES "xsaves")
18407          (UNSPECV_XSAVES64 "xsaves64")])
18408
18409 (define_int_iterator ANY_XRSTOR
18410         [UNSPECV_XRSTOR
18411          (UNSPECV_XRSTORS "TARGET_XSAVES")])
18412
18413 (define_int_iterator ANY_XRSTOR64
18414         [UNSPECV_XRSTOR64
18415          (UNSPECV_XRSTORS64 "TARGET_XSAVES")])
18416
18417 (define_int_attr xrstor
18418         [(UNSPECV_XRSTOR "xrstor")
18419          (UNSPECV_XRSTOR64 "xrstor")
18420          (UNSPECV_XRSTORS "xrstors")
18421          (UNSPECV_XRSTORS64 "xrstors")])
18422
18423 (define_insn "<xsave>"
18424   [(set (match_operand:BLK 0 "memory_operand" "=m")
18425         (unspec_volatile:BLK
18426          [(match_operand:DI 1 "register_operand" "A")]
18427          ANY_XSAVE))]
18428   "!TARGET_64BIT && TARGET_XSAVE"
18429   "<xsave>\t%0"
18430   [(set_attr "type" "other")
18431    (set_attr "memory" "store")
18432    (set (attr "length")
18433         (symbol_ref "ix86_attr_length_address_default (insn) + 3"))])
18434
18435 (define_insn "<xsave>_rex64"
18436   [(set (match_operand:BLK 0 "memory_operand" "=m")
18437         (unspec_volatile:BLK
18438          [(match_operand:SI 1 "register_operand" "a")
18439           (match_operand:SI 2 "register_operand" "d")]
18440          ANY_XSAVE))]
18441   "TARGET_64BIT && TARGET_XSAVE"
18442   "<xsave>\t%0"
18443   [(set_attr "type" "other")
18444    (set_attr "memory" "store")
18445    (set (attr "length")
18446         (symbol_ref "ix86_attr_length_address_default (insn) + 3"))])
18447
18448 (define_insn "<xsave>"
18449   [(set (match_operand:BLK 0 "memory_operand" "=m")
18450         (unspec_volatile:BLK
18451          [(match_operand:SI 1 "register_operand" "a")
18452           (match_operand:SI 2 "register_operand" "d")]
18453          ANY_XSAVE64))]
18454   "TARGET_64BIT && TARGET_XSAVE"
18455   "<xsave>\t%0"
18456   [(set_attr "type" "other")
18457    (set_attr "memory" "store")
18458    (set (attr "length")
18459         (symbol_ref "ix86_attr_length_address_default (insn) + 4"))])
18460
18461 (define_insn "<xrstor>"
18462    [(unspec_volatile:BLK
18463      [(match_operand:BLK 0 "memory_operand" "m")
18464       (match_operand:DI 1 "register_operand" "A")]
18465      ANY_XRSTOR)]
18466   "!TARGET_64BIT && TARGET_XSAVE"
18467   "<xrstor>\t%0"
18468   [(set_attr "type" "other")
18469    (set_attr "memory" "load")
18470    (set (attr "length")
18471         (symbol_ref "ix86_attr_length_address_default (insn) + 3"))])
18472
18473 (define_insn "<xrstor>_rex64"
18474    [(unspec_volatile:BLK
18475      [(match_operand:BLK 0 "memory_operand" "m")
18476       (match_operand:SI 1 "register_operand" "a")
18477       (match_operand:SI 2 "register_operand" "d")]
18478      ANY_XRSTOR)]
18479   "TARGET_64BIT && TARGET_XSAVE"
18480   "<xrstor>\t%0"
18481   [(set_attr "type" "other")
18482    (set_attr "memory" "load")
18483    (set (attr "length")
18484         (symbol_ref "ix86_attr_length_address_default (insn) + 3"))])
18485
18486 (define_insn "<xrstor>64"
18487    [(unspec_volatile:BLK
18488      [(match_operand:BLK 0 "memory_operand" "m")
18489       (match_operand:SI 1 "register_operand" "a")
18490       (match_operand:SI 2 "register_operand" "d")]
18491      ANY_XRSTOR64)]
18492   "TARGET_64BIT && TARGET_XSAVE"
18493   "<xrstor>64\t%0"
18494   [(set_attr "type" "other")
18495    (set_attr "memory" "load")
18496    (set (attr "length")
18497         (symbol_ref "ix86_attr_length_address_default (insn) + 4"))])
18498
18499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18500 ;;
18501 ;; Floating-point instructions for atomic compound assignments
18502 ;;
18503 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18504
18505 ; Clobber all floating-point registers on environment save and restore
18506 ; to ensure that the TOS value saved at fnstenv is valid after fldenv.
18507 (define_insn "fnstenv"
18508   [(set (match_operand:BLK 0 "memory_operand" "=m")
18509         (unspec_volatile:BLK [(const_int 0)] UNSPECV_FNSTENV))
18510    (clobber (reg:HI FPCR_REG))
18511    (clobber (reg:XF ST0_REG))
18512    (clobber (reg:XF ST1_REG))
18513    (clobber (reg:XF ST2_REG))
18514    (clobber (reg:XF ST3_REG))
18515    (clobber (reg:XF ST4_REG))
18516    (clobber (reg:XF ST5_REG))
18517    (clobber (reg:XF ST6_REG))
18518    (clobber (reg:XF ST7_REG))]
18519   "TARGET_80387"
18520   "fnstenv\t%0"
18521   [(set_attr "type" "other")
18522    (set_attr "memory" "store")
18523    (set (attr "length")
18524         (symbol_ref "ix86_attr_length_address_default (insn) + 2"))])
18525
18526 (define_insn "fldenv"
18527   [(unspec_volatile [(match_operand:BLK 0 "memory_operand" "m")]
18528                     UNSPECV_FLDENV)
18529    (clobber (reg:CCFP FPSR_REG))
18530    (clobber (reg:HI FPCR_REG))
18531    (clobber (reg:XF ST0_REG))
18532    (clobber (reg:XF ST1_REG))
18533    (clobber (reg:XF ST2_REG))
18534    (clobber (reg:XF ST3_REG))
18535    (clobber (reg:XF ST4_REG))
18536    (clobber (reg:XF ST5_REG))
18537    (clobber (reg:XF ST6_REG))
18538    (clobber (reg:XF ST7_REG))]
18539   "TARGET_80387"
18540   "fldenv\t%0"
18541   [(set_attr "type" "other")
18542    (set_attr "memory" "load")
18543    (set (attr "length")
18544         (symbol_ref "ix86_attr_length_address_default (insn) + 2"))])
18545
18546 (define_insn "fnstsw"
18547   [(set (match_operand:HI 0 "nonimmediate_operand" "=a,m")
18548         (unspec_volatile:HI [(const_int 0)] UNSPECV_FNSTSW))]
18549   "TARGET_80387"
18550   "fnstsw\t%0"
18551   [(set_attr "type" "other,other")
18552    (set_attr "memory" "none,store")
18553    (set (attr "length")
18554         (symbol_ref "ix86_attr_length_address_default (insn) + 2"))])
18555
18556 (define_insn "fnclex"
18557   [(unspec_volatile [(const_int 0)] UNSPECV_FNCLEX)]
18558   "TARGET_80387"
18559   "fnclex"
18560   [(set_attr "type" "other")
18561    (set_attr "memory" "none")
18562    (set_attr "length" "2")])
18563
18564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18565 ;;
18566 ;; LWP instructions
18567 ;;
18568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18569
18570 (define_expand "lwp_llwpcb"
18571   [(unspec_volatile [(match_operand 0 "register_operand" "r")]
18572                     UNSPECV_LLWP_INTRINSIC)]
18573   "TARGET_LWP")
18574
18575 (define_insn "*lwp_llwpcb<mode>1"
18576   [(unspec_volatile [(match_operand:P 0 "register_operand" "r")]
18577                     UNSPECV_LLWP_INTRINSIC)]
18578   "TARGET_LWP"
18579   "llwpcb\t%0"
18580   [(set_attr "type" "lwp")
18581    (set_attr "mode" "<MODE>")
18582    (set_attr "length" "5")])
18583
18584 (define_expand "lwp_slwpcb"
18585   [(set (match_operand 0 "register_operand" "=r")
18586         (unspec_volatile [(const_int 0)] UNSPECV_SLWP_INTRINSIC))]
18587   "TARGET_LWP"
18588 {
18589   rtx (*insn)(rtx);
18590
18591   insn = (Pmode == DImode
18592           ? gen_lwp_slwpcbdi
18593           : gen_lwp_slwpcbsi);
18594
18595   emit_insn (insn (operands[0]));
18596   DONE;
18597 })
18598
18599 (define_insn "lwp_slwpcb<mode>"
18600   [(set (match_operand:P 0 "register_operand" "=r")
18601         (unspec_volatile:P [(const_int 0)] UNSPECV_SLWP_INTRINSIC))]
18602   "TARGET_LWP"
18603   "slwpcb\t%0"
18604   [(set_attr "type" "lwp")
18605    (set_attr "mode" "<MODE>")
18606    (set_attr "length" "5")])
18607
18608 (define_expand "lwp_lwpval<mode>3"
18609   [(unspec_volatile [(match_operand:SWI48 1 "register_operand" "r")
18610                      (match_operand:SI 2 "nonimmediate_operand" "rm")
18611                      (match_operand:SI 3 "const_int_operand" "i")]
18612                     UNSPECV_LWPVAL_INTRINSIC)]
18613   "TARGET_LWP"
18614   ;; Avoid unused variable warning.
18615   "(void) operands[0];")
18616
18617 (define_insn "*lwp_lwpval<mode>3_1"
18618   [(unspec_volatile [(match_operand:SWI48 0 "register_operand" "r")
18619                      (match_operand:SI 1 "nonimmediate_operand" "rm")
18620                      (match_operand:SI 2 "const_int_operand" "i")]
18621                     UNSPECV_LWPVAL_INTRINSIC)]
18622   "TARGET_LWP"
18623   "lwpval\t{%2, %1, %0|%0, %1, %2}"
18624   [(set_attr "type" "lwp")
18625    (set_attr "mode" "<MODE>")
18626    (set (attr "length")
18627         (symbol_ref "ix86_attr_length_address_default (insn) + 9"))])
18628
18629 (define_expand "lwp_lwpins<mode>3"
18630   [(set (reg:CCC FLAGS_REG)
18631         (unspec_volatile:CCC [(match_operand:SWI48 1 "register_operand" "r")
18632                               (match_operand:SI 2 "nonimmediate_operand" "rm")
18633                               (match_operand:SI 3 "const_int_operand" "i")]
18634                              UNSPECV_LWPINS_INTRINSIC))
18635    (set (match_operand:QI 0 "nonimmediate_operand" "=qm")
18636         (eq:QI (reg:CCC FLAGS_REG) (const_int 0)))]
18637   "TARGET_LWP")
18638
18639 (define_insn "*lwp_lwpins<mode>3_1"
18640   [(set (reg:CCC FLAGS_REG)
18641         (unspec_volatile:CCC [(match_operand:SWI48 0 "register_operand" "r")
18642                               (match_operand:SI 1 "nonimmediate_operand" "rm")
18643                               (match_operand:SI 2 "const_int_operand" "i")]
18644                              UNSPECV_LWPINS_INTRINSIC))]
18645   "TARGET_LWP"
18646   "lwpins\t{%2, %1, %0|%0, %1, %2}"
18647   [(set_attr "type" "lwp")
18648    (set_attr "mode" "<MODE>")
18649    (set (attr "length")
18650         (symbol_ref "ix86_attr_length_address_default (insn) + 9"))])
18651
18652 (define_int_iterator RDFSGSBASE
18653         [UNSPECV_RDFSBASE
18654          UNSPECV_RDGSBASE])
18655
18656 (define_int_iterator WRFSGSBASE
18657         [UNSPECV_WRFSBASE
18658          UNSPECV_WRGSBASE])
18659
18660 (define_int_attr fsgs
18661         [(UNSPECV_RDFSBASE "fs")
18662          (UNSPECV_RDGSBASE "gs")
18663          (UNSPECV_WRFSBASE "fs")
18664          (UNSPECV_WRGSBASE "gs")])
18665
18666 (define_insn "rd<fsgs>base<mode>"
18667   [(set (match_operand:SWI48 0 "register_operand" "=r")
18668         (unspec_volatile:SWI48 [(const_int 0)] RDFSGSBASE))]
18669   "TARGET_64BIT && TARGET_FSGSBASE"
18670   "rd<fsgs>base\t%0"
18671   [(set_attr "type" "other")
18672    (set_attr "prefix_extra" "2")])
18673
18674 (define_insn "wr<fsgs>base<mode>"
18675   [(unspec_volatile [(match_operand:SWI48 0 "register_operand" "r")]
18676                     WRFSGSBASE)]
18677   "TARGET_64BIT && TARGET_FSGSBASE"
18678   "wr<fsgs>base\t%0"
18679   [(set_attr "type" "other")
18680    (set_attr "prefix_extra" "2")])
18681
18682 (define_insn "rdrand<mode>_1"
18683   [(set (match_operand:SWI248 0 "register_operand" "=r")
18684         (unspec_volatile:SWI248 [(const_int 0)] UNSPECV_RDRAND))
18685    (set (reg:CCC FLAGS_REG)
18686         (unspec_volatile:CCC [(const_int 0)] UNSPECV_RDRAND))]
18687   "TARGET_RDRND"
18688   "rdrand\t%0"
18689   [(set_attr "type" "other")
18690    (set_attr "prefix_extra" "1")])
18691
18692 (define_insn "rdseed<mode>_1"
18693   [(set (match_operand:SWI248 0 "register_operand" "=r")
18694         (unspec_volatile:SWI248 [(const_int 0)] UNSPECV_RDSEED))
18695    (set (reg:CCC FLAGS_REG)
18696         (unspec_volatile:CCC [(const_int 0)] UNSPECV_RDSEED))]
18697   "TARGET_RDSEED"
18698   "rdseed\t%0"
18699   [(set_attr "type" "other")
18700    (set_attr "prefix_extra" "1")])
18701
18702 (define_expand "pause"
18703   [(set (match_dup 0)
18704         (unspec:BLK [(match_dup 0)] UNSPEC_PAUSE))]
18705   ""
18706 {
18707   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
18708   MEM_VOLATILE_P (operands[0]) = 1;
18709 })
18710
18711 ;; Use "rep; nop", instead of "pause", to support older assemblers.
18712 ;; They have the same encoding.
18713 (define_insn "*pause"
18714   [(set (match_operand:BLK 0)
18715         (unspec:BLK [(match_dup 0)] UNSPEC_PAUSE))]
18716   ""
18717   "rep%; nop"
18718   [(set_attr "length" "2")
18719    (set_attr "memory" "unknown")])
18720
18721 (define_expand "xbegin"
18722   [(set (match_operand:SI 0 "register_operand")
18723         (unspec_volatile:SI [(const_int 0)] UNSPECV_XBEGIN))]
18724   "TARGET_RTM"
18725 {
18726   rtx_code_label *label = gen_label_rtx ();
18727
18728   /* xbegin is emitted as jump_insn, so reload won't be able
18729      to reload its operand.  Force the value into AX hard register.  */
18730   rtx ax_reg = gen_rtx_REG (SImode, AX_REG);
18731   emit_move_insn (ax_reg, constm1_rtx);
18732
18733   emit_jump_insn (gen_xbegin_1 (ax_reg, label));
18734
18735   emit_label (label);
18736   LABEL_NUSES (label) = 1;
18737
18738   emit_move_insn (operands[0], ax_reg);
18739
18740   DONE;
18741 })
18742
18743 (define_insn "xbegin_1"
18744   [(set (pc)
18745         (if_then_else (ne (unspec [(const_int 0)] UNSPEC_XBEGIN_ABORT)
18746                           (const_int 0))
18747                       (label_ref (match_operand 1))
18748                       (pc)))
18749    (set (match_operand:SI 0 "register_operand" "+a")
18750         (unspec_volatile:SI [(match_dup 0)] UNSPECV_XBEGIN))]
18751   "TARGET_RTM"
18752   "xbegin\t%l1"
18753   [(set_attr "type" "other")
18754    (set_attr "length" "6")])
18755
18756 (define_insn "xend"
18757   [(unspec_volatile [(const_int 0)] UNSPECV_XEND)]
18758   "TARGET_RTM"
18759   "xend"
18760   [(set_attr "type" "other")
18761    (set_attr "length" "3")])
18762
18763 (define_insn "xabort"
18764   [(unspec_volatile [(match_operand:SI 0 "const_0_to_255_operand" "n")]
18765                     UNSPECV_XABORT)]
18766   "TARGET_RTM"
18767   "xabort\t%0"
18768   [(set_attr "type" "other")
18769    (set_attr "length" "3")])
18770
18771 (define_expand "xtest"
18772   [(set (match_operand:QI 0 "register_operand")
18773         (unspec_volatile:QI [(const_int 0)] UNSPECV_XTEST))]
18774   "TARGET_RTM"
18775 {
18776   emit_insn (gen_xtest_1 ());
18777
18778   ix86_expand_setcc (operands[0], NE,
18779                      gen_rtx_REG (CCZmode, FLAGS_REG), const0_rtx);
18780   DONE;
18781 })
18782
18783 (define_insn "xtest_1"
18784   [(set (reg:CCZ FLAGS_REG)
18785         (unspec_volatile:CCZ [(const_int 0)] UNSPECV_XTEST))]
18786   "TARGET_RTM"
18787   "xtest"
18788   [(set_attr "type" "other")
18789    (set_attr "length" "3")])
18790
18791 (define_insn "pcommit"
18792   [(unspec_volatile [(const_int 0)] UNSPECV_PCOMMIT)]
18793   "TARGET_PCOMMIT"
18794   "pcommit"
18795   [(set_attr "type" "other")
18796    (set_attr "length" "4")])
18797
18798 (define_insn "clwb"
18799   [(unspec_volatile [(match_operand 0 "address_operand" "p")]
18800                    UNSPECV_CLWB)]
18801   "TARGET_CLWB"
18802   "clwb\t%a0"
18803   [(set_attr "type" "sse")
18804    (set_attr "atom_sse_attr" "fence")
18805    (set_attr "memory" "unknown")])
18806
18807 (define_insn "clflushopt"
18808   [(unspec_volatile [(match_operand 0 "address_operand" "p")]
18809                    UNSPECV_CLFLUSHOPT)]
18810   "TARGET_CLFLUSHOPT"
18811   "clflushopt\t%a0"
18812   [(set_attr "type" "sse")
18813    (set_attr "atom_sse_attr" "fence")
18814    (set_attr "memory" "unknown")])
18815
18816 ;; MPX instructions
18817
18818 (define_expand "<mode>_mk"
18819   [(set (match_operand:BND 0 "register_operand")
18820     (unspec:BND
18821       [(mem:<bnd_ptr>
18822        (match_par_dup 3
18823         [(match_operand:<bnd_ptr> 1 "register_operand")
18824          (match_operand:<bnd_ptr> 2 "address_mpx_no_base_operand")]))]
18825       UNSPEC_BNDMK))]
18826   "TARGET_MPX"
18827 {
18828   operands[3] = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, operands[1],
18829                                                   operands[2]),
18830                                 UNSPEC_BNDMK_ADDR);
18831 })
18832
18833 (define_insn "*<mode>_mk"
18834   [(set (match_operand:BND 0 "register_operand" "=w")
18835     (unspec:BND
18836       [(match_operator:<bnd_ptr> 3 "bnd_mem_operator"
18837         [(unspec:<bnd_ptr>
18838            [(match_operand:<bnd_ptr> 1 "register_operand" "r")
18839             (match_operand:<bnd_ptr> 2 "address_mpx_no_base_operand" "Tb")]
18840            UNSPEC_BNDMK_ADDR)])]
18841       UNSPEC_BNDMK))]
18842   "TARGET_MPX"
18843   "bndmk\t{%3, %0|%0, %3}"
18844   [(set_attr "type" "mpxmk")])
18845
18846 (define_expand "mov<mode>"
18847   [(set (match_operand:BND 0 "general_operand")
18848         (match_operand:BND 1 "general_operand"))]
18849   "TARGET_MPX"
18850 {
18851   ix86_expand_move (<MODE>mode, operands);DONE;
18852 })
18853
18854 (define_insn "*mov<mode>_internal_mpx"
18855   [(set (match_operand:BND 0 "nonimmediate_operand" "=w,m")
18856         (match_operand:BND 1 "general_operand" "wm,w"))]
18857   "TARGET_MPX"
18858   "bndmov\t{%1, %0|%0, %1}"
18859   [(set_attr "type" "mpxmov")])
18860
18861 (define_expand "<mode>_<bndcheck>"
18862   [(parallel [(unspec [(match_operand:BND 0 "register_operand")
18863                        (match_operand:<bnd_ptr> 1 "address_no_seg_operand")] BNDCHECK)
18864               (set (match_dup 2)
18865                    (unspec:BLK [(match_dup 2)] UNSPEC_MPX_FENCE))])]
18866   "TARGET_MPX"
18867 {
18868   operands[2] = gen_rtx_MEM (BLKmode, operands[1]);
18869   MEM_VOLATILE_P (operands[2]) = 1;
18870 })
18871
18872 (define_insn "*<mode>_<bndcheck>"
18873   [(parallel [(unspec [(match_operand:BND 0 "register_operand" "w")
18874                        (match_operand:<bnd_ptr> 1 "address_no_seg_operand" "Ts")] BNDCHECK)
18875               (set (match_operand:BLK 2 "bnd_mem_operator")
18876                    (unspec:BLK [(match_dup 2)] UNSPEC_MPX_FENCE))])]
18877   "TARGET_MPX"
18878   "bnd<bndcheck>\t{%a1, %0|%0, %a1}"
18879   [(set_attr "type" "mpxchk")])
18880
18881 (define_expand "<mode>_ldx"
18882   [(parallel [(set:BND (match_operand:BND 0 "register_operand")
18883                        (unspec:BND
18884                          [(mem:<bnd_ptr>
18885                            (match_par_dup 3
18886                              [(match_operand:<bnd_ptr> 1 "address_mpx_no_index_operand")
18887                               (match_operand:<bnd_ptr> 2 "register_operand")]))]
18888                          UNSPEC_BNDLDX))
18889               (use (mem:BLK (match_dup 1)))])]
18890   "TARGET_MPX"
18891 {
18892   /* Avoid registers which connot be used as index.  */
18893   if (!index_register_operand (operands[2], Pmode))
18894     {
18895       rtx temp = gen_reg_rtx (Pmode);
18896       emit_move_insn (temp, operands[2]);
18897       operands[2] = temp;
18898     }
18899
18900   /* If it was a register originally then it may have
18901      mode other than Pmode.  We need to extend in such
18902      case because bndldx may work only with Pmode regs.  */
18903   if (GET_MODE (operands[2]) != Pmode)
18904     operands[2] = ix86_zero_extend_to_Pmode (operands[2]);
18905
18906   operands[3] = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, operands[1],
18907                                                   operands[2]),
18908                                 UNSPEC_BNDLDX_ADDR);
18909 })
18910
18911 (define_insn "*<mode>_ldx"
18912   [(parallel [(set:BND (match_operand:BND 0 "register_operand" "=w")
18913                        (unspec:BND
18914                          [(match_operator:<bnd_ptr> 3 "bnd_mem_operator"
18915                            [(unspec:<bnd_ptr>
18916                              [(match_operand:<bnd_ptr> 1 "address_mpx_no_index_operand" "Ti")
18917                               (match_operand:<bnd_ptr> 2 "register_operand" "l")]
18918                             UNSPEC_BNDLDX_ADDR)])]
18919                          UNSPEC_BNDLDX))
18920               (use (mem:BLK (match_dup 1)))])]
18921   "TARGET_MPX"
18922   "bndldx\t{%3, %0|%0, %3}"
18923   [(set_attr "type" "mpxld")])
18924
18925 (define_expand "<mode>_stx"
18926   [(parallel [(unspec [(mem:<bnd_ptr>
18927                          (match_par_dup 3
18928                            [(match_operand:<bnd_ptr> 0 "address_mpx_no_index_operand")
18929                             (match_operand:<bnd_ptr> 1 "register_operand")]))
18930                        (match_operand:BND 2 "register_operand")] UNSPEC_BNDSTX)
18931               (set (match_dup 4)
18932                    (unspec:BLK [(match_dup 4)] UNSPEC_MPX_FENCE))])]
18933   "TARGET_MPX"
18934 {
18935   /* Avoid registers which connot be used as index.  */
18936   if (!index_register_operand (operands[1], Pmode))
18937     {
18938       rtx temp = gen_reg_rtx (Pmode);
18939       emit_move_insn (temp, operands[1]);
18940       operands[1] = temp;
18941     }
18942
18943   /* If it was a register originally then it may have
18944      mode other than Pmode.  We need to extend in such
18945      case because bndstx may work only with Pmode regs.  */
18946   if (GET_MODE (operands[1]) != Pmode)
18947     operands[1] = ix86_zero_extend_to_Pmode (operands[1]);
18948
18949   operands[3] = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, operands[0],
18950                                                   operands[1]),
18951                                 UNSPEC_BNDLDX_ADDR);
18952   operands[4] = gen_rtx_MEM (BLKmode, operands[0]);
18953   MEM_VOLATILE_P (operands[4]) = 1;
18954 })
18955
18956 (define_insn "*<mode>_stx"
18957   [(parallel [(unspec [(match_operator:<bnd_ptr> 3 "bnd_mem_operator"
18958                          [(unspec:<bnd_ptr>
18959                           [(match_operand:<bnd_ptr> 0 "address_mpx_no_index_operand" "Ti")
18960                            (match_operand:<bnd_ptr> 1 "register_operand" "l")]
18961                          UNSPEC_BNDLDX_ADDR)])
18962                        (match_operand:BND 2 "register_operand" "w")] UNSPEC_BNDSTX)
18963               (set (match_operand:BLK 4 "bnd_mem_operator")
18964                    (unspec:BLK [(match_dup 4)] UNSPEC_MPX_FENCE))])]
18965   "TARGET_MPX"
18966   "bndstx\t{%2, %3|%3, %2}"
18967   [(set_attr "type" "mpxst")])
18968
18969 (define_insn "move_size_reloc_<mode>"
18970   [(set (match_operand:SWI48 0 "register_operand" "=r")
18971        (unspec:SWI48
18972         [(match_operand:SWI48 1 "symbol_operand")]
18973         UNSPEC_SIZEOF))]
18974   "TARGET_MPX"
18975 {
18976   if (x86_64_immediate_size_operand (operands[1], VOIDmode))
18977     return "mov{l}\t{%1@SIZE, %k0|%k0, %1@SIZE}";
18978   else
18979     return "movabs{q}\t{%1@SIZE, %0|%0, %1@SIZE}";
18980 }
18981   [(set_attr "type" "imov")
18982    (set_attr "mode" "<MODE>")])
18983
18984 (include "mmx.md")
18985 (include "sse.md")
18986 (include "sync.md")