OSDN Git Service

7016e74ec484139cbf592ec43d47b64e3bc93ddc
[pf3gnuchains/gcc-fork.git] / gcc / config / sh / sh.md
1 ;;- Machine description for Hitachi / SuperH SH.
2 ;;  Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;  Free Software Foundation, Inc.
4 ;;  Contributed by Steve Chamberlain (sac@cygnus.com).
5 ;;  Improved by Jim Wilson (wilson@cygnus.com).
6
7 ;; This file is part of GNU CC.
8
9 ;; GNU CC is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU CC is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU CC; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24
25 ;; ??? Should prepend a * to all pattern names which are not used.
26 ;; This will make the compiler smaller, and rebuilds after changes faster.
27
28 ;; ??? Should be enhanced to include support for many more GNU superoptimizer
29 ;; sequences.  Especially the sequences for arithmetic right shifts.
30
31 ;; ??? Should check all DImode patterns for consistency and usefulness.
32
33 ;; ??? The MAC.W and MAC.L instructions are not supported.  There is no
34 ;; way to generate them.
35
36 ;; ??? The cmp/str instruction is not supported.  Perhaps it can be used
37 ;; for a str* inline function.
38
39 ;; BSR is not generated by the compiler proper, but when relaxing, it
40 ;; generates .uses pseudo-ops that allow linker relaxation to create
41 ;; BSR.  This is actually implemented in bfd/{coff,elf32}-sh.c
42
43 ;; Special constraints for SH machine description:
44 ;;
45 ;;    t -- T
46 ;;    x -- mac
47 ;;    l -- pr
48 ;;    z -- r0
49 ;;
50 ;; Special formats used for outputting SH instructions:
51 ;;
52 ;;   %.  --  print a .s if insn needs delay slot
53 ;;   %@  --  print rte/rts if is/isn't an interrupt function
54 ;;   %#  --  output a nop if there is nothing to put in the delay slot
55 ;;   %O  --  print a constant without the #
56 ;;   %R  --  print the lsw reg of a double
57 ;;   %S  --  print the msw reg of a double
58 ;;   %T  --  print next word of a double REG or MEM
59 ;;
60 ;; Special predicates:
61 ;;
62 ;;  arith_operand          -- operand is valid source for arithmetic op
63 ;;  arith_reg_operand      -- operand is valid register for arithmetic op
64 ;;  general_movdst_operand -- operand is valid move destination
65 ;;  general_movsrc_operand -- operand is valid move source
66 ;;  logical_operand        -- operand is valid source for logical op
67
68 ;; -------------------------------------------------------------------------
69 ;; Constants
70 ;; -------------------------------------------------------------------------
71
72 (define_constants [
73   (AP_REG       145)
74   (PR_REG       146)
75   (T_REG        147)
76   (GBR_REG      144)
77   (MACH_REG     148)
78   (MACL_REG     149)
79   (FPUL_REG     150)
80   (RAP_REG      152)
81
82   (FPSCR_REG    151)
83
84   (PIC_REG      12)
85   (FP_REG       14)
86   (SP_REG       15)
87
88   (PR_MEDIA_REG 18)
89   (T_MEDIA_REG  19)
90
91   (R0_REG       0)
92   (R1_REG       1)
93   (R2_REG       2)
94   (R3_REG       3)
95   (R4_REG       4)
96   (R5_REG       5)
97   (R6_REG       6)
98   (R7_REG       7)
99   (R8_REG       8)
100   (R9_REG       9)
101   (R10_REG      10)
102   (R20_REG      20)
103   (R21_REG      21)
104   (R22_REG      22)
105   (R23_REG      23)
106
107   (DR0_REG      64)
108   (DR2_REG      66)
109   (DR4_REG      68)
110   (FR23_REG     87)
111
112   (TR0_REG      128)
113   (TR1_REG      129)
114   (TR2_REG      130)
115
116   (XD0_REG      136)
117
118   ;; These are used with unspec.
119   (UNSPEC_COMPACT_ARGS  0)
120   (UNSPEC_MOVA          1)
121   (UNSPEC_CASESI        2)
122   (UNSPEC_DATALABEL     3)
123   (UNSPEC_BBR           4)
124   (UNSPEC_SFUNC         5)
125   (UNSPEC_PIC           6)
126   (UNSPEC_GOT           7)
127   (UNSPEC_GOTOFF        8)
128   (UNSPEC_PLT           9)
129   (UNSPEC_CALLER        10)
130   (UNSPEC_GOTPLT        11)
131   (UNSPEC_ICACHE        12)
132   (UNSPEC_INIT_TRAMP    13)
133   (UNSPEC_FCOSA         14)
134   (UNSPEC_FSRRA         15)
135   (UNSPEC_FSINA         16)
136   (UNSPEC_NSB           17)
137   (UNSPEC_ALLOCO        18)
138
139   ;; These are used with unspec_volatile.
140   (UNSPECV_BLOCKAGE     0)
141   (UNSPECV_ALIGN        1)
142   (UNSPECV_CONST2       2)
143   (UNSPECV_CONST4       4)
144   (UNSPECV_CONST8       6)
145   (UNSPECV_WINDOW_END   10)
146   (UNSPECV_CONST_END    11)
147 ])  
148
149 ;; -------------------------------------------------------------------------
150 ;; Attributes
151 ;; -------------------------------------------------------------------------
152
153 ;; Target CPU.
154
155 (define_attr "cpu"
156  "sh1,sh2,sh3,sh3e,sh4,sh5"
157   (const (symbol_ref "sh_cpu_attr")))
158
159 (define_attr "endian" "big,little"
160  (const (if_then_else (symbol_ref "TARGET_LITTLE_ENDIAN")
161                       (const_string "little") (const_string "big"))))
162
163 ;; Indicate if the default fpu mode is single precision.
164 (define_attr "fpu_single" "yes,no"
165   (const (if_then_else (symbol_ref "TARGET_FPU_SINGLE")
166                          (const_string "yes") (const_string "no"))))
167
168 (define_attr "fmovd" "yes,no"
169   (const (if_then_else (symbol_ref "TARGET_FMOVD")
170                        (const_string "yes") (const_string "no"))))
171 ;; pipeline model
172 (define_attr "pipe_model" "sh1,sh4,sh5media"
173   (const
174    (cond [(symbol_ref "TARGET_SHMEDIA") (const_string "sh5media")
175           (symbol_ref "TARGET_SUPERSCALAR") (const_string "sh4")]
176          (const_string "sh1"))))
177
178 ;; cbranch      conditional branch instructions
179 ;; jump         unconditional jumps
180 ;; arith        ordinary arithmetic
181 ;; arith3       a compound insn that behaves similarly to a sequence of
182 ;;              three insns of type arith
183 ;; arith3b      like above, but might end with a redirected branch
184 ;; load         from memory
185 ;; load_si      Likewise, SImode variant for general register.
186 ;; store        to memory
187 ;; move         register to register
188 ;; fmove        register to register, floating point
189 ;; smpy         word precision integer multiply
190 ;; dmpy         longword or doublelongword precision integer multiply
191 ;; return       rts
192 ;; pload        load of pr reg, which can't be put into delay slot of rts
193 ;; prset        copy register to pr reg, ditto
194 ;; pstore       store of pr reg, which can't be put into delay slot of jsr
195 ;; prget        copy pr to register, ditto
196 ;; pcload       pc relative load of constant value
197 ;; pcload_si    Likewise, SImode variant for general register.
198 ;; rte          return from exception
199 ;; sfunc        special function call with known used registers
200 ;; call         function call
201 ;; fp           floating point
202 ;; fdiv         floating point divide (or square root)
203 ;; gp_fpul      move between general purpose register and fpul
204 ;; dfp_arith, dfp_cmp,dfp_conv
205 ;; dfdiv        double precision floating point divide (or square root)
206 ;; arith_media  SHmedia arithmetic, logical, and shift instructions
207 ;; cbranch_media SHmedia conditional branch instructions
208 ;; cmp_media    SHmedia compare instructions
209 ;; dfdiv_media  SHmedia double precision divide and square root
210 ;; dfmul_media  SHmedia double precision multiply instruction
211 ;; dfparith_media SHmedia double precision floating point arithmetic
212 ;; dfpconv_media SHmedia double precision floating point conversions
213 ;; dmpy_media   SHmedia longword multiply
214 ;; fcmp_media   SHmedia floating point compare instructions
215 ;; fdiv_media   SHmedia single precision divide and square root
216 ;; fload_media  SHmedia floating point register load instructions
217 ;; fmove_media  SHmedia floating point register moves (inc. fabs and fneg)
218 ;; fparith_media SHmedia single precision floating point arithmetic
219 ;; fpconv_media SHmedia single precision floating point conversions
220 ;; fstore_media SHmedia floating point register store instructions
221 ;; gettr_media  SHmedia gettr instruction
222 ;; invalidate_line_media SHmedia invaldiate_line sequence
223 ;; jump_media   SHmedia unconditional branch instructions
224 ;; load_media   SHmedia general register load instructions
225 ;; pt_media     SHmedia pt instruction (expanded by assembler)
226 ;; ptabs_media  SHmedia ptabs instruction
227 ;; store_media  SHmedia general register store instructions
228 ;; mcmp_media   SHmedia multimedia compare, absolute, saturating ops
229 ;; mac_media    SHmedia mac-style fixed point operations
230 ;; d2mpy_media  SHmedia: two 32 bit integer multiplies
231 ;; atrans       SHmedia approximate transcendential functions
232 ;; ustore_media SHmedia unaligned stores
233 ;; nil          no-op move, will be deleted.
234
235 (define_attr "type"
236  "cbranch,jump,jump_ind,arith,arith3,arith3b,dyn_shift,load,load_si,store,move,fmove,smpy,dmpy,return,pload,prset,pstore,prget,pcload,pcload_si,rte,sfunc,call,fp,fdiv,dfp_arith,dfp_cmp,dfp_conv,dfdiv,gp_fpul,arith_media,cbranch_media,cmp_media,dfdiv_media,dfmul_media,dfparith_media,dfpconv_media,dmpy_media,fcmp_media,fdiv_media,fload_media,fmove_media,fparith_media,fpconv_media,fstore_media,gettr_media,invalidate_line_media,jump_media,load_media,pt_media,ptabs_media,store_media,mcmp_media,mac_media,d2mpy_media,atrans_media,ustore_media,nil,other"
237   (const_string "other"))
238
239 ;; We define a new attribute namely "insn_class".We use
240 ;; this for DFA based pipeline description.
241 ;; Although the "type" attribute covers almost all insn 
242 ;; classes,it is more convenient to define new attribute
243 ;; for certain reservations.
244 ;;
245 ;; mt_group      SH4 "mt" group instructions.
246 ;;
247 ;; ex_group      SH4 "ex" group instructions.They mostly
248 ;;               overlap with arithmetic instructions but
249 ;;               new attribute defined to distinguish from
250 ;;               mt group instructions.
251 ;;
252 ;; lds_to_fpscr  The "type" attribute couldn't sufficiently
253 ;;               distinguish it from others.It is part of 
254 ;;               new attribute.Similar case with ldsmem_to_fpscr
255 ;;               and cwb. 
256
257 (define_attr "insn_class"
258              "mt_group,ex_group,lds_to_fpscr,ldsmem_to_fpscr,cwb,none"
259              (const_string "none"))
260
261 ;; Indicate what precision must be selected in fpscr for this insn, if any.
262
263 (define_attr "fp_mode" "single,double,none" (const_string "none"))
264
265 ; If a conditional branch destination is within -252..258 bytes away
266 ; from the instruction it can be 2 bytes long.  Something in the
267 ; range -4090..4100 bytes can be 6 bytes long.  All other conditional
268 ; branches are initially assumed to be 16 bytes long.
269 ; In machine_dependent_reorg, we split all branches that are longer than
270 ; 2 bytes.
271
272 ;; The maximum range used for SImode constant pool entries is 1018.  A final
273 ;; instruction can add 8 bytes while only being 4 bytes in size, thus we
274 ;; can have a total of 1022 bytes in the pool.  Add 4 bytes for a branch
275 ;; instruction around the pool table, 2 bytes of alignment before the table,
276 ;; and 30 bytes of alignment after the table.  That gives a maximum total
277 ;; pool size of 1058 bytes.
278 ;; Worst case code/pool content size ratio is 1:2 (using asms).
279 ;; Thus, in the worst case, there is one instruction in front of a maximum
280 ;; sized pool, and then there are 1052 bytes of pool for every 508 bytes of
281 ;; code.  For the last n bytes of code, there are 2n + 36 bytes of pool.
282 ;; If we have a forward branch, the initial table will be put after the
283 ;; unconditional branch.
284 ;;
285 ;; ??? We could do much better by keeping track of the actual pcloads within
286 ;; the branch range and in the pcload range in front of the branch range.
287
288 ;; ??? This looks ugly because genattrtab won't allow if_then_else or cond
289 ;; inside an le.
290 (define_attr "short_cbranch_p" "no,yes"
291   (cond [(ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
292          (const_string "no")
293          (leu (plus (minus (match_dup 0) (pc)) (const_int 252)) (const_int 506))
294          (const_string "yes")
295          (ne (symbol_ref "NEXT_INSN (PREV_INSN (insn)) != insn") (const_int 0))
296          (const_string "no")
297          (leu (plus (minus (match_dup 0) (pc)) (const_int 252)) (const_int 508))
298          (const_string "yes")
299          ] (const_string "no")))
300
301 (define_attr "med_branch_p" "no,yes"
302   (cond [(leu (plus (minus (match_dup 0) (pc)) (const_int 990))
303               (const_int 1988))
304          (const_string "yes")
305          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
306          (const_string "no")
307          (leu (plus (minus (match_dup 0) (pc)) (const_int 4092))
308               (const_int 8186))
309          (const_string "yes")
310          ] (const_string "no")))
311
312 (define_attr "med_cbranch_p" "no,yes"
313   (cond [(leu (plus (minus (match_dup 0) (pc)) (const_int 988))
314               (const_int 1986))
315          (const_string "yes")
316          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
317          (const_string "no")
318          (leu (plus (minus (match_dup 0) (pc)) (const_int 4090))
319                (const_int 8184))
320          (const_string "yes")
321          ] (const_string "no")))
322
323 (define_attr "braf_branch_p" "no,yes"
324   (cond [(ne (symbol_ref "! TARGET_SH2") (const_int 0))
325          (const_string "no")
326          (leu (plus (minus (match_dup 0) (pc)) (const_int 10330))
327               (const_int 20660))
328          (const_string "yes")
329          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
330          (const_string "no")
331          (leu (plus (minus (match_dup 0) (pc)) (const_int 32764))
332               (const_int 65530))
333          (const_string "yes")
334          ] (const_string "no")))
335
336 (define_attr "braf_cbranch_p" "no,yes"
337   (cond [(ne (symbol_ref "! TARGET_SH2") (const_int 0))
338          (const_string "no")
339          (leu (plus (minus (match_dup 0) (pc)) (const_int 10328))
340               (const_int 20658))
341          (const_string "yes")
342          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
343          (const_string "no")
344          (leu (plus (minus (match_dup 0) (pc)) (const_int 32762))
345               (const_int 65528))
346          (const_string "yes")
347          ] (const_string "no")))
348
349 ; An unconditional jump in the range -4092..4098 can be 2 bytes long.
350 ; For wider ranges, we need a combination of a code and a data part.
351 ; If we can get a scratch register for a long range jump, the code
352 ; part can be 4 bytes long; otherwise, it must be 8 bytes long.
353 ; If the jump is in the range -32764..32770, the data part can be 2 bytes
354 ; long; otherwise, it must be 6 bytes long.
355
356 ; All other instructions are two bytes long by default.
357
358 ;; ??? This should use something like *branch_p (minus (match_dup 0) (pc)),
359 ;; but getattrtab doesn't understand this.
360 (define_attr "length" ""
361   (cond [(eq_attr "type" "cbranch")
362          (cond [(eq_attr "short_cbranch_p" "yes")
363                 (const_int 2)
364                 (eq_attr "med_cbranch_p" "yes")
365                 (const_int 6)
366                 (eq_attr "braf_cbranch_p" "yes")
367                 (const_int 12)
368 ;; ??? using pc is not computed transitively.
369                 (ne (match_dup 0) (match_dup 0))
370                 (const_int 14)
371                 (ne (symbol_ref ("flag_pic")) (const_int 0))
372                 (const_int 24)
373                 ] (const_int 16))
374          (eq_attr "type" "jump")
375          (cond [(eq_attr "med_branch_p" "yes")
376                 (const_int 2)
377                 (and (eq (symbol_ref "GET_CODE (PREV_INSN (insn))")
378                          (symbol_ref "INSN"))
379                      (eq (symbol_ref "INSN_CODE (PREV_INSN (insn))")
380                          (symbol_ref "code_for_indirect_jump_scratch")))
381                 (if_then_else (eq_attr "braf_branch_p" "yes")
382                               (const_int 6)
383                               (const_int 10))
384                 (eq_attr "braf_branch_p" "yes")
385                 (const_int 10)
386 ;; ??? using pc is not computed transitively.
387                 (ne (match_dup 0) (match_dup 0))
388                 (const_int 12)
389                 (ne (symbol_ref ("flag_pic")) (const_int 0))
390                 (const_int 22)
391                 ] (const_int 14))
392          (eq_attr "type" "pt_media")
393          (if_then_else (ne (symbol_ref "TARGET_SHMEDIA64") (const_int 0))
394                        (const_int 20) (const_int 12))
395          ] (if_then_else (ne (symbol_ref "TARGET_SHMEDIA") (const_int 0))
396                          (const_int 4)
397                          (const_int 2))))
398
399 ;; (define_function_unit {name} {num-units} {n-users} {test}
400 ;;                       {ready-delay} {issue-delay} [{conflict-list}])
401
402 ;; Load and store instructions save a cycle if they are aligned on a
403 ;; four byte boundary.  Using a function unit for stores encourages
404 ;; gcc to separate load and store instructions by one instruction,
405 ;; which makes it more likely that the linker will be able to word
406 ;; align them when relaxing.
407
408 ;; Loads have a latency of two.
409 ;; However, call insns can have a delay slot, so that we want one more
410 ;; insn to be scheduled between the load of the function address and the call.
411 ;; This is equivalent to a latency of three.
412 ;; We cannot use a conflict list for this, because we need to distinguish
413 ;; between the actual call address and the function arguments.
414 ;; ADJUST_COST can only properly handle reductions of the cost, so we
415 ;; use a latency of three here.
416 ;; We only do this for SImode loads of general registers, to make the work
417 ;; for ADJUST_COST easier.
418 (define_function_unit "memory" 1 0
419   (and (eq_attr "pipe_model" "sh1")
420        (eq_attr "type" "load_si,pcload_si"))
421   3 2)
422 (define_function_unit "memory" 1 0
423   (and (eq_attr "pipe_model" "sh1")
424        (eq_attr "type" "load,pcload,pload,store,pstore"))
425   2 2)
426
427 (define_function_unit "int"    1 0
428   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "arith3,arith3b")) 3 3)
429
430 (define_function_unit "int"    1 0
431   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "dyn_shift")) 2 2)
432
433 (define_function_unit "int"    1 0
434   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "!arith3,arith3b,dyn_shift")) 1 1)
435
436 ;; ??? These are approximations.
437 (define_function_unit "mpy"    1 0
438   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "smpy")) 2 2)
439 (define_function_unit "mpy"    1 0
440   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "dmpy")) 3 3)
441
442 (define_function_unit "fp"     1 0
443   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "fp,fmove")) 2 1)
444 (define_function_unit "fp"     1 0
445   (and (eq_attr "pipe_model" "sh1") (eq_attr "type" "fdiv")) 13 12)
446
447
448 ;; SH4 scheduling
449 ;; The SH4 is a dual-issue implementation, thus we have to multiply all
450 ;; costs by at least two.
451 ;; There will be single increments of the modeled that don't correspond
452 ;; to the actual target ;; whenever two insns to be issued depend one a
453 ;; single resource, and the scheduler picks to be the first one.
454 ;; If we multiplied the costs just by two, just two of these single
455 ;; increments would amount to an actual cycle.  By picking a larger
456 ;; factor, we can ameliorate the effect; However, we then have to make sure
457 ;; that only two insns are modeled as issued per actual cycle.
458 ;; Moreover, we need a way to specify the latency of insns that don't
459 ;; use an actual function unit.
460 ;; We use an 'issue' function unit to do that, and a cost factor of 10.
461
462 (define_function_unit "issue" 2 0
463   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "!nil,arith3"))
464   10 10)
465
466 (define_function_unit "issue" 2 0
467   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "arith3"))
468   30 30)
469
470 ;; There is no point in providing exact scheduling information about branches,
471 ;; because they are at the starts / ends of basic blocks anyways.
472
473 ;; Some insns cannot be issued before/after another insn in the same cycle,
474 ;; irrespective of the type of the other insn.
475
476 ;; default is dual-issue, but can't be paired with an insn that
477 ;; uses multiple function units.
478 (define_function_unit "single_issue"     1 0
479   (and (eq_attr "pipe_model" "sh4")
480        (eq_attr "type" "!smpy,dmpy,pload,pstore,dfp_cmp,gp_fpul,call,sfunc,arith3,arith3b"))
481   1 10
482   [(eq_attr "type" "smpy,dmpy,pload,pstore,dfp_cmp,gp_fpul")])
483
484 (define_function_unit "single_issue"     1 0
485   (and (eq_attr "pipe_model" "sh4")
486        (eq_attr "type" "smpy,dmpy,pload,pstore,dfp_cmp,gp_fpul"))
487   10 10
488   [(const_int 1)])
489
490 ;; arith3 insns are always pairable at the start, but not inecessarily at
491 ;; the end; however, there doesn't seem to be a way to express that.
492 (define_function_unit "single_issue"     1 0
493   (and (eq_attr "pipe_model" "sh4")
494        (eq_attr "type" "arith3"))
495   30 20
496   [(const_int 1)])
497
498 ;; arith3b insn are pairable at the end and have latency that prevents pairing
499 ;; with the following branch, but we don't want this latency be respected;
500 ;; When the following branch is immediately adjacent, we can redirect the
501 ;; internal branch, which is likly to be a larger win.
502 (define_function_unit "single_issue"     1 0
503   (and (eq_attr "pipe_model" "sh4")
504        (eq_attr "type" "arith3b"))
505   20 20
506   [(const_int 1)])
507
508 ;; calls introduce a longisch delay that is likely to flush the pipelines.
509 (define_function_unit "single_issue"     1 0
510   (and (eq_attr "pipe_model" "sh4")
511        (eq_attr "type" "call,sfunc"))
512   160 160
513   [(eq_attr "type" "!call") (eq_attr "type" "call")])
514
515 ;; Load and store instructions have no alignment peculiarities for the SH4,
516 ;; but they use the load-store unit, which they share with the fmove type
517 ;; insns (fldi[01]; fmov frn,frm; flds; fsts; fabs; fneg) .
518 ;; Loads have a latency of two.
519 ;; However, call insns can only paired with a preceding insn, and have
520 ;; a delay slot, so that we want two more insns to be scheduled between the
521 ;; load of the function address and the call.  This is equivalent to a
522 ;; latency of three.
523 ;; We cannot use a conflict list for this, because we need to distinguish
524 ;; between the actual call address and the function arguments.
525 ;; ADJUST_COST can only properly handle reductions of the cost, so we
526 ;; use a latency of three here, which gets multiplied by 10 to yield 30.
527 ;; We only do this for SImode loads of general registers, to make the work
528 ;; for ADJUST_COST easier.
529
530 ;; When specifying different latencies for different insns using the
531 ;; the same function unit, genattrtab.c assumes a 'FIFO constraint'
532 ;; so that the blockage is at least READY-COST (E) + 1 - READY-COST (C)
533 ;; for an executing insn E and a candidate insn C.
534 ;; Therefore, we define three different function units for load_store:
535 ;; load_store, load and load_si.
536
537 (define_function_unit "load_si" 1 0
538   (and (eq_attr "pipe_model" "sh4")
539        (eq_attr "type" "load_si,pcload_si")) 30 10)
540 (define_function_unit "load" 1 0
541   (and (eq_attr "pipe_model" "sh4")
542        (eq_attr "type" "load,pcload,pload")) 20 10)
543 (define_function_unit "load_store" 1 0
544   (and (eq_attr "pipe_model" "sh4")
545        (eq_attr "type" "load_si,pcload_si,load,pcload,pload,store,pstore,fmove"))
546   10 10)
547
548 (define_function_unit "int"    1 0
549   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "arith,dyn_shift")) 10 10)
550
551 ;; Again, we have to pretend a lower latency for the "int" unit to avoid a
552 ;; spurious FIFO constraint; the multiply instructions use the "int"
553 ;; unit actually only for two cycles.
554 (define_function_unit "int"    1 0
555   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "smpy,dmpy")) 20 20)
556
557 ;; We use a fictous "mpy" unit to express the actual latency.
558 (define_function_unit "mpy"    1 0
559   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "smpy,dmpy")) 40 20)
560
561 ;; Again, we have to pretend a lower latency for the "int" unit to avoid a
562 ;; spurious FIFO constraint.
563 (define_function_unit "int"     1 0
564   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "gp_fpul")) 10 10)
565
566 ;; We use a fictous "gp_fpul" unit to express the actual latency.
567 (define_function_unit "gp_fpul"     1 0
568   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "gp_fpul")) 20 10)
569
570 ;; ??? multiply uses the floating point unit, but with a two cycle delay.
571 ;; Thus, a simple single-precision fp operation could finish if issued in
572 ;; the very next cycle, but stalls when issued two or three cycles later.
573 ;; Similarily, a divide / sqrt can work without stalls if issued in
574 ;; the very next cycle, while it would have to block if issued two or
575 ;; three cycles later.
576 ;; There is no way to model this with gcc's function units.  This problem is
577 ;; actually mentioned in md.texi.  Tackling this problem requires first that
578 ;; it is possible to speak about the target in an open discussion.
579 ;;
580 ;; However, simple double-precision operations always conflict.
581
582 (define_function_unit "fp"    1 0
583   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "smpy,dmpy")) 40 40
584   [(eq_attr "type" "dfp_cmp,dfp_conv,dfp_arith")])
585
586 ;; The "fp" unit is for pipeline stages F1 and F2.
587
588 (define_function_unit "fp"     1 0
589   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "fp")) 30 10)
590
591 ;; Again, we have to pretend a lower latency for the "fp" unit to avoid a
592 ;; spurious FIFO constraint; the bulk of the fdiv type insns executes in
593 ;; the F3 stage.
594 (define_function_unit "fp"     1 0
595   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "fdiv")) 30 10)
596
597 ;; The "fdiv" function unit models the aggregate effect of the F1, F2 and F3
598 ;; pipeline stages on the pipelining of fdiv/fsqrt insns.
599 ;; We also use it to give the actual latency here.
600 ;; fsqrt is actually one cycle faster than fdiv (and the value used here),
601 ;; but that will hardly matter in practice for scheduling.
602 (define_function_unit "fdiv"     1 0
603   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "fdiv")) 120 100)
604
605 ;; There is again a late use of the "fp" unit by [d]fdiv type insns
606 ;; that we can't express.
607
608 (define_function_unit "fp"     1 0
609   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "dfp_cmp,dfp_conv")) 40 20)
610
611 (define_function_unit "fp"     1 0
612   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "dfp_arith")) 80 60)
613
614 (define_function_unit "fp"     1 0
615   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "dfdiv")) 230 10)
616
617 (define_function_unit "fdiv"     1 0
618   (and (eq_attr "pipe_model" "sh4") (eq_attr "type" "dfdiv")) 230 210)
619
620 ;; SH-5 SHmedia scheduling
621 ;; When executing SHmedia code, the SH-5 is a fairly straightforward
622 ;; single-issue machine.  It has four pipelines, the branch unit (br),
623 ;; the integer and multimedia unit (imu), the load/store unit (lsu), and
624 ;; the floating point unit (fpu).
625 ;; Here model the instructions with a latency greater than one cycle.
626
627 ;; Every instruction on SH-5 occupies the issue resource for at least one
628 ;; cycle.
629 (define_function_unit "sh5issue" 1 0
630   (and (eq_attr "pipe_model" "sh5media")
631        (eq_attr "type" "!pt_media,ptabs_media,invalidate_line_media,dmpy_media,load_media,fload_media,fcmp_media,fmove_media,fparith_media,dfparith_media,fpconv_media,dfpconv_media,dfmul_media,store_media,fstore_media,mcmp_media,mac_media,d2mpy_media,atrans_media,ustore_media")) 1 1)
632
633 ;; Specify the various types of instruction which have latency > 1
634 (define_function_unit "sh5issue" 1 0
635   (and (eq_attr "pipe_model" "sh5media")
636        (eq_attr "type" "mcmp_media")) 2 1)
637
638 (define_function_unit "sh5issue" 1 0
639   (and (eq_attr "pipe_model" "sh5media")
640        (eq_attr "type" "dmpy_media,load_media,fcmp_media,mac_media")) 3 1)
641 ;; but see sh_adjust_cost for mac_media exception.
642
643 (define_function_unit "sh5issue" 1 0
644   (and (eq_attr "pipe_model" "sh5media")
645        (eq_attr "type" "fload_media,fmove_media")) 4 1)
646
647 (define_function_unit "sh5issue" 1 0
648   (and (eq_attr "pipe_model" "sh5media")
649        (eq_attr "type" "d2mpy_media")) 4 2)
650
651 (define_function_unit "sh5issue" 1 0
652   (and (eq_attr "pipe_model" "sh5media")
653        (eq_attr "type" "pt_media,ptabs_media")) 5 1)
654
655 (define_function_unit "sh5issue" 1 0
656   (and (eq_attr "pipe_model" "sh5media")
657        (eq_attr "type" "fparith_media,dfparith_media,fpconv_media,dfpconv_media")) 6 1)
658
659 (define_function_unit "sh5issue" 1 0
660   (and (eq_attr "pipe_model" "sh5media")
661        (eq_attr "type" "invalidate_line_media")) 7 7)
662
663 (define_function_unit "sh5issue" 1 0
664   (and (eq_attr "pipe_model" "sh5media") (eq_attr "type" "dfmul_media")) 9 4)
665
666 (define_function_unit "sh5issue" 1 0
667   (and (eq_attr "pipe_model" "sh5media") (eq_attr "type" "atrans_media")) 10 5)
668
669 ;; Floating-point divide and square-root occupy an additional resource,
670 ;; which is not internally pipelined.  However, other instructions
671 ;; can continue to issue.
672 (define_function_unit "sh5fds" 1 0
673   (and (eq_attr "pipe_model" "sh5media") (eq_attr "type" "fdiv_media"))  19 19)
674
675 (define_function_unit "sh5fds" 1 0
676   (and (eq_attr "pipe_model" "sh5media") (eq_attr "type" "dfdiv_media")) 35 35)
677
678 ; Definitions for filling branch delay slots.
679
680 (define_attr "needs_delay_slot" "yes,no" (const_string "no"))
681
682 ;; ??? This should be (nil) instead of (const_int 0)
683 (define_attr "hit_stack" "yes,no"
684         (cond [(eq (symbol_ref "find_regno_note (insn, REG_INC, SP_REG)")
685                    (const_int 0))
686                (const_string "no")]
687               (const_string "yes")))
688
689 (define_attr "interrupt_function" "no,yes"
690   (const (symbol_ref "current_function_interrupt")))
691
692 (define_attr "in_delay_slot" "yes,no"
693   (cond [(eq_attr "type" "cbranch") (const_string "no")
694          (eq_attr "type" "pcload,pcload_si") (const_string "no")
695          (eq_attr "needs_delay_slot" "yes") (const_string "no")
696          (eq_attr "length" "2") (const_string "yes")
697          ] (const_string "no")))
698
699 (define_attr "cond_delay_slot" "yes,no"
700   (cond [(eq_attr "in_delay_slot" "yes") (const_string "yes")
701          ] (const_string "no")))
702
703 (define_attr "is_sfunc" ""
704   (if_then_else (eq_attr "type" "sfunc") (const_int 1) (const_int 0)))
705
706 (define_attr "is_mac_media" ""
707   (if_then_else (eq_attr "type" "mac_media") (const_int 1) (const_int 0)))
708
709 (define_delay
710   (eq_attr "needs_delay_slot" "yes")
711   [(eq_attr "in_delay_slot" "yes") (nil) (nil)])
712
713 ;; On the SH and SH2, the rte instruction reads the return pc from the stack,
714 ;; and thus we can't put a pop instruction in its delay slot.
715 ;; ??? On the SH3, the rte instruction does not use the stack, so a pop
716 ;; instruction can go in the delay slot.
717
718 ;; Since a normal return (rts) implicitly uses the PR register,
719 ;; we can't allow PR register loads in an rts delay slot.
720
721 (define_delay
722   (eq_attr "type" "return")
723   [(and (eq_attr "in_delay_slot" "yes")
724         (ior (and (eq_attr "interrupt_function" "no")
725                   (eq_attr "type" "!pload,prset"))
726              (and (eq_attr "interrupt_function" "yes")
727                   (ior
728                    (ne (symbol_ref "TARGET_SH3") (const_int 0))
729                    (eq_attr "hit_stack" "no"))))) (nil) (nil)])
730
731 ;; Since a call implicitly uses the PR register, we can't allow
732 ;; a PR register store in a jsr delay slot.
733
734 (define_delay
735   (ior (eq_attr "type" "call") (eq_attr "type" "sfunc"))
736   [(and (eq_attr "in_delay_slot" "yes")
737         (eq_attr "type" "!pstore,prget")) (nil) (nil)])
738
739 ;; Say that we have annulled true branches, since this gives smaller and
740 ;; faster code when branches are predicted as not taken.
741
742 (define_delay
743   (and (eq_attr "type" "cbranch")
744        (ne (symbol_ref "TARGET_SH2") (const_int 0)))
745   [(eq_attr "in_delay_slot" "yes") (eq_attr "cond_delay_slot" "yes") (nil)])
746 \f
747 ;; -------------------------------------------------------------------------
748 ;; SImode signed integer comparisons
749 ;; -------------------------------------------------------------------------
750
751 (define_insn ""
752   [(set (reg:SI T_REG)
753         (eq:SI (and:SI (match_operand:SI 0 "arith_reg_operand" "z,r")
754                        (match_operand:SI 1 "arith_operand" "L,r"))
755                (const_int 0)))]
756   "TARGET_SH1"
757   "tst  %1,%0"
758   [(set_attr "insn_class" "mt_group")])
759
760 ;; ??? Perhaps should only accept reg/constant if the register is reg 0.
761 ;; That would still allow reload to create cmpi instructions, but would
762 ;; perhaps allow forcing the constant into a register when that is better.
763 ;; Probably should use r0 for mem/imm compares, but force constant into a
764 ;; register for pseudo/imm compares.
765
766 (define_insn "cmpeqsi_t"
767   [(set (reg:SI T_REG)
768         (eq:SI (match_operand:SI 0 "arith_reg_operand" "r,z,r")
769                (match_operand:SI 1 "arith_operand" "N,rI,r")))]
770   "TARGET_SH1"
771   "@
772         tst     %0,%0
773         cmp/eq  %1,%0
774         cmp/eq  %1,%0"
775    [(set_attr "insn_class" "mt_group,mt_group,mt_group")])
776
777 (define_insn "cmpgtsi_t"
778   [(set (reg:SI T_REG)
779         (gt:SI (match_operand:SI 0 "arith_reg_operand" "r,r")
780                (match_operand:SI 1 "arith_reg_or_0_operand" "r,N")))]
781   "TARGET_SH1"
782   "@
783         cmp/gt  %1,%0
784         cmp/pl  %0"
785    [(set_attr "insn_class" "mt_group,mt_group")])
786
787 (define_insn "cmpgesi_t"
788   [(set (reg:SI T_REG)
789         (ge:SI (match_operand:SI 0 "arith_reg_operand" "r,r")
790                (match_operand:SI 1 "arith_reg_or_0_operand" "r,N")))]
791   "TARGET_SH1"
792   "@
793         cmp/ge  %1,%0
794         cmp/pz  %0"
795    [(set_attr "insn_class" "mt_group,mt_group")])
796
797 ;; -------------------------------------------------------------------------
798 ;; SImode unsigned integer comparisons
799 ;; -------------------------------------------------------------------------
800
801 (define_insn "cmpgeusi_t"
802   [(set (reg:SI T_REG)
803         (geu:SI (match_operand:SI 0 "arith_reg_operand" "r")
804                 (match_operand:SI 1 "arith_reg_operand" "r")))]
805   "TARGET_SH1"
806   "cmp/hs       %1,%0"
807    [(set_attr "insn_class" "mt_group")])
808
809 (define_insn "cmpgtusi_t"
810   [(set (reg:SI T_REG)
811         (gtu:SI (match_operand:SI 0 "arith_reg_operand" "r")
812                 (match_operand:SI 1 "arith_reg_operand" "r")))]
813   "TARGET_SH1"
814   "cmp/hi       %1,%0"
815    [(set_attr "insn_class" "mt_group")])
816
817 ;; We save the compare operands in the cmpxx patterns and use them when
818 ;; we generate the branch.
819
820 (define_expand "cmpsi"
821   [(set (reg:SI T_REG)
822         (compare (match_operand:SI 0 "arith_operand" "")
823                  (match_operand:SI 1 "arith_operand" "")))]
824   "TARGET_SH1"
825   "
826 {
827   sh_compare_op0 = operands[0];
828   sh_compare_op1 = operands[1];
829   DONE;
830 }")
831 \f
832 ;; -------------------------------------------------------------------------
833 ;; DImode signed integer comparisons
834 ;; -------------------------------------------------------------------------
835
836 ;; ??? Could get better scheduling by splitting the initial test from the
837 ;; rest of the insn after reload.  However, the gain would hardly justify
838 ;; the sh.md size increase necessary to do that.
839
840 (define_insn ""
841   [(set (reg:SI T_REG)
842         (eq:SI (and:DI (match_operand:DI 0 "arith_reg_operand" "r")
843                        (match_operand:DI 1 "arith_operand" "r"))
844                (const_int 0)))]
845   "TARGET_SH1"
846   "* return output_branchy_insn (EQ, \"tst\\t%S1,%S0\;bf\\t%l9\;tst\\t%R1,%R0\",
847                                  insn, operands);"
848   [(set_attr "length" "6")
849    (set_attr "type" "arith3b")])
850
851 (define_insn "cmpeqdi_t"
852   [(set (reg:SI T_REG)
853         (eq:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
854                (match_operand:DI 1 "arith_reg_or_0_operand" "N,r")))]
855   "TARGET_SH1"
856   "@
857         tst     %S0,%S0\;bf     %,Ldi%=\;tst    %R0,%R0\\n%,Ldi%=:
858         cmp/eq  %S1,%S0\;bf     %,Ldi%=\;cmp/eq %R1,%R0\\n%,Ldi%=:"
859   [(set_attr "length" "6")
860    (set_attr "type" "arith3b")])
861
862 (define_split
863   [(set (reg:SI T_REG)
864         (eq:SI (match_operand:DI 0 "arith_reg_operand" "")
865                (match_operand:DI 1 "arith_reg_or_0_operand" "")))]
866 ;; If we applied this split when not optimizing, it would only be
867 ;; applied during the machine-dependent reorg, when no new basic blocks
868 ;; may be created.
869   "TARGET_SH1 && reload_completed && optimize"
870   [(set (reg:SI T_REG) (eq:SI (match_dup 2) (match_dup 3)))
871    (set (pc) (if_then_else (eq (reg:SI T_REG) (const_int 0))
872                            (label_ref (match_dup 6))
873                            (pc)))
874    (set (reg:SI T_REG) (eq:SI (match_dup 4) (match_dup 5)))
875    (match_dup 6)]
876   "
877 {
878   operands[2]
879     = gen_rtx_REG (SImode,
880                    true_regnum (operands[0]) + (TARGET_LITTLE_ENDIAN ? 1 : 0));
881   operands[3]
882     = (operands[1] == const0_rtx
883        ? const0_rtx
884        : gen_rtx_REG (SImode,
885                       true_regnum (operands[1])
886                       + (TARGET_LITTLE_ENDIAN ? 1 : 0)));
887   operands[4] = gen_lowpart (SImode, operands[0]);
888   operands[5] = gen_lowpart (SImode, operands[1]);
889   operands[6] = gen_label_rtx ();
890 }")
891
892 (define_insn "cmpgtdi_t"
893   [(set (reg:SI T_REG)
894         (gt:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
895                (match_operand:DI 1 "arith_reg_or_0_operand" "r,N")))]
896   "TARGET_SH2"
897   "@
898         cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/gt\\t%S1,%S0\;cmp/hi\\t%R1,%R0\\n%,Ldi%=:
899         tst\\t%S0,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/pl\\t%S0\;cmp/hi\\t%S0,%R0\\n%,Ldi%=:"
900   [(set_attr "length" "8")
901    (set_attr "type" "arith3")])
902
903 (define_insn "cmpgedi_t"
904   [(set (reg:SI T_REG)
905         (ge:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
906                (match_operand:DI 1 "arith_reg_or_0_operand" "r,N")))]
907   "TARGET_SH2"
908   "@
909         cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/ge\\t%S1,%S0\;cmp/hs\\t%R1,%R0\\n%,Ldi%=:
910         cmp/pz\\t%S0"
911   [(set_attr "length" "8,2")
912    (set_attr "type" "arith3,arith")])
913 \f
914 ;; -------------------------------------------------------------------------
915 ;; DImode unsigned integer comparisons
916 ;; -------------------------------------------------------------------------
917
918 (define_insn "cmpgeudi_t"
919   [(set (reg:SI T_REG)
920         (geu:SI (match_operand:DI 0 "arith_reg_operand" "r")
921                 (match_operand:DI 1 "arith_reg_operand" "r")))]
922   "TARGET_SH2"
923   "cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/hs\\t%S1,%S0\;cmp/hs\\t%R1,%R0\\n%,Ldi%=:"
924   [(set_attr "length" "8")
925    (set_attr "type" "arith3")])
926
927 (define_insn "cmpgtudi_t"
928   [(set (reg:SI T_REG)
929         (gtu:SI (match_operand:DI 0 "arith_reg_operand" "r")
930                 (match_operand:DI 1 "arith_reg_operand" "r")))]
931   "TARGET_SH2"
932   "cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/hi\\t%S1,%S0\;cmp/hi\\t%R1,%R0\\n%,Ldi%=:"
933   [(set_attr "length" "8")
934    (set_attr "type" "arith3")])
935
936 (define_insn "cmpeqdi_media"
937   [(set (match_operand:DI 0 "register_operand" "=r")
938         (eq:DI (match_operand:DI 1 "register_operand" "%r")
939                (match_operand:DI 2 "arith_reg_or_0_operand" "Nr")))]
940   "TARGET_SHMEDIA"
941   "cmpeq        %1, %N2, %0"
942   [(set_attr "type" "cmp_media")])
943
944 (define_insn "cmpgtdi_media"
945   [(set (match_operand:DI 0 "register_operand" "=r")
946         (gt:DI (match_operand:DI 1 "arith_reg_or_0_operand" "Nr")
947                (match_operand:DI 2 "arith_reg_or_0_operand" "rN")))]
948   "TARGET_SHMEDIA"
949   "cmpgt        %N1, %N2, %0"
950   [(set_attr "type" "cmp_media")])
951
952 (define_insn "cmpgtudi_media"
953   [(set (match_operand:DI 0 "register_operand" "=r")
954         (gtu:DI (match_operand:DI 1 "arith_reg_or_0_operand" "Nr")
955                 (match_operand:DI 2 "arith_reg_or_0_operand" "rN")))]
956   "TARGET_SHMEDIA"
957   "cmpgtu       %N1, %N2, %0"
958   [(set_attr "type" "cmp_media")])
959
960 ;; We save the compare operands in the cmpxx patterns and use them when
961 ;; we generate the branch.
962
963 (define_expand "cmpdi"
964   [(set (reg:SI T_REG)
965         (compare (match_operand:DI 0 "arith_operand" "")
966                  (match_operand:DI 1 "arith_operand" "")))]
967   "TARGET_SH2 || TARGET_SHMEDIA"
968   "
969 {
970   sh_compare_op0 = operands[0];
971   sh_compare_op1 = operands[1];
972   DONE;
973 }")
974 ;; -------------------------------------------------------------------------
975 ;; Conditional move instructions
976 ;; -------------------------------------------------------------------------
977
978 ;; The insn names may seem reversed, but note that cmveq performs the move
979 ;; if op1 == 0, and cmvne does it if op1 != 0.
980
981 (define_insn "movdicc_false"
982   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
983         (if_then_else:DI (eq (match_operand:DI 1 "arith_reg_operand" "r")
984                              (const_int 0))
985          (match_operand:DI 2 "arith_reg_or_0_operand" "rN")
986          (match_operand:DI 3 "arith_reg_operand" "0")))]
987   "TARGET_SHMEDIA"
988   "cmveq        %1, %N2, %0"
989   [(set_attr "type" "arith_media")])
990
991 (define_insn "movdicc_true"
992   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
993         (if_then_else:DI (ne (match_operand:DI 1 "arith_reg_operand" "r")
994                              (const_int 0))
995          (match_operand:DI 2 "arith_reg_or_0_operand" "rN")
996          (match_operand:DI 3 "arith_reg_operand" "0")))]
997   "TARGET_SHMEDIA"
998   "cmvne        %1, %N2, %0"
999   [(set_attr "type" "arith_media")])
1000
1001 (define_expand "movdicc"
1002   [(set (match_operand:DI 0 "register_operand" "")
1003         (if_then_else:DI (match_operand 1 "comparison_operator" "")
1004                          (match_operand:DI 2 "register_operand" "")
1005                          (match_operand:DI 3 "register_operand" "")))]
1006   "TARGET_SHMEDIA"
1007   "
1008 {
1009   if ((GET_CODE (operands[1]) == EQ || GET_CODE (operands[1]) == NE)
1010       && GET_MODE (sh_compare_op0) == DImode
1011       && sh_compare_op1 == const0_rtx)
1012     operands[1] = gen_rtx (GET_CODE (operands[1]), VOIDmode,
1013                            sh_compare_op0, sh_compare_op1);
1014   else
1015     {
1016       rtx tmp;
1017
1018       if (no_new_pseudos)
1019         FAIL;
1020
1021       tmp = gen_reg_rtx (DImode);
1022
1023       switch (GET_CODE (operands[1]))
1024         {
1025         case EQ:
1026           emit_insn (gen_seq (tmp));
1027           operands[1] = gen_rtx (NE, VOIDmode, tmp, const0_rtx);
1028           break;
1029
1030         case NE:
1031           emit_insn (gen_seq (tmp));
1032           operands[1] = gen_rtx (EQ, VOIDmode, tmp, const0_rtx);
1033           break;
1034
1035         case GT:
1036           emit_insn (gen_sgt (tmp));
1037           operands[1] = gen_rtx (NE, VOIDmode, tmp, const0_rtx);
1038           break;
1039
1040         case LT:
1041           emit_insn (gen_slt (tmp));
1042           operands[1] = gen_rtx (NE, VOIDmode, tmp, const0_rtx);
1043           break;
1044
1045         case GE:
1046           emit_insn (gen_slt (tmp));
1047           operands[1] = gen_rtx (EQ, VOIDmode, tmp, const0_rtx);
1048           break;
1049
1050         case LE:
1051           emit_insn (gen_sgt (tmp));
1052           operands[1] = gen_rtx (EQ, VOIDmode, tmp, const0_rtx);
1053           break;
1054
1055         case GTU:
1056           emit_insn (gen_sgtu (tmp));
1057           operands[1] = gen_rtx (NE, VOIDmode, tmp, const0_rtx);
1058           break;
1059
1060         case LTU:
1061           emit_insn (gen_sltu (tmp));
1062           operands[1] = gen_rtx (NE, VOIDmode, tmp, const0_rtx);
1063           break;
1064
1065         case GEU:
1066           emit_insn (gen_sltu (tmp));
1067           operands[1] = gen_rtx (EQ, VOIDmode, tmp, const0_rtx);
1068           break;
1069
1070         case LEU:
1071           emit_insn (gen_sgtu (tmp));
1072           operands[1] = gen_rtx (EQ, VOIDmode, tmp, const0_rtx);
1073           break;
1074
1075         case UNORDERED:
1076           emit_insn (gen_sunordered (tmp));
1077           operands[1] = gen_rtx (NE, VOIDmode, tmp, const0_rtx);
1078           break;
1079
1080         case ORDERED:
1081           emit_insn (gen_sunordered (tmp));
1082           operands[1] = gen_rtx (EQ, VOIDmode, tmp, const0_rtx);
1083           break;
1084
1085         case UNEQ:
1086         case UNGE:
1087         case UNGT:
1088         case UNLE:
1089         case UNLT:
1090         case LTGT:
1091           FAIL;
1092
1093         default:
1094           abort ();
1095         }
1096     }
1097 }")
1098 \f
1099 ;; -------------------------------------------------------------------------
1100 ;; Addition instructions
1101 ;; -------------------------------------------------------------------------
1102
1103 (define_expand "adddi3"
1104   [(set (match_operand:DI 0 "arith_reg_operand" "")
1105         (plus:DI (match_operand:DI 1 "arith_reg_operand" "")
1106                  (match_operand:DI 2 "arith_operand" "")))]
1107   ""
1108   "
1109 {
1110   if (TARGET_SH1)
1111     {
1112       if (no_new_pseudos && ! arith_reg_operand (operands[2], DImode))
1113         FAIL;
1114       operands[2] = force_reg (DImode, operands[2]);
1115       emit_insn (gen_adddi3_compact (operands[0], operands[1], operands[2]));
1116       DONE;
1117     }
1118 }")
1119
1120 (define_insn "*adddi3_media"
1121   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
1122         (plus:DI (match_operand:DI 1 "arith_reg_operand" "%r,r")
1123                  (match_operand:DI 2 "arith_operand" "r,P")))]
1124   "TARGET_SHMEDIA"
1125   "@
1126         add     %1, %2, %0
1127         addi    %1, %2, %0"
1128   [(set_attr "type" "arith_media")])
1129
1130 (define_insn "adddi3z_media"
1131   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1132         (zero_extend:DI
1133          (plus:SI (match_operand:SI 1 "extend_reg_operand" "r")
1134                   (match_operand:SI 2 "extend_reg_or_0_operand" "rN"))))]
1135   "TARGET_SHMEDIA"
1136   "addz.l       %1, %N2, %0"
1137   [(set_attr "type" "arith_media")])
1138
1139 (define_insn "adddi3_compact"
1140   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1141         (plus:DI (match_operand:DI 1 "arith_reg_operand" "%0")
1142                  (match_operand:DI 2 "arith_reg_operand" "r")))
1143    (clobber (reg:SI T_REG))]
1144   "TARGET_SH1"
1145   "#"
1146   [(set_attr "length" "6")])
1147
1148 (define_split
1149   [(set (match_operand:DI 0 "arith_reg_operand" "")
1150         (plus:DI (match_operand:DI 1 "arith_reg_operand" "")
1151                  (match_operand:DI 2 "arith_reg_operand" "")))
1152    (clobber (reg:SI T_REG))]
1153   "TARGET_SH1 && reload_completed"
1154   [(const_int 0)]
1155   "
1156 {
1157   rtx high0, high2, low0 = gen_lowpart (SImode, operands[0]);
1158   high0 = gen_rtx_REG (SImode,
1159                        true_regnum (operands[0])
1160                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
1161   high2 = gen_rtx_REG (SImode,
1162                        true_regnum (operands[2])
1163                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
1164   emit_insn (gen_clrt ());
1165   emit_insn (gen_addc (low0, low0, gen_lowpart (SImode, operands[2])));
1166   emit_insn (gen_addc1 (high0, high0, high2));
1167   DONE;
1168 }")
1169
1170 (define_insn "addc"
1171   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1172         (plus:SI (plus:SI (match_operand:SI 1 "arith_reg_operand" "0")
1173                           (match_operand:SI 2 "arith_reg_operand" "r"))
1174                  (reg:SI T_REG)))
1175    (set (reg:SI T_REG)
1176         (ltu:SI (plus:SI (match_dup 1) (match_dup 2)) (match_dup 1)))]
1177   "TARGET_SH1"
1178   "addc %2,%0"
1179   [(set_attr "type" "arith")
1180    (set_attr "insn_class" "ex_group")])
1181
1182 (define_insn "addc1"
1183   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1184         (plus:SI (plus:SI (match_operand:SI 1 "arith_reg_operand" "0")
1185                           (match_operand:SI 2 "arith_reg_operand" "r"))
1186                  (reg:SI T_REG)))
1187    (clobber (reg:SI T_REG))]
1188   "TARGET_SH1"
1189   "addc %2,%0"
1190   [(set_attr "type" "arith")
1191    (set_attr "insn_class" "ex_group")])
1192
1193 (define_expand "addsi3"
1194   [(set (match_operand:SI 0 "arith_reg_operand" "")
1195         (plus:SI (match_operand:SI 1 "arith_operand" "")
1196                  (match_operand:SI 2 "arith_operand" "")))]
1197   ""
1198   "
1199 {
1200   if (TARGET_SHMEDIA)
1201     operands[1] = force_reg (SImode, operands[1]);
1202 }")
1203
1204 (define_insn "addsi3_media"
1205   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
1206         (plus:SI (match_operand:SI 1 "extend_reg_operand" "%r,r")
1207                  (match_operand:SI 2 "arith_operand" "r,P")))]
1208   "TARGET_SHMEDIA"
1209   "@
1210         add.l   %1, %2, %0
1211         addi.l  %1, %2, %0"
1212   [(set_attr "type" "arith_media")])
1213
1214 (define_insn "*addsi3_compact"
1215   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1216         (plus:SI (match_operand:SI 1 "arith_operand" "%0")
1217                  (match_operand:SI 2 "arith_operand" "rI")))]
1218   "TARGET_SH1"
1219   "add  %2,%0"
1220   [(set_attr "type" "arith")
1221    (set_attr "insn_class" "ex_group")])
1222
1223 ;; -------------------------------------------------------------------------
1224 ;; Subtraction instructions
1225 ;; -------------------------------------------------------------------------
1226
1227 (define_expand "subdi3"
1228   [(set (match_operand:DI 0 "arith_reg_operand" "")
1229         (minus:DI (match_operand:DI 1 "arith_reg_or_0_operand" "")
1230                   (match_operand:DI 2 "arith_reg_operand" "")))]
1231   ""
1232   "
1233 {
1234   if (TARGET_SH1)
1235     {
1236       operands[1] = force_reg (DImode, operands[1]);
1237       emit_insn (gen_subdi3_compact (operands[0], operands[1], operands[2]));
1238       DONE;
1239     }
1240 }")
1241
1242 (define_insn "*subdi3_media"
1243   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1244         (minus:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rN")
1245                   (match_operand:DI 2 "arith_reg_operand" "r")))]
1246   "TARGET_SHMEDIA"
1247   "sub  %N1, %2, %0"
1248   [(set_attr "type" "arith_media")])
1249
1250 (define_insn "subdi3_compact"
1251   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1252         (minus:DI (match_operand:DI 1 "arith_reg_operand" "0")
1253                  (match_operand:DI 2 "arith_reg_operand" "r")))
1254    (clobber (reg:SI T_REG))]
1255   "TARGET_SH1"
1256   "#"
1257   [(set_attr "length" "6")])
1258
1259 (define_split
1260   [(set (match_operand:DI 0 "arith_reg_operand" "")
1261         (minus:DI (match_operand:DI 1 "arith_reg_operand" "")
1262                   (match_operand:DI 2 "arith_reg_operand" "")))
1263    (clobber (reg:SI T_REG))]
1264   "TARGET_SH1 && reload_completed"
1265   [(const_int 0)]
1266   "
1267 {
1268   rtx high0, high2, low0 = gen_lowpart (SImode, operands[0]);
1269   high0 = gen_rtx_REG (SImode,
1270                        true_regnum (operands[0])
1271                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
1272   high2 = gen_rtx_REG (SImode,
1273                        true_regnum (operands[2])
1274                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
1275   emit_insn (gen_clrt ());
1276   emit_insn (gen_subc (low0, low0, gen_lowpart (SImode, operands[2])));
1277   emit_insn (gen_subc1 (high0, high0, high2));
1278   DONE;
1279 }")
1280
1281 (define_insn "subc"
1282   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1283         (minus:SI (minus:SI (match_operand:SI 1 "arith_reg_operand" "0")
1284                             (match_operand:SI 2 "arith_reg_operand" "r"))
1285                   (reg:SI T_REG)))
1286    (set (reg:SI T_REG)
1287         (gtu:SI (minus:SI (match_dup 1) (match_dup 2)) (match_dup 1)))]
1288   "TARGET_SH1"
1289   "subc %2,%0"
1290   [(set_attr "type" "arith")
1291    (set_attr "insn_class" "ex_group")])
1292
1293 (define_insn "subc1"
1294   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1295         (minus:SI (minus:SI (match_operand:SI 1 "arith_reg_operand" "0")
1296                             (match_operand:SI 2 "arith_reg_operand" "r"))
1297                   (reg:SI T_REG)))
1298    (clobber (reg:SI T_REG))]
1299   "TARGET_SH1"
1300   "subc %2,%0"
1301   [(set_attr "type" "arith")
1302    (set_attr "insn_class" "ex_group")])
1303
1304 (define_insn "*subsi3_internal"
1305   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1306         (minus:SI (match_operand:SI 1 "arith_reg_operand" "0")
1307                   (match_operand:SI 2 "arith_reg_operand" "r")))]
1308   "TARGET_SH1"
1309   "sub  %2,%0"
1310   [(set_attr "type" "arith")
1311    (set_attr "insn_class" "ex_group")])
1312
1313 (define_insn "*subsi3_media"
1314   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1315         (minus:SI (match_operand:SI 1 "extend_reg_or_0_operand" "rN")
1316                   (match_operand:SI 2 "extend_reg_operand" "r")))]
1317   "TARGET_SHMEDIA"
1318   "sub.l        %N1, %2, %0"
1319   [(set_attr "type" "arith_media")])
1320
1321 ;; Convert `constant - reg' to `neg rX; add rX, #const' since this
1322 ;; will sometimes save one instruction.  Otherwise we might get
1323 ;; `mov #const, rY; sub rY,rX; mov rX, rY' if the source and dest regs
1324 ;; are the same.
1325
1326 (define_expand "subsi3"
1327   [(set (match_operand:SI 0 "arith_reg_operand" "")
1328         (minus:SI (match_operand:SI 1 "arith_operand" "")
1329                   (match_operand:SI 2 "arith_reg_operand" "")))]
1330   ""
1331   "
1332 {
1333   if (TARGET_SH1 && GET_CODE (operands[1]) == CONST_INT)
1334     {
1335       emit_insn (gen_negsi2 (operands[0], operands[2]));
1336       emit_insn (gen_addsi3 (operands[0], operands[0], operands[1]));
1337       DONE;
1338     }
1339   if (TARGET_SHMEDIA)
1340     {
1341       if (no_new_pseudos && ! arith_reg_or_0_operand (operands[1], SImode))
1342         FAIL;
1343       if (operands[1] != const0_rtx)
1344         operands[1] = force_reg (SImode, operands[1]);
1345     }
1346 }")
1347 \f
1348 ;; -------------------------------------------------------------------------
1349 ;; Division instructions
1350 ;; -------------------------------------------------------------------------
1351
1352 ;; We take advantage of the library routines which don't clobber as many
1353 ;; registers as a normal function call would.
1354
1355 ;; The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
1356 ;; also has an effect on the register that holds the address of the sfunc.
1357 ;; To make this work, we have an extra dummy insn that shows the use
1358 ;; of this register for reorg.
1359
1360 (define_insn "use_sfunc_addr"
1361   [(set (reg:SI PR_REG)
1362         (unspec:SI [(match_operand:SI 0 "register_operand" "r")] UNSPEC_SFUNC))]
1363   "TARGET_SH1"
1364   ""
1365   [(set_attr "length" "0")])
1366
1367 ;; We must use a pseudo-reg forced to reg 0 in the SET_DEST rather than
1368 ;; hard register 0.  If we used hard register 0, then the next instruction
1369 ;; would be a move from hard register 0 to a pseudo-reg.  If the pseudo-reg
1370 ;; gets allocated to a stack slot that needs its address reloaded, then
1371 ;; there is nothing to prevent reload from using r0 to reload the address.
1372 ;; This reload would clobber the value in r0 we are trying to store.
1373 ;; If we let reload allocate r0, then this problem can never happen.
1374
1375 (define_insn "udivsi3_i1"
1376   [(set (match_operand:SI 0 "register_operand" "=z")
1377         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1378    (clobber (reg:SI T_REG))
1379    (clobber (reg:SI PR_REG))
1380    (clobber (reg:SI R4_REG))
1381    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1382   "TARGET_SH1 && ! TARGET_SH4"
1383   "jsr  @%1%#"
1384   [(set_attr "type" "sfunc")
1385    (set_attr "needs_delay_slot" "yes")])
1386
1387 ; Since shmedia-nofpu code could be linked against shcompact code, and
1388 ; the udivsi3 libcall has the same name, we must consider all registers
1389 ; clobbered that are in the union of the registers clobbered by the
1390 ; shmedia and the shcompact implementation.  Note, if the shcompact
1391 ; implemenation actually used shcompact code, we'd need to clobber
1392 ; also r23 and fr23.
1393 (define_insn "udivsi3_i1_media"
1394   [(set (match_operand:SI 0 "register_operand" "=z")
1395         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1396    (clobber (reg:SI T_MEDIA_REG))
1397    (clobber (reg:SI PR_MEDIA_REG))
1398    (clobber (reg:SI R20_REG))
1399    (clobber (reg:SI R21_REG))
1400    (clobber (reg:SI R22_REG))
1401    (clobber (reg:DI TR0_REG))
1402    (clobber (reg:DI TR1_REG))
1403    (clobber (reg:DI TR2_REG))
1404    (use (match_operand:DI 1 "target_operand" "b"))]
1405   "TARGET_SHMEDIA && ! TARGET_SHMEDIA_FPU"
1406   "blink        %1, r18"
1407   [(set_attr "type" "sfunc")
1408    (set_attr "needs_delay_slot" "yes")])
1409
1410 (define_expand "udivsi3_i4_media"
1411   [(set (match_dup 3)
1412         (zero_extend:DI (match_operand:SI 1 "register_operand" "")))
1413    (set (match_dup 4)
1414         (zero_extend:DI (match_operand:SI 2 "register_operand" "")))
1415    (set (match_dup 5) (float:DF (match_dup 3)))
1416    (set (match_dup 6) (float:DF (match_dup 4)))
1417    (set (match_dup 7) (div:DF (match_dup 5) (match_dup 6)))
1418    (set (match_dup 8) (fix:DI (match_dup 7)))
1419    (set (match_operand:SI 0 "register_operand" "")
1420         (truncate:SI (match_dup 8)))]
1421   "TARGET_SHMEDIA_FPU"
1422   "
1423 {
1424   operands[3] = gen_reg_rtx (DImode);
1425   operands[4] = gen_reg_rtx (DImode);
1426   operands[5] = gen_reg_rtx (DFmode);
1427   operands[6] = gen_reg_rtx (DFmode);
1428   operands[7] = gen_reg_rtx (DFmode);
1429   operands[8] = gen_reg_rtx (DImode);
1430 }")
1431
1432 (define_insn "udivsi3_i4"
1433   [(set (match_operand:SI 0 "register_operand" "=y")
1434         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1435    (clobber (reg:SI T_REG))
1436    (clobber (reg:SI PR_REG))
1437    (clobber (reg:DF DR0_REG))
1438    (clobber (reg:DF DR2_REG))
1439    (clobber (reg:DF DR4_REG))
1440    (clobber (reg:SI R0_REG))
1441    (clobber (reg:SI R1_REG))
1442    (clobber (reg:SI R4_REG))
1443    (clobber (reg:SI R5_REG))
1444    (use (reg:PSI FPSCR_REG))
1445    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1446   "TARGET_SH4 && ! TARGET_FPU_SINGLE"
1447   "jsr  @%1%#"
1448   [(set_attr "type" "sfunc")
1449    (set_attr "fp_mode" "double")
1450    (set_attr "needs_delay_slot" "yes")])
1451
1452 (define_insn "udivsi3_i4_single"
1453   [(set (match_operand:SI 0 "register_operand" "=y")
1454         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1455    (clobber (reg:SI T_REG))
1456    (clobber (reg:SI PR_REG))
1457    (clobber (reg:DF DR0_REG))
1458    (clobber (reg:DF DR2_REG))
1459    (clobber (reg:DF DR4_REG))
1460    (clobber (reg:SI R0_REG))
1461    (clobber (reg:SI R1_REG))
1462    (clobber (reg:SI R4_REG))
1463    (clobber (reg:SI R5_REG))
1464    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1465   "(TARGET_HARD_SH4 || TARGET_SHCOMPACT) && TARGET_FPU_SINGLE"
1466   "jsr  @%1%#"
1467   [(set_attr "type" "sfunc")
1468    (set_attr "needs_delay_slot" "yes")])
1469
1470 (define_expand "udivsi3"
1471   [(set (match_dup 3) (symbol_ref:SI "__udivsi3"))
1472    (set (reg:SI R4_REG) (match_operand:SI 1 "general_operand" ""))
1473    (set (reg:SI R5_REG) (match_operand:SI 2 "general_operand" ""))
1474    (parallel [(set (match_operand:SI 0 "register_operand" "")
1475                    (udiv:SI (reg:SI R4_REG)
1476                             (reg:SI R5_REG)))
1477               (clobber (reg:SI T_REG))
1478               (clobber (reg:SI PR_REG))
1479               (clobber (reg:SI R4_REG))
1480               (use (match_dup 3))])]
1481   ""
1482   "
1483 {
1484   rtx first = 0, last;
1485
1486   operands[3] = gen_reg_rtx (Pmode);
1487   /* Emit the move of the address to a pseudo outside of the libcall.  */
1488   if (TARGET_HARD_SH4 && TARGET_SH3E)
1489     {
1490       emit_move_insn (operands[3],
1491                       gen_rtx_SYMBOL_REF (SImode, \"__udivsi3_i4\"));
1492       if (TARGET_FPU_SINGLE)
1493         last = gen_udivsi3_i4_single (operands[0], operands[3]);
1494       else
1495         last = gen_udivsi3_i4 (operands[0], operands[3]);
1496     }
1497   else if (TARGET_SHMEDIA_FPU)
1498     {
1499       operands[1] = force_reg (SImode, operands[1]);
1500       operands[2] = force_reg (SImode, operands[2]);
1501       last = gen_udivsi3_i4_media (operands[0], operands[1], operands[2]);
1502       first = last;
1503     }
1504   else if (TARGET_SH5)
1505     {
1506       emit_move_insn (operands[3],
1507                       gen_rtx_SYMBOL_REF (Pmode,
1508                                           (TARGET_FPU_ANY
1509                                            ? \"__udivsi3_i4\"
1510                                            : \"__udivsi3\")));
1511
1512       if (TARGET_SHMEDIA)
1513         last = gen_udivsi3_i1_media (operands[0],
1514                                      Pmode == DImode
1515                                      ? operands[3]
1516                                      : gen_rtx_SUBREG (DImode, operands[3],
1517                                                        0));
1518       else if (TARGET_FPU_ANY)
1519         last = gen_udivsi3_i4_single (operands[0], operands[3]);
1520       else
1521         last = gen_udivsi3_i1 (operands[0], operands[3]);
1522     }
1523   else
1524     {
1525       emit_move_insn (operands[3],
1526                       gen_rtx_SYMBOL_REF (SImode, \"__udivsi3\"));
1527       last = gen_udivsi3_i1 (operands[0], operands[3]);
1528     }
1529   if (! first)
1530     {
1531       first = emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1532       emit_move_insn (gen_rtx_REG (SImode, 5), operands[2]);
1533     }
1534   last = emit_insn (last);
1535   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1536      invariant code motion can move it.  */
1537   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1538   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1539   DONE;
1540 }")
1541
1542 (define_insn "divsi3_i1"
1543   [(set (match_operand:SI 0 "register_operand" "=z")
1544         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1545    (clobber (reg:SI T_REG))
1546    (clobber (reg:SI PR_REG))
1547    (clobber (reg:SI R1_REG))
1548    (clobber (reg:SI R2_REG))
1549    (clobber (reg:SI R3_REG))
1550    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1551   "TARGET_SH1 && ! TARGET_SH4"
1552   "jsr  @%1%#"
1553   [(set_attr "type" "sfunc")
1554    (set_attr "needs_delay_slot" "yes")])
1555
1556 ; Since shmedia-nofpu code could be linked against shcompact code, and
1557 ; the sdivsi3 libcall has the same name, we must consider all registers
1558 ; clobbered that are in the union of the registers clobbered by the
1559 ; shmedia and the shcompact implementation.  Note, if the shcompact
1560 ; implemenation actually used shcompact code, we'd need to clobber
1561 ; also r22, r23 and fr23.
1562 (define_insn "divsi3_i1_media"
1563   [(set (match_operand:SI 0 "register_operand" "=z")
1564         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1565    (clobber (reg:SI T_MEDIA_REG))
1566    (clobber (reg:SI PR_MEDIA_REG))
1567    (clobber (reg:SI R1_REG))
1568    (clobber (reg:SI R2_REG))
1569    (clobber (reg:SI R3_REG))
1570    (clobber (reg:SI R20_REG))
1571    (clobber (reg:SI R21_REG))
1572    (clobber (reg:DI TR0_REG))
1573    (clobber (reg:DI TR1_REG))
1574    (clobber (reg:DI TR2_REG))
1575    (use (match_operand:DI 1 "target_operand" "b"))]
1576   "TARGET_SHMEDIA && ! TARGET_SHMEDIA_FPU"
1577   "blink        %1, r18"
1578   [(set_attr "type" "sfunc")])
1579
1580 (define_expand "divsi3_i4_media"
1581   [(set (match_dup 3) (float:DF (match_operand:SI 1 "register_operand" "r")))
1582    (set (match_dup 4) (float:DF (match_operand:SI 2 "register_operand" "r")))
1583    (set (match_dup 5) (div:DF (match_dup 3) (match_dup 4)))
1584    (set (match_operand:SI 0 "register_operand" "=r")
1585         (fix:SI (match_dup 5)))]
1586   "TARGET_SHMEDIA_FPU"
1587   "
1588 {
1589   operands[3] = gen_reg_rtx (DFmode);
1590   operands[4] = gen_reg_rtx (DFmode);
1591   operands[5] = gen_reg_rtx (DFmode);
1592 }")
1593
1594 (define_insn "divsi3_i4"
1595   [(set (match_operand:SI 0 "register_operand" "=y")
1596         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1597    (clobber (reg:SI PR_REG))
1598    (clobber (reg:DF DR0_REG))
1599    (clobber (reg:DF DR2_REG))
1600    (use (reg:PSI FPSCR_REG))
1601    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1602   "TARGET_SH4 && ! TARGET_FPU_SINGLE"
1603   "jsr  @%1%#"
1604   [(set_attr "type" "sfunc")
1605    (set_attr "fp_mode" "double")
1606    (set_attr "needs_delay_slot" "yes")])
1607
1608 (define_insn "divsi3_i4_single"
1609   [(set (match_operand:SI 0 "register_operand" "=y")
1610         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1611    (clobber (reg:SI PR_REG))
1612    (clobber (reg:DF DR0_REG))
1613    (clobber (reg:DF DR2_REG))
1614    (clobber (reg:SI R2_REG))
1615    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1616   "(TARGET_HARD_SH4 || TARGET_SHCOMPACT) && TARGET_FPU_SINGLE"
1617   "jsr  @%1%#"
1618   [(set_attr "type" "sfunc")
1619    (set_attr "needs_delay_slot" "yes")])
1620
1621 (define_expand "divsi3"
1622   [(set (match_dup 3) (symbol_ref:SI "__sdivsi3"))
1623    (set (reg:SI R4_REG) (match_operand:SI 1 "general_operand" ""))
1624    (set (reg:SI R5_REG) (match_operand:SI 2 "general_operand" ""))
1625    (parallel [(set (match_operand:SI 0 "register_operand" "")
1626                    (div:SI (reg:SI R4_REG)
1627                            (reg:SI R5_REG)))
1628               (clobber (reg:SI T_REG))
1629               (clobber (reg:SI PR_REG))
1630               (clobber (reg:SI R1_REG))
1631               (clobber (reg:SI R2_REG))
1632               (clobber (reg:SI R3_REG))
1633               (use (match_dup 3))])]
1634   ""
1635   "
1636 {
1637   rtx first = 0, last;
1638
1639   operands[3] = gen_reg_rtx (Pmode);
1640   /* Emit the move of the address to a pseudo outside of the libcall.  */
1641   if (TARGET_HARD_SH4 && TARGET_SH3E)
1642     {
1643       emit_move_insn (operands[3],
1644                       gen_rtx_SYMBOL_REF (SImode, \"__sdivsi3_i4\"));
1645       if (TARGET_FPU_SINGLE)
1646         last = gen_divsi3_i4_single (operands[0], operands[3]);
1647       else
1648         last = gen_divsi3_i4 (operands[0], operands[3]);
1649     }
1650   else if (TARGET_SHMEDIA_FPU)
1651     {
1652       operands[1] = force_reg (SImode, operands[1]);
1653       operands[2] = force_reg (SImode, operands[2]);
1654       last = gen_divsi3_i4_media (operands[0], operands[1], operands[2]);
1655       first = last;
1656     }
1657   else if (TARGET_SH5)
1658     {
1659       emit_move_insn (operands[3],
1660                       gen_rtx_SYMBOL_REF (Pmode,
1661                                           (TARGET_FPU_ANY
1662                                            ? \"__sdivsi3_i4\"
1663                                            : \"__sdivsi3\")));
1664
1665       if (TARGET_SHMEDIA)
1666         last = gen_divsi3_i1_media (operands[0],
1667                                     Pmode == DImode
1668                                     ? operands[3]
1669                                     : gen_rtx_SUBREG (DImode, operands[3],
1670                                                       0));
1671       else if (TARGET_FPU_ANY)
1672         last = gen_divsi3_i4_single (operands[0], operands[3]);
1673       else
1674         last = gen_divsi3_i1 (operands[0], operands[3]);
1675     }
1676   else
1677     {
1678       emit_move_insn (operands[3], gen_rtx_SYMBOL_REF (SImode, \"__sdivsi3\"));
1679       last = gen_divsi3_i1 (operands[0], operands[3]);
1680     }
1681   if (! first)
1682     {
1683       first = emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1684       emit_move_insn (gen_rtx_REG (SImode, 5), operands[2]);
1685     }
1686   last = emit_insn (last);
1687   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1688      invariant code motion can move it.  */
1689   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1690   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1691   DONE;
1692 }")
1693 \f
1694 ;; -------------------------------------------------------------------------
1695 ;; Multiplication instructions
1696 ;; -------------------------------------------------------------------------
1697
1698 (define_insn "umulhisi3_i"
1699   [(set (reg:SI MACL_REG)
1700         (mult:SI (zero_extend:SI
1701                   (match_operand:HI 0 "arith_reg_operand" "r"))
1702                  (zero_extend:SI
1703                   (match_operand:HI 1 "arith_reg_operand" "r"))))]
1704   "TARGET_SH1"
1705   "mulu.w       %1,%0"
1706   [(set_attr "type" "smpy")])
1707
1708 (define_insn "mulhisi3_i"
1709   [(set (reg:SI MACL_REG)
1710         (mult:SI (sign_extend:SI
1711                   (match_operand:HI 0 "arith_reg_operand" "r"))
1712                  (sign_extend:SI
1713                   (match_operand:HI 1 "arith_reg_operand" "r"))))]
1714   "TARGET_SH1"
1715   "muls.w       %1,%0"
1716   [(set_attr "type" "smpy")])
1717
1718 (define_expand "mulhisi3"
1719   [(set (reg:SI MACL_REG)
1720         (mult:SI (sign_extend:SI
1721                   (match_operand:HI 1 "arith_reg_operand" ""))
1722                  (sign_extend:SI
1723                   (match_operand:HI 2 "arith_reg_operand" ""))))
1724    (set (match_operand:SI 0 "arith_reg_operand" "")
1725         (reg:SI MACL_REG))]
1726   "TARGET_SH1"
1727   "
1728 {
1729   rtx first, last;
1730
1731   first = emit_insn (gen_mulhisi3_i (operands[1], operands[2]));
1732   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACL_REG));
1733   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1734      invariant code motion can move it.  */
1735   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1736   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1737   DONE;
1738 }")
1739
1740 (define_expand "umulhisi3"
1741   [(set (reg:SI MACL_REG)
1742         (mult:SI (zero_extend:SI
1743                   (match_operand:HI 1 "arith_reg_operand" ""))
1744                  (zero_extend:SI
1745                   (match_operand:HI 2 "arith_reg_operand" ""))))
1746    (set (match_operand:SI 0 "arith_reg_operand" "")
1747         (reg:SI MACL_REG))]
1748   "TARGET_SH1"
1749   "
1750 {
1751   rtx first, last;
1752
1753   first = emit_insn (gen_umulhisi3_i (operands[1], operands[2]));
1754   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACL_REG));
1755   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1756      invariant code motion can move it.  */
1757   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1758   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1759   DONE;
1760 }")
1761
1762 ;; mulsi3 on the SH2 can be done in one instruction, on the SH1 we generate
1763 ;; a call to a routine which clobbers known registers.
1764
1765 (define_insn ""
1766   [(set (match_operand:SI 1 "register_operand" "=z")
1767         (mult:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1768    (clobber (reg:SI MACL_REG))
1769    (clobber (reg:SI T_REG))
1770    (clobber (reg:SI PR_REG))
1771    (clobber (reg:SI R3_REG))
1772    (clobber (reg:SI R2_REG))
1773    (clobber (reg:SI R1_REG))
1774    (use (match_operand:SI 0 "arith_reg_operand" "r"))]
1775   "TARGET_SH1"
1776   "jsr  @%0%#"
1777   [(set_attr "type" "sfunc")
1778    (set_attr "needs_delay_slot" "yes")])
1779
1780 (define_expand "mulsi3_call"
1781   [(set (reg:SI R4_REG) (match_operand:SI 1 "general_operand" ""))
1782    (set (reg:SI R5_REG) (match_operand:SI 2 "general_operand" ""))
1783    (parallel[(set (match_operand:SI 0 "register_operand" "")
1784                   (mult:SI (reg:SI R4_REG)
1785                            (reg:SI R5_REG)))
1786              (clobber (reg:SI MACL_REG))
1787              (clobber (reg:SI T_REG))
1788              (clobber (reg:SI PR_REG))
1789              (clobber (reg:SI R3_REG))
1790              (clobber (reg:SI R2_REG))
1791              (clobber (reg:SI R1_REG))
1792              (use (match_operand:SI 3 "register_operand" ""))])]
1793   "TARGET_SH1"
1794   "")
1795
1796 (define_insn "mul_l"
1797   [(set (reg:SI MACL_REG)
1798         (mult:SI (match_operand:SI 0 "arith_reg_operand" "r")
1799                  (match_operand:SI 1 "arith_reg_operand" "r")))]
1800   "TARGET_SH2"
1801   "mul.l        %1,%0"
1802   [(set_attr "type" "dmpy")])
1803
1804 (define_expand "mulsi3"
1805   [(set (reg:SI MACL_REG)
1806         (mult:SI  (match_operand:SI 1 "arith_reg_operand" "")
1807                   (match_operand:SI 2 "arith_reg_operand" "")))
1808    (set (match_operand:SI 0 "arith_reg_operand" "")
1809         (reg:SI MACL_REG))]
1810   "TARGET_SH1"
1811   "
1812 {
1813   rtx first, last;
1814
1815   if (!TARGET_SH2)
1816     {
1817       /* The address must be set outside the libcall,
1818          since it goes into a pseudo.  */
1819       rtx sym = gen_rtx_SYMBOL_REF (SImode, \"__mulsi3\");
1820       rtx addr = force_reg (SImode, sym);
1821       rtx insns = gen_mulsi3_call (operands[0], operands[1],
1822                                    operands[2], addr);
1823       first = insns;
1824       last = emit_insn (insns);
1825     }
1826   else
1827     {
1828       rtx macl = gen_rtx_REG (SImode, MACL_REG);
1829
1830       first = emit_insn (gen_mul_l (operands[1], operands[2]));
1831       /* consec_sets_giv can only recognize the first insn that sets a
1832          giv as the giv insn.  So we must tag this also with a REG_EQUAL
1833          note.  */
1834       last = emit_insn (gen_movsi_i ((operands[0]), macl));
1835     }
1836   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1837      invariant code motion can move it.  */
1838   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1839   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1840   DONE;
1841 }")
1842
1843 (define_insn "mulsidi3_i"
1844   [(set (reg:SI MACH_REG)
1845         (truncate:SI
1846          (lshiftrt:DI
1847           (mult:DI
1848            (sign_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1849            (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1850           (const_int 32))))
1851    (set (reg:SI MACL_REG)
1852         (mult:SI (match_dup 0)
1853                  (match_dup 1)))]
1854   "TARGET_SH2"
1855   "dmuls.l      %1,%0"
1856   [(set_attr "type" "dmpy")])
1857
1858 (define_expand "mulsidi3"
1859   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1860         (mult:DI (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r"))
1861                  (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" "r"))))]
1862   "TARGET_SH2 || TARGET_SHMEDIA"
1863   "
1864 {
1865   if (TARGET_SH2)
1866     {
1867        emit_insn (gen_mulsidi3_compact (operands[0], operands[1],
1868                                         operands[2]));
1869        DONE;
1870     }
1871 }")
1872
1873 (define_insn "mulsidi3_media"
1874   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1875         (mult:DI (sign_extend:DI (match_operand:SI 1 "extend_reg_operand" "%r"))
1876                  (sign_extend:DI (match_operand:SI 2 "extend_reg_operand" "r"))))]
1877   "TARGET_SHMEDIA"
1878   "muls.l       %1, %2, %0"
1879   [(set_attr "type" "dmpy_media")])
1880
1881 (define_insn "mulsidi3_compact"
1882   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1883         (mult:DI
1884          (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r"))
1885          (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" "r"))))
1886    (clobber (reg:SI MACH_REG))
1887    (clobber (reg:SI MACL_REG))]
1888   "TARGET_SH2"
1889   "#")
1890
1891 (define_split
1892   [(set (match_operand:DI 0 "arith_reg_operand" "")
1893         (mult:DI
1894          (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
1895          (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" ""))))
1896    (clobber (reg:SI MACH_REG))
1897    (clobber (reg:SI MACL_REG))]
1898   "TARGET_SH2"
1899   [(const_int 0)]
1900   "
1901 {
1902   rtx low_dst = gen_lowpart (SImode, operands[0]);
1903   rtx high_dst = gen_highpart (SImode, operands[0]);
1904
1905   emit_insn (gen_mulsidi3_i (operands[1], operands[2]));
1906
1907   emit_move_insn (low_dst, gen_rtx_REG (SImode, MACL_REG));
1908   emit_move_insn (high_dst, gen_rtx_REG (SImode, MACH_REG));
1909   /* We need something to tag the possible REG_EQUAL notes on to.  */
1910   emit_move_insn (operands[0], operands[0]);
1911   DONE;
1912 }")
1913
1914 (define_insn "umulsidi3_i"
1915   [(set (reg:SI MACH_REG)
1916         (truncate:SI
1917          (lshiftrt:DI
1918           (mult:DI
1919            (zero_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1920            (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1921           (const_int 32))))
1922    (set (reg:SI MACL_REG)
1923         (mult:SI (match_dup 0)
1924                  (match_dup 1)))]
1925   "TARGET_SH2"
1926   "dmulu.l      %1,%0"
1927   [(set_attr "type" "dmpy")])
1928
1929 (define_expand "umulsidi3"
1930   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1931         (mult:DI (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r"))
1932                  (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" "r"))))]
1933   "TARGET_SH2 || TARGET_SHMEDIA"
1934   "
1935 {
1936   if (TARGET_SH2)
1937     {
1938        emit_insn (gen_umulsidi3_compact (operands[0], operands[1],
1939                                          operands[2]));
1940        DONE;
1941     }
1942 }")
1943
1944 (define_insn "umulsidi3_media"
1945   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1946         (mult:DI (zero_extend:DI (match_operand:SI 1 "extend_reg_operand" "%r"))
1947                  (zero_extend:DI (match_operand:SI 2 "extend_reg_operand" "r"))))]
1948   "TARGET_SHMEDIA"
1949   "mulu.l       %1, %2, %0"
1950   [(set_attr "type" "dmpy_media")])
1951
1952 (define_insn "umulsidi3_compact"
1953   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1954         (mult:DI
1955          (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r"))
1956          (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" "r"))))
1957    (clobber (reg:SI MACH_REG))
1958    (clobber (reg:SI MACL_REG))]
1959   "TARGET_SH2"
1960   "#")
1961
1962 (define_split
1963   [(set (match_operand:DI 0 "arith_reg_operand" "")
1964         (mult:DI (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
1965                  (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" ""))))
1966    (clobber (reg:SI MACH_REG))
1967    (clobber (reg:SI MACL_REG))]
1968   "TARGET_SH2"
1969   [(const_int 0)]
1970   "
1971 {
1972   rtx low_dst = gen_lowpart (SImode, operands[0]);
1973   rtx high_dst = gen_highpart (SImode, operands[0]);
1974
1975   emit_insn (gen_umulsidi3_i (operands[1], operands[2]));
1976
1977   emit_move_insn (low_dst, gen_rtx_REG (SImode, MACL_REG));
1978   emit_move_insn (high_dst, gen_rtx_REG (SImode, MACH_REG));
1979   /* We need something to tag the possible REG_EQUAL notes on to.  */
1980   emit_move_insn (operands[0], operands[0]);
1981   DONE;
1982 }")
1983
1984 (define_insn "smulsi3_highpart_i"
1985   [(set (reg:SI MACH_REG)
1986         (truncate:SI
1987          (lshiftrt:DI
1988           (mult:DI
1989            (sign_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1990            (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1991           (const_int 32))))
1992    (clobber (reg:SI MACL_REG))]
1993   "TARGET_SH2"
1994   "dmuls.l      %1,%0"
1995   [(set_attr "type" "dmpy")])
1996
1997 (define_expand "smulsi3_highpart"
1998   [(parallel
1999     [(set (reg:SI MACH_REG)
2000           (truncate:SI
2001            (lshiftrt:DI
2002             (mult:DI
2003              (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
2004              (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" "")))
2005             (const_int 32))))
2006     (clobber (reg:SI MACL_REG))])
2007    (set (match_operand:SI 0 "arith_reg_operand" "")
2008         (reg:SI MACH_REG))]
2009   "TARGET_SH2"
2010   "
2011 {
2012   rtx first, last;
2013
2014   first = emit_insn (gen_smulsi3_highpart_i (operands[1], operands[2]));
2015   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACH_REG));
2016   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
2017      invariant code motion can move it.  */
2018   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
2019   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
2020   /* expand_binop can't find a suitable code in mul_highpart_optab to
2021      make a REG_EQUAL note from, so make one here.
2022      ??? Alternatively, we could put this at the calling site of expand_binop,
2023      i.e. expand_mult_highpart.  */
2024   REG_NOTES (last)
2025     = gen_rtx_EXPR_LIST (REG_EQUAL, copy_rtx (SET_SRC (single_set (first))),
2026                          REG_NOTES (last));
2027   DONE;
2028 }")
2029
2030 (define_insn "umulsi3_highpart_i"
2031   [(set (reg:SI MACH_REG)
2032         (truncate:SI
2033          (lshiftrt:DI
2034           (mult:DI
2035            (zero_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
2036            (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
2037           (const_int 32))))
2038    (clobber (reg:SI MACL_REG))]
2039   "TARGET_SH2"
2040   "dmulu.l      %1,%0"
2041   [(set_attr "type" "dmpy")])
2042
2043 (define_expand "umulsi3_highpart"
2044   [(parallel
2045     [(set (reg:SI MACH_REG)
2046           (truncate:SI
2047            (lshiftrt:DI
2048             (mult:DI
2049              (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
2050              (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" "")))
2051             (const_int 32))))
2052     (clobber (reg:SI MACL_REG))])
2053    (set (match_operand:SI 0 "arith_reg_operand" "")
2054         (reg:SI MACH_REG))]
2055   "TARGET_SH2"
2056   "
2057 {
2058   rtx first, last;
2059
2060   first = emit_insn (gen_umulsi3_highpart_i (operands[1], operands[2]));
2061   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACH_REG));
2062   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
2063      invariant code motion can move it.  */
2064   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
2065   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
2066   DONE;
2067 }")
2068 \f
2069 ;; -------------------------------------------------------------------------
2070 ;; Logical operations
2071 ;; -------------------------------------------------------------------------
2072
2073 (define_insn "*andsi3_compact"
2074   [(set (match_operand:SI 0 "arith_reg_operand" "=r,z")
2075         (and:SI (match_operand:SI 1 "arith_reg_operand" "%0,0")
2076                 (match_operand:SI 2 "logical_operand" "r,L")))]
2077   "TARGET_SH1"
2078   "and  %2,%0"
2079   [(set_attr "type" "arith")
2080    (set_attr "insn_class" "ex_group")])
2081
2082 ;; If the constant is 255, then emit a extu.b instruction instead of an
2083 ;; and, since that will give better code.
2084
2085 (define_expand "andsi3"
2086   [(set (match_operand:SI 0 "arith_reg_operand" "")
2087         (and:SI (match_operand:SI 1 "arith_reg_operand" "")
2088                 (match_operand:SI 2 "logical_operand" "")))]
2089   "TARGET_SH1"
2090   "
2091 {
2092   if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) == 255)
2093     {
2094       emit_insn (gen_zero_extendqisi2 (operands[0],
2095                                        gen_lowpart (QImode, operands[1])));
2096       DONE;
2097     }
2098 }")
2099
2100 (define_insn_and_split "anddi3"
2101   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r,r")
2102         (and:DI (match_operand:DI 1 "arith_reg_operand" "%r,r,r")
2103                 (match_operand:DI 2 "and_operand" "r,P,n")))]
2104   "TARGET_SHMEDIA"
2105   "@
2106         and     %1, %2, %0
2107         andi    %1, %2, %0
2108         #"
2109   "reload_completed
2110    && ! logical_operand (operands[2], DImode)"
2111   [(const_int 0)]
2112   "
2113 {
2114   if (INTVAL (operands[2]) == (unsigned) 0xffffffff)
2115     emit_insn (gen_mshflo_l_di (operands[0], operands[1], CONST0_RTX (DImode)));
2116   else
2117     emit_insn (gen_mshfhi_l_di (operands[0], CONST0_RTX (DImode), operands[1]));
2118   DONE;
2119 }"
2120   [(set_attr "type" "arith_media")])
2121
2122 (define_insn "andcdi3"
2123   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
2124         (and:DI (match_operand:DI 1 "arith_reg_operand" "r")
2125                 (not:DI (match_operand:DI 2 "arith_reg_operand" "r"))))]
2126   "TARGET_SHMEDIA"
2127   "andc %1,%2,%0"
2128   [(set_attr "type" "arith_media")])
2129
2130 (define_insn "iorsi3"
2131   [(set (match_operand:SI 0 "arith_reg_operand" "=r,z")
2132         (ior:SI (match_operand:SI 1 "arith_reg_operand" "%0,0")
2133                 (match_operand:SI 2 "logical_operand" "r,L")))]
2134   "TARGET_SH1"
2135   "or   %2,%0"
2136   [(set_attr "type" "arith")
2137    (set_attr "insn_class" "ex_group")])
2138
2139 (define_insn "iordi3"
2140   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
2141         (ior:DI (match_operand:DI 1 "arith_reg_operand" "%r,r")
2142                 (match_operand:DI 2 "logical_operand" "r,P")))]
2143   "TARGET_SHMEDIA"
2144   "@
2145         or      %1, %2, %0
2146         ori     %1, %2, %0"
2147   [(set_attr "type" "arith_media")])
2148
2149 (define_insn "xorsi3"
2150   [(set (match_operand:SI 0 "arith_reg_operand" "=z,r")
2151         (xor:SI (match_operand:SI 1 "arith_reg_operand" "%0,0")
2152                 (match_operand:SI 2 "logical_operand" "L,r")))]
2153   "TARGET_SH1"
2154   "xor  %2,%0"
2155   [(set_attr "type" "arith")
2156    (set_attr "insn_class" "ex_group")])
2157
2158 (define_insn "xordi3"
2159   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
2160         (xor:DI (match_operand:DI 1 "arith_reg_operand" "%r,r")
2161                 (match_operand:DI 2 "shmedia_6bit_operand" "r,O")))]
2162   "TARGET_SHMEDIA"
2163   "@
2164         xor     %1, %2, %0
2165         xori    %1, %2, %0"
2166   [(set_attr "type" "arith_media")])
2167 \f
2168 ;; -------------------------------------------------------------------------
2169 ;; Shifts and rotates
2170 ;; -------------------------------------------------------------------------
2171
2172 (define_expand "rotldi3"
2173   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
2174         (rotate:DI (match_operand:DI 1 "arith_reg_operand" "r")
2175                    (match_operand:HI 2 "mextr_bit_offset" "i")))]
2176   "TARGET_SHMEDIA"
2177   "if (! mextr_bit_offset (operands[2], HImode)) FAIL;")
2178
2179 (define_insn "rotldi3_mextr"
2180   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
2181         (rotate:DI (match_operand:DI 1 "arith_reg_operand" "r")
2182                    (match_operand:HI 2 "mextr_bit_offset" "i")))]
2183   "TARGET_SHMEDIA"
2184   "*
2185 {
2186   static char templ[16];
2187
2188   sprintf (templ, \"mextr%d\\t%%1,%%1,%%0\",
2189            8 - (int) (INTVAL (operands[2]) >> 3));
2190   return templ;
2191 }"
2192   [(set_attr "type" "arith_media")])
2193
2194 (define_expand "rotrdi3"
2195   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
2196         (rotatert:DI (match_operand:DI 1 "arith_reg_operand" "r")
2197                      (match_operand:HI 2 "mextr_bit_offset" "i")))]
2198   "TARGET_SHMEDIA"
2199   "if (! mextr_bit_offset (operands[2], HImode)) FAIL;")
2200
2201 (define_insn "rotrdi3_mextr"
2202   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
2203         (rotatert:DI (match_operand:DI 1 "arith_reg_operand" "r")
2204                      (match_operand:HI 2 "mextr_bit_offset" "i")))]
2205   "TARGET_SHMEDIA"
2206   "*
2207 {
2208   static char templ[16];
2209
2210   sprintf (templ, \"mextr%d\\t%%1,%%1,%%0\", (int) INTVAL (operands[2]) >> 3);
2211   return templ;
2212 }"
2213   [(set_attr "type" "arith_media")])
2214
2215 (define_insn "rotlsi3_1"
2216   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2217         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "0")
2218                    (const_int 1)))
2219    (set (reg:SI T_REG)
2220         (lshiftrt:SI (match_dup 1) (const_int 31)))]
2221   "TARGET_SH1"
2222   "rotl %0"
2223   [(set_attr "type" "arith")
2224    (set_attr "insn_class" "ex_group")])
2225
2226 (define_insn "rotlsi3_31"
2227   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2228         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "0")
2229                    (const_int 31)))
2230    (clobber (reg:SI T_REG))]
2231   "TARGET_SH1"
2232   "rotr %0"
2233   [(set_attr "type" "arith")
2234    (set_attr "insn_class" "ex_group")])
2235
2236 (define_insn "rotlsi3_16"
2237   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2238         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "r")
2239                    (const_int 16)))]
2240   "TARGET_SH1"
2241   "swap.w       %1,%0"
2242   [(set_attr "type" "arith")
2243   (set_attr "insn_class" "ex_group")])
2244
2245 (define_expand "rotlsi3"
2246   [(set (match_operand:SI 0 "arith_reg_operand" "")
2247         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "")
2248                    (match_operand:SI 2 "immediate_operand" "")))]
2249   "TARGET_SH1"
2250   "
2251 {
2252   static const char rot_tab[] = {
2253     000, 000, 000, 000, 000, 000, 010, 001,
2254     001, 001, 011, 013, 003, 003, 003, 003,
2255     003, 003, 003, 003, 003, 013, 012, 002,
2256     002, 002, 010, 000, 000, 000, 000, 000,
2257   };
2258
2259   int count, choice;
2260
2261   if (GET_CODE (operands[2]) != CONST_INT)
2262     FAIL;
2263   count = INTVAL (operands[2]);
2264   choice = rot_tab[count];
2265   if (choice & 010 && SH_DYNAMIC_SHIFT_COST <= 1)
2266     FAIL;
2267   choice &= 7;
2268   switch (choice)
2269     {
2270     case 0:
2271       emit_move_insn (operands[0], operands[1]);
2272       count -= (count & 16) * 2;
2273       break;
2274     case 3:
2275      emit_insn (gen_rotlsi3_16 (operands[0], operands[1]));
2276      count -= 16;
2277      break;
2278     case 1:
2279     case 2:
2280       {
2281         rtx parts[2];
2282         parts[0] = gen_reg_rtx (SImode);
2283         parts[1] = gen_reg_rtx (SImode);
2284         emit_insn (gen_rotlsi3_16 (parts[2-choice], operands[1]));
2285         parts[choice-1] = operands[1];
2286         emit_insn (gen_ashlsi3 (parts[0], parts[0], GEN_INT (8)));
2287         emit_insn (gen_lshrsi3 (parts[1], parts[1], GEN_INT (8)));
2288         emit_insn (gen_iorsi3 (operands[0], parts[0], parts[1]));
2289         count = (count & ~16) - 8;
2290       }
2291     }
2292
2293   for (; count > 0; count--)
2294     emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
2295   for (; count < 0; count++)
2296     emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
2297
2298   DONE;
2299 }")
2300
2301 (define_insn "*rotlhi3_8"
2302   [(set (match_operand:HI 0 "arith_reg_operand" "=r")
2303         (rotate:HI (match_operand:HI 1 "arith_reg_operand" "r")
2304                    (const_int 8)))]
2305   "TARGET_SH1"
2306   "swap.b       %1,%0"
2307   [(set_attr "type" "arith")
2308    (set_attr "insn_class" "ex_group")])
2309
2310 (define_expand "rotlhi3"
2311   [(set (match_operand:HI 0 "arith_reg_operand" "")
2312         (rotate:HI (match_operand:HI 1 "arith_reg_operand" "")
2313                    (match_operand:HI 2 "immediate_operand" "")))]
2314   "TARGET_SH1"
2315   "
2316 {
2317   if (GET_CODE (operands[2]) != CONST_INT || INTVAL (operands[2]) != 8)
2318     FAIL;
2319 }")
2320
2321 ;;
2322 ;; shift left
2323
2324 ;; This pattern is used by init_expmed for computing the costs of shift
2325 ;; insns.
2326
2327 (define_insn_and_split "ashlsi3_std"
2328   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r,r,r")
2329         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "0,0,0,0")
2330                    (match_operand:SI 2 "nonmemory_operand" "r,M,K,?ri")))
2331    (clobber (match_scratch:SI 3 "=X,X,X,&r"))]
2332   "TARGET_SH3
2333    || (TARGET_SH1 && GET_CODE (operands[2]) == CONST_INT
2334        && CONST_OK_FOR_K (INTVAL (operands[2])))"
2335   "@
2336    shld %2,%0
2337    add  %0,%0
2338    shll%O2      %0
2339    #"
2340   "TARGET_SH3
2341    && reload_completed
2342    && GET_CODE (operands[2]) == CONST_INT
2343    && ! CONST_OK_FOR_K (INTVAL (operands[2]))"
2344   [(set (match_dup 3) (match_dup 2))
2345    (parallel
2346     [(set (match_dup 0) (ashift:SI (match_dup 1) (match_dup 3)))
2347      (clobber (match_dup 4))])]
2348   "operands[4] = gen_rtx_SCRATCH (SImode);"
2349   [(set_attr "length" "*,*,*,4")
2350    (set_attr "type" "dyn_shift,arith,arith,arith")
2351    (set_attr "insn_class" "ex_group,ex_group,ex_group,ex_group")])
2352
2353 (define_insn "ashlhi3_k"
2354   [(set (match_operand:HI 0 "arith_reg_operand" "=r,r")
2355         (ashift:HI (match_operand:HI 1 "arith_reg_operand" "0,0")
2356                    (match_operand:HI 2 "const_int_operand" "M,K")))]
2357   "TARGET_SH1 && CONST_OK_FOR_K (INTVAL (operands[2]))"
2358   "@
2359         add     %0,%0
2360         shll%O2 %0"
2361   [(set_attr "type" "arith")
2362    (set_attr "insn_class" "ex_group")])
2363
2364 (define_insn "ashlsi3_n"
2365   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2366         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "0")
2367                    (match_operand:SI 2 "const_int_operand" "n")))
2368    (clobber (reg:SI T_REG))]
2369   "TARGET_SH1 && ! sh_dynamicalize_shift_p (operands[2])"
2370   "#"
2371   [(set (attr "length")
2372         (cond [(eq (symbol_ref "shift_insns_rtx (insn)") (const_int 1))
2373                (const_string "2")
2374                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 2))
2375                (const_string "4")
2376                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 3))
2377                (const_string "6")]
2378               (const_string "8")))
2379    (set_attr "type" "arith")
2380    (set_attr "insn_class" "ex_group")])
2381
2382 (define_split
2383   [(set (match_operand:SI 0 "arith_reg_operand" "")
2384         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "")
2385                    (match_operand:SI 2 "const_int_operand" "")))
2386    (clobber (reg:SI T_REG))]
2387   "TARGET_SH1 && reload_completed"
2388   [(use (reg:SI R0_REG))]
2389   "
2390 {
2391   gen_shifty_op (ASHIFT, operands);
2392   DONE;
2393 }")
2394
2395 (define_insn "ashlsi3_media"
2396   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
2397         (ashift:SI (match_operand:SI 1 "extend_reg_operand" "r,r")
2398                    (match_operand:SI 2 "nonmemory_operand" "r,n")))]
2399   "TARGET_SHMEDIA"
2400   "@
2401         shlld.l %1, %2, %0
2402         shlli.l %1, %2, %0"
2403   [(set_attr "type" "arith_media")])
2404
2405 (define_expand "ashlsi3"
2406   [(parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
2407                    (ashift:SI (match_operand:SI 1 "arith_reg_operand" "")
2408                               (match_operand:SI 2 "nonmemory_operand" "")))
2409               (clobber (reg:SI T_REG))])]
2410   ""
2411   "
2412 {
2413   if (TARGET_SHMEDIA)
2414     {
2415       emit_insn (gen_ashlsi3_media (operands[0], operands[1], operands[2]));
2416       DONE;
2417     }
2418   if (GET_CODE (operands[2]) == CONST_INT
2419       && sh_dynamicalize_shift_p (operands[2]))
2420     operands[2] = force_reg (SImode, operands[2]);
2421   if (TARGET_SH3)
2422     {
2423       emit_insn (gen_ashlsi3_std (operands[0], operands[1], operands[2]));
2424       DONE;
2425     }
2426   if (! immediate_operand (operands[2], GET_MODE (operands[2])))
2427     FAIL;
2428 }")
2429
2430 (define_insn "ashlhi3"
2431   [(set (match_operand:HI 0 "arith_reg_operand" "=r")
2432         (ashift:HI (match_operand:HI 1 "arith_reg_operand" "0")
2433                    (match_operand:HI 2 "const_int_operand" "n")))
2434    (clobber (reg:SI T_REG))]
2435   "TARGET_SH1"
2436   "#"
2437   [(set (attr "length")
2438         (cond [(eq (symbol_ref "shift_insns_rtx (insn)") (const_int 1))
2439                (const_string "2")
2440                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 2))
2441                (const_string "4")]
2442               (const_string "6")))
2443    (set_attr "type" "arith")])
2444
2445 (define_split
2446   [(set (match_operand:HI 0 "arith_reg_operand" "")
2447         (ashift:HI (match_operand:HI 1 "arith_reg_operand" "")
2448                    (match_operand:HI 2 "const_int_operand" "")))
2449    (clobber (reg:SI T_REG))]
2450   "TARGET_SH1 && reload_completed"
2451   [(use (reg:SI R0_REG))]
2452   "
2453 {
2454   gen_shifty_hi_op (ASHIFT, operands);
2455   DONE;
2456 }")
2457
2458 ;
2459 ; arithmetic shift right
2460 ;
2461
2462 (define_insn "ashrsi3_k"
2463   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2464         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2465                      (match_operand:SI 2 "const_int_operand" "M")))
2466    (clobber (reg:SI T_REG))]
2467   "TARGET_SH1 && INTVAL (operands[2]) == 1"
2468   "shar %0"
2469   [(set_attr "type" "arith")
2470    (set_attr "insn_class" "ex_group")])
2471
2472 ;; We can't do HImode right shifts correctly unless we start out with an
2473 ;; explicit zero / sign extension; doing that would result in worse overall
2474 ;; code, so just let the machine independent code widen the mode.
2475 ;; That's why we don't have ashrhi3_k / lshrhi3_k / lshrhi3_m / lshrhi3 .
2476
2477
2478 ;; ??? This should be a define expand.
2479
2480 (define_insn "ashrsi2_16"
2481   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2482         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "r")
2483                      (const_int 16)))]
2484   "TARGET_SH1"
2485   "#"
2486   [(set_attr "length" "4")])
2487
2488 (define_split
2489   [(set (match_operand:SI 0 "arith_reg_operand" "")
2490         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
2491                      (const_int 16)))]
2492   "TARGET_SH1"
2493   [(set (match_dup 0) (rotate:SI (match_dup 1) (const_int 16)))
2494    (set (match_dup 0) (sign_extend:SI (match_dup 2)))]
2495   "operands[2] = gen_lowpart (HImode, operands[0]);")
2496
2497 ;; ??? This should be a define expand.
2498
2499 (define_insn "ashrsi2_31"
2500   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2501         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2502                      (const_int 31)))
2503    (clobber (reg:SI T_REG))]
2504   "TARGET_SH1"
2505   "#"
2506   [(set_attr "length" "4")])
2507
2508 (define_split
2509   [(set (match_operand:SI 0 "arith_reg_operand" "")
2510         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
2511                      (const_int 31)))
2512    (clobber (reg:SI T_REG))]
2513   "TARGET_SH1"
2514   [(const_int 0)]
2515   "
2516 {
2517   emit_insn (gen_ashlsi_c (operands[0], operands[1]));
2518   emit_insn (gen_subc1 (operands[0], operands[0], operands[0]));
2519   DONE;
2520 }")
2521
2522 (define_insn "ashlsi_c"
2523   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2524         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "0") (const_int 1)))
2525    (set (reg:SI T_REG)
2526         (lt:SI (match_dup 1) (const_int 0)))]
2527   "TARGET_SH1"
2528   "shll %0"
2529   [(set_attr "type" "arith")
2530    (set_attr "insn_class" "ex_group")])
2531
2532 (define_insn "ashrsi3_d"
2533   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2534         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2535                      (neg:SI (match_operand:SI 2 "arith_reg_operand" "r"))))]
2536   "TARGET_SH3"
2537   "shad %2,%0"
2538   [(set_attr "type" "dyn_shift")
2539    (set_attr "insn_class" "ex_group")])
2540
2541 (define_insn "ashrsi3_n"
2542   [(set (reg:SI R4_REG)
2543         (ashiftrt:SI (reg:SI R4_REG)
2544                      (match_operand:SI 0 "const_int_operand" "i")))
2545    (clobber (reg:SI T_REG))
2546    (clobber (reg:SI PR_REG))
2547    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
2548   "TARGET_SH1"
2549   "jsr  @%1%#"
2550   [(set_attr "type" "sfunc")
2551    (set_attr "needs_delay_slot" "yes")])
2552
2553 (define_insn "ashrsi3_media"
2554   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
2555         (ashiftrt:SI (match_operand:SI 1 "extend_reg_operand" "r,r")
2556                      (match_operand:SI 2 "nonmemory_operand" "r,n")))]
2557   "TARGET_SHMEDIA"
2558   "@
2559         shard.l %1, %2, %0
2560         shari.l %1, %2, %0"
2561   [(set_attr "type" "arith_media")])
2562
2563 (define_expand "ashrsi3"
2564   [(parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
2565                    (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
2566                                 (match_operand:SI 2 "nonmemory_operand" "")))
2567               (clobber (reg:SI T_REG))])]
2568   ""
2569   "
2570 {
2571   if (TARGET_SHMEDIA)
2572     {
2573       emit_insn (gen_ashrsi3_media (operands[0], operands[1], operands[2]));
2574       DONE;
2575     }
2576   if (expand_ashiftrt (operands))
2577     DONE;
2578   else
2579     FAIL;
2580 }")
2581
2582 ;; logical shift right
2583
2584 (define_insn "lshrsi3_d"
2585   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2586         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2587                      (neg:SI (match_operand:SI 2 "arith_reg_operand" "r"))))]
2588   "TARGET_SH3"
2589   "shld %2,%0"
2590   [(set_attr "type" "dyn_shift")
2591    (set_attr "insn_class" "ex_group")])
2592
2593 ;;  Only the single bit shift clobbers the T bit.
2594
2595 (define_insn "lshrsi3_m"
2596   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2597         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2598                      (match_operand:SI 2 "const_int_operand" "M")))
2599    (clobber (reg:SI T_REG))]
2600   "TARGET_SH1 && CONST_OK_FOR_M (INTVAL (operands[2]))"
2601   "shlr %0"
2602   [(set_attr "type" "arith")
2603    (set_attr "insn_class" "ex_group")])
2604
2605 (define_insn "lshrsi3_k"
2606   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2607         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2608                      (match_operand:SI 2 "const_int_operand" "K")))]
2609   "TARGET_SH1 && CONST_OK_FOR_K (INTVAL (operands[2]))
2610    && ! CONST_OK_FOR_M (INTVAL (operands[2]))"
2611   "shlr%O2      %0"
2612   [(set_attr "type" "arith")
2613    (set_attr "insn_class" "ex_group")])
2614
2615 (define_insn "lshrsi3_n"
2616   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2617         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2618                      (match_operand:SI 2 "const_int_operand" "n")))
2619    (clobber (reg:SI T_REG))]
2620   "TARGET_SH1 && ! sh_dynamicalize_shift_p (operands[2])"
2621   "#"
2622   [(set (attr "length")
2623         (cond [(eq (symbol_ref "shift_insns_rtx (insn)") (const_int 1))
2624                (const_string "2")
2625                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 2))
2626                (const_string "4")
2627                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 3))
2628                (const_string "6")]
2629               (const_string "8")))
2630    (set_attr "type" "arith")])
2631
2632 (define_split
2633   [(set (match_operand:SI 0 "arith_reg_operand" "")
2634         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
2635                      (match_operand:SI 2 "const_int_operand" "")))
2636    (clobber (reg:SI T_REG))]
2637   "TARGET_SH1 && reload_completed"
2638   [(use (reg:SI R0_REG))]
2639   "
2640 {
2641   gen_shifty_op (LSHIFTRT, operands);
2642   DONE;
2643 }")
2644
2645 (define_insn "lshrsi3_media"
2646   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
2647         (lshiftrt:SI (match_operand:SI 1 "extend_reg_operand" "r,r")
2648                      (match_operand:SI 2 "nonmemory_operand" "r,n")))]
2649   "TARGET_SHMEDIA"
2650   "@
2651         shlrd.l %1, %2, %0
2652         shlri.l %1, %2, %0"
2653   [(set_attr "type" "arith_media")])
2654
2655 (define_expand "lshrsi3"
2656   [(parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
2657                    (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
2658                                 (match_operand:SI 2 "nonmemory_operand" "")))
2659               (clobber (reg:SI T_REG))])]
2660   ""
2661   "
2662 {
2663   if (TARGET_SHMEDIA)
2664     {
2665       emit_insn (gen_lshrsi3_media (operands[0], operands[1], operands[2]));
2666       DONE;
2667     }
2668   if (GET_CODE (operands[2]) == CONST_INT
2669       && sh_dynamicalize_shift_p (operands[2]))
2670     operands[2] = force_reg (SImode, operands[2]);
2671   if (TARGET_SH3 && arith_reg_operand (operands[2], GET_MODE (operands[2])))
2672     {
2673       rtx count = copy_to_mode_reg (SImode, operands[2]);
2674       emit_insn (gen_negsi2 (count, count));
2675       emit_insn (gen_lshrsi3_d (operands[0], operands[1], count));
2676       DONE;
2677     }
2678   if (! immediate_operand (operands[2], GET_MODE (operands[2])))
2679     FAIL;
2680 }")
2681
2682 ;; ??? This should be a define expand.
2683
2684 (define_insn "ashldi3_k"
2685   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
2686         (ashift:DI (match_operand:DI 1 "arith_reg_operand" "0")
2687                    (const_int 1)))
2688    (clobber (reg:SI T_REG))]
2689   "TARGET_SH1"
2690   "shll %R0\;rotcl      %S0"
2691   [(set_attr "length" "4")
2692    (set_attr "type" "arith")
2693    (set_attr "insn_class" "ex_group")])
2694
2695 (define_insn "ashldi3_media"
2696   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
2697         (ashift:DI (match_operand:DI 1 "arith_reg_operand" "r,r")
2698                    (match_operand:DI 2 "nonmemory_operand" "r,n")))]
2699   "TARGET_SHMEDIA"
2700   "@
2701         shlld   %1, %2, %0
2702         shlli   %1, %2, %0"
2703   [(set_attr "type" "arith_media")])
2704
2705 (define_expand "ashldi3"
2706   [(parallel [(set (match_operand:DI 0 "arith_reg_operand" "")
2707                    (ashift:DI (match_operand:DI 1 "arith_reg_operand" "")
2708                               (match_operand:DI 2 "immediate_operand" "")))
2709               (clobber (reg:SI T_REG))])]
2710   ""
2711   "
2712 {
2713   if (TARGET_SHMEDIA)
2714     {
2715       emit_insn (gen_ashldi3_media (operands[0], operands[1], operands[2]));
2716       DONE;
2717     }
2718   if (GET_CODE (operands[2]) != CONST_INT
2719       || INTVAL (operands[2]) != 1)
2720     FAIL;
2721 }")
2722
2723 ;; ??? This should be a define expand.
2724
2725 (define_insn "lshrdi3_k"
2726   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
2727         (lshiftrt:DI (match_operand:DI 1 "arith_reg_operand" "0")
2728                      (const_int 1)))
2729    (clobber (reg:SI T_REG))]
2730   "TARGET_SH1"
2731   "shlr %S0\;rotcr      %R0"
2732   [(set_attr "length" "4")
2733    (set_attr "type" "arith")
2734    (set_attr "insn_class" "ex_group")])
2735
2736 (define_insn "lshrdi3_media"
2737   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
2738         (lshiftrt:DI (match_operand:DI 1 "arith_reg_operand" "r,r")
2739                      (match_operand:DI 2 "nonmemory_operand" "r,n")))]
2740   "TARGET_SHMEDIA"
2741   "@
2742         shlrd   %1, %2, %0
2743         shlri   %1, %2, %0"
2744   [(set_attr "type" "arith_media")])
2745
2746 (define_expand "lshrdi3"
2747   [(parallel [(set (match_operand:DI 0 "arith_reg_operand" "")
2748                    (lshiftrt:DI (match_operand:DI 1 "arith_reg_operand" "")
2749                                (match_operand:DI 2 "immediate_operand" "")))
2750              (clobber (reg:SI T_REG))])]
2751   ""
2752   "
2753 {
2754   if (TARGET_SHMEDIA)
2755     {
2756       emit_insn (gen_lshrdi3_media (operands[0], operands[1], operands[2]));
2757       DONE;
2758     }
2759   if (GET_CODE (operands[2]) != CONST_INT
2760       || INTVAL (operands[2]) != 1)
2761     FAIL;
2762 }")
2763
2764 ;; ??? This should be a define expand.
2765
2766 (define_insn "ashrdi3_k"
2767   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
2768         (ashiftrt:DI (match_operand:DI 1 "arith_reg_operand" "0")
2769                      (const_int 1)))
2770    (clobber (reg:SI T_REG))]
2771   "TARGET_SH1"
2772   "shar %S0\;rotcr      %R0"
2773   [(set_attr "length" "4")
2774    (set_attr "type" "arith")
2775    (set_attr "insn_class" "ex_group")])
2776
2777 (define_insn "ashrdi3_media"
2778   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
2779         (ashiftrt:DI (match_operand:DI 1 "arith_reg_operand" "r,r")
2780                      (match_operand:DI 2 "nonmemory_operand" "r,n")))]
2781   "TARGET_SHMEDIA"
2782   "@
2783         shard   %1, %2, %0
2784         shari   %1, %2, %0"
2785   [(set_attr "type" "arith_media")])
2786
2787 (define_expand "ashrdi3"
2788   [(parallel [(set (match_operand:DI 0 "arith_reg_operand" "")
2789                    (ashiftrt:DI (match_operand:DI 1 "arith_reg_operand" "")
2790                                 (match_operand:DI 2 "immediate_operand" "")))
2791               (clobber (reg:SI T_REG))])]
2792   ""
2793   "
2794 {
2795   if (TARGET_SHMEDIA)
2796     {
2797       emit_insn (gen_ashrdi3_media (operands[0], operands[1], operands[2]));
2798       DONE;
2799     }
2800   if (GET_CODE (operands[2]) != CONST_INT
2801       || INTVAL (operands[2]) != 1)
2802     FAIL;
2803 }")
2804
2805 ;; combined left/right shift
2806
2807 (define_split
2808   [(set (match_operand:SI 0 "register_operand" "")
2809         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
2810                            (match_operand:SI 2 "const_int_operand" ""))
2811                 (match_operand:SI 3 "const_int_operand" "")))]
2812   "TARGET_SH1 && (unsigned)INTVAL (operands[2]) < 32"
2813   [(use (reg:SI R0_REG))]
2814   "if (gen_shl_and (operands[0], operands[2], operands[3], operands[1])) FAIL;
2815    DONE;")
2816
2817 (define_split
2818   [(set (match_operand:SI 0 "register_operand" "")
2819         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
2820                            (match_operand:SI 2 "const_int_operand" ""))
2821                 (match_operand:SI 3 "const_int_operand" "")))
2822    (clobber (reg:SI T_REG))]
2823   "TARGET_SH1 && (unsigned)INTVAL (operands[2]) < 32"
2824   [(use (reg:SI R0_REG))]
2825   "if (gen_shl_and (operands[0], operands[2], operands[3], operands[1])) FAIL;
2826    DONE;")
2827
2828 (define_insn ""
2829   [(set (match_operand:SI 0 "register_operand" "=r")
2830         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
2831                            (match_operand:SI 2 "const_int_operand" "n"))
2832                 (match_operand:SI 3 "const_int_operand" "n")))
2833    (clobber (reg:SI T_REG))]
2834   "TARGET_SH1 && shl_and_kind (operands[2], operands[3], 0) == 1"
2835  "#"
2836   [(set (attr "length")
2837         (cond [(eq (symbol_ref "shl_and_length (insn)") (const_int 2))
2838                (const_string "4")
2839                (eq (symbol_ref "shl_and_length (insn)") (const_int 3))
2840                (const_string "6")
2841                (eq (symbol_ref "shl_and_length (insn)") (const_int 4))
2842                (const_string "8")
2843                (eq (symbol_ref "shl_and_length (insn)") (const_int 5))
2844                (const_string "10")
2845                (eq (symbol_ref "shl_and_length (insn)") (const_int 6))
2846                (const_string "12")
2847                (eq (symbol_ref "shl_and_length (insn)") (const_int 7))
2848                (const_string "14")
2849                (eq (symbol_ref "shl_and_length (insn)") (const_int 8))
2850                (const_string "16")]
2851               (const_string "18")))
2852    (set_attr "type" "arith")])
2853
2854 (define_insn ""
2855   [(set (match_operand:SI 0 "register_operand" "=z")
2856         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
2857                            (match_operand:SI 2 "const_int_operand" "n"))
2858                 (match_operand:SI 3 "const_int_operand" "n")))
2859    (clobber (reg:SI T_REG))]
2860   "TARGET_SH1 && shl_and_kind (operands[2], operands[3], 0) == 2"
2861  "#"
2862   [(set (attr "length")
2863         (cond [(eq (symbol_ref "shl_and_length (insn)") (const_int 2))
2864                (const_string "4")
2865                (eq (symbol_ref "shl_and_length (insn)") (const_int 3))
2866                (const_string "6")
2867                (eq (symbol_ref "shl_and_length (insn)") (const_int 4))
2868                (const_string "8")]
2869               (const_string "10")))
2870    (set_attr "type" "arith")])
2871
2872 ;; shift left / and combination with a scratch register: The combine pass
2873 ;; does not accept the individual instructions, even though they are
2874 ;; cheap.  But it needs a precise description so that it is usable after
2875 ;; reload.
2876 (define_insn "and_shl_scratch"
2877   [(set (match_operand:SI 0 "register_operand" "=r,&r")
2878         (lshiftrt:SI
2879          (ashift:SI
2880           (and:SI
2881            (lshiftrt:SI (match_operand:SI 1 "register_operand" "r,0")
2882                         (match_operand:SI 2 "const_int_operand" "N,n"))
2883            (match_operand:SI 3 "" "0,r"))
2884           (match_operand:SI 4 "const_int_operand" "n,n"))
2885          (match_operand:SI 5 "const_int_operand" "n,n")))
2886    (clobber (reg:SI T_REG))]
2887   "TARGET_SH1"
2888   "#"
2889   [(set (attr "length")
2890         (cond [(eq (symbol_ref "shl_and_scr_length (insn)") (const_int 2))
2891                (const_string "4")
2892                (eq (symbol_ref "shl_and_scr_length (insn)") (const_int 3))
2893                (const_string "6")
2894                (eq (symbol_ref "shl_and_scr_length (insn)") (const_int 4))
2895                (const_string "8")
2896                (eq (symbol_ref "shl_and_scr_length (insn)") (const_int 5))
2897                (const_string "10")]
2898               (const_string "12")))
2899    (set_attr "type" "arith")])
2900
2901 (define_split
2902   [(set (match_operand:SI 0 "register_operand" "")
2903         (lshiftrt:SI
2904          (ashift:SI
2905           (and:SI
2906            (lshiftrt:SI (match_operand:SI 1 "register_operand" "")
2907                         (match_operand:SI 2 "const_int_operand" ""))
2908            (match_operand:SI 3 "register_operand" ""))
2909           (match_operand:SI 4 "const_int_operand" ""))
2910          (match_operand:SI 5 "const_int_operand" "")))
2911    (clobber (reg:SI T_REG))]
2912   "TARGET_SH1"
2913   [(use (reg:SI R0_REG))]
2914   "
2915 {
2916   rtx and_source = operands[rtx_equal_p (operands[0], operands[1]) ? 3 : 1];
2917
2918   if (INTVAL (operands[2]))
2919     {
2920       gen_shifty_op (LSHIFTRT, operands);
2921     }
2922   emit_insn (gen_andsi3 (operands[0], operands[0], and_source));
2923   operands[2] = operands[4];
2924   gen_shifty_op (ASHIFT, operands);
2925   if (INTVAL (operands[5]))
2926     {
2927       operands[2] = operands[5];
2928       gen_shifty_op (LSHIFTRT, operands);
2929     }
2930   DONE;
2931 }")
2932
2933 ;; signed left/right shift combination.
2934 (define_split
2935   [(set (match_operand:SI 0 "register_operand" "")
2936         (sign_extract:SI
2937          (ashift:SI (match_operand:SI 1 "register_operand" "")
2938                     (match_operand:SI 2 "const_int_operand" ""))
2939          (match_operand:SI 3 "const_int_operand" "")
2940          (const_int 0)))
2941    (clobber (reg:SI T_REG))]
2942   "TARGET_SH1"
2943   [(use (reg:SI R0_REG))]
2944   "if (gen_shl_sext (operands[0], operands[2], operands[3], operands[1])) FAIL;
2945    DONE;")
2946
2947 (define_insn "shl_sext_ext"
2948   [(set (match_operand:SI 0 "register_operand" "=r")
2949         (sign_extract:SI
2950          (ashift:SI (match_operand:SI 1 "register_operand" "0")
2951                     (match_operand:SI 2 "const_int_operand" "n"))
2952          (match_operand:SI 3 "const_int_operand" "n")
2953          (const_int 0)))
2954    (clobber (reg:SI T_REG))]
2955   "TARGET_SH1 && (unsigned)shl_sext_kind (operands[2], operands[3], 0) - 1 < 5"
2956   "#"
2957   [(set (attr "length")
2958         (cond [(eq (symbol_ref "shl_sext_length (insn)") (const_int 1))
2959                (const_string "2")
2960                (eq (symbol_ref "shl_sext_length (insn)") (const_int 2))
2961                (const_string "4")
2962                (eq (symbol_ref "shl_sext_length (insn)") (const_int 3))
2963                (const_string "6")
2964                (eq (symbol_ref "shl_sext_length (insn)") (const_int 4))
2965                (const_string "8")
2966                (eq (symbol_ref "shl_sext_length (insn)") (const_int 5))
2967                (const_string "10")
2968                (eq (symbol_ref "shl_sext_length (insn)") (const_int 6))
2969                (const_string "12")
2970                (eq (symbol_ref "shl_sext_length (insn)") (const_int 7))
2971                (const_string "14")
2972                (eq (symbol_ref "shl_sext_length (insn)") (const_int 8))
2973                (const_string "16")]
2974               (const_string "18")))
2975     (set_attr "type" "arith")])
2976
2977 (define_insn "shl_sext_sub"
2978   [(set (match_operand:SI 0 "register_operand" "=z")
2979         (sign_extract:SI
2980          (ashift:SI (match_operand:SI 1 "register_operand" "0")
2981                     (match_operand:SI 2 "const_int_operand" "n"))
2982          (match_operand:SI 3 "const_int_operand" "n")
2983          (const_int 0)))
2984    (clobber (reg:SI T_REG))]
2985   "TARGET_SH1 && (shl_sext_kind (operands[2], operands[3], 0) & ~1) == 6"
2986   "#"
2987   [(set (attr "length")
2988         (cond [(eq (symbol_ref "shl_sext_length (insn)") (const_int 3))
2989                (const_string "6")
2990                (eq (symbol_ref "shl_sext_length (insn)") (const_int 4))
2991                (const_string "8")
2992                (eq (symbol_ref "shl_sext_length (insn)") (const_int 5))
2993                (const_string "10")
2994                (eq (symbol_ref "shl_sext_length (insn)") (const_int 6))
2995                (const_string "12")]
2996               (const_string "14")))
2997     (set_attr "type" "arith")])
2998
2999 ;; These patterns are found in expansions of DImode shifts by 16, and
3000 ;; allow the xtrct instruction to be generated from C source.
3001
3002 (define_insn "xtrct_left"
3003   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3004         (ior:SI (ashift:SI (match_operand:SI 1 "arith_reg_operand" "r")
3005                            (const_int 16))
3006                 (lshiftrt:SI (match_operand:SI 2 "arith_reg_operand" "0")
3007                              (const_int 16))))]
3008   "TARGET_SH1"
3009   "xtrct        %1,%0"
3010   [(set_attr "type" "arith")
3011    (set_attr "insn_class" "ex_group")])
3012
3013 (define_insn "xtrct_right"
3014   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3015         (ior:SI (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
3016                              (const_int 16))
3017                 (ashift:SI (match_operand:SI 2 "arith_reg_operand" "r")
3018                            (const_int 16))))]
3019   "TARGET_SH1"
3020   "xtrct        %2,%0"
3021   [(set_attr "type" "arith")
3022    (set_attr "insn_class" "ex_group")])
3023
3024 ;; -------------------------------------------------------------------------
3025 ;; Unary arithmetic
3026 ;; -------------------------------------------------------------------------
3027
3028 (define_insn "negc"
3029   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3030         (neg:SI (plus:SI (reg:SI T_REG)
3031                          (match_operand:SI 1 "arith_reg_operand" "r"))))
3032    (set (reg:SI T_REG)
3033         (ne:SI (ior:SI (reg:SI T_REG) (match_dup 1))
3034                (const_int 0)))]
3035   "TARGET_SH1"
3036   "negc %1,%0"
3037   [(set_attr "type" "arith")
3038    (set_attr "insn_class" "ex_group")])
3039
3040 (define_insn "*negdi_media"
3041   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
3042         (neg:DI (match_operand:DI 1 "arith_reg_operand" "r")))]
3043   "TARGET_SHMEDIA"
3044   "sub  r63, %1, %0"
3045   [(set_attr "type" "arith_media")])
3046
3047 (define_expand "negdi2"
3048   [(set (match_operand:DI 0 "arith_reg_operand" "")
3049         (neg:DI (match_operand:DI 1 "arith_reg_operand" "")))]
3050   ""
3051   "
3052 {
3053   if (TARGET_SH1)
3054     {
3055       int low_word = (TARGET_LITTLE_ENDIAN ? 0 : 1);
3056       int high_word = (TARGET_LITTLE_ENDIAN ? 1 : 0);
3057
3058       rtx low_src = operand_subword (operands[1], low_word, 0, DImode);
3059       rtx high_src = operand_subword (operands[1], high_word, 0, DImode);
3060
3061       rtx low_dst = operand_subword (operands[0], low_word, 1, DImode);
3062       rtx high_dst = operand_subword (operands[0], high_word, 1, DImode);
3063
3064       emit_insn (gen_clrt ());
3065       emit_insn (gen_negc (low_dst, low_src));
3066       emit_insn (gen_negc (high_dst, high_src));
3067       DONE;
3068     }
3069 }")
3070
3071 (define_insn "negsi2"
3072   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3073         (neg:SI (match_operand:SI 1 "arith_reg_operand" "r")))]
3074   "TARGET_SH1"
3075   "neg  %1,%0"
3076   [(set_attr "type" "arith")
3077    (set_attr "insn_class" "ex_group")])
3078
3079 (define_insn "one_cmplsi2"
3080   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3081         (not:SI (match_operand:SI 1 "arith_reg_operand" "r")))]
3082   "TARGET_SH1"
3083   "not  %1,%0"
3084   [(set_attr "type" "arith")
3085    (set_attr "insn_class" "ex_group")])
3086
3087 (define_expand "one_cmpldi2"
3088   [(set (match_operand:DI 0 "arith_reg_operand" "")
3089         (xor:DI (match_operand:DI 1 "arith_reg_operand" "")
3090                 (const_int -1)))]
3091   "TARGET_SHMEDIA" "")
3092 \f
3093 ;; -------------------------------------------------------------------------
3094 ;; Zero extension instructions
3095 ;; -------------------------------------------------------------------------
3096
3097 (define_insn "zero_extendsidi2"
3098   [(set (match_operand:DI 0 "register_operand" "=r")
3099         (zero_extend:DI (match_operand:SI 1 "extend_reg_operand" "r")))]
3100   "TARGET_SHMEDIA"
3101   "addz.l       %1, r63, %0"
3102   [(set_attr "type" "arith_media")])
3103
3104 (define_insn "zero_extendhidi2"
3105   [(set (match_operand:DI 0 "register_operand" "=r,r")
3106         (zero_extend:DI (match_operand:HI 1 "general_extend_operand" "r,m")))]
3107   "TARGET_SHMEDIA"
3108   "@
3109         #
3110         ld%M1.uw        %m1, %0"
3111   [(set_attr "type" "*,load_media")])
3112
3113 (define_split
3114   [(set (match_operand:DI 0 "register_operand" "")
3115         (zero_extend:DI (match_operand:HI 1 "extend_reg_operand" "")))]
3116   "TARGET_SHMEDIA && reload_completed"
3117   [(set (match_dup 0) (ashift:DI (subreg:DI (match_dup 1) 0) (const_int 48)))
3118    (set (match_dup 0) (lshiftrt:DI (match_dup 0) (const_int 48)))]
3119   "
3120 {
3121   if (GET_CODE (operands[1]) == TRUNCATE)
3122     operands[1] = XEXP (operands[1], 0);
3123 }")
3124
3125 ;; ??? when a truncated input to a zero_extrend is reloaded, reload will
3126 ;; reload the entrire truncate expression.
3127 (define_insn_and_split "*loaddi_trunc"
3128   [(set (match_operand 0 "int_gpr_dest" "=r")
3129         (truncate (match_operand:DI 1 "memory_operand" "m")))]
3130   "TARGET_SHMEDIA && reload_completed"
3131   "#"
3132   "TARGET_SHMEDIA && reload_completed"
3133   [(set (match_dup 0) (match_dup 1))]
3134   "operands[0] = gen_rtx_REG (DImode, true_regnum (operands[0]));")
3135
3136 (define_insn "zero_extendqidi2"
3137   [(set (match_operand:DI 0 "register_operand" "=r,r")
3138         (zero_extend:DI (match_operand:QI 1 "general_extend_operand" "r,m")))]
3139   "TARGET_SHMEDIA"
3140   "@
3141         andi    %1, 255, %0
3142         ld%M1.ub        %m1, %0"
3143   [(set_attr "type" "arith_media,load_media")])
3144
3145 (define_expand "zero_extendhisi2"
3146   [(set (match_operand:SI 0 "arith_reg_operand" "")
3147         (zero_extend:SI (match_operand:HI 1 "general_extend_operand" "")))]
3148   ""
3149   "
3150 {
3151   if (! TARGET_SHMEDIA && ! arith_reg_operand (operands[1], HImode))
3152     operands[1] = copy_to_mode_reg (HImode, operands[1]);
3153 }")
3154
3155 (define_insn "*zero_extendhisi2_compact"
3156   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3157         (zero_extend:SI (match_operand:HI 1 "arith_reg_operand" "r")))]
3158   "TARGET_SH1"
3159   "extu.w       %1,%0"
3160   [(set_attr "type" "arith")
3161    (set_attr "insn_class" "ex_group")])
3162
3163 (define_insn "*zero_extendhisi2_media"
3164   [(set (match_operand:SI 0 "register_operand" "=r,r")
3165         (zero_extend:SI (match_operand:HI 1 "general_extend_operand" "r,m")))]
3166   "TARGET_SHMEDIA"
3167   "@
3168         #
3169         ld%M1.uw        %m1, %0"
3170   [(set_attr "type" "arith_media,load_media")])
3171
3172 (define_split
3173   [(set (match_operand:SI 0 "register_operand" "")
3174         (zero_extend:SI (match_operand:HI 1 "extend_reg_operand" "")))]
3175   "TARGET_SHMEDIA && reload_completed"
3176   [(set (match_dup 0) (ashift:SI (subreg:SI (match_dup 1) 0) (const_int 16)))
3177    (set (match_dup 0) (lshiftrt:SI (match_dup 0) (const_int 16)))]
3178   "
3179 {
3180   if (GET_CODE (operands[1]) == TRUNCATE)
3181     operands[1] = XEXP (operands[1], 0);
3182 }")
3183
3184 (define_expand "zero_extendqisi2"
3185   [(set (match_operand:SI 0 "arith_reg_operand" "")
3186         (zero_extend:SI (match_operand:QI 1 "general_extend_operand" "")))]
3187   ""
3188   "
3189 {
3190   if (! TARGET_SHMEDIA && ! arith_reg_operand (operands[1], QImode))
3191     operands[1] = copy_to_mode_reg (QImode, operands[1]);
3192 }")
3193
3194 (define_insn "*zero_extendqisi2_compact"
3195   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3196         (zero_extend:SI (match_operand:QI 1 "arith_reg_operand" "r")))]
3197   "TARGET_SH1"
3198   "extu.b       %1,%0"
3199   [(set_attr "type" "arith")
3200    (set_attr "insn_class" "ex_group")])
3201
3202 (define_insn "*zero_extendqisi2_media"
3203   [(set (match_operand:SI 0 "register_operand" "=r,r")
3204         (zero_extend:SI (match_operand:QI 1 "general_extend_operand" "r,m")))]
3205   "TARGET_SHMEDIA"
3206   "@
3207         andi    %1, 255, %0
3208         ld%M1.ub        %m1, %0"
3209   [(set_attr "type" "arith_media,load_media")])
3210
3211 (define_insn "zero_extendqihi2"
3212   [(set (match_operand:HI 0 "arith_reg_operand" "=r")
3213         (zero_extend:HI (match_operand:QI 1 "arith_reg_operand" "r")))]
3214   "TARGET_SH1"
3215   "extu.b       %1,%0"
3216   [(set_attr "type" "arith")
3217    (set_attr "insn_class" "ex_group")])
3218
3219 ;; -------------------------------------------------------------------------
3220 ;; Sign extension instructions
3221 ;; -------------------------------------------------------------------------
3222
3223 ;; ??? This should be a define expand.
3224 ;; ??? Or perhaps it should be dropped?
3225
3226 ;; convert_move generates good code for SH[1-4].
3227 (define_insn "extendsidi2"
3228   [(set (match_operand:DI 0 "register_operand" "=r,r")
3229         (sign_extend:DI (match_operand:SI 1 "nonimmediate_operand" "r,m")))]
3230   "TARGET_SHMEDIA"
3231   "@
3232         add.l   %1, r63, %0
3233         ld%M1.l %m1, %0"
3234   [(set_attr "type" "arith_media,load_media")])
3235
3236 (define_insn "extendhidi2"
3237   [(set (match_operand:DI 0 "register_operand" "=r,r")
3238         (sign_extend:DI (match_operand:HI 1 "general_extend_operand" "r,m")))]
3239   "TARGET_SHMEDIA"
3240   "@
3241         #
3242         ld%M1.w %m1, %0"
3243   [(set_attr "type" "*,load_media")])
3244
3245 (define_split
3246   [(set (match_operand:DI 0 "register_operand" "")
3247         (sign_extend:DI (match_operand:HI 1 "extend_reg_operand" "")))]
3248   "TARGET_SHMEDIA && reload_completed"
3249   [(set (match_dup 0) (ashift:DI (subreg:DI (match_dup 1) 0) (const_int 48)))
3250    (set (match_dup 0) (ashiftrt:DI (match_dup 0) (const_int 48)))]
3251   "
3252 {
3253   if (GET_CODE (operands[1]) == TRUNCATE)
3254     operands[1] = XEXP (operands[1], 0);
3255 }")
3256
3257 (define_insn "extendqidi2"
3258   [(set (match_operand:DI 0 "register_operand" "=r,r")
3259         (sign_extend:DI (match_operand:QI 1 "general_extend_operand" "r,m")))]
3260   "TARGET_SHMEDIA"
3261   "@
3262         #
3263         ld%M1.b %m1, %0"
3264   [(set_attr "type" "*,load_media")])
3265
3266 (define_split
3267   [(set (match_operand:DI 0 "register_operand" "")
3268         (sign_extend:DI (match_operand:QI 1 "extend_reg_operand" "")))]
3269   "TARGET_SHMEDIA && reload_completed"
3270   [(set (match_dup 0) (ashift:DI (subreg:DI (match_dup 1) 0) (const_int 56)))
3271    (set (match_dup 0) (ashiftrt:DI (match_dup 0) (const_int 56)))]
3272   "
3273 {
3274   if (GET_CODE (operands[1]) == TRUNCATE)
3275     operands[1] = XEXP (operands[1], 0);
3276 }")
3277
3278 (define_expand "extendhisi2"
3279   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
3280        (sign_extend:SI (match_operand:HI 1 "general_extend_operand" "r,m")))]
3281   ""
3282   "")
3283
3284 (define_insn "*extendhisi2_compact"
3285   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
3286         (sign_extend:SI (match_operand:HI 1 "general_movsrc_operand" "r,m")))]
3287   "TARGET_SH1"
3288   "@
3289         exts.w  %1,%0
3290         mov.w   %1,%0"
3291   [(set_attr "type" "arith,load")
3292    (set_attr "insn_class" "ex_group,*")])
3293
3294 (define_insn "*extendhisi2_media"
3295   [(set (match_operand:SI 0 "register_operand" "=r,r")
3296         (sign_extend:SI (match_operand:HI 1 "general_extend_operand" "r,m")))]
3297   "TARGET_SHMEDIA"
3298   "@
3299         #
3300         ld%M1.w %m1, %0"
3301   [(set_attr "type" "arith_media,load_media")])
3302
3303 (define_split
3304   [(set (match_operand:SI 0 "register_operand" "")
3305         (sign_extend:SI (match_operand:HI 1 "extend_reg_operand" "")))]
3306   "TARGET_SHMEDIA && reload_completed"
3307   [(set (match_dup 0) (ashift:SI (subreg:SI (match_dup 1) 0) (const_int 16)))
3308    (set (match_dup 0) (ashiftrt:SI (match_dup 0) (const_int 16)))]
3309   "
3310 {
3311   if (GET_CODE (operands[1]) == TRUNCATE)
3312     operands[1] = XEXP (operands[1], 0);
3313 }")
3314
3315 (define_expand "extendqisi2"
3316   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
3317         (sign_extend:SI (match_operand:QI 1 "general_extend_operand" "r,m")))]
3318   ""
3319   "")
3320
3321 (define_insn "*extendqisi2_compact"
3322   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
3323         (sign_extend:SI (match_operand:QI 1 "general_movsrc_operand" "r,m")))]
3324   "TARGET_SH1"
3325   "@
3326         exts.b  %1,%0
3327         mov.b   %1,%0"
3328   [(set_attr "type" "arith,load")
3329    (set_attr "insn_class" "ex_group,*")])
3330
3331 (define_insn "*extendqisi2_media"
3332   [(set (match_operand:SI 0 "register_operand" "=r,r")
3333         (sign_extend:SI (match_operand:QI 1 "general_extend_operand" "r,m")))]
3334   "TARGET_SHMEDIA"
3335   "@
3336         #
3337         ld%M1.b %m1, %0"
3338   [(set_attr "type" "arith_media,load_media")])
3339
3340 (define_split
3341   [(set (match_operand:SI 0 "register_operand" "")
3342         (sign_extend:SI (match_operand:QI 1 "extend_reg_operand" "")))]
3343   "TARGET_SHMEDIA && reload_completed"
3344   [(set (match_dup 0) (ashift:SI (subreg:SI (match_dup 1) 0) (const_int 24)))
3345    (set (match_dup 0) (ashiftrt:SI (match_dup 0) (const_int 24)))]
3346    "
3347 {
3348   if (GET_CODE (operands[1]) == TRUNCATE)
3349     operands[1] = XEXP (operands[1], 0);
3350 }")
3351
3352 (define_insn "extendqihi2"
3353   [(set (match_operand:HI 0 "arith_reg_operand" "=r,r")
3354         (sign_extend:HI (match_operand:QI 1 "general_movsrc_operand" "r,m")))]
3355   "TARGET_SH1"
3356   "@
3357         exts.b  %1,%0
3358         mov.b   %1,%0"
3359   [(set_attr "type" "arith,load")
3360    (set_attr "insn_class" "ex_group,*")])
3361
3362 /* It would seem useful to combine the truncXi patterns into the movXi
3363    patterns, but unary operators are ignored when matching constraints,
3364    so we need separate patterns.  */
3365 (define_insn "truncdisi2"
3366   [(set (match_operand:SI 0 "general_movdst_operand" "=r,m,m,f,r,f")
3367         (truncate:SI (match_operand:DI 1 "register_operand" "r,r,f,r,f,f")))]
3368   "TARGET_SHMEDIA"
3369   "@
3370         add.l   %1, r63, %0
3371         st%M0.l %m0, %1
3372         fst%M0.s        %m0, %T1
3373         fmov.ls %1, %0
3374         fmov.sl %T1, %0
3375         fmov.s  %T1, %0"
3376   [(set_attr "type"   "arith_media,store_media,fstore_media,fload_media,fpconv_media,fmove_media")])
3377
3378
3379 (define_insn "truncdihi2"
3380   [(set (match_operand:HI 0 "general_movdst_operand" "=?r,m")
3381         (truncate:HI (match_operand:DI 1 "register_operand" "r,r")))]
3382   "TARGET_SHMEDIA"
3383   "@
3384         shlli\\t%1,48,%0\;shlri\\t%0,48,%0
3385         st%M0.w %m0, %1"
3386   [(set_attr "type"   "arith_media,store_media")
3387    (set_attr "length" "8,4")])
3388
3389 ; N.B. This should agree with LOAD_EXTEND_OP and movqi.
3390 ; Because we use zero extension, we can't provide signed QImode compares
3391 ; using a simple compare or conditional banch insn.
3392 (define_insn "truncdiqi2"
3393   [(set (match_operand:QI 0 "general_movdst_operand" "=r,m")
3394         (truncate:QI (match_operand:DI 1 "register_operand" "r,r")))]
3395   "TARGET_SHMEDIA"
3396   "@
3397         and     %1, 255, %0
3398         st%M0.b %m0, %1"
3399   [(set_attr "type"   "arith_media,store")])
3400
3401 ;; -------------------------------------------------------------------------
3402 ;; Move instructions
3403 ;; -------------------------------------------------------------------------
3404
3405 ;; define push and pop so it is easy for sh.c
3406 ;; We can't use push and pop on SHcompact because the stack must always
3407 ;; be 8-byte aligned.
3408
3409 (define_expand "push"
3410   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3411         (match_operand:SI 0 "register_operand" "r,l,x"))]
3412   "TARGET_SH1 && ! TARGET_SH5"
3413   "")
3414
3415 (define_expand "pop"
3416   [(set (match_operand:SI 0 "register_operand" "=r,l,x")
3417         (mem:SI (post_inc:SI (reg:SI SP_REG))))]
3418   "TARGET_SH1 && ! TARGET_SH5"
3419   "")
3420
3421 (define_expand "push_e"
3422   [(parallel [(set (mem:SF (pre_dec:SI (reg:SI SP_REG)))
3423                    (match_operand:SF 0 "" ""))
3424               (use (reg:PSI FPSCR_REG))
3425               (clobber (scratch:SI))])]
3426   "TARGET_SH1 && ! TARGET_SH5"
3427   "")
3428
3429 (define_insn "push_fpul"
3430   [(set (mem:SF (pre_dec:SI (reg:SI SP_REG))) (reg:SF FPUL_REG))]
3431   "TARGET_SH3E && ! TARGET_SH5"
3432   "sts.l        fpul,@-r15"
3433   [(set_attr "type" "store")
3434    (set_attr "hit_stack" "yes")])
3435
3436 ;; DFmode pushes for sh4 require a lot of what is defined for movdf_i4,
3437 ;; so use that.
3438 (define_expand "push_4"
3439   [(parallel [(set (mem:DF (pre_dec:SI (reg:SI SP_REG)))
3440                    (match_operand:DF 0 "" ""))
3441               (use (reg:PSI FPSCR_REG))
3442               (clobber (scratch:SI))])]
3443   "TARGET_SH1 && ! TARGET_SH5"
3444   "")
3445
3446 (define_expand "pop_e"
3447   [(parallel [(set (match_operand:SF 0 "" "")
3448               (mem:SF (post_inc:SI (reg:SI SP_REG))))
3449               (use (reg:PSI FPSCR_REG))
3450               (clobber (scratch:SI))])]
3451   "TARGET_SH1 && ! TARGET_SH5"
3452   "")
3453
3454 (define_insn "pop_fpul"
3455   [(set (reg:SF FPUL_REG) (mem:SF (post_inc:SI (reg:SI SP_REG))))]
3456   "TARGET_SH3E && ! TARGET_SH5"
3457   "lds.l        @r15+,fpul"
3458   [(set_attr "type" "load")
3459    (set_attr "hit_stack" "yes")])
3460
3461 (define_expand "pop_4"
3462   [(parallel [(set (match_operand:DF 0 "" "")
3463                    (mem:DF (post_inc:SI (reg:SI SP_REG))))
3464               (use (reg:PSI FPSCR_REG))
3465               (clobber (scratch:SI))])]
3466   "TARGET_SH1 && ! TARGET_SH5"
3467   "")
3468
3469 ;; These two patterns can happen as the result of optimization, when
3470 ;; comparisons get simplified to a move of zero or 1 into the T reg.
3471 ;; They don't disappear completely, because the T reg is a fixed hard reg.
3472
3473 (define_insn "clrt"
3474   [(set (reg:SI T_REG) (const_int 0))]
3475   "TARGET_SH1"
3476   "clrt")
3477
3478 (define_insn "sett"
3479   [(set (reg:SI T_REG) (const_int 1))]
3480   "TARGET_SH1"
3481   "sett")
3482
3483 ;; t/r must come after r/r, lest reload will try to reload stuff like
3484 ;; (set (subreg:SI (mem:QI (plus:SI (reg:SI SP_REG) (const_int 12)) 0) 0)
3485 ;; (made from (set (subreg:SI (reg:QI ###) 0) ) into T.
3486 (define_insn "movsi_i"
3487   [(set (match_operand:SI 0 "general_movdst_operand" "=r,r,t,r,r,r,r,m,<,<,x,l,x,l,r")
3488         (match_operand:SI 1 "general_movsrc_operand" "Q,rI,r,mr,x,l,t,r,x,l,r,r,>,>,i"))]
3489   "TARGET_SH1
3490    && ! TARGET_SH3E
3491    && (register_operand (operands[0], SImode)
3492        || register_operand (operands[1], SImode))"
3493   "@
3494         mov.l   %1,%0
3495         mov     %1,%0
3496         cmp/pl  %1
3497         mov.l   %1,%0
3498         sts     %1,%0
3499         sts     %1,%0
3500         movt    %0
3501         mov.l   %1,%0
3502         sts.l   %1,%0
3503         sts.l   %1,%0
3504         lds     %1,%0
3505         lds     %1,%0
3506         lds.l   %1,%0
3507         lds.l   %1,%0
3508         fake    %1,%0"
3509   [(set_attr "type" "pcload_si,move,*,load_si,move,prget,move,store,store,pstore,move,prset,load,pload,pcload_si")
3510    (set_attr "insn_class"  "*,*,mt_group,*,*,*,*,*,*,*,*,*,*,*,*")
3511    (set_attr "length" "*,*,*,*,*,*,*,*,*,*,*,*,*,*,*")])
3512
3513 ;; t/r must come after r/r, lest reload will try to reload stuff like
3514 ;; (subreg:SI (reg:SF FR14_REG) 0) into T (compiling stdlib/strtod.c -m3e -O2)
3515 ;; ??? This allows moves from macl to fpul to be recognized, but these moves
3516 ;; will require a reload.
3517 (define_insn "movsi_ie"
3518   [(set (match_operand:SI 0 "general_movdst_operand" "=r,r,t,r,r,r,r,m,<,<,x,l,x,l,y,<,r,y,r,y")
3519         (match_operand:SI 1 "general_movsrc_operand" "Q,rI,r,mr,x,l,t,r,x,l,r,r,>,>,>,y,i,r,y,y"))]
3520   "TARGET_SH3E
3521    && (register_operand (operands[0], SImode)
3522        || register_operand (operands[1], SImode))"
3523   "@
3524         mov.l   %1,%0
3525         mov     %1,%0
3526         cmp/pl  %1
3527         mov.l   %1,%0
3528         sts     %1,%0
3529         sts     %1,%0
3530         movt    %0
3531         mov.l   %1,%0
3532         sts.l   %1,%0
3533         sts.l   %1,%0
3534         lds     %1,%0
3535         lds     %1,%0
3536         lds.l   %1,%0
3537         lds.l   %1,%0
3538         lds.l   %1,%0
3539         sts.l   %1,%0
3540         fake    %1,%0
3541         lds     %1,%0
3542         sts     %1,%0
3543         ! move optimized away"
3544   [(set_attr "type" "pcload_si,move,*,load_si,move,prget,move,store,store,pstore,move,prset,load,pload,load,store,pcload_si,gp_fpul,gp_fpul,nil")
3545    (set_attr "length" "*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,0")])
3546
3547 (define_insn "movsi_i_lowpart"
3548   [(set (strict_low_part (match_operand:SI 0 "general_movdst_operand" "+r,r,r,r,r,r,m,r"))
3549         (match_operand:SI 1 "general_movsrc_operand" "Q,rI,mr,x,l,t,r,i"))]
3550    "TARGET_SH1
3551     && (register_operand (operands[0], SImode)
3552         || register_operand (operands[1], SImode))"
3553   "@
3554         mov.l   %1,%0
3555         mov     %1,%0
3556         mov.l   %1,%0
3557         sts     %1,%0
3558         sts     %1,%0
3559         movt    %0
3560         mov.l   %1,%0
3561         fake    %1,%0"
3562   [(set_attr "type" "pcload,move,load,move,prget,move,store,pcload")])
3563
3564 (define_insn "*movsi_media"
3565   [(set (match_operand:SI 0 "general_movdst_operand" "=r,r,r,r,m,f,m,f,r,f,*b,r,b")
3566         (match_operand:SI 1 "general_movsrc_operand" "r,JS,ns,m,r,m,f,rU,f,f,r,*b,T"))]
3567   "TARGET_SHMEDIA_FPU
3568    && (register_operand (operands[0], SImode)
3569        || register_operand (operands[1], SImode))"
3570   "@
3571         add.l   %1, r63, %0
3572         movi    %1, %0
3573         #
3574         ld%M1.l %m1, %0
3575         st%M0.l %m0, %1
3576         fld%M1.s        %m1, %0
3577         fst%M0.s        %m0, %1
3578         fmov.ls %N1, %0
3579         fmov.sl %1, %0
3580         fmov.s  %1, %0
3581         ptabs   %1, %0
3582         gettr   %1, %0
3583         pt      %1, %0"
3584   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media,fload_media,fstore_media,fload_media,fpconv_media,fmove_media,ptabs_media,gettr_media,pt_media")
3585    (set_attr "length" "4,4,8,4,4,4,4,4,4,4,4,4,12")])
3586
3587 (define_insn "*movsi_media_nofpu"
3588   [(set (match_operand:SI 0 "general_movdst_operand" "=r,r,r,r,m,*b,r,b")
3589         (match_operand:SI 1 "general_movsrc_operand" "r,JS,ns,m,r,r,*b,T"))]
3590   "TARGET_SHMEDIA
3591    && (register_operand (operands[0], SImode)
3592        || register_operand (operands[1], SImode))"
3593   "@
3594         add.l   %1, r63, %0
3595         movi    %1, %0
3596         #
3597         ld%M1.l %m1, %0
3598         st%M0.l %m0, %1
3599         ptabs   %1, %0
3600         gettr   %1, %0
3601         pt      %1, %0"
3602   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media,ptabs_media,gettr_media,pt_media")
3603    (set_attr "length" "4,4,8,4,4,4,4,12")])
3604
3605 (define_split
3606   [(set (match_operand:SI 0 "arith_reg_operand" "")
3607         (match_operand:SI 1 "immediate_operand" ""))]
3608   "TARGET_SHMEDIA && reload_completed
3609    && MOVI_SHORI_BASE_OPERAND_P (operands[1])"
3610   [(set (subreg:DI (match_dup 0) 0) (match_dup 2))]
3611   "
3612 {
3613   operands[2] = shallow_copy_rtx (operands[1]);
3614   PUT_MODE (operands[2], DImode);
3615 }")
3616
3617 (define_split
3618   [(set (match_operand:SI 0 "register_operand" "")
3619         (match_operand:SI 1 "immediate_operand" ""))]
3620   "TARGET_SHMEDIA && reload_completed
3621    && ((GET_CODE (operands[1]) == CONST_INT
3622         && ! CONST_OK_FOR_J (INTVAL (operands[1])))
3623        || GET_CODE (operands[1]) == CONST_DOUBLE)"
3624   [(set (subreg:DI (match_dup 0) 0) (match_dup 1))])
3625
3626 (define_expand "movsi"
3627   [(set (match_operand:SI 0 "general_movdst_operand" "")
3628         (match_operand:SI 1 "general_movsrc_operand" ""))]
3629   ""
3630   "{ if (prepare_move_operands (operands, SImode)) DONE; }")
3631
3632 (define_expand "ic_invalidate_line"
3633   [(parallel [(unspec_volatile [(match_operand:SI 0 "register_operand" "+r")
3634                                 (match_dup 1)] UNSPEC_ICACHE)
3635               (clobber (scratch:SI))])]
3636   "TARGET_HARD_SH4 || TARGET_SH5"
3637   "
3638 {
3639   if (TARGET_SHMEDIA)
3640     {
3641       emit_insn (gen_ic_invalidate_line_media (operands[0]));
3642       DONE;
3643     }
3644   else if (TARGET_SHCOMPACT)
3645     {
3646       operands[1] = gen_rtx_SYMBOL_REF (Pmode, \"__ic_invalidate\");
3647       operands[1] = force_reg (Pmode, operands[1]);
3648       emit_insn (gen_ic_invalidate_line_compact (operands[0], operands[1]));
3649       DONE;
3650     }
3651   operands[0] = force_reg (Pmode, operands[0]);
3652   operands[1] = force_reg (Pmode, GEN_INT (trunc_int_for_mode (0xf0000008,
3653                                                                Pmode)));
3654 }")
3655
3656 ;; The address %0 is assumed to be 4-aligned at least.  Thus, by ORing
3657 ;; 0xf0000008, we get the low-oder bits *1*00 (binary), which fits
3658 ;; the requirement *1*00 for associative address writes.  The alignment of
3659 ;; %0 implies that its least significant bit is cleared,
3660 ;; thus we clear the V bit of a matching entry if there is one.
3661 (define_insn "ic_invalidate_line_i"
3662   [(unspec_volatile [(match_operand:SI 0 "register_operand" "r")
3663                      (match_operand:SI 1 "register_operand" "r")]
3664                      UNSPEC_ICACHE)
3665    (clobber (match_scratch:SI 2 "=&r"))]
3666   "TARGET_HARD_SH4"
3667   "ocbwb\\t@%0\;extu.w\\t%0,%2\;or\\t%1,%2\;mov.l\\t%0,@%2"
3668   [(set_attr "length" "8")
3669    (set_attr "insn_class" "cwb")])
3670
3671 ;; ??? could make arg 0 an offsettable memory operand to allow to save
3672 ;; an add in the code that calculates the address.
3673 (define_insn "ic_invalidate_line_media"
3674   [(unspec_volatile [(match_operand 0 "register_operand" "r")]
3675                     UNSPEC_ICACHE)]
3676   "TARGET_SHMEDIA"
3677   "ocbwb        %0,0\;synco\;icbi       %0, 0\;synci"
3678   [(set_attr "length" "16")
3679    (set_attr "type" "invalidate_line_media")])
3680
3681 (define_insn "ic_invalidate_line_compact"
3682   [(unspec_volatile [(match_operand:SI 0 "register_operand" "z")
3683                      (match_operand:SI 1 "register_operand" "r")]
3684                     UNSPEC_ICACHE)
3685    (clobber (reg:SI PR_REG))]
3686   "TARGET_SHCOMPACT"
3687   "jsr @%1%#"
3688   [(set_attr "type" "sfunc")
3689    (set_attr "needs_delay_slot" "yes")])
3690
3691 (define_expand "initialize_trampoline"
3692   [(match_operand:SI 0 "" "")
3693    (match_operand:SI 1 "" "")
3694    (match_operand:SI 2 "" "")]
3695   "TARGET_SHCOMPACT"
3696   "
3697 {
3698   rtx sfun, tramp;
3699
3700   sfun = force_reg (Pmode, gen_rtx_SYMBOL_REF (Pmode, \"__init_trampoline\"));
3701   tramp = gen_rtx_REG (SImode, R0_REG);
3702   emit_move_insn (tramp, operands[0]);
3703   emit_move_insn (gen_rtx_REG (SImode, R2_REG), operands[1]);
3704   emit_move_insn (gen_rtx_REG (SImode, R3_REG), operands[2]);
3705
3706   emit_insn (gen_initialize_trampoline_compact (tramp, sfun));
3707   DONE;
3708 }")
3709
3710 (define_insn "initialize_trampoline_compact"
3711   [(unspec_volatile [(match_operand:SI 0 "register_operand" "z")
3712                      (match_operand:SI 1 "register_operand" "r")
3713                      (reg:SI R2_REG) (reg:SI R3_REG)]
3714                     UNSPEC_INIT_TRAMP)
3715
3716    (clobber (reg:SI PR_REG))]
3717   "TARGET_SHCOMPACT"
3718   "jsr @%1%#"
3719   [(set_attr "type" "sfunc")
3720    (set_attr "needs_delay_slot" "yes")])
3721
3722 (define_insn "movqi_i"
3723   [(set (match_operand:QI 0 "general_movdst_operand" "=r,r,m,r,r,l")
3724         (match_operand:QI 1 "general_movsrc_operand"  "ri,m,r,t,l,r"))]
3725   "TARGET_SH1
3726    && (arith_reg_operand (operands[0], QImode)
3727        || arith_reg_operand (operands[1], QImode))"
3728   "@
3729         mov     %1,%0
3730         mov.b   %1,%0
3731         mov.b   %1,%0
3732         movt    %0
3733         sts     %1,%0
3734         lds     %1,%0"
3735  [(set_attr "type" "move,load,store,move,move,move")])
3736
3737 (define_insn "*movqi_media"
3738   [(set (match_operand:QI 0 "general_movdst_operand" "=r,r,r,m")
3739         (match_operand:QI 1 "general_movsrc_operand" "r,JS,m,r"))]
3740   "TARGET_SHMEDIA
3741    && (arith_reg_operand (operands[0], QImode)
3742        || arith_reg_operand (operands[1], QImode))"
3743   "@
3744         add.l   %1, r63, %0
3745         movi    %1, %0
3746         ld%M1.ub        %m1, %0
3747         st%M0.b %m0, %1"
3748   [(set_attr "type" "arith_media,arith_media,load_media,store_media")])
3749
3750 (define_expand "movqi"
3751   [(set (match_operand:QI 0 "general_operand" "")
3752         (match_operand:QI 1 "general_operand"  ""))]
3753   ""
3754   "{ if (prepare_move_operands (operands, QImode)) DONE; }")
3755
3756 (define_expand "reload_inqi"
3757   [(set (match_operand:SI 2 "" "=&r")
3758         (match_operand:QI 1 "inqhi_operand" ""))
3759    (set (match_operand:QI 0 "arith_reg_operand" "=r")
3760         (truncate:HI (match_dup 3)))]
3761   "TARGET_SHMEDIA"
3762   "
3763 {
3764   rtx inner = XEXP (operands[1], 0);
3765   int regno = REGNO (inner);
3766
3767   regno += HARD_REGNO_NREGS (regno, GET_MODE (inner)) - 1;
3768   operands[1] = gen_rtx_REG (SImode, regno);
3769   operands[3] = gen_rtx_REG (DImode, REGNO (operands[2]));
3770 }")
3771
3772 (define_insn "movhi_i"
3773   [(set (match_operand:HI 0 "general_movdst_operand" "=r,r,r,r,m,r,l,r")
3774         (match_operand:HI 1 "general_movsrc_operand" "Q,rI,m,t,r,l,r,i"))]
3775   "TARGET_SH1
3776    && (arith_reg_operand (operands[0], HImode)
3777        || arith_reg_operand (operands[1], HImode))"
3778   "@
3779         mov.w   %1,%0
3780         mov     %1,%0
3781         mov.w   %1,%0
3782         movt    %0
3783         mov.w   %1,%0
3784         sts     %1,%0
3785         lds     %1,%0
3786         fake    %1,%0"
3787   [(set_attr "type" "pcload,move,load,move,store,move,move,pcload")])
3788
3789 (define_insn "*movhi_media"
3790   [(set (match_operand:HI 0 "general_movdst_operand" "=r,r,r,r,m")
3791         (match_operand:HI 1 "general_movsrc_operand" "r,JS,n,m,r"))]
3792   "TARGET_SHMEDIA
3793    && (arith_reg_operand (operands[0], HImode)
3794        || arith_reg_operand (operands[1], HImode))"
3795   "@
3796         add.l   %1, r63, %0
3797         movi    %1, %0
3798         #
3799         ld%M1.w %m1, %0
3800         st%M0.w %m0, %1"
3801   [(set_attr "type" "arith_media,arith_media,*,load_media,store_media")])
3802
3803 (define_split
3804   [(set (match_operand:HI 0 "register_operand" "")
3805         (match_operand:HI 1 "immediate_operand" ""))]
3806   "TARGET_SHMEDIA && reload_completed
3807    && ! CONST_OK_FOR_J (INTVAL (operands[1]))"
3808   [(set (subreg:DI (match_dup 0) 0) (match_dup 1))])
3809
3810 (define_expand "movhi"
3811   [(set (match_operand:HI 0 "general_movdst_operand" "")
3812         (match_operand:HI 1 "general_movsrc_operand"  ""))]
3813   ""
3814   "{ if (prepare_move_operands (operands, HImode)) DONE; }")
3815
3816 (define_expand "reload_inhi"
3817   [(set (match_operand:SI 2 "" "=&r")
3818         (match_operand:HI 1 "inqhi_operand" ""))
3819    (set (match_operand:HI 0 "arith_reg_operand" "=r")
3820         (truncate:HI (match_dup 3)))]
3821   "TARGET_SHMEDIA"
3822   "
3823 {
3824   rtx inner = XEXP (operands[1], 0);
3825   int regno = REGNO (inner);
3826
3827   regno += HARD_REGNO_NREGS (regno, GET_MODE (inner)) - 1;
3828   operands[1] = gen_rtx_REG (SImode, regno);
3829   operands[3] = gen_rtx_REG (DImode, REGNO (operands[2]));
3830 }")
3831
3832 ;; ??? This should be a define expand.
3833
3834 ;; x/r can be created by inlining/cse, e.g. for execute/961213-1.c
3835 ;; compiled with -m2 -ml -O3 -funroll-loops
3836 (define_insn ""
3837   [(set (match_operand:DI 0 "general_movdst_operand" "=r,r,r,m,r,r,r,*!x")
3838         (match_operand:DI 1 "general_movsrc_operand" "Q,r,m,r,I,i,x,r"))]
3839   "TARGET_SH1
3840    && (arith_reg_operand (operands[0], DImode)
3841        || arith_reg_operand (operands[1], DImode))"
3842   "* return output_movedouble (insn, operands, DImode);"
3843   [(set_attr "length" "4")
3844    (set_attr "type" "pcload,move,load,store,move,pcload,move,move")])
3845
3846 ;; If the output is a register and the input is memory or a register, we have
3847 ;; to be careful and see which word needs to be loaded first.
3848
3849 (define_split
3850   [(set (match_operand:DI 0 "general_movdst_operand" "")
3851         (match_operand:DI 1 "general_movsrc_operand" ""))]
3852   "TARGET_SH1 && reload_completed"
3853   [(set (match_dup 2) (match_dup 3))
3854    (set (match_dup 4) (match_dup 5))]
3855   "
3856 {
3857   int regno;
3858
3859   if ((GET_CODE (operands[0]) == MEM
3860        && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
3861       || (GET_CODE (operands[1]) == MEM
3862           && GET_CODE (XEXP (operands[1], 0)) == POST_INC))
3863     FAIL;
3864
3865   if (GET_CODE (operands[0]) == REG)
3866     regno = REGNO (operands[0]);
3867   else if (GET_CODE (operands[0]) == SUBREG)
3868     regno = subreg_regno (operands[0]);
3869   else if (GET_CODE (operands[0]) == MEM)
3870     regno = -1;
3871   else
3872     abort ();
3873
3874   if (regno == -1
3875       || ! refers_to_regno_p (regno, regno + 1, operands[1], 0))
3876     {
3877       operands[2] = operand_subword (operands[0], 0, 0, DImode);
3878       operands[3] = operand_subword (operands[1], 0, 0, DImode);
3879       operands[4] = operand_subword (operands[0], 1, 0, DImode);
3880       operands[5] = operand_subword (operands[1], 1, 0, DImode);
3881     }
3882   else
3883     {
3884       operands[2] = operand_subword (operands[0], 1, 0, DImode);
3885       operands[3] = operand_subword (operands[1], 1, 0, DImode);
3886       operands[4] = operand_subword (operands[0], 0, 0, DImode);
3887       operands[5] = operand_subword (operands[1], 0, 0, DImode);
3888     }
3889
3890   if (operands[2] == 0 || operands[3] == 0
3891       || operands[4] == 0 || operands[5] == 0)
3892     FAIL;
3893 }")
3894
3895 (define_insn "*movdi_media"
3896   [(set (match_operand:DI 0 "general_movdst_operand" "=r,r,r,rl,m,f,m,f,r,f,*b,r,b")
3897         (match_operand:DI 1 "general_movsrc_operand" "r,JS,iF,m,rl,m,f,rU,f,f,r,*b,T"))]
3898   "TARGET_SHMEDIA_FPU
3899    && (register_operand (operands[0], DImode)
3900        || register_operand (operands[1], DImode))"
3901   "@
3902         add     %1, r63, %0
3903         movi    %1, %0
3904         #
3905         ld%M1.q %m1, %0
3906         st%M0.q %m0, %1
3907         fld%M1.d        %m1, %0
3908         fst%M0.d        %m0, %1
3909         fmov.qd %N1, %0
3910         fmov.dq %1, %0
3911         fmov.d  %1, %0
3912         ptabs   %1, %0
3913         gettr   %1, %0
3914         pt      %1, %0"
3915   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media,fload_media,fstore_media,fload_media,dfpconv_media,fmove_media,ptabs_media,gettr_media,pt_media")
3916    (set_attr "length" "4,4,16,4,4,4,4,4,4,4,4,4,*")])
3917
3918 (define_insn "*movdi_media_nofpu"
3919   [(set (match_operand:DI 0 "general_movdst_operand" "=r,r,r,rl,m,*b,r,b")
3920         (match_operand:DI 1 "general_movsrc_operand" "r,JS,iF,m,rl,r,*b,T"))]
3921   "TARGET_SHMEDIA
3922    && (register_operand (operands[0], DImode)
3923        || register_operand (operands[1], DImode))"
3924   "@
3925         add     %1, r63, %0
3926         movi    %1, %0
3927         #
3928         ld%M1.q %m1, %0
3929         st%M0.q %m0, %1
3930         ptabs   %1, %0
3931         gettr   %1, %0
3932         pt      %1, %0"
3933   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media,ptabs_media,gettr_media,pt_media")
3934    (set_attr "length" "4,4,16,4,4,4,4,*")])
3935
3936 (define_split
3937   [(set (match_operand:DI 0 "arith_reg_operand" "")
3938         (match_operand:DI 1 "immediate_operand" ""))]
3939   "TARGET_SHMEDIA && reload_completed
3940    && MOVI_SHORI_BASE_OPERAND_P (operands[1])"
3941   [(set (match_dup 0) (match_dup 1))]
3942   "
3943 {
3944   rtx insn;
3945
3946   if (TARGET_SHMEDIA64)
3947     insn = emit_insn (gen_movdi_const (operands[0], operands[1]));
3948   else
3949     insn = emit_insn (gen_movdi_const_32bit (operands[0], operands[1]));
3950
3951   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, operands[1],
3952                                         REG_NOTES (insn));
3953
3954   DONE;
3955 }")
3956
3957 (define_expand "movdi_const"
3958   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
3959         (const:DI (sign_extend:DI
3960                    (truncate:HI
3961                     (ashiftrt:DI
3962                      (match_operand:DI 1 "immediate_operand" "s")
3963                      (const_int 48))))))
3964    (set (match_dup 0)
3965         (ior:DI (ashift:DI (match_dup 0) (const_int 16))
3966                 (zero_extend:DI
3967                  (truncate:HI
3968                   (const:DI
3969                    (sign_extend:DI
3970                     (truncate:HI
3971                      (ashiftrt:SI
3972                       (match_dup 1)
3973                       (const_int 32)))))))))
3974    (set (match_dup 0)
3975         (ior:DI (ashift:DI (match_dup 0) (const_int 16))
3976                 (zero_extend:DI
3977                  (truncate:HI
3978                   (const:DI
3979                    (sign_extend:DI
3980                     (truncate:HI
3981                      (ashiftrt:SI
3982                       (match_dup 1)
3983                       (const_int 16)))))))))
3984    (set (match_dup 0)
3985         (ior:DI (ashift:DI (match_dup 0) (const_int 16))
3986                 (zero_extend:DI
3987                  (truncate:HI
3988                   (const:DI
3989                    (sign_extend:DI
3990                     (truncate:HI
3991                      (match_dup 1))))))))]
3992   "TARGET_SHMEDIA64 && reload_completed
3993    && MOVI_SHORI_BASE_OPERAND_P (operands[1])"
3994   "
3995 {
3996   if (GET_CODE (operands[1]) == LABEL_REF
3997       && GET_CODE (XEXP (operands[1], 0)) == CODE_LABEL)
3998     LABEL_NUSES (XEXP (operands[1], 0)) += 4;
3999   else if (GOTOFF_P (operands[1])
4000            && GET_CODE (XVECEXP (XEXP (operands[1], 0), 0, 0)) == LABEL_REF
4001            && (GET_CODE (XEXP (XVECEXP (XEXP (operands[1], 0), 0, 0), 0))
4002                == CODE_LABEL))
4003     LABEL_NUSES (XEXP (XVECEXP (XEXP (operands[1], 0), 0, 0), 0)) += 4;
4004 }")
4005
4006 (define_expand "movdi_const_32bit"
4007   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
4008         (const:DI (sign_extend:DI
4009                    (truncate:HI
4010                     (ashiftrt:DI
4011                      (match_operand:DI 1 "immediate_operand" "s")
4012                      (const_int 16))))))
4013    (set (match_dup 0)
4014         (ior:DI (ashift:DI (match_dup 0) (const_int 16))
4015                 (zero_extend:DI
4016                  (truncate:HI
4017                   (const:DI
4018                    (sign_extend:DI
4019                     (truncate:HI
4020                      (match_dup 1))))))))]
4021   "TARGET_SHMEDIA32 && reload_completed
4022    && MOVI_SHORI_BASE_OPERAND_P (operands[1])"
4023   "
4024 {
4025   if (GET_CODE (operands[1]) == LABEL_REF
4026       && GET_CODE (XEXP (operands[1], 0)) == CODE_LABEL)
4027     LABEL_NUSES (XEXP (operands[1], 0)) += 2;
4028   else if (GOTOFF_P (operands[1])
4029            && GET_CODE (XVECEXP (XEXP (operands[1], 0), 0, 0)) == LABEL_REF
4030            && (GET_CODE (XEXP (XVECEXP (XEXP (operands[1], 0), 0, 0), 0))
4031                == CODE_LABEL))
4032     LABEL_NUSES (XEXP (XVECEXP (XEXP (operands[1], 0), 0, 0), 0)) += 2;
4033 }")
4034
4035 (define_expand "movdi_const_16bit"
4036   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
4037         (const:DI (sign_extend:DI
4038                    (truncate:HI
4039                     (match_operand:DI 1 "immediate_operand" "s")))))]
4040   "TARGET_SHMEDIA && flag_pic && reload_completed
4041    && GET_CODE (operands[1]) == SYMBOL_REF"
4042   "")
4043
4044 (define_split
4045   [(set (match_operand:DI 0 "arith_reg_operand" "")
4046         (match_operand:DI 1 "immediate_operand" ""))]
4047   "TARGET_SHMEDIA && reload_completed
4048    && GET_CODE (operands[1]) == CONST_INT
4049    && ! CONST_OK_FOR_J (INTVAL (operands[1]))"
4050   [(set (match_dup 0) (match_dup 2))
4051    (match_dup 1)]
4052   "
4053 {
4054   unsigned HOST_WIDE_INT val = INTVAL (operands[1]);
4055   unsigned HOST_WIDE_INT low = val;
4056   unsigned HOST_WIDE_INT high = val;
4057   unsigned HOST_WIDE_INT sign;
4058   unsigned HOST_WIDE_INT val2 = val ^ (val-1);
4059
4060   /* Sign-extend the 16 least-significant bits.  */
4061   low &= 0xffff;
4062   low ^= 0x8000;
4063   low -= 0x8000;
4064
4065   /* Arithmetic shift right the word by 16 bits.  */
4066   high >>= 16;
4067   sign = 1;
4068   sign <<= (HOST_BITS_PER_WIDE_INT - 16 - 1);
4069   high ^= sign;
4070   high -= sign;
4071   do
4072     {
4073       /* If we can't generate the constant with a two-insn movi / shori
4074          sequence, try some other strategies.  */
4075       if (! CONST_OK_FOR_J (high))
4076         {
4077           /* Try constant load / left shift.  We know VAL != 0.  */
4078           val2 = val ^ (val-1);
4079           if (val2 > 0x1ffff)
4080             {
4081               int trailing_zeroes = exact_log2 ((val2 >> 16) + 1) + 15;
4082
4083               if (CONST_OK_FOR_J (val >> trailing_zeroes)
4084                   || (! CONST_OK_FOR_J (high >> 16)
4085                       && CONST_OK_FOR_J (val >> (trailing_zeroes + 16))))
4086                 {
4087                   val2 = (HOST_WIDE_INT) val >> trailing_zeroes;
4088                   operands[1] = gen_ashldi3_media (operands[0], operands[0],
4089                                                    GEN_INT (trailing_zeroes));
4090                   break;
4091                 }
4092             }
4093           /* Try constant load / right shift.  */
4094           val2 = (val >> 15) + 1;
4095           if (val2 == (val2 & -val2))
4096             {
4097               int shift = 49 - exact_log2 (val2);
4098
4099               val2 = trunc_int_for_mode (val << shift, DImode);
4100               if (CONST_OK_FOR_J (val2))
4101                 {
4102                   operands[1] = gen_lshrdi3_media (operands[0], operands[0],
4103                                                    GEN_INT (shift));
4104                   break;
4105                 }
4106             }
4107           /* Try mperm.w .  */
4108           val2 = val & 0xffff;
4109           if ((val >> 16 & 0xffff) == val2
4110               && (val >> 32 & 0xffff) == val2
4111               && (val >> 48 & 0xffff) == val2)
4112             {
4113               val2 = (HOST_WIDE_INT) val >> 48;
4114               operands[1] = gen_rtx_REG (V4HImode, true_regnum (operands[0]));
4115               operands[1] = gen_mperm_w0 (operands[1], operands[1]);
4116               break;
4117             }
4118           /* Try movi / mshflo.l  */
4119           val2 = (HOST_WIDE_INT) val >> 32;
4120           if (val2 == trunc_int_for_mode (val, SImode))
4121             {
4122               operands[1] = gen_mshflo_l_di (operands[0], operands[0],
4123                                              operands[0]);
4124               break;
4125             }
4126           /* Try movi / mshflo.l w/ r63.  */
4127           val2 = val + ((HOST_WIDE_INT) -1 << 32);
4128           if ((HOST_WIDE_INT) val2 < 0 && CONST_OK_FOR_J (val2))
4129             {
4130               operands[1] = gen_mshflo_l_di (operands[0], operands[0],
4131                                              GEN_INT (0));
4132               break;
4133             }
4134         }
4135       val2 = high;
4136       operands[1] = gen_shori_media (operands[0], operands[0], GEN_INT (low));
4137     }
4138   while (0);
4139   operands[2] = GEN_INT (val2);
4140 }")
4141
4142 (define_split
4143   [(set (match_operand:DI 0 "arith_reg_operand" "")
4144         (match_operand:DI 1 "immediate_operand" ""))]
4145   "TARGET_SHMEDIA && reload_completed
4146    && GET_CODE (operands[1]) == CONST_DOUBLE"
4147   [(set (match_dup 0) (match_dup 2))
4148   (set (match_dup 0)
4149        (ior:DI (ashift:DI (match_dup 0) (const_int 16))
4150                (zero_extend:DI (truncate:HI (match_dup 1)))))]
4151   "
4152 {
4153   unsigned HOST_WIDE_INT low = CONST_DOUBLE_LOW (operands[1]);
4154   unsigned HOST_WIDE_INT high = CONST_DOUBLE_HIGH (operands[1]);
4155   unsigned HOST_WIDE_INT val = low;
4156   unsigned HOST_WIDE_INT sign;
4157
4158   /* Sign-extend the 16 least-significant bits.  */
4159   val &= 0xffff;
4160   val ^= 0x8000;
4161   val -= 0x8000;
4162   operands[1] = GEN_INT (val);
4163
4164   /* Arithmetic shift right the double-word by 16 bits.  */
4165   low >>= 16;
4166   low |= (high & 0xffff) << (HOST_BITS_PER_WIDE_INT - 16);
4167   high >>= 16;
4168   sign = 1;
4169   sign <<= (HOST_BITS_PER_WIDE_INT - 16 - 1);
4170   high ^= sign;
4171   high -= sign;
4172
4173   /* This will only be true if high is a sign-extension of low, i.e.,
4174      it must be either 0 or (unsigned)-1, and be zero iff the
4175      most-significant bit of low is set.  */
4176   if (high + (low >> (HOST_BITS_PER_WIDE_INT - 1)) == 0)
4177     operands[2] = GEN_INT (low);
4178   else
4179     operands[2] = immed_double_const (low, high, DImode);
4180 }")
4181
4182 (define_insn "shori_media"
4183   [(set (match_operand:DI 0 "arith_reg_operand" "=r,r")
4184         (ior:DI (ashift:DI (match_operand:DI 1 "arith_reg_operand" "0,0")
4185                            (const_int 16))
4186                 (zero_extend:DI
4187                  (truncate:HI
4188                   (match_operand:DI 2 "immediate_operand" "JS,nF")))))]
4189   "TARGET_SHMEDIA"
4190   "@
4191         shori   %u2, %0
4192         #"
4193   [(set_attr "type" "arith_media,*")])
4194
4195 (define_expand "movdi"
4196   [(set (match_operand:DI 0 "general_movdst_operand" "")
4197         (match_operand:DI 1 "general_movsrc_operand" ""))]
4198   ""
4199   "{ if (prepare_move_operands (operands, DImode)) DONE; }")
4200
4201 (define_insn "movdf_media"
4202   [(set (match_operand:DF 0 "general_movdst_operand" "=f,f,r,r,r,f,m,r,m")
4203         (match_operand:DF 1 "general_movsrc_operand" "f,rU,f,r,F,m,f,m,r"))]
4204   "TARGET_SHMEDIA_FPU
4205    && (register_operand (operands[0], DFmode)
4206        || register_operand (operands[1], DFmode))"
4207   "@
4208         fmov.d  %1, %0
4209         fmov.qd %N1, %0
4210         fmov.dq %1, %0
4211         add     %1, r63, %0
4212         #
4213         fld%M1.d        %m1, %0
4214         fst%M0.d        %m0, %1
4215         ld%M1.q %m1, %0
4216         st%M0.q %m0, %1"
4217   [(set_attr "type" "fmove_media,fload_media,dfpconv_media,arith_media,*,fload_media,fstore_media,load_media,store_media")])
4218
4219 (define_insn "movdf_media_nofpu"
4220   [(set (match_operand:DF 0 "general_movdst_operand" "=r,r,r,m")
4221         (match_operand:DF 1 "general_movsrc_operand" "r,F,m,r"))]
4222   "TARGET_SHMEDIA
4223    && (register_operand (operands[0], DFmode)
4224        || register_operand (operands[1], DFmode))"
4225   "@
4226         add     %1, r63, %0
4227         #
4228         ld%M1.q %m1, %0
4229         st%M0.q %m0, %1"
4230   [(set_attr "type" "arith_media,*,load_media,store_media")])
4231
4232 (define_split
4233   [(set (match_operand:DF 0 "arith_reg_operand" "")
4234         (match_operand:DF 1 "immediate_operand" ""))]
4235   "TARGET_SHMEDIA && reload_completed"
4236   [(set (match_dup 3) (match_dup 2))]
4237   "
4238 {
4239   int endian = WORDS_BIG_ENDIAN ? 1 : 0;
4240   long values[2];
4241   REAL_VALUE_TYPE value;
4242
4243   REAL_VALUE_FROM_CONST_DOUBLE (value, operands[1]);
4244   REAL_VALUE_TO_TARGET_DOUBLE (value, values);
4245
4246   if (HOST_BITS_PER_WIDE_INT >= 64)
4247     operands[2] = immed_double_const ((unsigned long) values[endian]
4248                                       | ((HOST_WIDE_INT) values[1 - endian]
4249                                          << 32), 0, DImode);
4250   else if (HOST_BITS_PER_WIDE_INT == 32)
4251     operands[2] = immed_double_const (values[endian], values[1 - endian],
4252                                       DImode);
4253   else
4254     abort ();
4255
4256   operands[3] = gen_rtx_REG (DImode, true_regnum (operands[0]));
4257 }")
4258
4259 ;; ??? This should be a define expand.
4260
4261 (define_insn "movdf_k"
4262   [(set (match_operand:DF 0 "general_movdst_operand" "=r,r,r,m")
4263         (match_operand:DF 1 "general_movsrc_operand" "r,FQ,m,r"))]
4264   "TARGET_SH1
4265    && (! TARGET_SH4 || reload_completed
4266        /* ??? We provide some insn so that direct_{load,store}[DFmode] get set */
4267        || (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 3)
4268        || (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 3))
4269    && (arith_reg_operand (operands[0], DFmode)
4270        || arith_reg_operand (operands[1], DFmode))"
4271   "* return output_movedouble (insn, operands, DFmode);"
4272   [(set_attr "length" "4")
4273    (set_attr "type" "move,pcload,load,store")])
4274
4275 ;; All alternatives of movdf_i4 are split for ! TARGET_FMOVD.
4276 ;; However, the d/F/c/z alternative cannot be split directly; it is converted
4277 ;; with special code in machine_dependent_reorg into a load of the R0_REG and
4278 ;; the d/m/c/X alternative, which is split later into single-precision
4279 ;; instructions.  And when not optimizing, no splits are done before fixing
4280 ;; up pcloads, so we need usable length information for that.
4281 (define_insn "movdf_i4"
4282   [(set (match_operand:DF 0 "general_movdst_operand" "=d,r,d,d,m,r,r,m,!??r,!???d")
4283         (match_operand:DF 1 "general_movsrc_operand" "d,r,F,m,d,FQ,m,r,d,r"))
4284    (use (match_operand:PSI 2 "fpscr_operand" "c,c,c,c,c,c,c,c,c,c"))
4285    (clobber (match_scratch:SI 3 "=X,X,&z,X,X,X,X,X,X,X"))]
4286   "TARGET_SH4
4287    && (arith_reg_operand (operands[0], DFmode)
4288        || arith_reg_operand (operands[1], DFmode))"
4289   "@
4290         fmov    %1,%0
4291         #
4292         #
4293         fmov.d  %1,%0
4294         fmov.d  %1,%0
4295         #
4296         #
4297         #
4298         #
4299         #"
4300   [(set_attr_alternative "length"
4301      [(if_then_else (eq_attr "fmovd" "yes") (const_int 2) (const_int 4))
4302       (const_int 4)
4303       (if_then_else (eq_attr "fmovd" "yes") (const_int 4) (const_int 6))
4304       (if_then_else (eq_attr "fmovd" "yes") (const_int 2) (const_int 6))
4305       (if_then_else (eq_attr "fmovd" "yes") (const_int 2) (const_int 6))
4306       (const_int 4)
4307       (const_int 8) (const_int 8) ;; these need only 8 bytes for @(r0,rn)
4308       ;; We can't use 4-byte push/pop on SHcompact, so we have to
4309       ;; increment or decrement r15 explicitly.
4310       (if_then_else
4311        (ne (symbol_ref "TARGET_SHCOMPACT") (const_int 0))
4312        (const_int 10) (const_int 8))
4313       (if_then_else
4314        (ne (symbol_ref "TARGET_SHCOMPACT") (const_int 0))
4315        (const_int 10) (const_int 8))])
4316    (set_attr "type" "fmove,move,pcload,load,store,pcload,load,store,load,load")
4317    (set (attr "fp_mode") (if_then_else (eq_attr "fmovd" "yes")
4318                                            (const_string "double")
4319                                            (const_string "none")))])
4320
4321 ;; Moving DFmode between fp/general registers through memory
4322 ;; (the top of the stack) is faster than moving through fpul even for
4323 ;; little endian.  Because the type of an instruction is important for its
4324 ;; scheduling,  it is beneficial to split these operations, rather than
4325 ;; emitting them in one single chunk, even if this will expose a stack
4326 ;; use that will prevent scheduling of other stack accesses beyond this
4327 ;; instruction.
4328 (define_split
4329   [(set (match_operand:DF 0 "register_operand" "")
4330         (match_operand:DF 1 "register_operand" ""))
4331    (use (match_operand:PSI 2 "fpscr_operand" ""))
4332    (clobber (match_scratch:SI 3 "=X"))]
4333   "TARGET_SH4 && reload_completed
4334    && (true_regnum (operands[0]) < 16) != (true_regnum (operands[1]) < 16)"
4335   [(const_int 0)]
4336   "
4337 {
4338   rtx insn, tos;
4339
4340   if (TARGET_SH5 && true_regnum (operands[1]) < 16)
4341     {
4342       emit_move_insn (stack_pointer_rtx,
4343                       plus_constant (stack_pointer_rtx, -8));
4344       tos = gen_rtx_MEM (DFmode, stack_pointer_rtx);
4345     }
4346   else
4347     tos = gen_rtx (MEM, DFmode, gen_rtx (PRE_DEC, Pmode, stack_pointer_rtx));
4348   insn = emit_insn (gen_movdf_i4 (tos, operands[1], operands[2]));
4349   if (! (TARGET_SH5 && true_regnum (operands[1]) < 16))
4350     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, stack_pointer_rtx, NULL_RTX);
4351   if (TARGET_SH5 && true_regnum (operands[0]) < 16)
4352     tos = gen_rtx_MEM (DFmode, stack_pointer_rtx);
4353   else
4354     tos = gen_rtx (MEM, DFmode, gen_rtx (POST_INC, Pmode, stack_pointer_rtx));
4355   insn = emit_insn (gen_movdf_i4 (operands[0], tos, operands[2]));
4356   if (TARGET_SH5 && true_regnum (operands[0]) < 16)
4357     emit_move_insn (stack_pointer_rtx, plus_constant (stack_pointer_rtx, 8));
4358   else
4359     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, stack_pointer_rtx, NULL_RTX);
4360   DONE;
4361 }")
4362
4363 ;; local-alloc sometimes allocates scratch registers even when not required,
4364 ;; so we must be prepared to handle these.
4365
4366 ;; Remove the use and clobber from a movdf_i4 so that we can use movdf_k.
4367 (define_split
4368   [(set (match_operand:DF 0 "general_movdst_operand" "")
4369         (match_operand:DF 1 "general_movsrc_operand"  ""))
4370    (use (match_operand:PSI 2 "fpscr_operand" ""))
4371    (clobber (match_scratch:SI 3 ""))]
4372   "TARGET_SH4
4373    && reload_completed
4374    && true_regnum (operands[0]) < 16
4375    && true_regnum (operands[1]) < 16"
4376   [(set (match_dup 0) (match_dup 1))]
4377   "
4378 {
4379   /* If this was a reg <-> mem operation with base + index reg addressing,
4380      we have to handle this in a special way.  */
4381   rtx mem = operands[0];
4382   int store_p = 1;
4383   if (! memory_operand (mem, DFmode))
4384     {
4385       mem = operands[1];
4386       store_p = 0;
4387     }
4388   if (GET_CODE (mem) == SUBREG && SUBREG_BYTE (mem) == 0)
4389     mem = SUBREG_REG (mem);
4390   if (GET_CODE (mem) == MEM)
4391     {
4392       rtx addr = XEXP (mem, 0);
4393       if (GET_CODE (addr) == PLUS
4394           && GET_CODE (XEXP (addr, 0)) == REG
4395           && GET_CODE (XEXP (addr, 1)) == REG)
4396         {
4397           int offset;
4398           rtx reg0 = gen_rtx (REG, Pmode, 0);
4399           rtx regop = operands[store_p], word0 ,word1;
4400
4401           if (GET_CODE (regop) == SUBREG)
4402             alter_subreg (&regop);
4403           if (REGNO (XEXP (addr, 0)) == REGNO (XEXP (addr, 1)))
4404             offset = 2;
4405           else
4406             offset = 4;
4407           mem = copy_rtx (mem);
4408           PUT_MODE (mem, SImode);
4409           word0 = gen_rtx (SUBREG, SImode, regop, 0);
4410           alter_subreg (&word0);
4411           word1 = gen_rtx (SUBREG, SImode, regop, 4);
4412           alter_subreg (&word1);
4413           if (store_p || ! refers_to_regno_p (REGNO (word0),
4414                                               REGNO (word0) + 1, addr, 0))
4415             {
4416               emit_insn (store_p
4417                          ? gen_movsi_ie (mem, word0)
4418                          : gen_movsi_ie (word0, mem));
4419               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (offset)));
4420               mem = copy_rtx (mem);
4421               emit_insn (store_p
4422                          ? gen_movsi_ie (mem, word1)
4423                          : gen_movsi_ie (word1, mem));
4424               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (-offset)));
4425             }
4426           else
4427             {
4428               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (offset)));
4429               emit_insn (gen_movsi_ie (word1, mem));
4430               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (-offset)));
4431               mem = copy_rtx (mem);
4432               emit_insn (gen_movsi_ie (word0, mem));
4433             }
4434           DONE;
4435         }
4436     }
4437 }")
4438
4439 ;; Split away the clobber of r0 after machine_dependent_reorg has fixed pcloads.
4440 (define_split
4441   [(set (match_operand:DF 0 "register_operand" "")
4442         (match_operand:DF 1 "memory_operand"  ""))
4443    (use (match_operand:PSI 2 "fpscr_operand" ""))
4444    (clobber (reg:SI R0_REG))]
4445   "TARGET_SH4 && reload_completed"
4446   [(parallel [(set (match_dup 0) (match_dup 1))
4447               (use (match_dup 2))
4448               (clobber (scratch:SI))])]
4449   "")
4450
4451 (define_expand "reload_indf"
4452   [(parallel [(set (match_operand:DF 0 "register_operand" "=f")
4453                    (match_operand:DF 1 "immediate_operand" "FQ"))
4454               (use (reg:PSI FPSCR_REG))
4455               (clobber (match_operand:SI 2 "register_operand" "=&z"))])]
4456   "TARGET_SH1"
4457   "")
4458
4459 (define_expand "reload_outdf"
4460   [(parallel [(set (match_operand:DF 0 "register_operand" "=r,f")
4461                    (match_operand:DF 1 "register_operand" "af,r"))
4462               (clobber (match_operand:SI 2 "register_operand" "=&y,y"))])]
4463   "TARGET_SH1"
4464   "")
4465
4466 ;; Simplify no-op moves.
4467 (define_split
4468   [(set (match_operand:SF 0 "register_operand" "")
4469         (match_operand:SF 1 "register_operand" ""))
4470    (use (match_operand:PSI 2 "fpscr_operand" ""))
4471    (clobber (match_scratch:SI 3 "X"))]
4472   "TARGET_SH3E && reload_completed
4473    && true_regnum (operands[0]) == true_regnum (operands[1])"
4474   [(set (match_dup 0) (match_dup 0))]
4475   "")
4476
4477 ;; fmovd substitute post-reload splits
4478 (define_split
4479   [(set (match_operand:DF 0 "register_operand" "")
4480         (match_operand:DF 1 "register_operand" ""))
4481    (use (match_operand:PSI 2 "fpscr_operand" ""))
4482    (clobber (match_scratch:SI 3 "X"))]
4483   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
4484    && FP_OR_XD_REGISTER_P (true_regnum (operands[0]))
4485    && FP_OR_XD_REGISTER_P (true_regnum (operands[1]))"
4486   [(const_int 0)]
4487   "
4488 {
4489   int dst = true_regnum (operands[0]), src = true_regnum (operands[1]);
4490   emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode, dst),
4491                            gen_rtx (REG, SFmode, src), operands[2]));
4492   emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode, dst + 1),
4493                            gen_rtx (REG, SFmode, src + 1), operands[2]));
4494   DONE;
4495 }")
4496
4497 (define_split
4498   [(set (match_operand:DF 0 "register_operand" "")
4499         (mem:DF (match_operand:SI 1 "register_operand" "")))
4500    (use (match_operand:PSI 2 "fpscr_operand" ""))
4501    (clobber (match_scratch:SI 3 ""))]
4502   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
4503    && FP_OR_XD_REGISTER_P (true_regnum (operands[0]))
4504    && find_regno_note (insn, REG_DEAD, true_regnum (operands[1]))"
4505   [(const_int 0)]
4506   "
4507 {
4508   int regno = true_regnum (operands[0]);
4509   rtx insn;
4510   rtx mem2 = gen_rtx (MEM, SFmode, gen_rtx (POST_INC, Pmode, operands[1]));
4511
4512   insn = emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode,
4513                                            regno + !! TARGET_LITTLE_ENDIAN),
4514                                   mem2, operands[2]));
4515   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, operands[1], NULL_RTX);
4516   insn = emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode,
4517                                            regno + ! TARGET_LITTLE_ENDIAN),
4518                                   gen_rtx (MEM, SFmode, operands[1]),
4519                                   operands[2]));
4520   DONE;
4521 }")
4522
4523 (define_split
4524   [(set (match_operand:DF 0 "register_operand" "")
4525         (match_operand:DF 1 "memory_operand" ""))
4526    (use (match_operand:PSI 2 "fpscr_operand" ""))
4527    (clobber (match_scratch:SI 3 ""))]
4528   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
4529    && FP_OR_XD_REGISTER_P (true_regnum (operands[0]))"
4530   [(const_int 0)]
4531   "
4532 {
4533   int regno = true_regnum (operands[0]);
4534   rtx addr, insn, adjust = NULL_RTX;
4535   rtx mem2 = copy_rtx (operands[1]);
4536   rtx reg0 = gen_rtx_REG (SFmode, regno + !! TARGET_LITTLE_ENDIAN);
4537   rtx reg1 = gen_rtx_REG (SFmode, regno + ! TARGET_LITTLE_ENDIAN);
4538
4539   PUT_MODE (mem2, SFmode);
4540   operands[1] = copy_rtx (mem2);
4541   addr = XEXP (mem2, 0);
4542   if (GET_CODE (addr) != POST_INC)
4543     {
4544       /* If we have to modify the stack pointer, the value that we have
4545          read with post-increment might be modified by an interrupt,
4546          so write it back.  */
4547       if (REGNO (addr) == STACK_POINTER_REGNUM)
4548         adjust = gen_push_e (reg0);
4549       else
4550         adjust = gen_addsi3 (addr, addr, GEN_INT (-4));
4551       XEXP (mem2, 0) = addr = gen_rtx_POST_INC (SImode, addr);
4552     }
4553   addr = XEXP (addr, 0);
4554   insn = emit_insn (gen_movsf_ie (reg0, mem2, operands[2]));
4555   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, addr, NULL_RTX);
4556   insn = emit_insn (gen_movsf_ie (reg1, operands[1], operands[2]));
4557   if (adjust)
4558     emit_insn (adjust);
4559   else
4560     REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, addr, NULL_RTX);
4561   DONE;
4562 }")
4563
4564 (define_split
4565   [(set (match_operand:DF 0 "memory_operand" "")
4566         (match_operand:DF 1 "register_operand" ""))
4567    (use (match_operand:PSI 2 "fpscr_operand" ""))
4568    (clobber (match_scratch:SI 3 ""))]
4569   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
4570    && FP_OR_XD_REGISTER_P (true_regnum (operands[1]))"
4571   [(const_int 0)]
4572   "
4573 {
4574   int regno = true_regnum (operands[1]);
4575   rtx insn, addr, adjust = NULL_RTX;
4576
4577   operands[0] = copy_rtx (operands[0]);
4578   PUT_MODE (operands[0], SFmode);
4579   insn = emit_insn (gen_movsf_ie (operands[0],
4580                                   gen_rtx (REG, SFmode,
4581                                            regno + ! TARGET_LITTLE_ENDIAN),
4582                                   operands[2]));
4583   operands[0] = copy_rtx (operands[0]);
4584   addr = XEXP (operands[0], 0);
4585   if (GET_CODE (addr) != PRE_DEC)
4586     {
4587       adjust = gen_addsi3 (addr, addr, GEN_INT (4));
4588       emit_insn_before (adjust, insn);
4589       XEXP (operands[0], 0) = addr = gen_rtx (PRE_DEC, SImode, addr);
4590     }
4591   addr = XEXP (addr, 0);
4592   if (! adjust)
4593     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, addr, NULL_RTX);
4594   insn = emit_insn (gen_movsf_ie (operands[0],
4595                                   gen_rtx (REG, SFmode,
4596                                            regno + !! TARGET_LITTLE_ENDIAN),
4597                                   operands[2]));
4598   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, addr, NULL_RTX);
4599   DONE;
4600 }")
4601
4602 ;; If the output is a register and the input is memory or a register, we have
4603 ;; to be careful and see which word needs to be loaded first.
4604
4605 (define_split
4606   [(set (match_operand:DF 0 "general_movdst_operand" "")
4607         (match_operand:DF 1 "general_movsrc_operand" ""))]
4608   "TARGET_SH1 && reload_completed"
4609   [(set (match_dup 2) (match_dup 3))
4610    (set (match_dup 4) (match_dup 5))]
4611   "
4612 {
4613   int regno;
4614
4615   if ((GET_CODE (operands[0]) == MEM
4616        && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
4617       || (GET_CODE (operands[1]) == MEM
4618           && GET_CODE (XEXP (operands[1], 0)) == POST_INC))
4619     FAIL;
4620
4621   if (GET_CODE (operands[0]) == REG)
4622     regno = REGNO (operands[0]);
4623   else if (GET_CODE (operands[0]) == SUBREG)
4624     regno = subreg_regno (operands[0]);
4625   else if (GET_CODE (operands[0]) == MEM)
4626     regno = -1;
4627   else
4628     abort ();
4629
4630   if (regno == -1
4631       || ! refers_to_regno_p (regno, regno + 1, operands[1], 0))
4632     {
4633       operands[2] = operand_subword (operands[0], 0, 0, DFmode);
4634       operands[3] = operand_subword (operands[1], 0, 0, DFmode);
4635       operands[4] = operand_subword (operands[0], 1, 0, DFmode);
4636       operands[5] = operand_subword (operands[1], 1, 0, DFmode);
4637     }
4638   else
4639     {
4640       operands[2] = operand_subword (operands[0], 1, 0, DFmode);
4641       operands[3] = operand_subword (operands[1], 1, 0, DFmode);
4642       operands[4] = operand_subword (operands[0], 0, 0, DFmode);
4643       operands[5] = operand_subword (operands[1], 0, 0, DFmode);
4644     }
4645
4646   if (operands[2] == 0 || operands[3] == 0
4647       || operands[4] == 0 || operands[5] == 0)
4648     FAIL;
4649 }")
4650
4651 ;; If a base address generated by LEGITIMIZE_ADDRESS for SImode is
4652 ;; used only once, let combine add in the index again.
4653
4654 (define_split
4655   [(set (match_operand:SI 0 "register_operand" "")
4656         (match_operand:SI 1 "" ""))
4657    (clobber (match_operand 2 "register_operand" ""))]
4658   "TARGET_SH1 && ! reload_in_progress && ! reload_completed"
4659   [(use (reg:SI R0_REG))]
4660   "
4661 {
4662   rtx addr, reg, const_int;
4663
4664   if (GET_CODE (operands[1]) != MEM)
4665     FAIL;
4666   addr = XEXP (operands[1], 0);
4667   if (GET_CODE (addr) != PLUS)
4668     FAIL;
4669   reg = XEXP (addr, 0);
4670   const_int = XEXP (addr, 1);
4671   if (! (BASE_REGISTER_RTX_P (reg) && INDEX_REGISTER_RTX_P (operands[2])
4672          && GET_CODE (const_int) == CONST_INT))
4673     FAIL;
4674   emit_move_insn (operands[2], const_int);
4675   emit_move_insn (operands[0],
4676                   change_address (operands[1], VOIDmode,
4677                                   gen_rtx_PLUS (SImode, reg, operands[2])));
4678   DONE;
4679 }")
4680
4681 (define_split
4682   [(set (match_operand:SI 1 "" "")
4683         (match_operand:SI 0 "register_operand" ""))
4684    (clobber (match_operand 2 "register_operand" ""))]
4685   "TARGET_SH1 && ! reload_in_progress && ! reload_completed"
4686   [(use (reg:SI R0_REG))]
4687   "
4688 {
4689   rtx addr, reg, const_int;
4690
4691   if (GET_CODE (operands[1]) != MEM)
4692     FAIL;
4693   addr = XEXP (operands[1], 0);
4694   if (GET_CODE (addr) != PLUS)
4695     FAIL;
4696   reg = XEXP (addr, 0);
4697   const_int = XEXP (addr, 1);
4698   if (! (BASE_REGISTER_RTX_P (reg) && INDEX_REGISTER_RTX_P (operands[2])
4699          && GET_CODE (const_int) == CONST_INT))
4700     FAIL;
4701   emit_move_insn (operands[2], const_int);
4702   emit_move_insn (change_address (operands[1], VOIDmode,
4703                                   gen_rtx_PLUS (SImode, reg, operands[2])),
4704                   operands[0]);
4705   DONE;
4706 }")
4707
4708 (define_expand "movdf"
4709   [(set (match_operand:DF 0 "general_movdst_operand" "")
4710         (match_operand:DF 1 "general_movsrc_operand" ""))]
4711   ""
4712   "
4713 {
4714   if (prepare_move_operands (operands, DFmode)) DONE;
4715   if (TARGET_SHMEDIA)
4716     {
4717       if (TARGET_SHMEDIA_FPU)
4718         emit_insn (gen_movdf_media (operands[0], operands[1]));
4719       else
4720         emit_insn (gen_movdf_media_nofpu (operands[0], operands[1]));
4721       DONE;
4722     }
4723   if (TARGET_SH4)
4724     {
4725       emit_df_insn (gen_movdf_i4 (operands[0], operands[1], get_fpscr_rtx ()));
4726       DONE;
4727     }
4728 }")
4729
4730 ;;This is incompatible with the way gcc uses subregs.
4731 ;;(define_insn "movv2sf_i"
4732 ;;  [(set (match_operand:V2SF 0 "nonimmediate_operand" "=f,f,m")
4733 ;;      (match_operand:V2SF 1 "nonimmediate_operand" "f,m,f"))]
4734 ;;  "TARGET_SHMEDIA_FPU
4735 ;;   && (fp_arith_reg_operand (operands[0], V2SFmode)
4736 ;;       || fp_arith_reg_operand (operands[1], V2SFmode))"
4737 ;;  "@
4738 ;;      #
4739 ;;      fld%M1.p        %m1, %0
4740 ;;      fst%M0.p        %m0, %1"
4741 ;;  [(set_attr "type" "*,fload_media,fstore_media")])
4742
4743 (define_insn_and_split "movv2sf_i"
4744   [(set (match_operand:V2SF 0 "general_movdst_operand" "=f,rf,r,m,mf")
4745         (match_operand:V2SF 1 "general_operand" "fm,rfm?,F?,f,rfU?"))]
4746   "TARGET_SHMEDIA_FPU"
4747   "#"
4748   "TARGET_SHMEDIA_FPU && reload_completed"
4749   [(set (match_dup 0) (match_dup 1))]
4750   "
4751 {
4752   operands[0] = simplify_gen_subreg (DFmode, operands[0], V2SFmode, 0);
4753   operands[1] = simplify_gen_subreg (DFmode, operands[1], V2SFmode, 0);
4754 }")
4755
4756 (define_expand "movv2sf"
4757   [(set (match_operand:V2SF 0 "general_movdst_operand" "")
4758         (match_operand:V2SF 1 "nonimmediate_operand" ""))]
4759   "TARGET_SHMEDIA_FPU"
4760   "
4761 {
4762   if (prepare_move_operands (operands, V2SFmode))
4763     DONE;
4764 }")
4765
4766 (define_expand "addv2sf3"
4767   [(match_operand:V2SF 0 "fp_arith_reg_operand" "")
4768    (match_operand:V2SF 1 "fp_arith_reg_operand" "")
4769    (match_operand:V2SF 2 "fp_arith_reg_operand" "")]
4770   "TARGET_SHMEDIA_FPU"
4771   "
4772 {
4773   sh_expand_binop_v2sf (PLUS, operands[0], operands[1], operands[2]);
4774   DONE;
4775 }")
4776
4777 (define_expand "subv2sf3"
4778   [(match_operand:V2SF 0 "fp_arith_reg_operand" "")
4779    (match_operand:V2SF 1 "fp_arith_reg_operand" "")
4780    (match_operand:V2SF 2 "fp_arith_reg_operand" "")]
4781   "TARGET_SHMEDIA_FPU"
4782   "
4783 {
4784   sh_expand_binop_v2sf (MINUS, operands[0], operands[1], operands[2]);
4785   DONE;
4786 }")
4787
4788 (define_expand "mulv2sf3"
4789   [(match_operand:V2SF 0 "fp_arith_reg_operand" "")
4790    (match_operand:V2SF 1 "fp_arith_reg_operand" "")
4791    (match_operand:V2SF 2 "fp_arith_reg_operand" "")]
4792   "TARGET_SHMEDIA_FPU"
4793   "
4794 {
4795   sh_expand_binop_v2sf (MULT, operands[0], operands[1], operands[2]);
4796   DONE;
4797 }")
4798
4799 (define_expand "divv2sf3"
4800   [(match_operand:V2SF 0 "fp_arith_reg_operand" "")
4801    (match_operand:V2SF 1 "fp_arith_reg_operand" "")
4802    (match_operand:V2SF 2 "fp_arith_reg_operand" "")]
4803   "TARGET_SHMEDIA_FPU"
4804   "
4805 {
4806   sh_expand_binop_v2sf (DIV, operands[0], operands[1], operands[2]);
4807   DONE;
4808 }")
4809
4810 (define_insn_and_split "*movv4sf_i"
4811   [(set (match_operand:V4SF 0 "nonimmediate_operand" "=f,f,m")
4812         (match_operand:V4SF 1 "general_operand" "fU,m,f"))]
4813   "TARGET_SHMEDIA_FPU"
4814   "#"
4815   "&& reload_completed"
4816   [(const_int 0)]
4817   "
4818 {
4819   int i;
4820
4821   for (i = 0; i < 4/2; i++)
4822     {
4823       rtx x, y;
4824
4825       if (GET_CODE (operands[0]) == MEM)
4826         x = gen_rtx_MEM (V2SFmode,
4827                          plus_constant (XEXP (operands[0], 0),
4828                                         i * GET_MODE_SIZE (V2SFmode)));
4829       else
4830         x = simplify_gen_subreg (V2SFmode, operands[0], V4SFmode, i * 8);
4831
4832       if (GET_CODE (operands[1]) == MEM)
4833         y = gen_rtx_MEM (V2SFmode,
4834                          plus_constant (XEXP (operands[1], 0),
4835                                         i * GET_MODE_SIZE (V2SFmode)));
4836       else
4837         y = simplify_gen_subreg (V2SFmode, operands[1], V4SFmode, i * 8);
4838
4839       emit_insn (gen_movv2sf_i (x, y));
4840     }
4841
4842   DONE;
4843 }"
4844   [(set_attr "length" "8")])
4845
4846 (define_expand "movv4sf"
4847   [(set (match_operand:V4SF 0 "nonimmediate_operand" "")
4848         (match_operand:V4SF 1 "general_operand" ""))]
4849   "TARGET_SHMEDIA_FPU"
4850   "
4851 {
4852   if (prepare_move_operands (operands, V4SFmode))
4853     DONE;
4854 }")
4855
4856 (define_insn_and_split "*movv16sf_i"
4857   [(set (match_operand:V16SF 0 "nonimmediate_operand" "=f,f,m")
4858         (match_operand:V16SF 1 "nonimmediate_operand" "f,m,f"))]
4859   "TARGET_SHMEDIA_FPU"
4860   "#"
4861   "&& reload_completed"
4862   [(const_int 0)]
4863   "
4864 {
4865   int i;
4866
4867   for (i = 0; i < 16/2; i++)
4868     {
4869       rtx x,y;
4870
4871       if (GET_CODE (operands[0]) == MEM)
4872         x = gen_rtx_MEM (V2SFmode,
4873                          plus_constant (XEXP (operands[0], 0),
4874                                         i * GET_MODE_SIZE (V2SFmode)));
4875       else
4876         {
4877           x = gen_rtx_SUBREG (V2SFmode, operands[0], i * 2);
4878           alter_subreg (&x);
4879         }
4880
4881       if (GET_CODE (operands[1]) == MEM)
4882         y = gen_rtx_MEM (V2SFmode,
4883                          plus_constant (XEXP (operands[1], 0),
4884                                         i * GET_MODE_SIZE (V2SFmode)));
4885       else
4886         {
4887           y = gen_rtx_SUBREG (V2SFmode, operands[1], i * 2);
4888           alter_subreg (&y);
4889         }
4890
4891       emit_insn (gen_movv2sf_i (x, y));
4892     }
4893
4894   DONE;
4895 }"
4896   [(set_attr "length" "32")])
4897
4898 (define_expand "movv16sf"
4899   [(set (match_operand:V16SF 0 "nonimmediate_operand" "=f,f,m")
4900         (match_operand:V16SF 1 "nonimmediate_operand" "f,m,f"))]
4901   "TARGET_SHMEDIA_FPU"
4902   "
4903 {
4904   if (prepare_move_operands (operands, V16SFmode))
4905     DONE;
4906 }")
4907
4908 (define_insn "movsf_media"
4909   [(set (match_operand:SF 0 "general_movdst_operand" "=f,f,r,r,r,f,m,r,m")
4910         (match_operand:SF 1 "general_movsrc_operand" "f,rU,f,r,F,m,f,m,r"))]
4911   "TARGET_SHMEDIA_FPU
4912    && (register_operand (operands[0], SFmode)
4913        || register_operand (operands[1], SFmode))"
4914   "@
4915         fmov.s  %1, %0
4916         fmov.ls %N1, %0
4917         fmov.sl %1, %0
4918         add.l   %1, r63, %0
4919         #
4920         fld%M1.s        %m1, %0
4921         fst%M0.s        %m0, %1
4922         ld%M1.l %m1, %0
4923         st%M0.l %m0, %1"
4924   [(set_attr "type" "fmove_media,fload_media,fpconv_media,arith_media,*,fload_media,fstore_media,load_media,store_media")])
4925
4926 (define_insn "movsf_media_nofpu"
4927   [(set (match_operand:SF 0 "general_movdst_operand" "=r,r,r,m")
4928         (match_operand:SF 1 "general_movsrc_operand" "r,F,m,r"))]
4929   "TARGET_SHMEDIA
4930    && (register_operand (operands[0], SFmode)
4931        || register_operand (operands[1], SFmode))"
4932   "@
4933         add.l   %1, r63, %0
4934         #
4935         ld%M1.l %m1, %0
4936         st%M0.l %m0, %1"
4937   [(set_attr "type" "arith_media,*,load_media,store_media")])
4938
4939 (define_split
4940   [(set (match_operand:SF 0 "arith_reg_operand" "")
4941         (match_operand:SF 1 "immediate_operand" ""))]
4942   "TARGET_SHMEDIA && reload_completed
4943    && ! FP_REGISTER_P (true_regnum (operands[0]))"
4944   [(set (match_dup 3) (match_dup 2))]
4945   "
4946 {
4947   long values;
4948   REAL_VALUE_TYPE value;
4949
4950   REAL_VALUE_FROM_CONST_DOUBLE (value, operands[1]);
4951   REAL_VALUE_TO_TARGET_SINGLE (value, values);
4952   operands[2] = GEN_INT (values);
4953
4954   operands[3] = gen_rtx_REG (DImode, true_regnum (operands[0]));
4955 }")
4956
4957 (define_insn "movsf_i"
4958   [(set (match_operand:SF 0 "general_movdst_operand" "=r,r,r,r,m,l,r")
4959         (match_operand:SF 1 "general_movsrc_operand"  "r,I,FQ,mr,r,r,l"))]
4960   "TARGET_SH1
4961    && (! TARGET_SH3E
4962        /* ??? We provide some insn so that direct_{load,store}[SFmode] get set */
4963        || (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 3)
4964        || (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 3))
4965    && (arith_reg_operand (operands[0], SFmode)
4966        || arith_reg_operand (operands[1], SFmode))"
4967   "@
4968         mov     %1,%0
4969         mov     %1,%0
4970         mov.l   %1,%0
4971         mov.l   %1,%0
4972         mov.l   %1,%0
4973         lds     %1,%0
4974         sts     %1,%0"
4975   [(set_attr "type" "move,move,pcload,load,store,move,move")])
4976
4977 ;; We may not split the ry/yr/XX alternatives to movsi_ie, since
4978 ;; update_flow_info would not know where to put REG_EQUAL notes
4979 ;; when the destination changes mode.
4980 (define_insn "movsf_ie"
4981   [(set (match_operand:SF 0 "general_movdst_operand"
4982          "=f,r,f,f,fy,f,m,r,r,m,f,y,y,rf,r,y,<,y,y")
4983         (match_operand:SF 1 "general_movsrc_operand"
4984           "f,r,G,H,FQ,mf,f,FQ,mr,r,y,f,>,fr,y,r,y,>,y"))
4985    (use (match_operand:PSI 2 "fpscr_operand" "c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c,c"))
4986    (clobber (match_scratch:SI 3 "=X,X,X,X,&z,X,X,X,X,X,X,X,X,y,X,X,X,X,X"))]
4987
4988   "TARGET_SH3E
4989    && (arith_reg_operand (operands[0], SFmode)
4990        || arith_reg_operand (operands[1], SFmode)
4991        || arith_reg_operand (operands[3], SImode)
4992        || (fpul_operand (operands[0], SFmode)
4993            && memory_operand (operands[1], SFmode)
4994            && GET_CODE (XEXP (operands[1], 0)) == POST_INC)
4995        || (fpul_operand (operands[1], SFmode)
4996            && memory_operand (operands[0], SFmode)
4997            && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC))"
4998   "@
4999         fmov    %1,%0
5000         mov     %1,%0
5001         fldi0   %0
5002         fldi1   %0
5003         #
5004         fmov.s  %1,%0
5005         fmov.s  %1,%0
5006         mov.l   %1,%0
5007         mov.l   %1,%0
5008         mov.l   %1,%0
5009         fsts    fpul,%0
5010         flds    %1,fpul
5011         lds.l   %1,%0
5012         #
5013         sts     %1,%0
5014         lds     %1,%0
5015         sts.l   %1,%0
5016         lds.l   %1,%0
5017         ! move optimized away"
5018   [(set_attr "type" "fmove,move,fmove,fmove,pcload,load,store,pcload,load,store,fmove,fmove,load,*,gp_fpul,gp_fpul,store,load,nil")
5019    (set_attr "length" "*,*,*,*,4,*,*,*,*,*,2,2,2,4,2,2,2,2,0")
5020    (set (attr "fp_mode") (if_then_else (eq_attr "fmovd" "yes")
5021                                            (const_string "single")
5022                                            (const_string "none")))])
5023
5024 (define_split
5025   [(set (match_operand:SF 0 "register_operand" "")
5026         (match_operand:SF 1 "register_operand" ""))
5027    (use (match_operand:PSI 2 "fpscr_operand" ""))
5028    (clobber (reg:SI FPUL_REG))]
5029   "TARGET_SH1"
5030   [(parallel [(set (reg:SF FPUL_REG) (match_dup 1))
5031               (use (match_dup 2))
5032               (clobber (scratch:SI))])
5033    (parallel [(set (match_dup 0) (reg:SF FPUL_REG))
5034               (use (match_dup 2))
5035               (clobber (scratch:SI))])]
5036   "")
5037
5038 (define_expand "movsf"
5039   [(set (match_operand:SF 0 "general_movdst_operand" "")
5040         (match_operand:SF 1 "general_movsrc_operand" ""))]
5041   ""
5042   "
5043 {
5044   if (prepare_move_operands (operands, SFmode))
5045     DONE;
5046   if (TARGET_SHMEDIA)
5047     {
5048       if (TARGET_SHMEDIA_FPU)
5049         emit_insn (gen_movsf_media (operands[0], operands[1]));
5050       else
5051         emit_insn (gen_movsf_media_nofpu (operands[0], operands[1]));
5052       DONE;
5053     }
5054   if (TARGET_SH3E)
5055     {
5056       emit_sf_insn (gen_movsf_ie (operands[0], operands[1], get_fpscr_rtx ()));
5057       DONE;
5058     }
5059 }")
5060
5061 (define_insn "mov_nop"
5062   [(set (match_operand 0 "any_register_operand" "") (match_dup 0))]
5063   "TARGET_SH3E"
5064   ""
5065   [(set_attr "length" "0")
5066    (set_attr "type" "nil")])
5067
5068 (define_expand "reload_insf"
5069   [(parallel [(set (match_operand:SF 0 "register_operand" "=a")
5070                    (match_operand:SF 1 "immediate_operand" "FQ"))
5071               (use (reg:PSI FPSCR_REG))
5072               (clobber (match_operand:SI 2 "register_operand" "=&z"))])]
5073   "TARGET_SH1"
5074   "")
5075
5076 (define_expand "reload_insi"
5077   [(parallel [(set (match_operand:SF 0 "register_operand" "=y")
5078                    (match_operand:SF 1 "immediate_operand" "FQ"))
5079               (clobber (match_operand:SI 2 "register_operand" "=&z"))])]
5080   "TARGET_SH1"
5081   "")
5082
5083 (define_insn "*movsi_y"
5084   [(set (match_operand:SI 0 "register_operand" "=y,y")
5085         (match_operand:SI 1 "immediate_operand" "Qi,I"))
5086    (clobber (match_scratch:SI 2 "=&z,r"))]
5087   "TARGET_SH3E
5088    && (reload_in_progress || reload_completed)"
5089   "#"
5090   [(set_attr "length" "4")
5091    (set_attr "type" "pcload,move")])
5092
5093 (define_split
5094   [(set (match_operand:SI 0 "register_operand" "")
5095         (match_operand:SI 1 "immediate_operand" ""))
5096    (clobber (match_operand:SI 2 "register_operand" ""))]
5097   "TARGET_SH1"
5098   [(set (match_dup 2) (match_dup 1))
5099    (set (match_dup 0) (match_dup 2))]
5100   "")
5101
5102 (define_split
5103   [(set (match_operand:SI 0 "register_operand" "")
5104         (match_operand:SI 1 "memory_operand" ""))
5105    (clobber (reg:SI R0_REG))]
5106   "TARGET_SH1"
5107   [(set (match_dup 0) (match_dup 1))]
5108   "")
5109 \f
5110 ;; ------------------------------------------------------------------------
5111 ;; Define the real conditional branch instructions.
5112 ;; ------------------------------------------------------------------------
5113
5114 (define_insn "branch_true"
5115   [(set (pc) (if_then_else (ne (reg:SI T_REG) (const_int 0))
5116                            (label_ref (match_operand 0 "" ""))
5117                            (pc)))]
5118   "TARGET_SH1"
5119   "* return output_branch (1, insn, operands);"
5120   [(set_attr "type" "cbranch")])
5121
5122 (define_insn "branch_false"
5123   [(set (pc) (if_then_else (eq (reg:SI T_REG) (const_int 0))
5124                            (label_ref (match_operand 0 "" ""))
5125                            (pc)))]
5126   "TARGET_SH1"
5127   "* return output_branch (0, insn, operands);"
5128   [(set_attr "type" "cbranch")])
5129
5130 ;; Patterns to prevent reorg from re-combining a condbranch with a branch
5131 ;; which destination is too far away.
5132 ;; The const_int_operand is distinct for each branch target; it avoids
5133 ;; unwanted matches with redundant_insn.
5134 (define_insn "block_branch_redirect"
5135   [(set (pc) (unspec [(match_operand 0 "const_int_operand" "")] UNSPEC_BBR))]
5136   "TARGET_SH1"
5137   ""
5138   [(set_attr "length" "0")])
5139
5140 ;; This one has the additional purpose to record a possible scratch register
5141 ;; for the following branch.
5142 (define_insn "indirect_jump_scratch"
5143   [(set (match_operand:SI 0 "register_operand" "=r")
5144         (unspec:SI [(match_operand 1 "const_int_operand" "")] UNSPEC_BBR))]
5145   "TARGET_SH1"
5146   ""
5147   [(set_attr "length" "0")])
5148
5149 ;; This one is used to preemt an insn from beyond the bra / braf / jmp
5150 ;; being pulled into the delay slot of a condbranch that has been made to
5151 ;; jump around the unconditional jump because it was out of range.
5152 (define_insn "stuff_delay_slot"
5153   [(set (pc)
5154         (unspec [(match_operand 0 "const_int_operand" "") (pc)] UNSPEC_BBR))
5155    (set (reg:SI T_REG) (match_operand 1 "const_int_operand" ""))]
5156   "TARGET_SH1"
5157   ""
5158   [(set_attr "length" "0")
5159    (set_attr "cond_delay_slot" "yes")])
5160 \f
5161 ;; Conditional branch insns
5162
5163 (define_expand "beq_media"
5164   [(set (pc)
5165         (if_then_else (eq (match_operand:DI 1 "arith_reg_operand" "r,r")
5166                           (match_operand:DI 2 "arith_operand" "r,O"))
5167                       (label_ref:DI (match_operand 0 "" ""))
5168                       (pc)))]
5169   "TARGET_SHMEDIA"
5170   "")
5171
5172 (define_insn "*beq_media_i"
5173   [(set (pc)
5174         (if_then_else (match_operator 3 "equality_comparison_operator"
5175                         [(match_operand:DI 1 "arith_reg_operand" "r,r")
5176                          (match_operand:DI 2 "arith_operand" "r,O")])
5177                       (match_operand:DI 0 "target_operand" "b,b")
5178                       (pc)))]
5179   "TARGET_SHMEDIA"
5180   "@
5181         b%o3%'  %1, %2, %0
5182         b%o3i%' %1, %2, %0"
5183   [(set_attr "type" "cbranch_media")])
5184
5185 (define_expand "bne_media"
5186   [(set (pc)
5187         (if_then_else (ne (match_operand:DI 1 "arith_reg_operand" "r,r")
5188                           (match_operand:DI 2 "arith_operand" "r,O"))
5189                       (label_ref:DI (match_operand 0 "" ""))
5190                       (pc)))]
5191   "TARGET_SHMEDIA"
5192   "")
5193
5194 (define_expand "bgt_media"
5195   [(set (pc)
5196         (if_then_else (gt (match_operand:DI 1 "arith_reg_or_0_operand" "r")
5197                           (match_operand:DI 2 "arith_reg_or_0_operand" "r"))
5198                       (label_ref:DI (match_operand 0 "" ""))
5199                       (pc)))]
5200   "TARGET_SHMEDIA"
5201   "")
5202
5203 (define_expand "bge_media"
5204   [(set (pc)
5205         (if_then_else (ge (match_operand:DI 1 "arith_reg_or_0_operand" "r")
5206                           (match_operand:DI 2 "arith_reg_or_0_operand" "r"))
5207                       (label_ref:DI (match_operand 0 "" ""))
5208                       (pc)))]
5209   "TARGET_SHMEDIA"
5210   "")
5211
5212 (define_expand "bgtu_media"
5213   [(set (pc)
5214         (if_then_else (gtu (match_operand:DI 1 "arith_reg_or_0_operand" "r")
5215                            (match_operand:DI 2 "arith_reg_or_0_operand" "r"))
5216                       (label_ref:DI (match_operand 0 "" ""))
5217                       (pc)))]
5218   "TARGET_SHMEDIA"
5219   "")
5220
5221 (define_expand "bgeu_media"
5222   [(set (pc)
5223         (if_then_else (geu (match_operand:DI 1 "arith_reg_or_0_operand" "r")
5224                            (match_operand:DI 2 "arith_reg_or_0_operand" "r"))
5225                       (label_ref:DI (match_operand 0 "" ""))
5226                       (pc)))]
5227   "TARGET_SHMEDIA"
5228   "")
5229
5230 (define_insn "*bgt_media_i"
5231   [(set (pc)
5232         (if_then_else (match_operator 3 "greater_comparison_operator"
5233                         [(match_operand:DI 1 "arith_reg_or_0_operand" "rN")
5234                          (match_operand:DI 2 "arith_reg_or_0_operand" "rN")])
5235                       (match_operand:DI 0 "target_operand" "b")
5236                       (pc)))]
5237   "TARGET_SHMEDIA"
5238   "b%o3%'       %N1, %N2, %0"
5239   [(set_attr "type" "cbranch_media")])
5240
5241 ;; These are only needed to make invert_jump() happy.
5242 (define_insn "*blt_media_i"
5243   [(set (pc)
5244         (if_then_else (match_operator 3 "less_comparison_operator"
5245                         [(match_operand:DI 1 "arith_reg_or_0_operand" "rN")
5246                          (match_operand:DI 2 "arith_reg_or_0_operand" "rN")])
5247                       (match_operand:DI 0 "target_operand" "b")
5248                       (pc)))]
5249   "TARGET_SHMEDIA"
5250   "b%o3%'       %N2, %N1, %0"
5251   [(set_attr "type" "cbranch_media")])
5252
5253 (define_expand "beq"
5254   [(set (pc)
5255         (if_then_else (ne (reg:SI T_REG) (const_int 0))
5256                       (label_ref (match_operand 0 "" ""))
5257                       (pc)))]
5258   ""
5259   "
5260 {
5261   if (TARGET_SHMEDIA)
5262     {
5263       if (GET_MODE (sh_compare_op0) != DImode)
5264         {
5265           rtx tmp = gen_reg_rtx (DImode);
5266
5267           emit_insn (gen_seq (tmp));
5268           emit_jump_insn (gen_bne_media (operands[0], tmp, const0_rtx));
5269           DONE;
5270         }
5271
5272       sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5273       emit_jump_insn (gen_beq_media (operands[0],
5274                                      sh_compare_op0, sh_compare_op1));
5275       DONE;
5276     }
5277
5278   from_compare (operands, EQ);
5279 }")
5280
5281 (define_expand "bne"
5282   [(set (pc)
5283         (if_then_else (eq (reg:SI T_REG) (const_int 0))
5284                       (label_ref (match_operand 0 "" ""))
5285                       (pc)))]
5286   ""
5287   "
5288 {
5289   if (TARGET_SHMEDIA)
5290     {
5291       if (GET_MODE (sh_compare_op0) != DImode)
5292         {
5293           rtx tmp = gen_reg_rtx (DImode);
5294
5295           emit_insn (gen_seq (tmp));
5296           emit_jump_insn (gen_beq_media (operands[0], tmp, const0_rtx));
5297           DONE;
5298         }
5299
5300       sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5301       emit_jump_insn (gen_bne_media (operands[0],
5302                                      sh_compare_op0, sh_compare_op1));
5303       DONE;
5304     }
5305
5306   from_compare (operands, EQ);
5307 }")
5308
5309 (define_expand "bgt"
5310   [(set (pc)
5311         (if_then_else (ne (reg:SI T_REG) (const_int 0))
5312                       (label_ref (match_operand 0 "" ""))
5313                       (pc)))]
5314   ""
5315   "
5316 {
5317   if (TARGET_SHMEDIA)
5318     {
5319       if (GET_MODE (sh_compare_op0) != DImode)
5320         {
5321           rtx tmp = gen_reg_rtx (DImode);
5322
5323           emit_insn (gen_sgt (tmp));
5324           emit_jump_insn (gen_bne_media (operands[0], tmp, const0_rtx));
5325           DONE;
5326         }
5327
5328       if (sh_compare_op0 != const0_rtx)
5329         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5330       if (sh_compare_op1 != const0_rtx)
5331         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5332       emit_jump_insn (gen_bgt_media (operands[0],
5333                                      sh_compare_op0, sh_compare_op1));
5334       DONE;
5335     }
5336
5337   from_compare (operands, GT);
5338 }")
5339
5340 (define_expand "blt"
5341   [(set (pc)
5342         (if_then_else (eq (reg:SI T_REG) (const_int 0))
5343                       (label_ref (match_operand 0 "" ""))
5344                       (pc)))]
5345   ""
5346   "
5347 {
5348   if (TARGET_SHMEDIA)
5349     {
5350       if (GET_MODE (sh_compare_op0) != DImode)
5351         {
5352           rtx tmp = gen_reg_rtx (DImode);
5353
5354           emit_insn (gen_slt (tmp));
5355           emit_jump_insn (gen_bne_media (operands[0], tmp, const0_rtx));
5356           DONE;
5357         }
5358
5359       if (sh_compare_op0 != const0_rtx)
5360         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5361       if (sh_compare_op1 != const0_rtx)
5362         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5363       emit_jump_insn (gen_bgt_media (operands[0],
5364                                      sh_compare_op1, sh_compare_op0));
5365       DONE;
5366     }
5367
5368   if (GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
5369     {
5370       rtx tmp = sh_compare_op0;
5371       sh_compare_op0 = sh_compare_op1;
5372       sh_compare_op1 = tmp;
5373       emit_insn (gen_bgt (operands[0]));
5374       DONE;
5375     }
5376   from_compare (operands, GE);
5377 }")
5378
5379 (define_expand "ble"
5380   [(set (pc)
5381         (if_then_else (eq (reg:SI T_REG) (const_int 0))
5382                       (label_ref (match_operand 0 "" ""))
5383                       (pc)))]
5384   ""
5385   "
5386 {
5387   if (TARGET_SHMEDIA)
5388     {
5389       if (GET_MODE (sh_compare_op0) != DImode)
5390         {
5391           rtx tmp = gen_reg_rtx (DImode);
5392
5393           emit_insn (gen_sle (tmp));
5394           emit_jump_insn (gen_bne_media (operands[0], tmp, const0_rtx));
5395           DONE;
5396         }
5397
5398       if (sh_compare_op0 != const0_rtx)
5399         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5400       if (sh_compare_op1 != const0_rtx)
5401         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5402       emit_jump_insn (gen_bge_media (operands[0],
5403                                      sh_compare_op1, sh_compare_op0));
5404       DONE;
5405     }
5406
5407   if (TARGET_SH3E
5408       && TARGET_IEEE
5409       && GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
5410     {
5411       rtx tmp = sh_compare_op0;
5412       sh_compare_op0 = sh_compare_op1;
5413       sh_compare_op1 = tmp;
5414       emit_insn (gen_bge (operands[0]));
5415       DONE;
5416     }
5417   from_compare (operands, GT);
5418 }")
5419
5420 (define_expand "bge"
5421   [(set (pc)
5422         (if_then_else (ne (reg:SI T_REG) (const_int 0))
5423                       (label_ref (match_operand 0 "" ""))
5424                       (pc)))]
5425   ""
5426   "
5427 {
5428   if (TARGET_SHMEDIA)
5429     {
5430       if (GET_MODE (sh_compare_op0) != DImode)
5431         {
5432           rtx tmp = gen_reg_rtx (DImode);
5433
5434           emit_insn (gen_sge (tmp));
5435           emit_jump_insn (gen_bne_media (operands[0], tmp, const0_rtx));
5436           DONE;
5437         }
5438
5439       if (sh_compare_op0 != const0_rtx)
5440         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5441       if (sh_compare_op1 != const0_rtx)
5442         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5443       emit_jump_insn (gen_bge_media (operands[0],
5444                                      sh_compare_op0, sh_compare_op1));
5445       DONE;
5446     }
5447
5448   if (TARGET_SH3E
5449       && ! TARGET_IEEE
5450       && GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
5451     {
5452       rtx tmp = sh_compare_op0;
5453       sh_compare_op0 = sh_compare_op1;
5454       sh_compare_op1 = tmp;
5455       emit_insn (gen_ble (operands[0]));
5456       DONE;
5457     }
5458   from_compare (operands, GE);
5459 }")
5460
5461 (define_expand "bgtu"
5462   [(set (pc)
5463         (if_then_else (ne (reg:SI T_REG) (const_int 0))
5464                       (label_ref (match_operand 0 "" ""))
5465                       (pc)))]
5466   ""
5467   "
5468 {
5469   if (TARGET_SHMEDIA)
5470     {
5471       if (sh_compare_op0 != const0_rtx)
5472         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5473       if (sh_compare_op1 != const0_rtx)
5474         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5475       emit_jump_insn (gen_bgtu_media (operands[0],
5476                                       sh_compare_op0, sh_compare_op1));
5477       DONE;
5478     }
5479
5480   from_compare (operands, GTU);
5481 }")
5482
5483 (define_expand "bltu"
5484   [(set (pc)
5485         (if_then_else (eq (reg:SI T_REG) (const_int 0))
5486                       (label_ref (match_operand 0 "" ""))
5487                       (pc)))]
5488   ""
5489   "
5490 {
5491   if (TARGET_SHMEDIA)
5492     {
5493       if (sh_compare_op0 != const0_rtx)
5494         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5495       if (sh_compare_op1 != const0_rtx)
5496         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5497       emit_jump_insn (gen_bgtu_media (operands[0],
5498                                       sh_compare_op1, sh_compare_op0));
5499       DONE;
5500     }
5501
5502   from_compare (operands, GEU);
5503 }")
5504
5505 (define_expand "bgeu"
5506   [(set (pc)
5507         (if_then_else (ne (reg:SI T_REG) (const_int 0))
5508                       (label_ref (match_operand 0 "" ""))
5509                       (pc)))]
5510   ""
5511   "
5512 {
5513   if (TARGET_SHMEDIA)
5514     {
5515       if (sh_compare_op0 != const0_rtx)
5516         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5517       if (sh_compare_op1 != const0_rtx)
5518         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5519       emit_jump_insn (gen_bgeu_media (operands[0],
5520                                       sh_compare_op0, sh_compare_op1));
5521       DONE;
5522     }
5523
5524   from_compare (operands, GEU);
5525 }")
5526
5527 (define_expand "bleu"
5528   [(set (pc)
5529         (if_then_else (eq (reg:SI T_REG) (const_int 0))
5530                       (label_ref (match_operand 0 "" ""))
5531                       (pc)))]
5532   ""
5533   "
5534 {
5535   if (TARGET_SHMEDIA)
5536     {
5537       if (sh_compare_op0 != const0_rtx)
5538         sh_compare_op0 = force_reg (DImode, sh_compare_op0);
5539       if (sh_compare_op1 != const0_rtx)
5540         sh_compare_op1 = force_reg (DImode, sh_compare_op1);
5541       emit_jump_insn (gen_bgeu_media (operands[0],
5542                                       sh_compare_op1, sh_compare_op0));
5543       DONE;
5544     }
5545
5546   from_compare (operands, GTU);
5547 }")
5548
5549 (define_expand "bunordered"
5550   [(set (match_dup 1) (unordered:DI (match_dup 2) (match_dup 3)))
5551    (set (pc)
5552         (if_then_else (ne (match_dup 1) (const_int 0))
5553                       (label_ref:DI (match_operand 0 "" ""))
5554                       (pc)))]
5555   "TARGET_SHMEDIA"
5556   "
5557 {
5558   operands[1] = gen_reg_rtx (DImode);
5559   operands[2] = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
5560   operands[3] = force_reg (GET_MODE (sh_compare_op1), sh_compare_op1);
5561 }")
5562 \f
5563 ;; ------------------------------------------------------------------------
5564 ;; Jump and linkage insns
5565 ;; ------------------------------------------------------------------------
5566
5567 (define_insn "jump_compact"
5568   [(set (pc)
5569         (label_ref (match_operand 0 "" "")))]
5570   "TARGET_SH1"
5571   "*
5572 {
5573   /* The length is 16 if the delay slot is unfilled.  */
5574   if (get_attr_length(insn) > 4)
5575     return output_far_jump(insn, operands[0]);
5576   else
5577     return   \"bra      %l0%#\";
5578 }"
5579   [(set_attr "type" "jump")
5580    (set_attr "needs_delay_slot" "yes")])
5581
5582 (define_insn "jump_media"
5583   [(set (pc)
5584         (match_operand:DI 0 "target_operand" "b"))]
5585   "TARGET_SHMEDIA"
5586   "blink        %0, r63"
5587   [(set_attr "type" "jump_media")])
5588
5589 (define_expand "jump"
5590   [(set (pc)
5591         (label_ref (match_operand 0 "" "")))]
5592   ""
5593   "
5594 {
5595   if (TARGET_SH1)
5596     emit_jump_insn (gen_jump_compact (operands[0]));
5597   else if (TARGET_SHMEDIA)
5598     {
5599       if (reload_in_progress || reload_completed)
5600         FAIL;
5601       emit_jump_insn (gen_jump_media (gen_rtx_LABEL_REF (DImode,
5602                                                          operands[0])));
5603     }
5604   DONE;
5605 }")
5606
5607 (define_insn "force_mode_for_call"
5608   [(use (reg:PSI FPSCR_REG))]
5609   "TARGET_SHCOMPACT"
5610   ""
5611   [(set_attr "length" "0")
5612    (set (attr "fp_mode")
5613         (if_then_else (eq_attr "fpu_single" "yes")
5614                       (const_string "single") (const_string "double")))])
5615
5616 (define_insn "calli"
5617   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
5618          (match_operand 1 "" ""))
5619    (use (reg:PSI FPSCR_REG))
5620    (clobber (reg:SI PR_REG))]
5621   "TARGET_SH1"
5622   "jsr  @%0%#"
5623   [(set_attr "type" "call")
5624    (set (attr "fp_mode")
5625         (if_then_else (eq_attr "fpu_single" "yes")
5626                       (const_string "single") (const_string "double")))
5627    (set_attr "needs_delay_slot" "yes")])
5628
5629 ;; This is a pc-rel call, using bsrf, for use with PIC.
5630
5631 (define_insn "calli_pcrel"
5632   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
5633          (match_operand 1 "" ""))
5634    (use (reg:PSI FPSCR_REG))
5635    (use (reg:SI PIC_REG))
5636    (use (match_operand 2 "" ""))
5637    (clobber (reg:SI PR_REG))]
5638   "TARGET_SH2"
5639   "bsrf %0\\n%O2:%#"
5640   [(set_attr "type" "call")
5641    (set (attr "fp_mode")
5642         (if_then_else (eq_attr "fpu_single" "yes")
5643                       (const_string "single") (const_string "double")))
5644    (set_attr "needs_delay_slot" "yes")])
5645
5646 (define_insn_and_split "call_pcrel"
5647   [(call (mem:SI (match_operand:SI 0 "symbol_ref_operand" ""))
5648          (match_operand 1 "" ""))
5649    (use (reg:PSI FPSCR_REG))
5650    (use (reg:SI PIC_REG))
5651    (clobber (reg:SI PR_REG))
5652    (clobber (match_scratch:SI 2 "=r"))]
5653   "TARGET_SH2"
5654   "#"
5655   "reload_completed"
5656   [(const_int 0)]
5657   "
5658 {
5659   rtx lab = PATTERN (gen_call_site ());
5660
5661   if (SYMBOL_REF_FLAG (operands[0]))
5662     emit_insn (gen_sym_label2reg (operands[2], operands[0], lab));
5663   else
5664     emit_insn (gen_symPLT_label2reg (operands[2], operands[0], lab));
5665   emit_call_insn (gen_calli_pcrel (operands[2], operands[1], lab));
5666   DONE;
5667 }"
5668   [(set_attr "type" "call")
5669    (set (attr "fp_mode")
5670         (if_then_else (eq_attr "fpu_single" "yes")
5671                       (const_string "single") (const_string "double")))
5672    (set_attr "needs_delay_slot" "yes")])
5673
5674 (define_insn "call_compact"
5675   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
5676          (match_operand 1 "" ""))
5677    (match_operand 2 "immediate_operand" "n")
5678    (use (reg:SI R0_REG))
5679    (use (reg:SI R1_REG))
5680    (use (reg:PSI FPSCR_REG))
5681    (clobber (reg:SI PR_REG))]
5682   "TARGET_SHCOMPACT && ! (INTVAL (operands[2]) & CALL_COOKIE_RET_TRAMP (1))"
5683   "jsr  @%0%#"
5684   [(set_attr "type" "call")
5685    (set (attr "fp_mode")
5686         (if_then_else (eq_attr "fpu_single" "yes")
5687                       (const_string "single") (const_string "double")))
5688    (set_attr "needs_delay_slot" "yes")])
5689
5690 (define_insn "call_compact_rettramp"
5691   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
5692          (match_operand 1 "" ""))
5693    (match_operand 2 "immediate_operand" "n")
5694    (use (reg:SI R0_REG))
5695    (use (reg:SI R1_REG))
5696    (use (reg:PSI FPSCR_REG))
5697    (clobber (reg:SI R10_REG))
5698    (clobber (reg:SI PR_REG))]
5699   "TARGET_SHCOMPACT && (INTVAL (operands[2]) & CALL_COOKIE_RET_TRAMP (1))"
5700   "jsr  @%0%#"
5701   [(set_attr "type" "call")
5702    (set (attr "fp_mode")
5703         (if_then_else (eq_attr "fpu_single" "yes")
5704                       (const_string "single") (const_string "double")))
5705    (set_attr "needs_delay_slot" "yes")])
5706
5707 (define_insn "call_media"
5708   [(call (mem:DI (match_operand:DI 0 "target_reg_operand" "b"))
5709          (match_operand 1 "" ""))
5710    (clobber (reg:DI PR_MEDIA_REG))]
5711   "TARGET_SHMEDIA"
5712   "blink        %0, r18"
5713   [(set_attr "type" "jump_media")])
5714
5715 (define_insn "call_valuei"
5716   [(set (match_operand 0 "" "=rf")
5717         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
5718               (match_operand 2 "" "")))
5719    (use (reg:PSI FPSCR_REG))
5720    (clobber (reg:SI PR_REG))]
5721   "TARGET_SH1"
5722   "jsr  @%1%#"
5723   [(set_attr "type" "call")
5724    (set (attr "fp_mode")
5725         (if_then_else (eq_attr "fpu_single" "yes")
5726                       (const_string "single") (const_string "double")))
5727    (set_attr "needs_delay_slot" "yes")])
5728
5729 (define_insn "call_valuei_pcrel"
5730   [(set (match_operand 0 "" "=rf")
5731         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
5732               (match_operand 2 "" "")))
5733    (use (reg:PSI FPSCR_REG))
5734    (use (reg:SI PIC_REG))
5735    (use (match_operand 3 "" ""))
5736    (clobber (reg:SI PR_REG))]
5737   "TARGET_SH2"
5738   "bsrf %1\\n%O3:%#"
5739   [(set_attr "type" "call")
5740    (set (attr "fp_mode")
5741         (if_then_else (eq_attr "fpu_single" "yes")
5742                       (const_string "single") (const_string "double")))
5743    (set_attr "needs_delay_slot" "yes")])
5744
5745 (define_insn_and_split "call_value_pcrel"
5746   [(set (match_operand 0 "" "=rf")
5747         (call (mem:SI (match_operand:SI 1 "symbol_ref_operand" ""))
5748               (match_operand 2 "" "")))
5749    (use (reg:PSI FPSCR_REG))
5750    (use (reg:SI PIC_REG))
5751    (clobber (reg:SI PR_REG))
5752    (clobber (match_scratch:SI 3 "=r"))]
5753   "TARGET_SH2"
5754   "#"
5755   "reload_completed"
5756   [(const_int 0)]
5757   "
5758 {
5759   rtx lab = PATTERN (gen_call_site ());
5760
5761   if (SYMBOL_REF_FLAG (operands[1]))
5762     emit_insn (gen_sym_label2reg (operands[3], operands[1], lab));
5763   else
5764     emit_insn (gen_symPLT_label2reg (operands[3], operands[1], lab));
5765   emit_call_insn (gen_call_valuei_pcrel (operands[0], operands[3],
5766                                          operands[2], lab));
5767   DONE;
5768 }"
5769   [(set_attr "type" "call")
5770    (set (attr "fp_mode")
5771         (if_then_else (eq_attr "fpu_single" "yes")
5772                       (const_string "single") (const_string "double")))
5773    (set_attr "needs_delay_slot" "yes")])
5774
5775 (define_insn "call_value_compact"
5776   [(set (match_operand 0 "" "=rf")
5777         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
5778               (match_operand 2 "" "")))
5779    (match_operand 3 "immediate_operand" "n")
5780    (use (reg:SI R0_REG))
5781    (use (reg:SI R1_REG))
5782    (use (reg:PSI FPSCR_REG))
5783    (clobber (reg:SI PR_REG))]
5784   "TARGET_SHCOMPACT && ! (INTVAL (operands[3]) & CALL_COOKIE_RET_TRAMP (1))"
5785   "jsr  @%1%#"
5786   [(set_attr "type" "call")
5787    (set (attr "fp_mode")
5788         (if_then_else (eq_attr "fpu_single" "yes")
5789                       (const_string "single") (const_string "double")))
5790    (set_attr "needs_delay_slot" "yes")])
5791
5792 (define_insn "call_value_compact_rettramp"
5793   [(set (match_operand 0 "" "=rf")
5794         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
5795               (match_operand 2 "" "")))
5796    (match_operand 3 "immediate_operand" "n")
5797    (use (reg:SI R0_REG))
5798    (use (reg:SI R1_REG))
5799    (use (reg:PSI FPSCR_REG))
5800    (clobber (reg:SI R10_REG))
5801    (clobber (reg:SI PR_REG))]
5802   "TARGET_SHCOMPACT && (INTVAL (operands[3]) & CALL_COOKIE_RET_TRAMP (1))"
5803   "jsr  @%1%#"
5804   [(set_attr "type" "call")
5805    (set (attr "fp_mode")
5806         (if_then_else (eq_attr "fpu_single" "yes")
5807                       (const_string "single") (const_string "double")))
5808    (set_attr "needs_delay_slot" "yes")])
5809
5810 (define_insn "call_value_media"
5811   [(set (match_operand 0 "" "=rf")
5812         (call (mem:DI (match_operand:DI 1 "target_reg_operand" "b"))
5813               (match_operand 2 "" "")))
5814    (clobber (reg:DI PR_MEDIA_REG))]
5815   "TARGET_SHMEDIA"
5816   "blink        %1, r18"
5817   [(set_attr "type" "jump_media")])
5818
5819 (define_expand "call"
5820   [(parallel [(call (mem:SI (match_operand 0 "arith_reg_operand" ""))
5821                             (match_operand 1 "" ""))
5822               (match_operand 2 "" "")
5823               (use (reg:PSI FPSCR_REG))
5824               (clobber (reg:SI PR_REG))])]
5825   ""
5826   "
5827 {
5828   if (TARGET_SHMEDIA)
5829     {
5830       operands[0] = XEXP (operands[0], 0);
5831       if (flag_pic && GET_CODE (operands[0]) == SYMBOL_REF)
5832         {
5833           if (! SYMBOL_REF_FLAG (operands[0]))
5834             {
5835               rtx reg = gen_reg_rtx (Pmode);
5836
5837               emit_insn (gen_symGOTPLT2reg (reg, operands[0]));
5838               operands[0] = reg;
5839             }
5840           else
5841             {
5842               operands[0] = gen_sym2PIC (operands[0]);
5843               PUT_MODE (operands[0], Pmode);
5844             }
5845         }
5846       if (GET_MODE (operands[0]) == SImode)
5847         {
5848           if (GET_CODE (operands[0]) == REG)
5849             operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
5850           else if (GET_CODE (operands[0]) == SUBREG)
5851             {
5852               operands[0] = SUBREG_REG (operands[0]);
5853               if (GET_MODE (operands[0]) != DImode)
5854                 operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
5855             }
5856           else
5857             {
5858               operands[0] = shallow_copy_rtx (operands[0]);
5859               PUT_MODE (operands[0], DImode);
5860             }
5861         }
5862       if (! target_reg_operand (operands[0], DImode))
5863         operands[0] = copy_to_mode_reg (DImode, operands[0]);
5864       emit_call_insn (gen_call_media (operands[0], operands[1]));
5865       DONE;
5866     }
5867   else if (TARGET_SHCOMPACT && operands[2] && INTVAL (operands[2]))
5868     {
5869       rtx cookie_rtx = operands[2];
5870       long cookie = INTVAL (cookie_rtx);
5871       rtx func = XEXP (operands[0], 0);
5872       rtx r0, r1;
5873
5874       if (flag_pic)
5875         {
5876           if (GET_CODE (func) == SYMBOL_REF && ! SYMBOL_REF_FLAG (func))
5877             {
5878               rtx reg = gen_reg_rtx (Pmode);
5879
5880               emit_insn (gen_symGOTPLT2reg (reg, func));
5881               func = reg;
5882             }
5883           else
5884             func = legitimize_pic_address (func, Pmode, 0);
5885         }
5886
5887       r0 = gen_rtx_REG (SImode, R0_REG);
5888       r1 = gen_rtx_REG (SImode, R1_REG);
5889
5890       /* Since such a call function may use all call-clobbered
5891          registers, we force a mode switch earlier, so that we don't
5892          run out of registers when adjusting fpscr for the call.  */
5893       emit_insn (gen_force_mode_for_call ());
5894
5895       operands[0] = gen_rtx_SYMBOL_REF (SImode,
5896                                         \"__GCC_shcompact_call_trampoline\");
5897       if (flag_pic)
5898         {
5899           rtx reg = gen_reg_rtx (Pmode);
5900
5901           emit_insn (gen_symGOTPLT2reg (reg, operands[0]));
5902           operands[0] = reg;
5903         }
5904       operands[0] = force_reg (SImode, operands[0]);
5905
5906       emit_move_insn (r0, func);
5907       emit_move_insn (r1, cookie_rtx);
5908
5909       if (cookie & CALL_COOKIE_RET_TRAMP (1))
5910         emit_call_insn (gen_call_compact_rettramp (operands[0], operands[1],
5911                                                    operands[2]));
5912       else
5913         emit_call_insn (gen_call_compact (operands[0], operands[1],
5914                                           operands[2]));
5915
5916       DONE;
5917     }
5918   else if (TARGET_SHCOMPACT && flag_pic
5919            && GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
5920            && ! SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
5921     {
5922       rtx reg = gen_reg_rtx (Pmode);
5923
5924       emit_insn (gen_symGOTPLT2reg (reg, XEXP (operands[0], 0)));
5925       XEXP (operands[0], 0) = reg;
5926     }
5927   if (flag_pic && TARGET_SH2
5928       && GET_CODE (operands[0]) == MEM
5929       && GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF)
5930     {
5931       emit_call_insn (gen_call_pcrel (XEXP (operands[0], 0), operands[1]));
5932       DONE;
5933     }
5934   else
5935     operands[0] = force_reg (SImode, XEXP (operands[0], 0));
5936
5937   emit_call_insn (gen_calli (operands[0], operands[1]));
5938   DONE;
5939 }")
5940
5941 (define_insn "call_pop_compact"
5942   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
5943          (match_operand 1 "" ""))
5944    (match_operand 2 "immediate_operand" "n")
5945    (set (reg:SI SP_REG) (plus:SI (reg:SI SP_REG)
5946                                  (match_operand 3 "immediate_operand" "n")))
5947    (use (reg:SI R0_REG))
5948    (use (reg:SI R1_REG))
5949    (use (reg:PSI FPSCR_REG))
5950    (clobber (reg:SI PR_REG))]
5951   "TARGET_SHCOMPACT && ! (INTVAL (operands[2]) & CALL_COOKIE_RET_TRAMP (1))"
5952   "jsr  @%0%#"
5953   [(set_attr "type" "call")
5954    (set (attr "fp_mode")
5955         (if_then_else (eq_attr "fpu_single" "yes")
5956                       (const_string "single") (const_string "double")))
5957    (set_attr "needs_delay_slot" "yes")])
5958
5959 (define_insn "call_pop_compact_rettramp"
5960   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
5961          (match_operand 1 "" ""))
5962    (match_operand 2 "immediate_operand" "n")
5963    (set (reg:SI SP_REG) (plus:SI (reg:SI SP_REG)
5964                                  (match_operand 3 "immediate_operand" "n")))
5965    (use (reg:SI R0_REG))
5966    (use (reg:SI R1_REG))
5967    (use (reg:PSI FPSCR_REG))
5968    (clobber (reg:SI R10_REG))
5969    (clobber (reg:SI PR_REG))]
5970   "TARGET_SHCOMPACT && (INTVAL (operands[2]) & CALL_COOKIE_RET_TRAMP (1))"
5971   "jsr  @%0%#"
5972   [(set_attr "type" "call")
5973    (set (attr "fp_mode")
5974         (if_then_else (eq_attr "fpu_single" "yes")
5975                       (const_string "single") (const_string "double")))
5976    (set_attr "needs_delay_slot" "yes")])
5977
5978 (define_expand "call_pop"
5979   [(parallel [(call (mem:SI (match_operand 0 "arith_reg_operand" ""))
5980                     (match_operand 1 "" ""))
5981              (match_operand 2 "" "")
5982              (set (reg:SI SP_REG) (plus:SI (reg:SI SP_REG)
5983                                            (match_operand 3 "" "")))])]
5984   "TARGET_SHCOMPACT"
5985   "
5986 {
5987   if (operands[2] && INTVAL (operands[2]))
5988     {
5989       rtx cookie_rtx = operands[2];
5990       long cookie = INTVAL (cookie_rtx);
5991       rtx func = XEXP (operands[0], 0);
5992       rtx r0, r1;
5993
5994       if (flag_pic)
5995         {
5996           if (GET_CODE (func) == SYMBOL_REF && ! SYMBOL_REF_FLAG (func))
5997             {
5998               rtx reg = gen_reg_rtx (Pmode);
5999
6000               emit_insn (gen_symGOTPLT2reg (reg, func));
6001               func = reg;
6002             }
6003           else
6004             func = legitimize_pic_address (func, Pmode, 0);
6005         }
6006
6007       r0 = gen_rtx_REG (SImode, R0_REG);
6008       r1 = gen_rtx_REG (SImode, R1_REG);
6009
6010       /* Since such a call function may use all call-clobbered
6011          registers, we force a mode switch earlier, so that we don't
6012          run out of registers when adjusting fpscr for the call.  */
6013       emit_insn (gen_force_mode_for_call ());
6014
6015       operands[0] = gen_rtx_SYMBOL_REF (SImode,
6016                                         \"__GCC_shcompact_call_trampoline\");
6017       if (flag_pic)
6018         {
6019           rtx reg = gen_reg_rtx (Pmode);
6020
6021           emit_insn (gen_symGOTPLT2reg (reg, operands[0]));
6022           operands[0] = reg;
6023         }
6024       operands[0] = force_reg (SImode, operands[0]);
6025
6026       emit_move_insn (r0, func);
6027       emit_move_insn (r1, cookie_rtx);
6028
6029       if (cookie & CALL_COOKIE_RET_TRAMP (1))
6030         emit_call_insn (gen_call_pop_compact_rettramp
6031                         (operands[0], operands[1], operands[2], operands[3]));
6032       else
6033         emit_call_insn (gen_call_pop_compact
6034                         (operands[0], operands[1], operands[2], operands[3]));
6035
6036       DONE;
6037     }
6038
6039   abort ();
6040 }")
6041
6042 (define_expand "call_value"
6043   [(parallel [(set (match_operand 0 "arith_reg_operand" "")
6044                    (call (mem:SI (match_operand 1 "arith_reg_operand" ""))
6045                                  (match_operand 2 "" "")))
6046               (match_operand 3 "" "")
6047               (use (reg:PSI FPSCR_REG))
6048               (clobber (reg:SI PR_REG))])]
6049   ""
6050   "
6051 {
6052   if (TARGET_SHMEDIA)
6053     {
6054       operands[1] = XEXP (operands[1], 0);
6055       if (flag_pic && GET_CODE (operands[1]) == SYMBOL_REF)
6056         {
6057           if (! SYMBOL_REF_FLAG (operands[1]))
6058             {
6059               rtx reg = gen_reg_rtx (Pmode);
6060
6061               emit_insn (gen_symGOTPLT2reg (reg, operands[1]));
6062               operands[1] = reg;
6063             }
6064           else
6065             {
6066               operands[1] = gen_sym2PIC (operands[1]);
6067               PUT_MODE (operands[1], Pmode);
6068             }
6069         }
6070       if (GET_MODE (operands[1]) == SImode)
6071         {
6072           if (GET_CODE (operands[1]) == REG)
6073             operands[1] = gen_rtx_SUBREG (DImode, operands[1], 0);
6074           else if (GET_CODE (operands[1]) == SUBREG)
6075             {
6076               operands[1] = SUBREG_REG (operands[1]);
6077               if (GET_MODE (operands[1]) != DImode)
6078                 operands[1] = gen_rtx_SUBREG (DImode, operands[1], 0);
6079             }
6080           else
6081             {
6082               operands[1] = shallow_copy_rtx (operands[1]);
6083               PUT_MODE (operands[1], DImode);
6084             }
6085         }
6086       if (! target_reg_operand (operands[1], DImode))
6087         operands[1] = copy_to_mode_reg (DImode, operands[1]);
6088       emit_call_insn (gen_call_value_media (operands[0], operands[1],
6089                                             operands[2]));
6090       DONE;
6091     }
6092   else if (TARGET_SHCOMPACT && operands[3] && INTVAL (operands[3]))
6093     {
6094       rtx cookie_rtx = operands[3];
6095       long cookie = INTVAL (cookie_rtx);
6096       rtx func = XEXP (operands[1], 0);
6097       rtx r0, r1;
6098
6099       if (flag_pic)
6100         {
6101           if (GET_CODE (func) == SYMBOL_REF && ! SYMBOL_REF_FLAG (func))
6102             {
6103               rtx reg = gen_reg_rtx (Pmode);
6104
6105               emit_insn (gen_symGOTPLT2reg (reg, func));
6106               func = reg;
6107             }
6108           else
6109             func = legitimize_pic_address (func, Pmode, 0);
6110         }
6111
6112       r0 = gen_rtx_REG (SImode, R0_REG);
6113       r1 = gen_rtx_REG (SImode, R1_REG);
6114
6115       /* Since such a call function may use all call-clobbered
6116          registers, we force a mode switch earlier, so that we don't
6117          run out of registers when adjusting fpscr for the call.  */
6118       emit_insn (gen_force_mode_for_call ());
6119
6120       operands[1] = gen_rtx_SYMBOL_REF (SImode,
6121                                         \"__GCC_shcompact_call_trampoline\");
6122       if (flag_pic)
6123         {
6124           rtx reg = gen_reg_rtx (Pmode);
6125
6126           emit_insn (gen_symGOTPLT2reg (reg, operands[1]));
6127           operands[1] = reg;
6128         }
6129       operands[1] = force_reg (SImode, operands[1]);
6130
6131       emit_move_insn (r0, func);
6132       emit_move_insn (r1, cookie_rtx);
6133
6134       if (cookie & CALL_COOKIE_RET_TRAMP (1))
6135         emit_call_insn (gen_call_value_compact_rettramp (operands[0],
6136                                                          operands[1],
6137                                                          operands[2],
6138                                                          operands[3]));
6139       else
6140         emit_call_insn (gen_call_value_compact (operands[0], operands[1],
6141                                                 operands[2], operands[3]));
6142
6143       DONE;
6144     }
6145   else if (TARGET_SHCOMPACT && flag_pic
6146            && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
6147            && ! SYMBOL_REF_FLAG (XEXP (operands[1], 0)))
6148     {
6149       rtx reg = gen_reg_rtx (Pmode);
6150
6151       emit_insn (gen_symGOTPLT2reg (reg, XEXP (operands[1], 0)));
6152       XEXP (operands[1], 0) = reg;
6153     }
6154   if (flag_pic && TARGET_SH2
6155       && GET_CODE (operands[1]) == MEM
6156       && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF)
6157     {
6158       emit_call_insn (gen_call_value_pcrel (operands[0], XEXP (operands[1], 0),
6159                                             operands[2]));
6160       DONE;
6161     }
6162   else
6163     operands[1] = force_reg (SImode, XEXP (operands[1], 0));
6164
6165   emit_call_insn (gen_call_valuei (operands[0], operands[1], operands[2]));
6166   DONE;
6167 }")
6168
6169 (define_insn "sibcalli"
6170   [(call (mem:SI (match_operand:SI 0 "register_operand" "k"))
6171          (match_operand 1 "" ""))
6172    (use (reg:PSI FPSCR_REG))
6173    (return)]
6174   "TARGET_SH1"
6175   "jmp  @%0%#"
6176   [(set_attr "needs_delay_slot" "yes")
6177    (set (attr "fp_mode")
6178         (if_then_else (eq_attr "fpu_single" "yes")
6179                       (const_string "single") (const_string "double")))
6180    (set_attr "type" "jump_ind")])
6181
6182 (define_insn "sibcalli_pcrel"
6183   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "k"))
6184          (match_operand 1 "" ""))
6185    (use (match_operand 2 "" ""))
6186    (use (reg:PSI FPSCR_REG))
6187    (return)]
6188   "TARGET_SH2"
6189   "braf %0\\n%O2:%#"
6190   [(set_attr "needs_delay_slot" "yes")
6191    (set (attr "fp_mode")
6192         (if_then_else (eq_attr "fpu_single" "yes")
6193                       (const_string "single") (const_string "double")))
6194    (set_attr "type" "jump_ind")])
6195
6196 (define_insn_and_split "sibcall_pcrel"
6197   [(call (mem:SI (match_operand:SI 0 "symbol_ref_operand" ""))
6198          (match_operand 1 "" ""))
6199    (use (reg:PSI FPSCR_REG))
6200    (clobber (match_scratch:SI 2 "=k"))
6201    (return)]
6202   "TARGET_SH2"
6203   "#"
6204   "reload_completed"
6205   [(const_int 0)]
6206   "
6207 {
6208   rtx lab = PATTERN (gen_call_site ());
6209   rtx call_insn;
6210
6211   emit_insn (gen_sym_label2reg (operands[2], operands[0], lab));
6212   call_insn = emit_call_insn (gen_sibcalli_pcrel (operands[2], operands[1],
6213                                                   lab));
6214   SIBLING_CALL_P (call_insn) = 1;
6215   DONE;
6216 }"
6217   [(set_attr "needs_delay_slot" "yes")
6218    (set (attr "fp_mode")
6219         (if_then_else (eq_attr "fpu_single" "yes")
6220                       (const_string "single") (const_string "double")))
6221    (set_attr "type" "jump_ind")])
6222
6223 (define_insn "sibcall_compact"
6224   [(call (mem:SI (match_operand:SI 0 "register_operand" "k,k"))
6225          (match_operand 1 "" ""))
6226    (return)
6227    (use (match_operand:SI 2 "register_operand" "z,x"))
6228    (use (reg:SI R1_REG))
6229    (use (reg:PSI FPSCR_REG))
6230    ;; We want to make sure the `x' above will only match MACH_REG
6231    ;; because sibcall_epilogue may clobber MACL_REG.
6232    (clobber (reg:SI MACL_REG))]
6233   "TARGET_SHCOMPACT"
6234   "@
6235         jmp     @%0%#
6236         jmp     @%0\\n  sts     %2, r0"
6237   [(set_attr "needs_delay_slot" "yes,no")
6238    (set_attr "length" "2,4")
6239    (set (attr "fp_mode") (const_string "single"))
6240    (set_attr "type" "jump_ind")])
6241
6242 (define_insn "sibcall_media"
6243   [(call (mem:DI (match_operand:DI 0 "target_reg_operand" "k"))
6244          (match_operand 1 "" ""))
6245    (return)]
6246   "TARGET_SHMEDIA"
6247   "blink        %0, r63"
6248   [(set_attr "type" "jump_media")])
6249
6250 (define_expand "sibcall"
6251   [(parallel
6252     [(call (mem:SI (match_operand 0 "arith_reg_operand" ""))
6253            (match_operand 1 "" ""))
6254      (match_operand 2 "" "")
6255      (use (reg:PSI FPSCR_REG))
6256      (return)])]
6257   ""
6258   "
6259 {
6260   if (TARGET_SHMEDIA)
6261     {
6262       operands[0] = XEXP (operands[0], 0);
6263       if (flag_pic && GET_CODE (operands[0]) == SYMBOL_REF)
6264         {
6265           if (! SYMBOL_REF_FLAG (operands[0]))
6266             {
6267               rtx reg = gen_reg_rtx (Pmode);
6268
6269               /* We must not use GOTPLT for sibcalls, because PIC_REG
6270                  must be restored before the PLT code gets to run.  */
6271               emit_insn (gen_symGOT2reg (reg, operands[0]));
6272               operands[0] = reg;
6273             }
6274           else
6275             {
6276               operands[0] = gen_sym2PIC (operands[0]);
6277               PUT_MODE (operands[0], Pmode);
6278             }
6279         }
6280       if (GET_MODE (operands[0]) == SImode)
6281         {
6282           if (GET_CODE (operands[0]) == REG)
6283             operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
6284           else if (GET_CODE (operands[0]) == SUBREG)
6285             {
6286               operands[0] = SUBREG_REG (operands[0]);
6287               if (GET_MODE (operands[0]) != DImode)
6288                 operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
6289             }
6290           else
6291             {
6292               operands[0] = shallow_copy_rtx (operands[0]);
6293               PUT_MODE (operands[0], DImode);
6294             }
6295         }
6296       if (! target_reg_operand (operands[0], DImode))
6297         operands[0] = copy_to_mode_reg (DImode, operands[0]);
6298       emit_call_insn (gen_sibcall_media (operands[0], operands[1]));
6299       DONE;
6300     }
6301   else if (TARGET_SHCOMPACT && operands[2]
6302            && (INTVAL (operands[2]) & ~ CALL_COOKIE_RET_TRAMP (1)))
6303     {
6304       rtx cookie_rtx = operands[2];
6305       long cookie = INTVAL (cookie_rtx);
6306       rtx func = XEXP (operands[0], 0);
6307       rtx mach, r1;
6308
6309       if (flag_pic)
6310         {
6311           if (GET_CODE (func) == SYMBOL_REF && ! SYMBOL_REF_FLAG (func))
6312             {
6313               rtx reg = gen_reg_rtx (Pmode);
6314
6315               emit_insn (gen_symGOT2reg (reg, func));
6316               func = reg;
6317             }
6318           else
6319             func = legitimize_pic_address (func, Pmode, 0);
6320         }
6321
6322       /* FIXME: if we could tell whether all argument registers are
6323          already taken, we could decide whether to force the use of
6324          MACH_REG or to stick to R0_REG.  Unfortunately, there's no
6325          simple way to tell.  We could use the CALL_COOKIE, but we
6326          can't currently tell a register used for regular argument
6327          passing from one that is unused.  If we leave it up to reload
6328          to decide which register to use, it seems to always choose
6329          R0_REG, which leaves no available registers in SIBCALL_REGS
6330          to hold the address of the trampoline.  */
6331       mach = gen_rtx_REG (SImode, MACH_REG);
6332       r1 = gen_rtx_REG (SImode, R1_REG);
6333
6334       /* Since such a call function may use all call-clobbered
6335          registers, we force a mode switch earlier, so that we don't
6336          run out of registers when adjusting fpscr for the call.  */
6337       emit_insn (gen_force_mode_for_call ());
6338
6339       operands[0] = gen_rtx_SYMBOL_REF (SImode,
6340                                         \"__GCC_shcompact_call_trampoline\");
6341       if (flag_pic)
6342         {
6343           rtx reg = gen_reg_rtx (Pmode);
6344
6345           emit_insn (gen_symGOT2reg (reg, operands[0]));
6346           operands[0] = reg;
6347         }
6348       operands[0] = force_reg (SImode, operands[0]);
6349
6350       /* We don't need a return trampoline, since the callee will
6351          return directly to the upper caller.  */
6352       if (cookie & CALL_COOKIE_RET_TRAMP (1))
6353         {
6354           cookie &= ~ CALL_COOKIE_RET_TRAMP (1);
6355           cookie_rtx = GEN_INT (cookie);
6356         }
6357
6358       emit_move_insn (mach, func);
6359       emit_move_insn (r1, cookie_rtx);
6360
6361       emit_call_insn (gen_sibcall_compact (operands[0], operands[1], mach));
6362       DONE;
6363     }
6364   else if (TARGET_SHCOMPACT && flag_pic
6365            && GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
6366            && ! SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
6367     {
6368       rtx reg = gen_reg_rtx (Pmode);
6369
6370       emit_insn (gen_symGOT2reg (reg, XEXP (operands[0], 0)));
6371       XEXP (operands[0], 0) = reg;
6372     }
6373   if (flag_pic && TARGET_SH2
6374       && GET_CODE (operands[0]) == MEM
6375       && GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
6376       /* The PLT needs the PIC register, but the epilogue would have
6377          to restore it, so we can only use PC-relative PIC calls for
6378          static functions.  */
6379       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
6380     {
6381       emit_call_insn (gen_sibcall_pcrel (XEXP (operands[0], 0), operands[1]));
6382       DONE;
6383     }
6384   else
6385     operands[0] = force_reg (SImode, XEXP (operands[0], 0));
6386
6387   emit_call_insn (gen_sibcalli (operands[0], operands[1]));
6388   DONE;
6389 }")
6390
6391 (define_expand "sibcall_value"
6392   [(set (match_operand 0 "" "")
6393         (call (match_operand 1 "" "")
6394               (match_operand 2 "" "")))
6395    (match_operand 3 "" "")]
6396   ""
6397   "
6398 {
6399   emit_call_insn (gen_sibcall (operands[1], operands[2], operands[3]));
6400   DONE;
6401 }")
6402
6403 (define_insn "call_value_pop_compact"
6404   [(set (match_operand 0 "" "=rf")
6405         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
6406               (match_operand 2 "" "")))
6407    (set (reg:SI SP_REG) (plus:SI (reg:SI SP_REG)
6408                                  (match_operand 4 "immediate_operand" "n")))
6409    (match_operand 3 "immediate_operand" "n")
6410    (use (reg:SI R0_REG))
6411    (use (reg:SI R1_REG))
6412    (use (reg:PSI FPSCR_REG))
6413    (clobber (reg:SI PR_REG))]
6414   "TARGET_SHCOMPACT && ! (INTVAL (operands[3]) & CALL_COOKIE_RET_TRAMP (1))"
6415   "jsr  @%1%#"
6416   [(set_attr "type" "call")
6417    (set (attr "fp_mode")
6418         (if_then_else (eq_attr "fpu_single" "yes")
6419                       (const_string "single") (const_string "double")))
6420    (set_attr "needs_delay_slot" "yes")])
6421
6422 (define_insn "call_value_pop_compact_rettramp"
6423   [(set (match_operand 0 "" "=rf")
6424         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
6425               (match_operand 2 "" "")))
6426    (set (reg:SI SP_REG) (plus:SI (reg:SI SP_REG)
6427                                  (match_operand 4 "immediate_operand" "n")))
6428    (match_operand 3 "immediate_operand" "n")
6429    (use (reg:SI R0_REG))
6430    (use (reg:SI R1_REG))
6431    (use (reg:PSI FPSCR_REG))
6432    (clobber (reg:SI R10_REG))
6433    (clobber (reg:SI PR_REG))]
6434   "TARGET_SHCOMPACT && (INTVAL (operands[3]) & CALL_COOKIE_RET_TRAMP (1))"
6435   "jsr  @%1%#"
6436   [(set_attr "type" "call")
6437    (set (attr "fp_mode")
6438         (if_then_else (eq_attr "fpu_single" "yes")
6439                       (const_string "single") (const_string "double")))
6440    (set_attr "needs_delay_slot" "yes")])
6441
6442 (define_expand "call_value_pop"
6443   [(parallel [(set (match_operand 0 "arith_reg_operand" "")
6444                    (call (mem:SI (match_operand 1 "arith_reg_operand" ""))
6445                                  (match_operand 2 "" "")))
6446               (match_operand 3 "" "")
6447               (set (reg:SI SP_REG) (plus:SI (reg:SI SP_REG)
6448                                             (match_operand 4 "" "")))])]
6449   "TARGET_SHCOMPACT"
6450   "
6451 {
6452   if (TARGET_SHCOMPACT && operands[3] && INTVAL (operands[3]))
6453     {
6454       rtx cookie_rtx = operands[3];
6455       long cookie = INTVAL (cookie_rtx);
6456       rtx func = XEXP (operands[1], 0);
6457       rtx r0, r1;
6458
6459       if (flag_pic)
6460         {
6461           if (GET_CODE (func) == SYMBOL_REF && ! SYMBOL_REF_FLAG (func))
6462             {
6463               rtx reg = gen_reg_rtx (Pmode);
6464
6465               emit_insn (gen_symGOTPLT2reg (reg, func));
6466               func = reg;
6467             }
6468           else
6469             func = legitimize_pic_address (func, Pmode, 0);
6470         }
6471
6472       r0 = gen_rtx_REG (SImode, R0_REG);
6473       r1 = gen_rtx_REG (SImode, R1_REG);
6474
6475       /* Since such a call function may use all call-clobbered
6476          registers, we force a mode switch earlier, so that we don't
6477          run out of registers when adjusting fpscr for the call.  */
6478       emit_insn (gen_force_mode_for_call ());
6479
6480       operands[1] = gen_rtx_SYMBOL_REF (SImode,
6481                                         \"__GCC_shcompact_call_trampoline\");
6482       if (flag_pic)
6483         {
6484           rtx reg = gen_reg_rtx (Pmode);
6485
6486           emit_insn (gen_symGOTPLT2reg (reg, operands[1]));
6487           operands[1] = reg;
6488         }
6489       operands[1] = force_reg (SImode, operands[1]);
6490
6491       emit_move_insn (r0, func);
6492       emit_move_insn (r1, cookie_rtx);
6493
6494       if (cookie & CALL_COOKIE_RET_TRAMP (1))
6495         emit_call_insn (gen_call_value_pop_compact_rettramp
6496                         (operands[0], operands[1], operands[2],
6497                          operands[3], operands[4]));
6498       else
6499         emit_call_insn (gen_call_value_pop_compact
6500                         (operands[0], operands[1], operands[2],
6501                          operands[3], operands[4]));
6502
6503       DONE;
6504     }
6505
6506   abort ();
6507 }")
6508
6509 (define_expand "sibcall_epilogue"
6510   [(return)]
6511   ""
6512   "
6513 {
6514   sh_expand_epilogue ();
6515   if (TARGET_SHCOMPACT)
6516     {
6517       rtx insn, set;
6518
6519       /* If epilogue clobbers r0, preserve it in macl.  */
6520       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6521         if ((set = single_set (insn))
6522             && GET_CODE (SET_DEST (set)) == REG
6523             && REGNO (SET_DEST (set)) == R0_REG)
6524           {
6525             rtx r0 = gen_rtx_REG (SImode, R0_REG);
6526             rtx tmp = gen_rtx_REG (SImode, MACL_REG);
6527             rtx i;
6528
6529             /* We can't tell at this point whether the sibcall is a
6530                sibcall_compact and, if it is, whether it uses r0 or
6531                mach as operand 2, so let the instructions that
6532                preserve r0 be optimized away if r0 turns out to be
6533                dead.  */
6534             i = emit_insn_before (gen_rtx_SET (SImode, tmp, r0), insn);
6535             REG_NOTES (i) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx,
6536                                                REG_NOTES (i));
6537             i = emit_move_insn (r0, tmp);
6538             REG_NOTES (i) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx,
6539                                                REG_NOTES (i));
6540             break;
6541           }
6542     }
6543   DONE;
6544 }")
6545
6546 (define_insn "indirect_jump_compact"
6547   [(set (pc)
6548         (match_operand:SI 0 "arith_reg_operand" "r"))]
6549   "TARGET_SH1"
6550   "jmp  @%0%#"
6551   [(set_attr "needs_delay_slot" "yes")
6552    (set_attr "type" "jump_ind")])
6553
6554 (define_expand "indirect_jump"
6555   [(set (pc)
6556         (match_operand 0 "register_operand" ""))]
6557   ""
6558   "
6559 {
6560   if (TARGET_SHMEDIA && GET_MODE (operands[0]) == SImode)
6561     operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
6562 }")
6563
6564 ;; The use of operand 1 / 2 helps us distinguish case table jumps
6565 ;; which can be present in structured code from indirect jumps which can not
6566 ;; be present in structured code.  This allows -fprofile-arcs to work.
6567
6568 ;; For SH1 processors.
6569 (define_insn "casesi_jump_1"
6570   [(set (pc)
6571         (match_operand:SI 0 "register_operand" "r"))
6572    (use (label_ref (match_operand 1 "" "")))]
6573   "TARGET_SH1"
6574   "jmp  @%0%#"
6575   [(set_attr "needs_delay_slot" "yes")
6576    (set_attr "type" "jump_ind")])
6577
6578 ;; For all later processors.
6579 (define_insn "casesi_jump_2"
6580   [(set (pc) (plus:SI (match_operand:SI 0 "register_operand" "r")
6581                       (label_ref (match_operand 1 "" ""))))
6582    (use (label_ref (match_operand 2 "" "")))]
6583   "TARGET_SH2
6584    && (! INSN_UID (operands[1]) || prev_real_insn (operands[1]) == insn)"
6585   "braf %0%#"
6586   [(set_attr "needs_delay_slot" "yes")
6587    (set_attr "type" "jump_ind")])
6588
6589 (define_insn "casesi_jump_media"
6590   [(set (pc) (match_operand:DI 0 "target_reg_operand" "b"))
6591    (use (label_ref (match_operand 1 "" "")))]
6592   "TARGET_SHMEDIA"
6593   "blink        %0, r63"
6594   [(set_attr "type" "jump_media")])
6595
6596 ;; Call subroutine returning any type.
6597 ;; ??? This probably doesn't work.
6598
6599 (define_expand "untyped_call"
6600   [(parallel [(call (match_operand 0 "" "")
6601                     (const_int 0))
6602               (match_operand 1 "" "")
6603               (match_operand 2 "" "")])]
6604   "TARGET_SH3E || TARGET_SHMEDIA"
6605   "
6606 {
6607   int i;
6608
6609   emit_call_insn (gen_call (operands[0], const0_rtx, const0_rtx));
6610
6611   for (i = 0; i < XVECLEN (operands[2], 0); i++)
6612     {
6613       rtx set = XVECEXP (operands[2], 0, i);
6614       emit_move_insn (SET_DEST (set), SET_SRC (set));
6615     }
6616
6617   /* The optimizer does not know that the call sets the function value
6618      registers we stored in the result block.  We avoid problems by
6619      claiming that all hard registers are used and clobbered at this
6620      point.  */
6621   emit_insn (gen_blockage ());
6622
6623   DONE;
6624 }")
6625 \f
6626 ;; ------------------------------------------------------------------------
6627 ;; Misc insns
6628 ;; ------------------------------------------------------------------------
6629
6630 (define_insn "dect"
6631   [(set (reg:SI T_REG)
6632         (eq:SI (match_operand:SI 0 "arith_reg_operand" "+r") (const_int 1)))
6633    (set (match_dup 0) (plus:SI (match_dup 0) (const_int -1)))]
6634   "TARGET_SH2"
6635   "dt   %0"
6636   [(set_attr "type" "arith")])
6637
6638 (define_insn "nop"
6639   [(const_int 0)]
6640   ""
6641   "nop")
6642
6643 ;; Load address of a label. This is only generated by the casesi expand,
6644 ;; and by machine_dependent_reorg (fixing up fp moves).
6645 ;; This must use unspec, because this only works for labels that are
6646 ;; within range,
6647
6648 (define_insn "mova"
6649   [(set (reg:SI R0_REG)
6650         (unspec:SI [(label_ref (match_operand 0 "" ""))] UNSPEC_MOVA))]
6651   "TARGET_SH1"
6652   "mova %O0,r0"
6653   [(set_attr "in_delay_slot" "no")
6654    (set_attr "type" "arith")])
6655
6656 ;; machine_dependent_reorg() will make this a `mova'.
6657 (define_insn "mova_const"
6658   [(set (reg:SI R0_REG)
6659         (unspec:SI [(match_operand 0 "immediate_operand" "i")] UNSPEC_MOVA))]
6660   "TARGET_SH1"
6661   "#"
6662   [(set_attr "in_delay_slot" "no")
6663    (set_attr "type" "arith")])
6664
6665 (define_expand "GOTaddr2picreg"
6666   [(set (reg:SI R0_REG)
6667         (unspec:SI [(const:SI (unspec:SI [(match_dup 1)] UNSPEC_PIC))]
6668                    UNSPEC_MOVA))
6669    (set (match_dup 0) (const:SI (unspec:SI [(match_dup 1)] UNSPEC_PIC)))
6670    (set (match_dup 0) (plus:SI (match_dup 0) (reg:SI R0_REG)))]
6671   "" "
6672 {
6673   operands[0] = gen_rtx_REG (Pmode, PIC_REG);
6674   operands[1] = gen_rtx_SYMBOL_REF (VOIDmode, GOT_SYMBOL_NAME);
6675
6676   if (TARGET_SH5)
6677     operands[1] = gen_datalabel_ref (operands[1]);
6678
6679   if (TARGET_SHMEDIA)
6680     {
6681       rtx tr = gen_rtx_REG (DImode, TR0_REG);
6682       rtx dipic = operands[0];
6683       rtx lab = PATTERN (gen_call_site ());
6684       rtx insn, equiv;
6685
6686       equiv = operands[1];
6687       operands[1] = gen_rtx_MINUS (DImode,
6688                                    operands[1],
6689                                    gen_rtx_CONST
6690                                    (DImode,
6691                                     gen_rtx_MINUS (DImode,
6692                                                    gen_rtx_CONST (DImode,
6693                                                                   lab),
6694                                                    pc_rtx)));
6695       operands[1] = gen_sym2PIC (operands[1]);
6696       PUT_MODE (operands[1], DImode);
6697
6698       if (GET_MODE (dipic) != DImode)
6699         dipic = gen_rtx_SUBREG (DImode, dipic, 0);
6700
6701       if (TARGET_SHMEDIA64)
6702         emit_insn (gen_movdi_const (dipic, operands[1]));
6703       else
6704         emit_insn (gen_movdi_const_32bit (dipic, operands[1]));
6705
6706       emit_insn (gen_ptrel (tr, dipic, lab));
6707
6708       if (GET_MODE (operands[0]) != GET_MODE (tr))
6709         tr = gen_rtx_SUBREG (GET_MODE (operands[0]), tr, 0);
6710
6711       insn = emit_move_insn (operands[0], tr);
6712
6713       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, equiv,
6714                                             REG_NOTES (insn));
6715
6716       DONE;
6717     }
6718 }
6719 ")
6720
6721 ;; When generating PIC, we must match label_refs especially, because
6722 ;; they do not satisfy LEGITIMATE_PIC_OPERAND_P(), and we don't want
6723 ;; them to do, because they can't be loaded directly into
6724 ;; non-branch-target registers.
6725 (define_insn "*pt"
6726   [(set (match_operand:DI 0 "target_reg_operand" "=b")
6727         (match_operand:DI 1 "" "T"))]
6728   "TARGET_SHMEDIA && flag_pic
6729    && EXTRA_CONSTRAINT_T (operands[1])"
6730   "pt   %1, %0"
6731   [(set_attr "type" "pt_media")
6732    (set_attr "length" "*")])
6733
6734 (define_insn "*ptb"
6735   [(set (match_operand:DI 0 "target_reg_operand" "=b")
6736         (const:DI (unspec:DI [(match_operand:DI 1 "" "T")]
6737                              UNSPEC_DATALABEL)))]
6738   "TARGET_SHMEDIA && flag_pic
6739    && EXTRA_CONSTRAINT_T (operands[1])"
6740   "ptb/u        datalabel %1, %0"
6741   [(set_attr "type" "pt_media")
6742    (set_attr "length" "*")])
6743
6744 (define_insn "ptrel"
6745   [(set (match_operand:DI 0 "target_reg_operand" "=b")
6746         (plus:DI (match_operand:DI 1 "register_operand" "r")
6747               (pc)))
6748    (match_operand:DI 2 "" "")]
6749   "TARGET_SHMEDIA"
6750   "%O2: ptrel/u %1, %0"
6751   [(set_attr "type" "ptabs_media")])
6752
6753 (define_expand "builtin_setjmp_receiver"
6754   [(match_operand 0 "" "")]
6755   "flag_pic"
6756   "
6757 {
6758   emit_insn (gen_GOTaddr2picreg ());
6759   DONE;
6760 }")
6761
6762 (define_expand "call_site"
6763   [(unspec [(match_dup 0)] UNSPEC_CALLER)]
6764   "TARGET_SH1"
6765   "
6766 {
6767   static HOST_WIDE_INT i = 0;
6768   operands[0] = GEN_INT (i);
6769   i++;
6770 }")
6771
6772 (define_expand "sym_label2reg"
6773   [(set (match_operand:SI 0 "" "")
6774         (const:SI (minus:SI
6775                    (const:SI
6776                     (unspec:SI [(match_operand:SI 1 "" "")] UNSPEC_PIC))
6777                    (const:SI
6778                     (plus:SI
6779                      (match_operand:SI 2 "" "")
6780                      (const_int 2))))))]
6781   "TARGET_SH1" "")
6782
6783 (define_expand "symGOT_load"
6784   [(set (match_dup 2) (match_operand 1 "" ""))
6785    (set (match_dup 3) (plus (match_dup 2) (reg PIC_REG)))
6786    (set (match_operand 0 "" "") (mem (match_dup 3)))]
6787   ""
6788   "
6789 {
6790   rtx insn;
6791
6792   operands[2] = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
6793   operands[3] = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
6794
6795   if (TARGET_SHMEDIA)
6796     {
6797       rtx reg = operands[2];
6798
6799       if (GET_MODE (reg) != DImode)
6800         reg = gen_rtx_SUBREG (DImode, reg, 0);
6801
6802       if (flag_pic > 1)
6803         emit_insn (gen_movdi_const_32bit (reg, operands[1]));
6804       else
6805         emit_insn (gen_movdi_const_16bit (reg, operands[1]));
6806     }
6807   else
6808     emit_move_insn (operands[2], operands[1]);
6809
6810   emit_move_insn (operands[3], gen_rtx_PLUS (Pmode,
6811                                              operands[2],
6812                                              gen_rtx_REG (Pmode, PIC_REG)));
6813
6814   insn = emit_move_insn (operands[0], gen_rtx_MEM (Pmode, operands[3]));
6815
6816   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, XVECEXP (XEXP (operands[1],
6817                                                                   0), 0, 0),
6818                                         REG_NOTES (insn));
6819
6820   DONE;
6821 }")
6822
6823 (define_expand "sym2GOT"
6824   [(const (unspec [(match_operand 0 "" "")] UNSPEC_GOT))]
6825   ""
6826   "")
6827
6828 (define_expand "symGOT2reg"
6829   [(match_operand 0 "" "") (match_operand 1 "" "")]
6830   ""
6831   "
6832 {
6833   rtx gotsym, insn;
6834
6835   gotsym = gen_sym2GOT (operands[1]);
6836   PUT_MODE (gotsym, Pmode);
6837   insn = emit_insn (gen_symGOT_load (operands[0], gotsym));
6838
6839   RTX_UNCHANGING_P (SET_SRC (PATTERN (insn))) = 1;
6840
6841   DONE;
6842 }")
6843
6844 (define_expand "sym2GOTPLT"
6845   [(const (unspec [(match_operand 0 "" "")] UNSPEC_GOTPLT))]
6846   ""
6847   "")
6848
6849 (define_expand "symGOTPLT2reg"
6850   [(match_operand 0 "" "") (match_operand 1 "" "")]
6851   ""
6852   "
6853 {
6854   emit_insn (gen_symGOT_load (operands[0], gen_sym2GOTPLT (operands[1])));
6855   DONE;
6856 }")
6857
6858 (define_expand "sym2GOTOFF"
6859   [(const (unspec [(match_operand 0 "" "")] UNSPEC_GOTOFF))]
6860   ""
6861   "")
6862
6863 (define_expand "symGOTOFF2reg"
6864   [(match_operand 0 "" "") (match_operand 1 "" "")]
6865   ""
6866   "
6867 {
6868   rtx gotoffsym, insn;
6869   rtx t = no_new_pseudos ? operands[0] : gen_reg_rtx (GET_MODE (operands[0]));
6870
6871   gotoffsym = gen_sym2GOTOFF (operands[1]);
6872   PUT_MODE (gotoffsym, Pmode);
6873   emit_move_insn (t, gotoffsym);
6874   insn = emit_move_insn (operands[0],
6875                          gen_rtx_PLUS (Pmode, t,
6876                                        gen_rtx_REG (Pmode, PIC_REG)));
6877
6878   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, operands[1],
6879                                         REG_NOTES (insn));
6880
6881   DONE;
6882 }")
6883
6884 (define_expand "symPLT_label2reg"
6885   [(set (match_operand:SI 0 "" "")
6886         (const:SI (minus:SI
6887                    (const:SI
6888                     (unspec:SI [(match_operand:SI 1 "" "")] UNSPEC_PLT))
6889                    (const:SI
6890                     (minus:SI
6891                      (const:SI (plus:SI
6892                                 (match_operand:SI 2 "" "")
6893                                 (const_int 2)))
6894                      (const:SI (unspec:SI [(pc)] UNSPEC_PIC)))))))
6895    ;; Even though the PIC register is not really used by the call
6896    ;; sequence in which this is expanded, the PLT code assumes the PIC
6897    ;; register is set, so we must not skip its initialization.  Since
6898    ;; we only use this expand as part of calling sequences, and never
6899    ;; to take the address of a function, this is the best point to
6900    ;; insert the (use).  Using the PLT to take the address of a
6901    ;; function would be wrong, not only because the PLT entry could
6902    ;; then be called from a function that doesn't initialize the PIC
6903    ;; register to the proper GOT, but also because pointers to the
6904    ;; same function might not compare equal, should they be set by
6905    ;; different shared libraries.
6906    (use (reg:SI PIC_REG))]
6907   "TARGET_SH1"
6908   "")
6909
6910 (define_expand "sym2PIC"
6911   [(const (unspec [(match_operand:SI 0 "" "")] UNSPEC_PIC))]
6912   ""
6913   "")
6914
6915 ;; case instruction for switch statements.
6916
6917 ;; Operand 0 is index
6918 ;; operand 1 is the minimum bound
6919 ;; operand 2 is the maximum bound - minimum bound + 1
6920 ;; operand 3 is CODE_LABEL for the table;
6921 ;; operand 4 is the CODE_LABEL to go to if index out of range.
6922
6923 (define_expand "casesi"
6924   [(match_operand:SI 0 "arith_reg_operand" "")
6925    (match_operand:SI 1 "arith_reg_operand" "")
6926    (match_operand:SI 2 "arith_reg_operand" "")
6927    (match_operand 3 "" "") (match_operand 4 "" "")]
6928   ""
6929   "
6930 {
6931   rtx reg = gen_reg_rtx (SImode);
6932   rtx reg2 = gen_reg_rtx (SImode);
6933   if (TARGET_SHMEDIA)
6934     {
6935       rtx reg = gen_reg_rtx (DImode);
6936       rtx reg2 = gen_reg_rtx (DImode);
6937       rtx reg3 = gen_reg_rtx (DImode);
6938       rtx reg4 = gen_reg_rtx (DImode);
6939       rtx reg5 = gen_reg_rtx (DImode);
6940
6941       operands[0] = convert_modes (DImode, SImode, operands[0], 0);
6942       operands[1] = convert_modes (DImode, SImode, operands[1], 0);
6943       operands[2] = convert_modes (DImode, SImode, operands[2], 1);
6944
6945       emit_jump_insn (gen_bgt_media (operands[4], operands[1], operands[0]));
6946       emit_move_insn (reg, gen_rtx_MINUS (DImode, operands[0], operands[1]));
6947       emit_jump_insn (gen_bgtu_media (operands[4], reg, operands[2]));
6948       emit_insn (gen_casesi_shift_media (reg2, reg, operands[3]));
6949       emit_move_insn (reg3, gen_datalabel_ref (gen_rtx_LABEL_REF
6950                                                (DImode, operands[3])));
6951       emit_insn (gen_casesi_load_media (reg4, reg3, reg2, operands[3]));
6952       emit_move_insn (reg5, gen_rtx_PLUS (DImode, reg3, reg4));
6953       emit_jump_insn (gen_casesi_jump_media (reg5, operands[3]));
6954       emit_barrier ();
6955       DONE;
6956     }
6957   operands[1] = copy_to_mode_reg (SImode, operands[1]);
6958   operands[2] = copy_to_mode_reg (SImode, operands[2]);
6959   /* If optimizing, casesi_worker depends on the mode of the instruction
6960      before label it 'uses' - operands[3].  */
6961   emit_insn (gen_casesi_0 (operands[0], operands[1], operands[2], operands[4],
6962                            reg));
6963   emit_insn (gen_casesi_worker_0 (reg2, reg, operands[3]));
6964   if (TARGET_SH2)
6965     emit_jump_insn (gen_casesi_jump_2 (reg2, gen_label_rtx (), operands[3]));
6966   else
6967     emit_jump_insn (gen_casesi_jump_1 (reg2, operands[3]));
6968   /* For SH2 and newer, the ADDR_DIFF_VEC is not actually relative to
6969      operands[3], but to lab.  We will fix this up in
6970      machine_dependent_reorg.  */
6971   emit_barrier ();
6972   DONE;
6973 }")
6974
6975 (define_expand "casesi_0"
6976   [(set (match_operand:SI 4 "" "") (match_operand:SI 0 "arith_reg_operand" ""))
6977    (set (match_dup 4) (minus:SI (match_dup 4)
6978                                 (match_operand:SI 1 "arith_operand" "")))
6979    (set (reg:SI T_REG)
6980         (gtu:SI (match_dup 4)
6981                 (match_operand:SI 2 "arith_reg_operand" "")))
6982    (set (pc)
6983         (if_then_else (ne (reg:SI T_REG)
6984                           (const_int 0))
6985                       (label_ref (match_operand 3 "" ""))
6986                       (pc)))]
6987   "TARGET_SH1"
6988   "")
6989
6990 ;; ??? reload might clobber r0 if we use it explicitly in the RTL before
6991 ;; reload; using a R0_REGS pseudo reg is likely to give poor code.
6992 ;; So we keep the use of r0 hidden in a R0_REGS clobber until after reload.
6993
6994 (define_insn "casesi_worker_0"
6995   [(set (match_operand:SI 0 "register_operand" "=r,r")
6996         (unspec:SI [(match_operand:SI 1 "register_operand" "0,r")
6997                  (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
6998    (clobber (match_scratch:SI 3 "=X,1"))
6999    (clobber (match_scratch:SI 4 "=&z,z"))]
7000   "TARGET_SH1"
7001   "#")
7002
7003 (define_split
7004   [(set (match_operand:SI 0 "register_operand" "")
7005         (unspec:SI [(match_operand:SI 1 "register_operand" "")
7006                     (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
7007    (clobber (match_scratch:SI 3 ""))
7008    (clobber (match_scratch:SI 4 ""))]
7009   "TARGET_SH1 && ! TARGET_SH2 && reload_completed"
7010   [(set (reg:SI R0_REG) (unspec:SI [(label_ref (match_dup 2))] UNSPEC_MOVA))
7011    (parallel [(set (match_dup 0)
7012               (unspec:SI [(reg:SI R0_REG) (match_dup 1)
7013                           (label_ref (match_dup 2))] UNSPEC_CASESI))
7014               (clobber (match_dup 3))])
7015    (set (match_dup 0) (plus:SI (match_dup 0) (reg:SI R0_REG)))]
7016   "if (GET_CODE (operands[2]) == CODE_LABEL) LABEL_NUSES (operands[2])++;")
7017
7018 (define_split
7019   [(set (match_operand:SI 0 "register_operand" "")
7020         (unspec:SI [(match_operand:SI 1 "register_operand" "")
7021                     (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
7022    (clobber (match_scratch:SI 3 ""))
7023    (clobber (match_scratch:SI 4 ""))]
7024   "TARGET_SH2 && reload_completed"
7025   [(set (reg:SI R0_REG) (unspec:SI [(label_ref (match_dup 2))] UNSPEC_MOVA))
7026    (parallel [(set (match_dup 0)
7027               (unspec:SI [(reg:SI R0_REG) (match_dup 1)
7028                           (label_ref (match_dup 2))] UNSPEC_CASESI))
7029               (clobber (match_dup 3))])]
7030   "if (GET_CODE (operands[2]) == CODE_LABEL) LABEL_NUSES (operands[2])++;")
7031
7032 (define_insn "*casesi_worker"
7033   [(set (match_operand:SI 0 "register_operand" "=r,r")
7034         (unspec:SI [(reg:SI R0_REG)
7035                     (match_operand:SI 1 "register_operand" "0,r")
7036                     (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
7037    (clobber (match_scratch:SI 3 "=X,1"))]
7038   "TARGET_SH1"
7039   "*
7040 {
7041   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
7042
7043   if (GET_CODE (diff_vec) != ADDR_DIFF_VEC)
7044     abort ();
7045
7046   switch (GET_MODE (diff_vec))
7047     {
7048     case SImode:
7049       return \"shll2    %1\;mov.l       @(r0,%1),%0\";
7050     case HImode:
7051       return \"add      %1,%1\;mov.w    @(r0,%1),%0\";
7052     case QImode:
7053       if (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned)
7054         return \"mov.b  @(r0,%1),%0\;extu.b     %0,%0\";
7055       return \"mov.b    @(r0,%1),%0\";
7056     default:
7057       abort ();
7058     }
7059 }"
7060   [(set_attr "length" "4")])
7061
7062 (define_insn "casesi_shift_media"
7063   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
7064         (ashift:DI (match_operand:DI 1 "arith_reg_operand" "r")
7065                    (unspec:DI [(label_ref:DI (match_operand 2 "" ""))]
7066                     UNSPEC_CASESI)))]
7067   "TARGET_SHMEDIA"
7068   "*
7069 {
7070   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
7071
7072   if (GET_CODE (diff_vec) != ADDR_DIFF_VEC)
7073     abort ();
7074
7075   switch (GET_MODE (diff_vec))
7076     {
7077     case SImode:
7078       return \"shlli    %1, 2, %0\";
7079     case HImode:
7080       return \"shlli    %1, 1, %0\";
7081     case QImode:
7082       if (rtx_equal_p (operands[0], operands[1]))
7083         return \"\";
7084       return \"add      %1, r63, %0\";
7085     default:
7086       abort ();
7087     }
7088 }"
7089   [(set_attr "type" "arith_media")])
7090
7091 (define_insn "casesi_load_media"
7092   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
7093         (mem:DI (unspec [(match_operand 1 "arith_reg_operand" "r")
7094                          (match_operand 2 "arith_reg_operand" "r")
7095                          (label_ref:DI (match_operand 3 "" ""))] 2)))]
7096   "TARGET_SHMEDIA"
7097   "*
7098 {
7099   rtx diff_vec = PATTERN (next_real_insn (operands[3]));
7100
7101   if (GET_CODE (diff_vec) != ADDR_DIFF_VEC)
7102     abort ();
7103
7104   switch (GET_MODE (diff_vec))
7105     {
7106     case SImode:
7107       return \"ldx.l    %1, %2, %0\";
7108     case HImode:
7109 #if 0
7110       if (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned)
7111         return \"ldx.uw %1, %2, %0\";
7112 #endif
7113       return \"ldx.w    %1, %2, %0\";
7114     case QImode:
7115       if (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned)
7116         return \"ldx.ub %1, %2, %0\";
7117       return \"ldx.b    %1, %2, %0\";
7118     default:
7119       abort ();
7120     }
7121 }"
7122   [(set_attr "type" "load_media")])
7123
7124 (define_expand "return"
7125   [(return)]
7126   "reload_completed && ! sh_need_epilogue ()"
7127   "
7128 {
7129   if (TARGET_SHMEDIA)
7130     {
7131       emit_jump_insn (gen_return_media ());
7132       DONE;
7133     }
7134
7135   if (TARGET_SHCOMPACT
7136       && (current_function_args_info.call_cookie & CALL_COOKIE_RET_TRAMP (1)))
7137     {
7138       emit_jump_insn (gen_shcompact_return_tramp ());
7139       DONE;
7140     }
7141 }")
7142
7143 (define_insn "*return_i"
7144   [(return)]
7145   "TARGET_SH1 && ! (TARGET_SHCOMPACT
7146                     && (current_function_args_info.call_cookie
7147                         & CALL_COOKIE_RET_TRAMP (1)))
7148    && reload_completed"
7149   "%@   %#"
7150   [(set_attr "type" "return")
7151    (set_attr "needs_delay_slot" "yes")])
7152
7153 (define_expand "shcompact_return_tramp"
7154   [(return)]
7155   "TARGET_SHCOMPACT
7156    && (current_function_args_info.call_cookie & CALL_COOKIE_RET_TRAMP (1))"
7157   "
7158 {
7159   rtx reg = gen_rtx_REG (Pmode, R0_REG);
7160   rtx sym = gen_rtx_SYMBOL_REF (Pmode,
7161                                 \"__GCC_shcompact_return_trampoline\");
7162
7163   if (flag_pic)
7164     emit_insn (gen_symGOTPLT2reg (reg, sym));
7165   else
7166     emit_move_insn (reg, sym);
7167
7168   emit_jump_insn (gen_shcompact_return_tramp_i ());
7169   DONE;
7170 }")
7171
7172 (define_insn "shcompact_return_tramp_i"
7173   [(parallel [(return) (use (reg:SI R0_REG))])]
7174   "TARGET_SHCOMPACT
7175    && (current_function_args_info.call_cookie & CALL_COOKIE_RET_TRAMP (1))"
7176   "jmp  @r0%#"
7177   [(set_attr "type" "jump_ind")
7178    (set_attr "needs_delay_slot" "yes")])
7179
7180 (define_insn "return_media_i"
7181   [(parallel [(return) (use (match_operand:DI 0 "target_reg_operand" "k"))])]
7182   "TARGET_SHMEDIA && reload_completed"
7183   "blink        %0, r63"
7184   [(set_attr "type" "jump_media")])
7185
7186 (define_expand "return_media"
7187   [(return)]
7188   "TARGET_SHMEDIA && reload_completed"
7189   "
7190 {
7191   int tr_regno = sh_media_register_for_return ();
7192   rtx tr;
7193
7194   if (tr_regno < 0)
7195     {
7196       rtx r18 = gen_rtx_REG (DImode, PR_MEDIA_REG);
7197
7198       tr_regno = TR0_REG;
7199       tr = gen_rtx_REG (DImode, tr_regno);
7200       emit_move_insn (tr, r18);
7201     }
7202   else
7203     tr = gen_rtx_REG (DImode, tr_regno);
7204
7205   emit_jump_insn (gen_return_media_i (tr));
7206   DONE;
7207 }")
7208
7209 (define_insn "shcompact_preserve_incoming_args"
7210   [(set (match_operand:SI 0 "register_operand" "+r")
7211         (unspec:SI [(match_dup 0)] UNSPEC_COMPACT_ARGS))]
7212   "TARGET_SHCOMPACT"
7213   ""
7214   [(set_attr "length" "0")])
7215
7216 (define_insn "shcompact_incoming_args"
7217   [(set (reg:SI R2_REG) (unspec:SI [(reg:SI R2_REG)] UNSPEC_COMPACT_ARGS))
7218    (set (reg:SI R3_REG) (unspec:SI [(reg:SI R3_REG)] UNSPEC_COMPACT_ARGS))
7219    (set (reg:SI R4_REG) (unspec:SI [(reg:SI R4_REG)] UNSPEC_COMPACT_ARGS))
7220    (set (reg:SI R5_REG) (unspec:SI [(reg:SI R5_REG)] UNSPEC_COMPACT_ARGS))
7221    (set (reg:SI R6_REG) (unspec:SI [(reg:SI R6_REG)] UNSPEC_COMPACT_ARGS))
7222    (set (reg:SI R7_REG) (unspec:SI [(reg:SI R7_REG)] UNSPEC_COMPACT_ARGS))
7223    (set (reg:SI R8_REG) (unspec:SI [(reg:SI R8_REG)] UNSPEC_COMPACT_ARGS))
7224    (set (reg:SI R9_REG) (unspec:SI [(reg:SI R9_REG)] UNSPEC_COMPACT_ARGS))
7225    (set (mem:BLK (reg:SI MACL_REG))
7226         (unspec:BLK [(reg:SI MACH_REG)] UNSPEC_COMPACT_ARGS))
7227    (use (reg:SI R0_REG))
7228    (clobber (reg:SI R0_REG))
7229    (clobber (reg:SI MACL_REG))
7230    (clobber (reg:SI MACH_REG))
7231    (clobber (reg:SI PR_REG))]
7232   "TARGET_SHCOMPACT"
7233   "jsr  @r0%#"
7234   [(set_attr "needs_delay_slot" "yes")])
7235
7236 (define_insn "shmedia_save_restore_regs_compact"
7237   [(set (reg:SI SP_REG)
7238         (plus:SI (reg:SI SP_REG)
7239                  (match_operand:SI 0 "immediate_operand" "i")))
7240    (use (reg:SI R0_REG))
7241    (clobber (reg:SI PR_REG))]
7242   "TARGET_SHCOMPACT
7243    && (INTVAL (operands[0]) == SHMEDIA_REGS_STACK_ADJUST ()
7244        || INTVAL (operands[0]) == - SHMEDIA_REGS_STACK_ADJUST ())"
7245   "jsr @r0%#"
7246   [(set_attr "needs_delay_slot" "yes")])
7247
7248 (define_expand "prologue"
7249   [(const_int 0)]
7250   ""
7251   "sh_expand_prologue (); DONE;")
7252
7253 (define_expand "epilogue"
7254   [(return)]
7255   ""
7256   "
7257 {
7258   sh_expand_epilogue ();
7259   emit_jump_insn (gen_return ());
7260   DONE;
7261 }")
7262
7263 (define_insn "blockage"
7264   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)]
7265   ""
7266   ""
7267   [(set_attr "length" "0")])
7268 \f
7269 ;; ------------------------------------------------------------------------
7270 ;; Scc instructions
7271 ;; ------------------------------------------------------------------------
7272
7273 (define_insn "movt"
7274   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
7275         (eq:SI (reg:SI T_REG) (const_int 1)))]
7276   "TARGET_SH1"
7277   "movt %0"
7278   [(set_attr "type" "arith")])
7279
7280 (define_expand "seq"
7281   [(set (match_operand:SI 0 "arith_reg_operand" "")
7282         (match_dup 1))]
7283   ""
7284   "
7285 {
7286   if (TARGET_SHMEDIA)
7287     {
7288       if (GET_MODE (operands[0]) != DImode)
7289         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7290       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7291       if (sh_compare_op1 != const0_rtx)
7292         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7293                                     ? GET_MODE (sh_compare_op0)
7294                                     : GET_MODE (sh_compare_op1),
7295                                     sh_compare_op1);
7296
7297       switch (GET_MODE (sh_compare_op0))
7298         {
7299         case DImode:
7300           emit_insn (gen_cmpeqdi_media (operands[0],
7301                                         sh_compare_op0, sh_compare_op1));
7302           break;
7303
7304         case SFmode:
7305           if (! TARGET_SHMEDIA_FPU)
7306             FAIL;
7307           emit_insn (gen_cmpeqsf_media (operands[0],
7308                                         sh_compare_op0, sh_compare_op1));
7309           break;
7310
7311         case DFmode:
7312           if (! TARGET_SHMEDIA_FPU)
7313             FAIL;
7314           emit_insn (gen_cmpeqdf_media (operands[0],
7315                                         sh_compare_op0, sh_compare_op1));
7316           break;
7317
7318         default:
7319           FAIL;
7320         }
7321       DONE;
7322     }
7323   operands[1] = prepare_scc_operands (EQ);
7324 }")
7325
7326 (define_expand "slt"
7327   [(set (match_operand:SI 0 "arith_reg_operand" "")
7328         (match_dup 1))]
7329   ""
7330   "
7331 {
7332   if (TARGET_SHMEDIA)
7333     {
7334       if (GET_MODE (operands[0]) != DImode)
7335         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7336       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7337       if (sh_compare_op1 != const0_rtx)
7338         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7339                                     ? GET_MODE (sh_compare_op0)
7340                                     : GET_MODE (sh_compare_op1),
7341                                     sh_compare_op1);
7342
7343       switch (GET_MODE (sh_compare_op0))
7344         {
7345         case DImode:
7346           emit_insn (gen_cmpgtdi_media (operands[0],
7347                                         sh_compare_op1, sh_compare_op0));
7348           break;
7349
7350         case SFmode:
7351           if (! TARGET_SHMEDIA_FPU)
7352             FAIL;
7353           emit_insn (gen_cmpgtsf_media (operands[0],
7354                                         sh_compare_op1, sh_compare_op0));
7355           break;
7356
7357         case DFmode:
7358           if (! TARGET_SHMEDIA_FPU)
7359             FAIL;
7360           emit_insn (gen_cmpgtdf_media (operands[0],
7361                                         sh_compare_op1, sh_compare_op0));
7362           break;
7363
7364         default:
7365           FAIL;
7366         }
7367       DONE;
7368     }
7369   operands[1] = prepare_scc_operands (LT);
7370 }")
7371
7372 (define_expand "sle"
7373   [(match_operand:SI 0 "arith_reg_operand" "")]
7374   ""
7375   "
7376 {
7377   rtx tmp = sh_compare_op0;
7378
7379   if (TARGET_SHMEDIA)
7380     {
7381       if (GET_MODE (operands[0]) != DImode)
7382         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7383       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7384       if (sh_compare_op1 != const0_rtx)
7385         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7386                                     ? GET_MODE (sh_compare_op0)
7387                                     : GET_MODE (sh_compare_op1),
7388                                     sh_compare_op1);
7389
7390       switch (GET_MODE (sh_compare_op0))
7391         {
7392         case DImode:
7393           {
7394             tmp = no_new_pseudos ? operands[0] : gen_reg_rtx (DImode);
7395
7396             emit_insn (gen_cmpgtdi_media (tmp,
7397                                           sh_compare_op0, sh_compare_op1));
7398             emit_insn (gen_cmpeqdi_media (operands[0], tmp, const0_rtx));
7399             break;
7400           }
7401
7402         case SFmode:
7403           if (! TARGET_SHMEDIA_FPU)
7404             FAIL;
7405           emit_insn (gen_cmpgesf_media (operands[0],
7406                                         sh_compare_op1, sh_compare_op0));
7407           break;
7408
7409         case DFmode:
7410           if (! TARGET_SHMEDIA_FPU)
7411             FAIL;
7412           emit_insn (gen_cmpgedf_media (operands[0],
7413                                         sh_compare_op1, sh_compare_op0));
7414           break;
7415
7416         default:
7417           FAIL;
7418         }
7419       DONE;
7420     }
7421
7422   sh_compare_op0 = sh_compare_op1;
7423   sh_compare_op1 = tmp;
7424   emit_insn (gen_sge (operands[0]));
7425   DONE;
7426 }")
7427
7428 (define_expand "sgt"
7429   [(set (match_operand:SI 0 "arith_reg_operand" "")
7430         (match_dup 1))]
7431   ""
7432   "
7433 {
7434   if (TARGET_SHMEDIA)
7435     {
7436       if (GET_MODE (operands[0]) != DImode)
7437         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7438       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7439       if (sh_compare_op1 != const0_rtx)
7440         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7441                                     ? GET_MODE (sh_compare_op0)
7442                                     : GET_MODE (sh_compare_op1),
7443                                     sh_compare_op1);
7444
7445       switch (GET_MODE (sh_compare_op0))
7446         {
7447         case DImode:
7448           emit_insn (gen_cmpgtdi_media (operands[0],
7449                                         sh_compare_op0, sh_compare_op1));
7450           break;
7451
7452         case SFmode:
7453           if (! TARGET_SHMEDIA_FPU)
7454             FAIL;
7455           emit_insn (gen_cmpgtsf_media (operands[0],
7456                                         sh_compare_op0, sh_compare_op1));
7457           break;
7458
7459         case DFmode:
7460           if (! TARGET_SHMEDIA_FPU)
7461             FAIL;
7462           emit_insn (gen_cmpgtdf_media (operands[0],
7463                                         sh_compare_op0, sh_compare_op1));
7464           break;
7465
7466         default:
7467           FAIL;
7468         }
7469       DONE;
7470     }
7471   operands[1] = prepare_scc_operands (GT);
7472 }")
7473
7474 (define_expand "sge"
7475   [(set (match_operand:SI 0 "arith_reg_operand" "")
7476         (match_dup 1))]
7477   ""
7478   "
7479 {
7480   if (TARGET_SHMEDIA)
7481     {
7482       if (GET_MODE (operands[0]) != DImode)
7483         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7484       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7485       if (sh_compare_op1 != const0_rtx)
7486         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7487                                     ? GET_MODE (sh_compare_op0)
7488                                     : GET_MODE (sh_compare_op1),
7489                                     sh_compare_op1);
7490
7491       switch (GET_MODE (sh_compare_op0))
7492         {
7493         case DImode:
7494           {
7495             rtx tmp = no_new_pseudos ? operands[0] : gen_reg_rtx (DImode);
7496
7497             emit_insn (gen_cmpgtdi_media (tmp,
7498                                           sh_compare_op1, sh_compare_op0));
7499             emit_insn (gen_cmpeqdi_media (operands[0], tmp, const0_rtx));
7500             break;
7501           }
7502
7503         case SFmode:
7504           if (! TARGET_SHMEDIA_FPU)
7505             FAIL;
7506           emit_insn (gen_cmpgesf_media (operands[0],
7507                                         sh_compare_op0, sh_compare_op1));
7508           break;
7509
7510         case DFmode:
7511           if (! TARGET_SHMEDIA_FPU)
7512             FAIL;
7513           emit_insn (gen_cmpgedf_media (operands[0],
7514                                         sh_compare_op0, sh_compare_op1));
7515           break;
7516
7517         default:
7518           FAIL;
7519         }
7520       DONE;
7521     }
7522
7523   if (GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
7524     {
7525       if (TARGET_IEEE)
7526         {
7527           rtx lab = gen_label_rtx ();
7528           prepare_scc_operands (EQ);
7529           emit_jump_insn (gen_branch_true (lab));
7530           prepare_scc_operands (GT);
7531           emit_label (lab);
7532           emit_insn (gen_movt (operands[0]));
7533         }
7534       else
7535         emit_insn (gen_movnegt (operands[0], prepare_scc_operands (LT)));
7536       DONE;
7537     }
7538   operands[1] = prepare_scc_operands (GE);
7539 }")
7540
7541 (define_expand "sgtu"
7542   [(set (match_operand:SI 0 "arith_reg_operand" "")
7543         (match_dup 1))]
7544   ""
7545   "
7546 {
7547   if (TARGET_SHMEDIA)
7548     {
7549       if (GET_MODE (operands[0]) != DImode)
7550         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7551       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7552       if (sh_compare_op1 != const0_rtx)
7553         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7554                                     ? GET_MODE (sh_compare_op0)
7555                                     : GET_MODE (sh_compare_op1),
7556                                     sh_compare_op1);
7557
7558       emit_insn (gen_cmpgtudi_media (operands[0],
7559                                      sh_compare_op0, sh_compare_op1));
7560       DONE;
7561     }
7562   operands[1] = prepare_scc_operands (GTU);
7563 }")
7564
7565 (define_expand "sltu"
7566   [(set (match_operand:SI 0 "arith_reg_operand" "")
7567         (match_dup 1))]
7568   ""
7569   "
7570 {
7571   if (TARGET_SHMEDIA)
7572     {
7573       if (GET_MODE (operands[0]) != DImode)
7574         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7575       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7576       if (sh_compare_op1 != const0_rtx)
7577         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7578                                     ? GET_MODE (sh_compare_op0)
7579                                     : GET_MODE (sh_compare_op1),
7580                                     sh_compare_op1);
7581
7582       emit_insn (gen_cmpgtudi_media (operands[0],
7583                                      sh_compare_op1, sh_compare_op0));
7584       DONE;
7585     }
7586   operands[1] = prepare_scc_operands (LTU);
7587 }")
7588
7589 (define_expand "sleu"
7590   [(set (match_operand:SI 0 "arith_reg_operand" "")
7591         (match_dup 1))]
7592   ""
7593   "
7594 {
7595   if (TARGET_SHMEDIA)
7596     {
7597       rtx tmp;
7598
7599       if (GET_MODE (operands[0]) != DImode)
7600         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7601       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7602       if (sh_compare_op1 != const0_rtx)
7603         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7604                                     ? GET_MODE (sh_compare_op0)
7605                                     : GET_MODE (sh_compare_op1),
7606                                     sh_compare_op1);
7607
7608       tmp = no_new_pseudos ? operands[0] : gen_reg_rtx (DImode);
7609
7610       emit_insn (gen_cmpgtudi_media (tmp, sh_compare_op0, sh_compare_op1));
7611       emit_insn (gen_cmpeqdi_media (operands[0], tmp, const0_rtx));
7612
7613       DONE;
7614     }
7615   operands[1] = prepare_scc_operands (LEU);
7616 }")
7617
7618 (define_expand "sgeu"
7619   [(set (match_operand:SI 0 "arith_reg_operand" "")
7620         (match_dup 1))]
7621   ""
7622   "
7623 {
7624   if (TARGET_SHMEDIA)
7625     {
7626       rtx tmp;
7627
7628       if (GET_MODE (operands[0]) != DImode)
7629         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7630       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7631       if (sh_compare_op1 != const0_rtx)
7632         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7633                                     ? GET_MODE (sh_compare_op0)
7634                                     : GET_MODE (sh_compare_op1),
7635                                     sh_compare_op1);
7636
7637       tmp = no_new_pseudos ? operands[0] : gen_reg_rtx (DImode);
7638
7639       emit_insn (gen_cmpgtudi_media (tmp, sh_compare_op1, sh_compare_op0));
7640       emit_insn (gen_cmpeqdi_media (operands[0], tmp, const0_rtx));
7641
7642       DONE;
7643     }
7644
7645   operands[1] = prepare_scc_operands (GEU);
7646 }")
7647
7648 ;; sne moves the complement of the T reg to DEST like this:
7649 ;;      cmp/eq ...
7650 ;;      mov    #-1,temp
7651 ;;      negc   temp,dest
7652 ;;   This is better than xoring compare result with 1 because it does
7653 ;;   not require r0 and further, the -1 may be CSE-ed or lifted out of a
7654 ;;   loop.
7655
7656 (define_expand "sne"
7657   [(set (match_dup 2) (const_int -1))
7658    (parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
7659                    (neg:SI (plus:SI (match_dup 1)
7660                                     (match_dup 2))))
7661               (set (reg:SI T_REG)
7662                    (ne:SI (ior:SI (match_dup 1) (match_dup 2))
7663                           (const_int 0)))])]
7664   ""
7665   "
7666 {
7667   if (TARGET_SHMEDIA)
7668     {
7669       rtx tmp;
7670
7671       if (GET_MODE (operands[0]) != DImode)
7672         operands[0] = gen_rtx_SUBREG (DImode, operands[0], 0);
7673
7674       if (! TARGET_SHMEDIA_FPU && GET_MODE (sh_compare_op0) != DImode)
7675         FAIL;
7676
7677       sh_compare_op0 = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7678       if (sh_compare_op1 != const0_rtx)
7679         sh_compare_op1 = force_reg (GET_MODE (sh_compare_op1) == VOIDmode
7680                                     ? GET_MODE (sh_compare_op0)
7681                                     : GET_MODE (sh_compare_op1),
7682                                     sh_compare_op1);
7683
7684       tmp = no_new_pseudos ? operands[0] : gen_reg_rtx (DImode);
7685
7686       emit_insn (gen_seq (tmp));
7687       emit_insn (gen_cmpeqdi_media (operands[0], tmp, const0_rtx));
7688
7689       DONE;
7690     }
7691
7692    operands[1] = prepare_scc_operands (EQ);
7693    operands[2] = gen_reg_rtx (SImode);
7694 }")
7695
7696 (define_expand "sunordered"
7697   [(set (match_operand:DI 0 "arith_reg_operand" "")
7698         (unordered:DI (match_dup 1) (match_dup 2)))]
7699   "TARGET_SHMEDIA_FPU"
7700   "
7701 {
7702   operands[1] = force_reg (GET_MODE (sh_compare_op0), sh_compare_op0);
7703   operands[2] = force_reg (GET_MODE (sh_compare_op1), sh_compare_op1);
7704 }")
7705
7706 ;; Use the same trick for FP sle / sge
7707 (define_expand "movnegt"
7708   [(set (match_dup 2) (const_int -1))
7709    (parallel [(set (match_operand 0 "" "")
7710                    (neg:SI (plus:SI (match_dup 1)
7711                                     (match_dup 2))))
7712               (set (reg:SI T_REG)
7713                    (ne:SI (ior:SI (match_operand 1 "" "") (match_dup 2))
7714                           (const_int 0)))])]
7715   "TARGET_SH1"
7716   "operands[2] = gen_reg_rtx (SImode);")
7717
7718 ;; Recognize mov #-1/negc/neg sequence, and change it to movt/add #-1.
7719 ;; This prevents a regression that occurred when we switched from xor to
7720 ;; mov/neg for sne.
7721
7722 (define_split
7723   [(set (match_operand:SI 0 "arith_reg_operand" "")
7724         (plus:SI (reg:SI T_REG)
7725                  (const_int -1)))]
7726   "TARGET_SH1"
7727   [(set (match_dup 0) (eq:SI (reg:SI T_REG) (const_int 1)))
7728    (set (match_dup 0) (plus:SI (match_dup 0) (const_int -1)))]
7729   "")
7730
7731 ;; -------------------------------------------------------------------------
7732 ;; Instructions to cope with inline literal tables
7733 ;; -------------------------------------------------------------------------
7734
7735 ; 2 byte integer in line
7736
7737 (define_insn "consttable_2"
7738  [(unspec_volatile [(match_operand:SI 0 "general_operand" "=g")
7739                     (match_operand 1 "" "")]
7740                    UNSPECV_CONST2)]
7741  ""
7742  "*
7743 {
7744   if (operands[1] != const0_rtx)
7745     assemble_integer (operands[0], 2, BITS_PER_UNIT * 2, 1);
7746   return \"\";
7747 }"
7748  [(set_attr "length" "2")
7749  (set_attr "in_delay_slot" "no")])
7750
7751 ; 4 byte integer in line
7752
7753 (define_insn "consttable_4"
7754  [(unspec_volatile [(match_operand:SI 0 "general_operand" "=g")
7755                     (match_operand 1 "" "")]
7756                    UNSPECV_CONST4)]
7757  ""
7758  "*
7759 {
7760   if (operands[1] != const0_rtx)
7761     assemble_integer (operands[0], 4, BITS_PER_UNIT * 4, 1);
7762   return \"\";
7763 }"
7764  [(set_attr "length" "4")
7765   (set_attr "in_delay_slot" "no")])
7766
7767 ; 8 byte integer in line
7768
7769 (define_insn "consttable_8"
7770  [(unspec_volatile [(match_operand:SI 0 "general_operand" "=g")
7771                     (match_operand 1 "" "")]
7772                    UNSPECV_CONST8)]
7773  ""
7774  "*
7775 {
7776   if (operands[1] != const0_rtx)
7777     assemble_integer (operands[0], 8, BITS_PER_UNIT * 8, 1);
7778   return \"\";
7779 }"
7780  [(set_attr "length" "8")
7781   (set_attr "in_delay_slot" "no")])
7782
7783 ; 4 byte floating point
7784
7785 (define_insn "consttable_sf"
7786  [(unspec_volatile [(match_operand:SF 0 "general_operand" "=g")
7787                     (match_operand 1 "" "")]
7788                    UNSPECV_CONST4)]
7789  ""
7790  "*
7791 {
7792   if (operands[1] != const0_rtx)
7793     {
7794       REAL_VALUE_TYPE d;
7795       REAL_VALUE_FROM_CONST_DOUBLE (d, operands[0]);
7796       assemble_real (d, SFmode, GET_MODE_ALIGNMENT (SFmode));
7797     }
7798   return \"\";
7799 }"
7800  [(set_attr "length" "4")
7801   (set_attr "in_delay_slot" "no")])
7802
7803 ; 8 byte floating point
7804
7805 (define_insn "consttable_df"
7806  [(unspec_volatile [(match_operand:DF 0 "general_operand" "=g")
7807                     (match_operand 1 "" "")]
7808                    UNSPECV_CONST8)]
7809  ""
7810  "*
7811 {
7812   if (operands[1] != const0_rtx)
7813     {
7814       REAL_VALUE_TYPE d;
7815       REAL_VALUE_FROM_CONST_DOUBLE (d, operands[0]);
7816       assemble_real (d, DFmode, GET_MODE_ALIGNMENT (DFmode));
7817     }
7818   return \"\";
7819 }"
7820  [(set_attr "length" "8")
7821   (set_attr "in_delay_slot" "no")])
7822
7823 ;; Alignment is needed for some constant tables; it may also be added for
7824 ;; Instructions at the start of loops, or after unconditional branches.
7825 ;; ??? We would get more accurate lengths if we did instruction
7826 ;; alignment based on the value of INSN_CURRENT_ADDRESS; the approach used
7827 ;; here is too conservative.
7828
7829 ; align to a two byte boundary
7830
7831 (define_expand "align_2"
7832  [(unspec_volatile [(const_int 1)] UNSPECV_ALIGN)]
7833  ""
7834  "")
7835
7836 ; align to a four byte boundary
7837 ;; align_4 and align_log are instructions for the starts of loops, or
7838 ;; after unconditional branches, which may take up extra room.
7839
7840 (define_expand "align_4"
7841  [(unspec_volatile [(const_int 2)] UNSPECV_ALIGN)]
7842  ""
7843  "")
7844
7845 ; align to a cache line boundary
7846
7847 (define_insn "align_log"
7848  [(unspec_volatile [(match_operand 0 "const_int_operand" "")] UNSPECV_ALIGN)]
7849  ""
7850  ""
7851  [(set_attr "length" "0")
7852   (set_attr "in_delay_slot" "no")])
7853
7854 ; emitted at the end of the literal table, used to emit the
7855 ; 32bit branch labels if needed.
7856
7857 (define_insn "consttable_end"
7858   [(unspec_volatile [(const_int 0)] UNSPECV_CONST_END)]
7859   ""
7860   "* return output_jump_label_table ();"
7861   [(set_attr "in_delay_slot" "no")])
7862
7863 ; emitted at the end of the window in the literal table.
7864
7865 (define_insn "consttable_window_end"
7866   [(unspec_volatile [(match_operand 0 "" "")] UNSPECV_WINDOW_END)]
7867   ""
7868   ""
7869   [(set_attr "length" "0")
7870    (set_attr "in_delay_slot" "no")])
7871
7872 ;; -------------------------------------------------------------------------
7873 ;; Misc
7874 ;; -------------------------------------------------------------------------
7875
7876 ;; String/block move insn.
7877
7878 (define_expand "movstrsi"
7879   [(parallel [(set (mem:BLK (match_operand:BLK 0 "" ""))
7880                    (mem:BLK (match_operand:BLK 1 "" "")))
7881               (use (match_operand:SI 2 "nonmemory_operand" ""))
7882               (use (match_operand:SI 3 "immediate_operand" ""))
7883               (clobber (reg:SI PR_REG))
7884               (clobber (reg:SI R4_REG))
7885               (clobber (reg:SI R5_REG))
7886               (clobber (reg:SI R0_REG))])]
7887   "TARGET_SH1 && ! TARGET_SH5"
7888   "
7889 {
7890   if(expand_block_move (operands))
7891      DONE;
7892   else FAIL;
7893 }")
7894
7895 (define_insn "block_move_real"
7896   [(parallel [(set (mem:BLK (reg:SI R4_REG))
7897                    (mem:BLK (reg:SI R5_REG)))
7898               (use (match_operand:SI 0 "arith_reg_operand" "r"))
7899               (clobber (reg:SI PR_REG))
7900               (clobber (reg:SI R0_REG))])]
7901   "TARGET_SH1 && ! TARGET_HARD_SH4"
7902   "jsr  @%0%#"
7903   [(set_attr "type" "sfunc")
7904    (set_attr "needs_delay_slot" "yes")])
7905
7906 (define_insn "block_lump_real"
7907   [(parallel [(set (mem:BLK (reg:SI R4_REG))
7908                    (mem:BLK (reg:SI R5_REG)))
7909               (use (match_operand:SI 0 "arith_reg_operand" "r"))
7910               (use (reg:SI R6_REG))
7911               (clobber (reg:SI PR_REG))
7912               (clobber (reg:SI T_REG))
7913               (clobber (reg:SI R4_REG))
7914               (clobber (reg:SI R5_REG))
7915               (clobber (reg:SI R6_REG))
7916               (clobber (reg:SI R0_REG))])]
7917   "TARGET_SH1 && ! TARGET_HARD_SH4"
7918   "jsr  @%0%#"
7919   [(set_attr "type" "sfunc")
7920    (set_attr "needs_delay_slot" "yes")])
7921
7922 (define_insn "block_move_real_i4"
7923   [(parallel [(set (mem:BLK (reg:SI R4_REG))
7924                    (mem:BLK (reg:SI R5_REG)))
7925               (use (match_operand:SI 0 "arith_reg_operand" "r"))
7926               (clobber (reg:SI PR_REG))
7927               (clobber (reg:SI R0_REG))
7928               (clobber (reg:SI R1_REG))
7929               (clobber (reg:SI R2_REG))])]
7930   "TARGET_HARD_SH4"
7931   "jsr  @%0%#"
7932   [(set_attr "type" "sfunc")
7933    (set_attr "needs_delay_slot" "yes")])
7934
7935 (define_insn "block_lump_real_i4"
7936   [(parallel [(set (mem:BLK (reg:SI R4_REG))
7937                    (mem:BLK (reg:SI R5_REG)))
7938               (use (match_operand:SI 0 "arith_reg_operand" "r"))
7939               (use (reg:SI R6_REG))
7940               (clobber (reg:SI PR_REG))
7941               (clobber (reg:SI T_REG))
7942               (clobber (reg:SI R4_REG))
7943               (clobber (reg:SI R5_REG))
7944               (clobber (reg:SI R6_REG))
7945               (clobber (reg:SI R0_REG))
7946               (clobber (reg:SI R1_REG))
7947               (clobber (reg:SI R2_REG))
7948               (clobber (reg:SI R3_REG))])]
7949   "TARGET_HARD_SH4"
7950   "jsr  @%0%#"
7951   [(set_attr "type" "sfunc")
7952    (set_attr "needs_delay_slot" "yes")])
7953 \f
7954 ;; -------------------------------------------------------------------------
7955 ;; Floating point instructions.
7956 ;; -------------------------------------------------------------------------
7957
7958 ;; ??? All patterns should have a type attribute.
7959
7960 (define_expand "fpu_switch0"
7961   [(set (match_operand:SI 0 "" "") (match_dup 2))
7962    (set (match_dup 1) (mem:PSI (match_dup 0)))]
7963   "TARGET_SH4"
7964   "
7965 {
7966   operands[1] = get_fpscr_rtx ();
7967   operands[2] = gen_rtx_SYMBOL_REF (SImode, \"__fpscr_values\");
7968   if (flag_pic)
7969     operands[2] = legitimize_pic_address (operands[2], SImode,
7970                                           no_new_pseudos ? operands[0] : 0);
7971 }")
7972
7973 (define_expand "fpu_switch1"
7974   [(set (match_operand:SI 0 "" "") (match_dup 2))
7975    (set (match_dup 3) (plus:SI (match_dup 0) (const_int 4)))
7976    (set (match_dup 1) (mem:PSI (match_dup 3)))]
7977   "TARGET_SH4"
7978   "
7979 {
7980   operands[1] = get_fpscr_rtx ();
7981   operands[2] = gen_rtx_SYMBOL_REF (SImode, \"__fpscr_values\");
7982   if (flag_pic)
7983     operands[2] = legitimize_pic_address (operands[2], SImode,
7984                                           no_new_pseudos ? operands[0] : 0);
7985   operands[3] = no_new_pseudos ? operands[0] : gen_reg_rtx (SImode);
7986 }")
7987
7988 (define_expand "movpsi"
7989   [(set (match_operand:PSI 0 "register_operand" "")
7990         (match_operand:PSI 1 "general_movsrc_operand" ""))]
7991   "TARGET_SH4"
7992   "")
7993
7994 ;; The c / m alternative is a fake to guide reload to load directly into
7995 ;; fpscr, since reload doesn't know how to use post-increment.
7996 ;; GO_IF_LEGITIMATE_ADDRESS guards about bogus addresses before reload,
7997 ;; SECONDARY_INPUT_RELOAD_CLASS does this during reload, and the insn's
7998 ;; predicate after reload.
7999 ;; The gp_fpul type for r/!c might look a bit odd, but it actually schedules
8000 ;; like a gpr <-> fpul move.
8001 (define_insn "fpu_switch"
8002   [(set (match_operand:PSI 0 "register_operand" "=c,c,r,c,c,r,m,r")
8003         (match_operand:PSI 1 "general_movsrc_operand" "c,>,m,m,r,r,r,!c"))]
8004   "TARGET_SH4
8005    && (! reload_completed
8006        || true_regnum (operands[0]) != FPSCR_REG
8007        || GET_CODE (operands[1]) != MEM
8008        || GET_CODE (XEXP (operands[1], 0)) != PLUS)"
8009   "@
8010         ! precision stays the same
8011         lds.l   %1,fpscr
8012         mov.l   %1,%0
8013         #
8014         lds     %1,fpscr
8015         mov     %1,%0
8016         mov.l   %1,%0
8017         sts     fpscr,%0"
8018   [(set_attr "length" "0,2,2,4,2,2,2,2")
8019    (set_attr "type" "dfp_conv,dfp_conv,load,dfp_conv,dfp_conv,move,store,gp_fpul")
8020    (set_attr "insn_class" "ldsmem_to_fpscr,*,*,lds_to_fpscr,*,*,*,*")])
8021
8022 (define_split
8023   [(set (reg:PSI FPSCR_REG)
8024         (mem:PSI (match_operand:SI 0 "register_operand" "")))]
8025   "TARGET_SH4 && find_regno_note (insn, REG_DEAD, true_regnum (operands[0]))"
8026   [(set (match_dup 0) (match_dup 0))]
8027   "
8028 {
8029   rtx insn = emit_insn (gen_fpu_switch (get_fpscr_rtx (),
8030                                         gen_rtx (MEM, PSImode,
8031                                                  gen_rtx (POST_INC, Pmode,
8032                                                           operands[0]))));
8033   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, operands[0], NULL_RTX);
8034 }")
8035
8036 (define_split
8037   [(set (reg:PSI FPSCR_REG)
8038         (mem:PSI (match_operand:SI 0 "register_operand" "")))]
8039   "TARGET_SH4"
8040   [(set (match_dup 0) (plus:SI (match_dup 0) (const_int -4)))]
8041   "
8042 {
8043   rtx insn = emit_insn (gen_fpu_switch (get_fpscr_rtx (),
8044                                         gen_rtx (MEM, PSImode,
8045                                                  gen_rtx (POST_INC, Pmode,
8046                                                           operands[0]))));
8047   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, operands[0], NULL_RTX);
8048 }")
8049
8050 ;; ??? This uses the fp unit, but has no type indicating that.
8051 ;; If we did that, this would either give a bogus latency or introduce
8052 ;; a bogus FIFO constraint.
8053 ;; Since this insn is currently only used for prologues/epilogues,
8054 ;; it is probably best to claim no function unit, which matches the
8055 ;; current setting.
8056 (define_insn "toggle_sz"
8057   [(set (reg:PSI FPSCR_REG)
8058         (xor:PSI (reg:PSI FPSCR_REG) (const_int 1048576)))]
8059   "TARGET_SH4"
8060   "fschg")
8061
8062 (define_expand "addsf3"
8063   [(set (match_operand:SF 0 "arith_reg_operand" "")
8064         (plus:SF (match_operand:SF 1 "arith_reg_operand" "")
8065                  (match_operand:SF 2 "arith_reg_operand" "")))]
8066   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8067   "
8068 {
8069   if (TARGET_SH3E)
8070     {
8071       expand_sf_binop (&gen_addsf3_i, operands);
8072       DONE;
8073     }
8074 }")
8075
8076 (define_insn "*addsf3_media"
8077   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8078         (plus:SF (match_operand:SF 1 "fp_arith_reg_operand" "%f")
8079                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8080   "TARGET_SHMEDIA_FPU"
8081   "fadd.s       %1, %2, %0"
8082   [(set_attr "type" "fparith_media")])
8083
8084 (define_insn_and_split "unary_sf_op"
8085   [(set (match_operand:V2SF 0 "fp_arith_reg_operand" "=f")
8086         (vec_select:V2SF
8087          (vec_concat:V2SF
8088           (vec_select:SF
8089            (match_dup 0)
8090            (parallel [(not:BI (match_operand 3 "const_int_operand" "n"))]))
8091           (match_operator:SF 2 "unary_float_operator"
8092             [(vec_select:SF (match_operand:V2SF 1 "fp_arith_reg_operand" "f")
8093                             (parallel [(match_operand 4
8094                                         "const_int_operand" "n")]))]))
8095          (parallel [(not:BI (match_dup 3)) (match_dup 3)])))]
8096   "TARGET_SHMEDIA_FPU"
8097   "#"
8098   "TARGET_SHMEDIA_FPU && reload_completed"
8099   [(set (match_dup 5) (match_dup 6))]
8100   "
8101 {
8102   int endian = TARGET_LITTLE_ENDIAN ? 0 : 1;
8103   rtx op1 = gen_rtx_REG (SFmode,
8104                          (true_regnum (operands[1])
8105                           + (INTVAL (operands[4]) ^ endian)));
8106
8107   operands[7] = gen_rtx_REG (SFmode,
8108                              (true_regnum (operands[0])
8109                               + (INTVAL (operands[3]) ^ endian)));
8110   operands[6] = gen_rtx (GET_CODE (operands[2]), SFmode, op1);
8111 }"
8112   [(set_attr "type" "fparith_media")])
8113
8114 (define_insn_and_split "binary_sf_op"
8115   [(set (match_operand:V2SF 0 "fp_arith_reg_operand" "=f")
8116         (vec_select:V2SF
8117          (vec_concat:V2SF
8118           (vec_select:SF
8119            (match_dup 0)
8120            (parallel [(not:BI (match_operand 4 "const_int_operand" "n"))]))
8121           (match_operator:SF 3 "binary_float_operator"
8122             [(vec_select:SF (match_operand:V2SF 1 "fp_arith_reg_operand" "f")
8123                             (parallel [(match_operand 5
8124                                         "const_int_operand" "n")]))
8125              (vec_select:SF (match_operand:V2SF 2 "fp_arith_reg_operand" "f")
8126                             (parallel [(match_operand 6
8127                                         "const_int_operand" "n")]))]))
8128          (parallel [(not:BI (match_dup 4)) (match_dup 4)])))]
8129   "TARGET_SHMEDIA_FPU"
8130   "#"
8131   "TARGET_SHMEDIA_FPU && reload_completed"
8132   [(set (match_dup 7) (match_dup 8))]
8133   "
8134 {
8135   int endian = TARGET_LITTLE_ENDIAN ? 0 : 1;
8136   rtx op1 = gen_rtx_REG (SFmode,
8137                          (true_regnum (operands[1])
8138                           + (INTVAL (operands[5]) ^ endian)));
8139   rtx op2 = gen_rtx_REG (SFmode,
8140                          (true_regnum (operands[2])
8141                           + (INTVAL (operands[6]) ^ endian)));
8142
8143   operands[7] = gen_rtx_REG (SFmode,
8144                              (true_regnum (operands[0])
8145                               + (INTVAL (operands[4]) ^ endian)));
8146   operands[8] = gen_rtx (GET_CODE (operands[3]), SFmode, op1, op2);
8147 }"
8148   [(set_attr "type" "fparith_media")])
8149
8150 (define_insn "addsf3_i"
8151   [(set (match_operand:SF 0 "arith_reg_operand" "=f")
8152         (plus:SF (match_operand:SF 1 "arith_reg_operand" "%0")
8153                  (match_operand:SF 2 "arith_reg_operand" "f")))
8154    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8155   "TARGET_SH3E"
8156   "fadd %2,%0"
8157   [(set_attr "type" "fp")
8158    (set_attr "fp_mode" "single")])
8159
8160 (define_expand "subsf3"
8161   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
8162         (minus:SF (match_operand:SF 1 "fp_arith_reg_operand" "")
8163                   (match_operand:SF 2 "fp_arith_reg_operand" "")))]
8164   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8165   "
8166 {
8167   if (TARGET_SH3E)
8168     {
8169       expand_sf_binop (&gen_subsf3_i, operands);
8170       DONE;
8171     }
8172 }")
8173
8174 (define_insn "*subsf3_media"
8175   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8176         (minus:SF (match_operand:SF 1 "fp_arith_reg_operand" "f")
8177                   (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8178   "TARGET_SHMEDIA_FPU"
8179   "fsub.s       %1, %2, %0"
8180   [(set_attr "type" "fparith_media")])
8181
8182 (define_insn "subsf3_i"
8183   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8184         (minus:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")
8185                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))
8186    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8187   "TARGET_SH3E"
8188   "fsub %2,%0"
8189   [(set_attr "type" "fp")
8190    (set_attr "fp_mode" "single")])
8191
8192 ;; Unfortunately, the combiner is unable to cope with the USE of the FPSCR
8193 ;; register in feeding fp instructions.  Thus, we cannot generate fmac for
8194 ;; mixed-precision SH4 targets.  To allow it to be still generated for the
8195 ;; SH3E, we use a separate insn for SH3E mulsf3.
8196
8197 (define_expand "mulsf3"
8198   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
8199         (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "")
8200                  (match_operand:SF 2 "fp_arith_reg_operand" "")))]
8201   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8202   "
8203 {
8204   if (TARGET_SH4)
8205     expand_sf_binop (&gen_mulsf3_i4, operands);
8206   else if (TARGET_SH3E)
8207     emit_insn (gen_mulsf3_ie (operands[0], operands[1], operands[2]));
8208   if (! TARGET_SHMEDIA)
8209     DONE;
8210 }")
8211
8212 (define_insn "*mulsf3_media"
8213   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8214         (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%f")
8215                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8216   "TARGET_SHMEDIA_FPU"
8217   "fmul.s       %1, %2, %0"
8218   [(set_attr "type" "fparith_media")])
8219
8220 (define_insn "mulsf3_i4"
8221   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8222         (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%0")
8223                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))
8224    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8225   "TARGET_SH3E"
8226   "fmul %2,%0"
8227   [(set_attr "type" "fp")
8228    (set_attr "fp_mode" "single")])
8229
8230 (define_insn "mulsf3_ie"
8231   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8232         (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%0")
8233                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8234   "TARGET_SH3E && ! TARGET_SH4"
8235   "fmul %2,%0"
8236   [(set_attr "type" "fp")])
8237
8238 (define_insn "*mac_media"
8239   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8240         (plus:SF (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%f")
8241                           (match_operand:SF 2 "fp_arith_reg_operand" "f"))
8242                  (match_operand:SF 3 "fp_arith_reg_operand" "0")))]
8243   "TARGET_SHMEDIA_FPU"
8244   "fmac.s %1, %2, %0"
8245   [(set_attr "type" "fparith_media")])
8246
8247 (define_insn "*macsf3"
8248   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8249         (plus:SF (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%w")
8250                           (match_operand:SF 2 "fp_arith_reg_operand" "f"))
8251                  (match_operand:SF 3 "arith_reg_operand" "0")))
8252    (use (match_operand:PSI 4 "fpscr_operand" "c"))]
8253   "TARGET_SH3E && ! TARGET_SH4"
8254   "fmac fr0,%2,%0"
8255   [(set_attr "type" "fp")
8256    (set_attr "fp_mode" "single")])
8257
8258 (define_expand "divsf3"
8259   [(set (match_operand:SF 0 "arith_reg_operand" "")
8260         (div:SF (match_operand:SF 1 "arith_reg_operand" "")
8261                 (match_operand:SF 2 "arith_reg_operand" "")))]
8262   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8263   "
8264 {
8265   if (TARGET_SH3E)
8266     {
8267       expand_sf_binop (&gen_divsf3_i, operands);
8268       DONE;
8269     }
8270 }")
8271
8272 (define_insn "*divsf3_media"
8273   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8274         (div:SF (match_operand:SF 1 "fp_arith_reg_operand" "f")
8275                 (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8276   "TARGET_SHMEDIA_FPU"
8277   "fdiv.s       %1, %2, %0"
8278   [(set_attr "type" "fdiv_media")])
8279
8280 (define_insn "divsf3_i"
8281   [(set (match_operand:SF 0 "arith_reg_operand" "=f")
8282         (div:SF (match_operand:SF 1 "arith_reg_operand" "0")
8283                  (match_operand:SF 2 "arith_reg_operand" "f")))
8284    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8285   "TARGET_SH3E"
8286   "fdiv %2,%0"
8287   [(set_attr "type" "fdiv")
8288    (set_attr "fp_mode" "single")])
8289
8290 (define_insn "floatdisf2"
8291   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8292         (float:SF (match_operand:DI 1 "fp_arith_reg_operand" "f")))]
8293   "TARGET_SHMEDIA_FPU"
8294   "float.qs %1, %0"
8295   [(set_attr "type" "fpconv_media")])
8296
8297 (define_expand "floatsisf2"
8298   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
8299         (float:SF (match_operand:SI 1 "fpul_operand" "")))]
8300   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8301   "
8302 {
8303   if (TARGET_SH4)
8304     {
8305       emit_sf_insn (gen_floatsisf2_i4 (operands[0], operands[1], get_fpscr_rtx ()));
8306       DONE;
8307     }
8308 }")
8309
8310 (define_insn "*floatsisf2_media"
8311   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8312         (float:SF (match_operand:SI 1 "fp_arith_reg_operand" "f")))]
8313   "TARGET_SHMEDIA_FPU"
8314   "float.ls     %1, %0"
8315   [(set_attr "type" "fpconv_media")])
8316
8317 (define_insn "floatsisf2_i4"
8318   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8319         (float:SF (match_operand:SI 1 "fpul_operand" "y")))
8320    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8321   "TARGET_SH4"
8322   "float        %1,%0"
8323   [(set_attr "type" "fp")
8324    (set_attr "fp_mode" "single")])
8325
8326 (define_insn "*floatsisf2_ie"
8327   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8328         (float:SF (match_operand:SI 1 "fpul_operand" "y")))]
8329   "TARGET_SH3E && ! TARGET_SH4"
8330   "float        %1,%0"
8331   [(set_attr "type" "fp")])
8332
8333 (define_insn "fix_truncsfdi2"
8334   [(set (match_operand:DI 0 "fp_arith_reg_operand" "=f")
8335         (fix:DI (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8336   "TARGET_SHMEDIA_FPU"
8337   "ftrc.sq %1, %0"
8338   [(set_attr "type" "fpconv_media")])
8339
8340 (define_expand "fix_truncsfsi2"
8341   [(set (match_operand:SI 0 "fpul_operand" "=y")
8342         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8343   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8344   "
8345 {
8346   if (TARGET_SH4)
8347     {
8348       emit_sf_insn (gen_fix_truncsfsi2_i4 (operands[0], operands[1], get_fpscr_rtx ()));
8349       DONE;
8350     }
8351 }")
8352
8353 (define_insn "*fix_truncsfsi2_media"
8354   [(set (match_operand:SI 0 "fp_arith_reg_operand" "=f")
8355         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8356   "TARGET_SHMEDIA_FPU"
8357   "ftrc.sl      %1, %0"
8358   [(set_attr "type" "fpconv_media")])
8359
8360 (define_insn "fix_truncsfsi2_i4"
8361   [(set (match_operand:SI 0 "fpul_operand" "=y")
8362         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))
8363    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8364   "TARGET_SH4"
8365   "ftrc %1,%0"
8366   [(set_attr "type" "fp")
8367    (set_attr "fp_mode" "single")])
8368
8369 ;; ??? This pattern is used nowhere.  fix_truncsfsi2 always expands to
8370 ;; fix_truncsfsi2_i4.
8371 ;; (define_insn "fix_truncsfsi2_i4_2"
8372 ;;  [(set (match_operand:SI 0 "arith_reg_operand" "=r")
8373 ;;      (fix:SI (match_operand:SF 1 "arith_reg_operand" "f")))
8374 ;;   (use (reg:PSI FPSCR_REG))
8375 ;;   (clobber (reg:SI FPUL_REG))]
8376 ;;  "TARGET_SH4"
8377 ;;  "#"
8378 ;;  [(set_attr "length" "4")
8379 ;;   (set_attr "fp_mode" "single")])
8380
8381 ;;(define_split
8382 ;;  [(set (match_operand:SI 0 "arith_reg_operand" "=r")
8383 ;;      (fix:SI (match_operand:SF 1 "arith_reg_operand" "f")))
8384 ;;   (use (match_operand:PSI 2 "fpscr_operand" "c"))
8385 ;;   (clobber (reg:SI FPUL_REG))]
8386 ;;  "TARGET_SH4"
8387 ;;  [(parallel [(set (reg:SI FPUL_REG) (fix:SI (match_dup 1)))
8388 ;;            (use (match_dup 2))])
8389 ;;   (set (match_dup 0) (reg:SI FPUL_REG))])
8390
8391 (define_insn "*fixsfsi"
8392   [(set (match_operand:SI 0 "fpul_operand" "=y")
8393         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8394   "TARGET_SH3E && ! TARGET_SH4"
8395   "ftrc %1,%0"
8396   [(set_attr "type" "fp")])
8397
8398 (define_insn "cmpgtsf_t"
8399   [(set (reg:SI T_REG)
8400         (gt:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
8401                (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8402   "TARGET_SH3E && ! TARGET_SH4"
8403   "fcmp/gt      %1,%0"
8404   [(set_attr "type" "fp")
8405    (set_attr "fp_mode" "single")])
8406
8407 (define_insn "cmpeqsf_t"
8408   [(set (reg:SI T_REG)
8409         (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
8410                (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8411   "TARGET_SH3E && ! TARGET_SH4"
8412   "fcmp/eq      %1,%0"
8413   [(set_attr "type" "fp")
8414    (set_attr "fp_mode" "single")])
8415
8416 (define_insn "ieee_ccmpeqsf_t"
8417   [(set (reg:SI T_REG)
8418         (ior:SI (reg:SI T_REG)
8419                 (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
8420                        (match_operand:SF 1 "fp_arith_reg_operand" "f"))))]
8421   "TARGET_SH3E && TARGET_IEEE && ! TARGET_SH4"
8422   "* return output_ieee_ccmpeq (insn, operands);"
8423   [(set_attr "length" "4")])
8424
8425
8426 (define_insn "cmpgtsf_t_i4"
8427   [(set (reg:SI T_REG)
8428         (gt:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
8429                (match_operand:SF 1 "fp_arith_reg_operand" "f")))
8430    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8431   "TARGET_SH4"
8432   "fcmp/gt      %1,%0"
8433   [(set_attr "type" "fp")
8434    (set_attr "fp_mode" "single")])
8435
8436 (define_insn "cmpeqsf_t_i4"
8437   [(set (reg:SI T_REG)
8438         (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
8439                (match_operand:SF 1 "fp_arith_reg_operand" "f")))
8440    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8441   "TARGET_SH4"
8442   "fcmp/eq      %1,%0"
8443   [(set_attr "type" "fp")
8444    (set_attr "fp_mode" "single")])
8445
8446 (define_insn "*ieee_ccmpeqsf_t_4"
8447   [(set (reg:SI T_REG)
8448         (ior:SI (reg:SI T_REG)
8449                 (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
8450                        (match_operand:SF 1 "fp_arith_reg_operand" "f"))))
8451    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8452   "TARGET_IEEE && TARGET_SH4"
8453   "* return output_ieee_ccmpeq (insn, operands);"
8454   [(set_attr "length" "4")
8455    (set_attr "fp_mode" "single")])
8456
8457 (define_insn "cmpeqsf_media"
8458   [(set (match_operand:DI 0 "register_operand" "=r")
8459         (eq:DI (match_operand:SF 1 "fp_arith_reg_operand" "f")
8460                (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8461   "TARGET_SHMEDIA_FPU"
8462   "fcmpeq.s     %1, %2, %0"
8463   [(set_attr "type" "fcmp_media")])
8464
8465 (define_insn "cmpgtsf_media"
8466   [(set (match_operand:DI 0 "register_operand" "=r")
8467         (gt:DI (match_operand:SF 1 "fp_arith_reg_operand" "f")
8468                (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8469   "TARGET_SHMEDIA_FPU"
8470   "fcmpgt.s     %1, %2, %0"
8471   [(set_attr "type" "fcmp_media")])
8472
8473 (define_insn "cmpgesf_media"
8474   [(set (match_operand:DI 0 "register_operand" "=r")
8475         (ge:DI (match_operand:SF 1 "fp_arith_reg_operand" "f")
8476                (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8477   "TARGET_SHMEDIA_FPU"
8478   "fcmpge.s     %1, %2, %0"
8479   [(set_attr "type" "fcmp_media")])
8480
8481 (define_insn "cmpunsf_media"
8482   [(set (match_operand:DI 0 "register_operand" "=r")
8483         (unordered:DI (match_operand:SF 1 "fp_arith_reg_operand" "f")
8484                       (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
8485   "TARGET_SHMEDIA_FPU"
8486   "fcmpun.s     %1, %2, %0"
8487   [(set_attr "type" "fcmp_media")])
8488
8489 (define_expand "cmpsf"
8490   [(set (reg:SI T_REG)
8491         (compare (match_operand:SF 0 "arith_operand" "")
8492                  (match_operand:SF 1 "arith_operand" "")))]
8493   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8494   "
8495 {
8496   sh_compare_op0 = operands[0];
8497   sh_compare_op1 = operands[1];
8498   DONE;
8499 }")
8500
8501 (define_expand "negsf2"
8502   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
8503         (neg:SF (match_operand:SF 1 "fp_arith_reg_operand" "")))]
8504   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8505   "
8506 {
8507   if (TARGET_SH3E)
8508     {
8509       expand_sf_unop (&gen_negsf2_i, operands);
8510       DONE;
8511     }
8512 }")
8513
8514 (define_insn "*negsf2_media"
8515   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8516         (neg:SF (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8517   "TARGET_SHMEDIA_FPU"
8518   "fneg.s       %1, %0"
8519   [(set_attr "type" "fmove_media")])
8520
8521 (define_insn "negsf2_i"
8522   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8523         (neg:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")))
8524    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8525   "TARGET_SH3E"
8526   "fneg %0"
8527   [(set_attr "type" "fmove")
8528    (set_attr "fp_mode" "single")])
8529
8530 (define_expand "sqrtsf2"
8531   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
8532         (sqrt:SF (match_operand:SF 1 "fp_arith_reg_operand" "")))]
8533   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8534   "
8535 {
8536   if (TARGET_SH3E)
8537     {
8538       expand_sf_unop (&gen_sqrtsf2_i, operands);
8539       DONE;
8540     }
8541 }")
8542
8543 (define_insn "*sqrtsf2_media"
8544   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8545         (sqrt:SF (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8546   "TARGET_SHMEDIA_FPU"
8547   "fsqrt.s      %1, %0"
8548   [(set_attr "type" "fdiv_media")])
8549
8550 (define_insn "sqrtsf2_i"
8551   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8552         (sqrt:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")))
8553    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8554   "TARGET_SH3E"
8555   "fsqrt        %0"
8556   [(set_attr "type" "fdiv")
8557    (set_attr "fp_mode" "single")])
8558
8559 (define_expand "abssf2"
8560   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
8561         (abs:SF (match_operand:SF 1 "fp_arith_reg_operand" "")))]
8562   "TARGET_SH3E || TARGET_SHMEDIA_FPU"
8563   "
8564 {
8565   if (TARGET_SH3E)
8566     {
8567       expand_sf_unop (&gen_abssf2_i, operands);
8568       DONE;
8569     }
8570 }")
8571
8572 (define_insn "*abssf2_media"
8573   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8574         (abs:SF (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8575   "TARGET_SHMEDIA_FPU"
8576   "fabs.s       %1, %0"
8577   [(set_attr "type" "fmove_media")])
8578
8579 (define_insn "abssf2_i"
8580   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
8581         (abs:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")))
8582    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8583   "TARGET_SH3E"
8584   "fabs %0"
8585   [(set_attr "type" "fmove")
8586    (set_attr "fp_mode" "single")])
8587
8588 (define_expand "adddf3"
8589   [(set (match_operand:DF 0 "fp_arith_reg_operand" "")
8590         (plus:DF (match_operand:DF 1 "fp_arith_reg_operand" "")
8591                  (match_operand:DF 2 "fp_arith_reg_operand" "")))]
8592   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8593   "
8594 {
8595   if (TARGET_SH4)
8596     {
8597       expand_df_binop (&gen_adddf3_i, operands);
8598       DONE;
8599     }
8600 }")
8601
8602 (define_insn "*adddf3_media"
8603   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8604         (plus:DF (match_operand:DF 1 "fp_arith_reg_operand" "%f")
8605                  (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8606   "TARGET_SHMEDIA_FPU"
8607   "fadd.d       %1, %2, %0"
8608   [(set_attr "type" "dfparith_media")])
8609
8610 (define_insn "adddf3_i"
8611   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8612         (plus:DF (match_operand:DF 1 "fp_arith_reg_operand" "%0")
8613                  (match_operand:DF 2 "fp_arith_reg_operand" "f")))
8614    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8615   "TARGET_SH4"
8616   "fadd %2,%0"
8617   [(set_attr "type" "dfp_arith")
8618    (set_attr "fp_mode" "double")])
8619
8620 (define_expand "subdf3"
8621   [(set (match_operand:DF 0 "fp_arith_reg_operand" "")
8622         (minus:DF (match_operand:DF 1 "fp_arith_reg_operand" "")
8623                   (match_operand:DF 2 "fp_arith_reg_operand" "")))]
8624   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8625   "
8626 {
8627   if (TARGET_SH4)
8628     {
8629       expand_df_binop (&gen_subdf3_i, operands);
8630       DONE;
8631     }
8632 }")
8633
8634 (define_insn "*subdf3_media"
8635   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8636         (minus:DF (match_operand:DF 1 "fp_arith_reg_operand" "f")
8637                   (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8638   "TARGET_SHMEDIA_FPU"
8639   "fsub.d       %1, %2, %0"
8640   [(set_attr "type" "dfparith_media")])
8641
8642 (define_insn "subdf3_i"
8643   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8644         (minus:DF (match_operand:DF 1 "fp_arith_reg_operand" "0")
8645                   (match_operand:DF 2 "fp_arith_reg_operand" "f")))
8646    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8647   "TARGET_SH4"
8648   "fsub %2,%0"
8649   [(set_attr "type" "dfp_arith")
8650    (set_attr "fp_mode" "double")])
8651
8652 (define_expand "muldf3"
8653   [(set (match_operand:DF 0 "fp_arith_reg_operand" "")
8654         (mult:DF (match_operand:DF 1 "fp_arith_reg_operand" "")
8655                  (match_operand:DF 2 "fp_arith_reg_operand" "")))]
8656   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8657   "
8658 {
8659   if (TARGET_SH4)
8660     {
8661       expand_df_binop (&gen_muldf3_i, operands);
8662       DONE;
8663     }
8664 }")
8665
8666 (define_insn "*muldf3_media"
8667   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8668         (mult:DF (match_operand:DF 1 "fp_arith_reg_operand" "%f")
8669                  (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8670   "TARGET_SHMEDIA_FPU"
8671   "fmul.d       %1, %2, %0"
8672   [(set_attr "type" "dfmul_media")])
8673
8674 (define_insn "muldf3_i"
8675   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8676         (mult:DF (match_operand:DF 1 "fp_arith_reg_operand" "%0")
8677                  (match_operand:DF 2 "fp_arith_reg_operand" "f")))
8678    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8679   "TARGET_SH4"
8680   "fmul %2,%0"
8681   [(set_attr "type" "dfp_arith")
8682    (set_attr "fp_mode" "double")])
8683
8684 (define_expand "divdf3"
8685   [(set (match_operand:DF 0 "fp_arith_reg_operand" "")
8686         (div:DF (match_operand:DF 1 "fp_arith_reg_operand" "")
8687                 (match_operand:DF 2 "fp_arith_reg_operand" "")))]
8688   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8689   "
8690 {
8691   if (TARGET_SH4)
8692     {
8693       expand_df_binop (&gen_divdf3_i, operands);
8694       DONE;
8695     }
8696 }")
8697
8698 (define_insn "*divdf3_media"
8699   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8700         (div:DF (match_operand:DF 1 "fp_arith_reg_operand" "f")
8701                 (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8702   "TARGET_SHMEDIA_FPU"
8703   "fdiv.d       %1, %2, %0"
8704   [(set_attr "type" "dfdiv_media")])
8705
8706 (define_insn "divdf3_i"
8707   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8708         (div:DF (match_operand:DF 1 "fp_arith_reg_operand" "0")
8709                 (match_operand:DF 2 "fp_arith_reg_operand" "f")))
8710    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
8711   "TARGET_SH4"
8712   "fdiv %2,%0"
8713   [(set_attr "type" "dfdiv")
8714    (set_attr "fp_mode" "double")])
8715
8716 (define_insn "floatdidf2"
8717   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8718         (float:DF (match_operand:DI 1 "fp_arith_reg_operand" "f")))]
8719   "TARGET_SHMEDIA_FPU"
8720   "float.qd     %1, %0"
8721   [(set_attr "type" "dfpconv_media")])
8722
8723 (define_expand "floatsidf2"
8724   [(set (match_operand:DF 0 "fp_arith_reg_operand" "")
8725         (float:DF (match_operand:SI 1 "fpul_operand" "")))]
8726   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8727   "
8728 {
8729   if (TARGET_SH4)
8730     {
8731       emit_df_insn (gen_floatsidf2_i (operands[0], operands[1],
8732                                       get_fpscr_rtx ()));
8733       DONE;
8734     }
8735 }")
8736
8737 (define_insn "*floatsidf2_media"
8738   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8739         (float:DF (match_operand:SI 1 "fp_arith_reg_operand" "f")))]
8740   "TARGET_SHMEDIA_FPU"
8741   "float.ld     %1, %0"
8742   [(set_attr "type" "dfpconv_media")])
8743
8744 (define_insn "floatsidf2_i"
8745   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8746         (float:DF (match_operand:SI 1 "fpul_operand" "y")))
8747    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8748   "TARGET_SH4"
8749   "float        %1,%0"
8750   [(set_attr "type" "dfp_conv")
8751    (set_attr "fp_mode" "double")])
8752
8753 (define_insn "fix_truncdfdi2"
8754   [(set (match_operand:DI 0 "fp_arith_reg_operand" "=f")
8755         (fix:DI (match_operand:DF 1 "fp_arith_reg_operand" "f")))]
8756   "TARGET_SHMEDIA_FPU"
8757   "ftrc.dq      %1, %0"
8758   [(set_attr "type" "dfpconv_media")])
8759
8760 (define_expand "fix_truncdfsi2"
8761   [(set (match_operand:SI 0 "fpul_operand" "")
8762         (fix:SI (match_operand:DF 1 "fp_arith_reg_operand" "")))]
8763   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8764   "
8765 {
8766   if (TARGET_SH4)
8767     {
8768       emit_df_insn (gen_fix_truncdfsi2_i (operands[0], operands[1],
8769                                           get_fpscr_rtx ()));
8770       DONE;
8771     }
8772 }")
8773
8774 (define_insn "*fix_truncdfsi2_media"
8775   [(set (match_operand:SI 0 "fp_arith_reg_operand" "=f")
8776         (fix:SI (match_operand:DF 1 "fp_arith_reg_operand" "f")))]
8777   "TARGET_SHMEDIA_FPU"
8778   "ftrc.dl      %1, %0"
8779   [(set_attr "type" "dfpconv_media")])
8780
8781 (define_insn "fix_truncdfsi2_i"
8782   [(set (match_operand:SI 0 "fpul_operand" "=y")
8783         (fix:SI (match_operand:DF 1 "fp_arith_reg_operand" "f")))
8784    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8785   "TARGET_SH4"
8786   "ftrc %1,%0"
8787   [(set_attr "type" "dfp_conv")
8788    (set_attr "fp_mode" "double")])
8789
8790 ;; ??? This pattern is used nowhere.  fix_truncdfsi2 always expands to
8791 ;; fix_truncdfsi2_i.
8792 ;; (define_insn "fix_truncdfsi2_i4"
8793 ;;   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
8794 ;;      (fix:SI (match_operand:DF 1 "arith_reg_operand" "f")))
8795 ;;    (use (match_operand:PSI 2 "fpscr_operand" "c"))
8796 ;;    (clobber (reg:SI FPUL_REG))]
8797 ;;   "TARGET_SH4"
8798 ;;   "#"
8799 ;;   [(set_attr "length" "4")
8800 ;;    (set_attr "fp_mode" "double")])
8801 ;;
8802 ;; (define_split
8803 ;;   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
8804 ;;      (fix:SI (match_operand:DF 1 "arith_reg_operand" "f")))
8805 ;;    (use (match_operand:PSI 2 "fpscr_operand" "c"))
8806 ;;    (clobber (reg:SI FPUL_REG))]
8807 ;;   "TARGET_SH4"
8808 ;;   [(parallel [(set (reg:SI FPUL_REG) (fix:SI (match_dup 1)))
8809 ;;            (use (match_dup 2))])
8810 ;;    (set (match_dup 0) (reg:SI FPUL_REG))])
8811
8812 (define_insn "cmpgtdf_t"
8813   [(set (reg:SI T_REG)
8814         (gt:SI (match_operand:DF 0 "arith_reg_operand" "f")
8815                (match_operand:DF 1 "arith_reg_operand" "f")))
8816    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8817   "TARGET_SH4"
8818   "fcmp/gt      %1,%0"
8819   [(set_attr "type" "dfp_cmp")
8820    (set_attr "fp_mode" "double")])
8821
8822 (define_insn "cmpeqdf_t"
8823   [(set (reg:SI T_REG)
8824         (eq:SI (match_operand:DF 0 "arith_reg_operand" "f")
8825                (match_operand:DF 1 "arith_reg_operand" "f")))
8826    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8827   "TARGET_SH4"
8828   "fcmp/eq      %1,%0"
8829   [(set_attr "type" "dfp_cmp")
8830    (set_attr "fp_mode" "double")])
8831
8832 (define_insn "*ieee_ccmpeqdf_t"
8833   [(set (reg:SI T_REG)
8834         (ior:SI (reg:SI T_REG)
8835                 (eq:SI (match_operand:DF 0 "arith_reg_operand" "f")
8836                        (match_operand:DF 1 "arith_reg_operand" "f"))))
8837    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8838   "TARGET_IEEE && TARGET_SH4"
8839   "* return output_ieee_ccmpeq (insn, operands);"
8840   [(set_attr "length" "4")
8841    (set_attr "fp_mode" "double")])
8842
8843 (define_insn "cmpeqdf_media"
8844   [(set (match_operand:DI 0 "register_operand" "=r")
8845         (eq:DI (match_operand:DF 1 "fp_arith_reg_operand" "f")
8846                (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8847   "TARGET_SHMEDIA_FPU"
8848   "fcmpeq.d     %1,%2,%0"
8849   [(set_attr "type" "fcmp_media")])
8850
8851 (define_insn "cmpgtdf_media"
8852   [(set (match_operand:DI 0 "register_operand" "=r")
8853         (gt:DI (match_operand:DF 1 "fp_arith_reg_operand" "f")
8854                (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8855   "TARGET_SHMEDIA_FPU"
8856   "fcmpgt.d     %1,%2,%0"
8857   [(set_attr "type" "fcmp_media")])
8858
8859 (define_insn "cmpgedf_media"
8860   [(set (match_operand:DI 0 "register_operand" "=r")
8861         (ge:DI (match_operand:DF 1 "fp_arith_reg_operand" "f")
8862                (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8863   "TARGET_SHMEDIA_FPU"
8864   "fcmpge.d     %1,%2,%0"
8865   [(set_attr "type" "fcmp_media")])
8866
8867 (define_insn "cmpundf_media"
8868   [(set (match_operand:DI 0 "register_operand" "=r")
8869         (unordered:DI (match_operand:DF 1 "fp_arith_reg_operand" "f")
8870                       (match_operand:DF 2 "fp_arith_reg_operand" "f")))]
8871   "TARGET_SHMEDIA_FPU"
8872   "fcmpun.d     %1,%2,%0"
8873   [(set_attr "type" "fcmp_media")])
8874
8875 (define_expand "cmpdf"
8876   [(set (reg:SI T_REG)
8877         (compare (match_operand:DF 0 "arith_operand" "")
8878                  (match_operand:DF 1 "arith_operand" "")))]
8879   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8880   "
8881 {
8882   sh_compare_op0 = operands[0];
8883   sh_compare_op1 = operands[1];
8884   DONE;
8885 }")
8886
8887 (define_expand "negdf2"
8888   [(set (match_operand:DF 0 "arith_reg_operand" "")
8889         (neg:DF (match_operand:DF 1 "arith_reg_operand" "")))]
8890   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8891   "
8892 {
8893   if (TARGET_SH4)
8894     {
8895       expand_df_unop (&gen_negdf2_i, operands);
8896       DONE;
8897     }
8898 }")
8899
8900 (define_insn "*negdf2_media"
8901   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8902         (neg:DF (match_operand:DF 1 "fp_arith_reg_operand" "f")))]
8903   "TARGET_SHMEDIA_FPU"
8904   "fneg.d       %1, %0"
8905   [(set_attr "type" "fmove_media")])
8906
8907 (define_insn "negdf2_i"
8908   [(set (match_operand:DF 0 "arith_reg_operand" "=f")
8909         (neg:DF (match_operand:DF 1 "arith_reg_operand" "0")))
8910    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8911   "TARGET_SH4"
8912   "fneg %0"
8913   [(set_attr "type" "fmove")
8914    (set_attr "fp_mode" "double")])
8915
8916 (define_expand "sqrtdf2"
8917   [(set (match_operand:DF 0 "arith_reg_operand" "")
8918         (sqrt:DF (match_operand:DF 1 "arith_reg_operand" "")))]
8919   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8920   "
8921 {
8922   if (TARGET_SH4)
8923     {
8924       expand_df_unop (&gen_sqrtdf2_i, operands);
8925       DONE;
8926     }
8927 }")
8928
8929 (define_insn "*sqrtdf2_media"
8930   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8931         (sqrt:DF (match_operand:DF 1 "fp_arith_reg_operand" "f")))]
8932   "TARGET_SHMEDIA_FPU"
8933   "fsqrt.d      %1, %0"
8934   [(set_attr "type" "dfdiv_media")])
8935
8936 (define_insn "sqrtdf2_i"
8937   [(set (match_operand:DF 0 "arith_reg_operand" "=f")
8938         (sqrt:DF (match_operand:DF 1 "arith_reg_operand" "0")))
8939    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8940   "TARGET_SH4"
8941   "fsqrt        %0"
8942   [(set_attr "type" "dfdiv")
8943    (set_attr "fp_mode" "double")])
8944
8945 (define_expand "absdf2"
8946   [(set (match_operand:DF 0 "arith_reg_operand" "")
8947         (abs:DF (match_operand:DF 1 "arith_reg_operand" "")))]
8948   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8949   "
8950 {
8951   if (TARGET_SH4)
8952     {
8953       expand_df_unop (&gen_absdf2_i, operands);
8954       DONE;
8955     }
8956 }")
8957
8958 (define_insn "*absdf2_media"
8959   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8960         (abs:DF (match_operand:DF 1 "fp_arith_reg_operand" "f")))]
8961   "TARGET_SHMEDIA_FPU"
8962   "fabs.d       %1, %0"
8963   [(set_attr "type" "fmove_media")])
8964
8965 (define_insn "absdf2_i"
8966   [(set (match_operand:DF 0 "arith_reg_operand" "=f")
8967         (abs:DF (match_operand:DF 1 "arith_reg_operand" "0")))
8968    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8969   "TARGET_SH4"
8970   "fabs %0"
8971   [(set_attr "type" "fmove")
8972    (set_attr "fp_mode" "double")])
8973
8974 (define_expand "extendsfdf2"
8975   [(set (match_operand:DF 0 "fp_arith_reg_operand" "")
8976         (float_extend:DF (match_operand:SF 1 "fpul_operand" "")))]
8977   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
8978   "
8979 {
8980   if (TARGET_SH4)
8981     {
8982       emit_df_insn (gen_extendsfdf2_i4 (operands[0], operands[1],
8983                                         get_fpscr_rtx ()));
8984       DONE;
8985     }
8986 }")
8987
8988 (define_insn "*extendsfdf2_media"
8989   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8990         (float_extend:DF (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
8991   "TARGET_SHMEDIA_FPU"
8992   "fcnv.sd      %1, %0"
8993   [(set_attr "type" "dfpconv_media")])
8994
8995 (define_insn "extendsfdf2_i4"
8996   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
8997         (float_extend:DF (match_operand:SF 1 "fpul_operand" "y")))
8998    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
8999   "TARGET_SH4"
9000   "fcnvsd  %1,%0"
9001   [(set_attr "type" "fp")
9002    (set_attr "fp_mode" "double")])
9003
9004 (define_expand "truncdfsf2"
9005   [(set (match_operand:SF 0 "fpul_operand" "")
9006         (float_truncate:SF (match_operand:DF 1 "fp_arith_reg_operand" "")))]
9007   "TARGET_SH4 || TARGET_SHMEDIA_FPU"
9008   "
9009 {
9010   if (TARGET_SH4)
9011     {
9012       emit_df_insn (gen_truncdfsf2_i4 (operands[0], operands[1],
9013                                        get_fpscr_rtx ()));
9014       DONE;
9015     }
9016 }")
9017
9018 (define_insn "*truncdfsf2_media"
9019   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
9020         (float_truncate:SF (match_operand:DF 1 "fp_arith_reg_operand" "f")))]
9021   "TARGET_SHMEDIA_FPU"
9022   "fcnv.ds      %1, %0"
9023   [(set_attr "type" "dfpconv_media")])
9024
9025 (define_insn "truncdfsf2_i4"
9026   [(set (match_operand:SF 0 "fpul_operand" "=y")
9027         (float_truncate:SF (match_operand:DF 1 "fp_arith_reg_operand" "f")))
9028    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
9029   "TARGET_SH4"
9030   "fcnvds  %1,%0"
9031   [(set_attr "type" "fp")
9032    (set_attr "fp_mode" "double")])
9033 \f
9034 ;; Bit field extract patterns.  These give better code for packed bitfields,
9035 ;; because they allow auto-increment addresses to be generated.
9036
9037 (define_expand "insv"
9038   [(set (zero_extract:SI (match_operand:QI 0 "memory_operand" "")
9039                          (match_operand:SI 1 "immediate_operand" "")
9040                          (match_operand:SI 2 "immediate_operand" ""))
9041         (match_operand:SI 3 "general_operand" ""))]
9042   "TARGET_SH1 && ! TARGET_LITTLE_ENDIAN"
9043   "
9044 {
9045   rtx addr_target, orig_address, shift_reg, qi_val;
9046   HOST_WIDE_INT bitsize, size, v;
9047   rtx x = operands[3];
9048
9049   /* ??? expmed doesn't care for non-register predicates.  */
9050   if (! memory_operand (operands[0], VOIDmode)
9051       || ! immediate_operand (operands[1], VOIDmode)
9052       || ! immediate_operand (operands[2], VOIDmode)
9053       || ! general_operand (x, VOIDmode))
9054     FAIL;
9055   /* If this isn't a 16 / 24 / 32 bit field, or if
9056      it doesn't start on a byte boundary, then fail.  */
9057   bitsize = INTVAL (operands[1]);
9058   if (bitsize < 16 || bitsize > 32 || bitsize % 8 != 0
9059       || (INTVAL (operands[2]) % 8) != 0)
9060     FAIL;
9061
9062   size = bitsize / 8;
9063   orig_address = XEXP (operands[0], 0);
9064   shift_reg = gen_reg_rtx (SImode);
9065   if (GET_CODE (x) == CONST_INT)
9066     {
9067       v = INTVAL (x);
9068       qi_val = force_reg (QImode, GEN_INT (trunc_int_for_mode (v, QImode)));
9069     }
9070   else
9071     {
9072       emit_insn (gen_movsi (shift_reg, operands[3]));
9073       qi_val = gen_rtx_SUBREG (QImode, shift_reg, 3);
9074     }
9075   addr_target = copy_addr_to_reg (plus_constant (orig_address, size - 1));
9076
9077   operands[0] = replace_equiv_address (operands[0], addr_target);
9078   emit_insn (gen_movqi (operands[0], qi_val));
9079
9080   while (size -= 1)
9081     {
9082       if (GET_CODE (x) == CONST_INT)
9083         qi_val
9084           = force_reg (QImode, GEN_INT (trunc_int_for_mode (v >>= 8, QImode)));
9085       else
9086         {
9087           emit_insn (gen_lshrsi3_k (shift_reg, shift_reg, GEN_INT (8)));
9088           qi_val = gen_rtx_SUBREG (QImode, shift_reg, 3);
9089         }
9090       emit_insn (gen_addsi3 (addr_target, addr_target, GEN_INT (-1)));
9091       emit_insn (gen_movqi (operands[0], qi_val));
9092     }
9093
9094   DONE;
9095 }")
9096 \f
9097 ;; -------------------------------------------------------------------------
9098 ;; Peepholes
9099 ;; -------------------------------------------------------------------------
9100
9101 ;; This matches cases where a stack pointer increment at the start of the
9102 ;; epilogue combines with a stack slot read loading the return value.
9103
9104 (define_peephole
9105   [(set (match_operand:SI 0 "arith_reg_operand" "")
9106         (mem:SI (match_operand:SI 1 "arith_reg_operand" "")))
9107    (set (match_dup 1) (plus:SI (match_dup 1) (const_int 4)))]
9108   "TARGET_SH1 && REGNO (operands[1]) != REGNO (operands[0])"
9109   "mov.l        @%1+,%0")
9110
9111 ;; See the comment on the dt combiner pattern above.
9112
9113 (define_peephole
9114   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
9115         (plus:SI (match_dup 0)
9116                  (const_int -1)))
9117    (set (reg:SI T_REG)
9118         (eq:SI (match_dup 0)
9119                (const_int 0)))]
9120   "TARGET_SH2"
9121   "dt   %0")
9122
9123 ;; These convert sequences such as `mov #k,r0; add r15,r0; mov.l @r0,rn'
9124 ;; to `mov #k,r0; mov.l @(r0,r15),rn'.  These sequences are generated by
9125 ;; reload when the constant is too large for a reg+offset address.
9126
9127 ;; ??? We would get much better code if this was done in reload.  This would
9128 ;; require modifying find_reloads_address to recognize that if the constant
9129 ;; is out-of-range for an immediate add, then we get better code by reloading
9130 ;; the constant into a register than by reloading the sum into a register,
9131 ;; since the former is one instruction shorter if the address does not need
9132 ;; to be offsettable.  Unfortunately this does not work, because there is
9133 ;; only one register, r0, that can be used as an index register.  This register
9134 ;; is also the function return value register.  So, if we try to force reload
9135 ;; to use double-reg addresses, then we end up with some instructions that
9136 ;; need to use r0 twice.  The only way to fix this is to change the calling
9137 ;; convention so that r0 is not used to return values.
9138
9139 (define_peephole
9140   [(set (match_operand:SI 0 "register_operand" "=r")
9141         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9142    (set (mem:SI (match_dup 0))
9143         (match_operand:SI 2 "general_movsrc_operand" ""))]
9144   "TARGET_SH1 && REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
9145   "mov.l        %2,@(%0,%1)")
9146
9147 (define_peephole
9148   [(set (match_operand:SI 0 "register_operand" "=r")
9149         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9150    (set (match_operand:SI 2 "general_movdst_operand" "")
9151         (mem:SI (match_dup 0)))]
9152   "TARGET_SH1 && REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
9153   "mov.l        @(%0,%1),%2")
9154
9155 (define_peephole
9156   [(set (match_operand:SI 0 "register_operand" "=r")
9157         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9158    (set (mem:HI (match_dup 0))
9159         (match_operand:HI 2 "general_movsrc_operand" ""))]
9160   "TARGET_SH1 && REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
9161   "mov.w        %2,@(%0,%1)")
9162
9163 (define_peephole
9164   [(set (match_operand:SI 0 "register_operand" "=r")
9165         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9166    (set (match_operand:HI 2 "general_movdst_operand" "")
9167         (mem:HI (match_dup 0)))]
9168   "TARGET_SH1 && REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
9169   "mov.w        @(%0,%1),%2")
9170
9171 (define_peephole
9172   [(set (match_operand:SI 0 "register_operand" "=r")
9173         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9174    (set (mem:QI (match_dup 0))
9175         (match_operand:QI 2 "general_movsrc_operand" ""))]
9176   "TARGET_SH1 && REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
9177   "mov.b        %2,@(%0,%1)")
9178
9179 (define_peephole
9180   [(set (match_operand:SI 0 "register_operand" "=r")
9181         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9182    (set (match_operand:QI 2 "general_movdst_operand" "")
9183         (mem:QI (match_dup 0)))]
9184   "TARGET_SH1 && REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
9185   "mov.b        @(%0,%1),%2")
9186
9187 (define_peephole
9188   [(set (match_operand:SI 0 "register_operand" "=r")
9189         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9190    (set (mem:SF (match_dup 0))
9191         (match_operand:SF 2 "general_movsrc_operand" ""))]
9192   "TARGET_SH1 && REGNO (operands[0]) == 0
9193    && ((GET_CODE (operands[2]) == REG && REGNO (operands[2]) < 16)
9194        || (GET_CODE (operands[2]) == SUBREG
9195            && REGNO (SUBREG_REG (operands[2])) < 16))
9196    && reg_unused_after (operands[0], insn)"
9197   "mov.l        %2,@(%0,%1)")
9198
9199 (define_peephole
9200   [(set (match_operand:SI 0 "register_operand" "=r")
9201         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9202    (set (match_operand:SF 2 "general_movdst_operand" "")
9203
9204         (mem:SF (match_dup 0)))]
9205   "TARGET_SH1 && REGNO (operands[0]) == 0
9206    && ((GET_CODE (operands[2]) == REG && REGNO (operands[2]) < 16)
9207        || (GET_CODE (operands[2]) == SUBREG
9208            && REGNO (SUBREG_REG (operands[2])) < 16))
9209    && reg_unused_after (operands[0], insn)"
9210   "mov.l        @(%0,%1),%2")
9211
9212 (define_peephole
9213   [(set (match_operand:SI 0 "register_operand" "=r")
9214         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9215    (set (mem:SF (match_dup 0))
9216         (match_operand:SF 2 "general_movsrc_operand" ""))]
9217   "TARGET_SH3E && REGNO (operands[0]) == 0
9218    && ((GET_CODE (operands[2]) == REG
9219         && FP_OR_XD_REGISTER_P (REGNO (operands[2])))
9220        || (GET_CODE (operands[2]) == SUBREG
9221            && FP_OR_XD_REGISTER_P (REGNO (SUBREG_REG (operands[2])))))
9222    && reg_unused_after (operands[0], insn)"
9223   "fmov{.s|}    %2,@(%0,%1)")
9224
9225 (define_peephole
9226   [(set (match_operand:SI 0 "register_operand" "=r")
9227         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
9228    (set (match_operand:SF 2 "general_movdst_operand" "")
9229
9230         (mem:SF (match_dup 0)))]
9231   "TARGET_SH3E && REGNO (operands[0]) == 0
9232    && ((GET_CODE (operands[2]) == REG
9233         && FP_OR_XD_REGISTER_P (REGNO (operands[2])))
9234        || (GET_CODE (operands[2]) == SUBREG
9235            && FP_OR_XD_REGISTER_P (REGNO (SUBREG_REG (operands[2])))))
9236    && reg_unused_after (operands[0], insn)"
9237   "fmov{.s|}    @(%0,%1),%2")
9238
9239 ;; Switch to a new stack with its address in sp_switch (a SYMBOL_REF).  */
9240 (define_insn "sp_switch_1"
9241   [(const_int 1)]
9242   "TARGET_SH1"
9243   "*
9244 {
9245   rtx xoperands[1];
9246
9247   xoperands[0] = sp_switch;
9248   output_asm_insn (\"mov.l r0,@-r15\;mov.l %0,r0\", xoperands);
9249   output_asm_insn (\"mov.l @r0,r0\;mov.l r15,@-r0\", xoperands);
9250   return \"mov r0,r15\";
9251 }"
9252   [(set_attr "length" "10")])
9253
9254 ;; Switch back to the original stack for interrupt functions with the
9255 ;; sp_switch attribute.  */
9256 (define_insn "sp_switch_2"
9257   [(const_int 2)]
9258   "TARGET_SH1"
9259   "mov.l @r15+,r15\;mov.l @r15+,r0"
9260   [(set_attr "length" "4")])
9261
9262 ;; Integer vector moves
9263
9264 (define_expand "movv8qi"
9265   [(set (match_operand:V8QI 0 "general_movdst_operand" "")
9266         (match_operand:V8QI 1 "general_movsrc_operand" ""))]
9267   "TARGET_SHMEDIA"
9268   "{ if (prepare_move_operands (operands, V8QImode)) DONE; }")
9269
9270 (define_insn "movv8qi_i"
9271   [(set (match_operand:V8QI 0 "general_movdst_operand" "=r,r,r,rl,m")
9272         (match_operand:V8QI 1 "general_movsrc_operand" "r,JSU,nW,m,rl"))]
9273   "TARGET_SHMEDIA
9274    && (register_operand (operands[0], V8QImode)
9275        || register_operand (operands[1], V8QImode))"
9276   "@
9277         add     %1, r63, %0
9278         movi    %1, %0
9279         #
9280         ld%M1.q %m1, %0
9281         st%M0.q %m0, %1"
9282   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media")
9283    (set_attr "length" "4,4,16,4,4")])
9284
9285 (define_split
9286   [(set (match_operand:V8QI 0 "arith_reg_dest" "")
9287         (subreg:V8QI (const_int 0) 0))]
9288   "TARGET_SHMEDIA"
9289   [(set (match_dup 0)
9290         (const_vector:V8QI [(const_int 0) (const_int 0) (const_int 0)
9291                             (const_int 0) (const_int 0) (const_int 0)
9292                             (const_int 0) (const_int 0)]))])
9293
9294 (define_split
9295   [(set (match_operand 0 "arith_reg_dest" "")
9296         (match_operand 1 "sh_rep_vec" ""))]
9297   "TARGET_SHMEDIA && reload_completed
9298    && GET_MODE (operands[0]) == GET_MODE (operands[1])
9299    && VECTOR_MODE_SUPPORTED_P (GET_MODE (operands[0]))
9300    && GET_MODE_SIZE (GET_MODE (operands[0])) == 8
9301    && (XVECEXP (operands[1], 0, 0) != const0_rtx
9302        || XVECEXP (operands[1], 0, 1) != const0_rtx)"
9303   [(set (match_dup 0) (match_dup 1))
9304    (match_dup 2)]
9305   "
9306 {
9307   int unit_size = GET_MODE_UNIT_SIZE (GET_MODE (operands[1]));
9308   rtx elt1 = XVECEXP (operands[1], 0, 1);
9309
9310   if (unit_size > 2)
9311     operands[2] = gen_mshflo_l (operands[0], operands[0], operands[0]);
9312   else
9313     operands[2] = gen_mperm_w0 (operands[0], operands[0]);
9314   operands[0] = gen_rtx_REG (DImode, true_regnum (operands[0]));
9315   operands[1] = XVECEXP (operands[1], 0, 0);
9316   if (unit_size < 2)
9317     {
9318       if (GET_CODE (operands[1]) == CONST_INT && GET_CODE (elt1) == CONST_INT)
9319         operands[1] = GEN_INT (TARGET_LITTLE_ENDIAN
9320                                ? INTVAL (operands[1]) + (INTVAL (elt1) << 8)
9321                                : (INTVAL (operands[1]) << 8) + INTVAL (elt1));
9322       else
9323         {
9324           operands[0] = gen_rtx_REG (V2QImode, true_regnum (operands[0]));
9325           operands[1]
9326             = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, operands[1], elt1));
9327         }
9328     }
9329 }")
9330
9331 (define_split
9332   [(set (match_operand 0 "arith_reg_dest" "")
9333         (match_operand 1 "sh_const_vec" ""))]
9334   "TARGET_SHMEDIA && reload_completed
9335    && GET_MODE (operands[0]) == GET_MODE (operands[1])
9336    && VECTOR_MODE_SUPPORTED_P (GET_MODE (operands[0]))
9337    && operands[1] != CONST0_RTX (GET_MODE (operands[1]))"
9338   [(set (match_dup 0) (match_dup 1))]
9339   "
9340 {
9341   rtx v = operands[1];
9342   enum machine_mode new_mode
9343     = mode_for_size (GET_MODE_BITSIZE (GET_MODE (v)), MODE_INT, 0);
9344
9345   operands[0] = gen_rtx_REG (new_mode, true_regnum (operands[0]));
9346   operands[1]
9347     = simplify_subreg (new_mode, operands[1], GET_MODE (operands[1]), 0);
9348 }")
9349
9350 (define_expand "movv2hi"
9351   [(set (match_operand:V2HI 0 "general_movdst_operand" "")
9352         (match_operand:V2HI 1 "general_movsrc_operand" ""))]
9353   "TARGET_SHMEDIA"
9354   "{ if (prepare_move_operands (operands, V2HImode)) DONE; }")
9355
9356 (define_insn "movv2hi_i"
9357   [(set (match_operand:V2HI 0 "general_movdst_operand" "=r,r,r,rl,m")
9358         (match_operand:V2HI 1 "general_movsrc_operand" "r,JSU,nW,m,rl"))]
9359   "TARGET_SHMEDIA
9360    && (register_operand (operands[0], V2HImode)
9361        || register_operand (operands[1], V2HImode))"
9362   "@
9363         addz.l  %1, r63, %0
9364         movi    %1, %0
9365         #
9366         ld%M1.l %m1, %0
9367         st%M0.l %m0, %1"
9368   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media")
9369    (set_attr "length" "4,4,16,4,4")])
9370
9371 (define_expand "movv4hi"
9372   [(set (match_operand:V4HI 0 "general_movdst_operand" "")
9373         (match_operand:V4HI 1 "general_movsrc_operand" ""))]
9374   "TARGET_SHMEDIA"
9375   "{ if (prepare_move_operands (operands, V4HImode)) DONE; }")
9376
9377 (define_insn "movv4hi_i"
9378   [(set (match_operand:V4HI 0 "general_movdst_operand" "=r,r,r,rl,m")
9379         (match_operand:V4HI 1 "general_movsrc_operand" "r,JSU,nW,m,rl"))]
9380   "TARGET_SHMEDIA
9381    && (register_operand (operands[0], V4HImode)
9382        || register_operand (operands[1], V4HImode))"
9383   "@
9384         add     %1, r63, %0
9385         movi    %1, %0
9386         #
9387         ld%M1.q %m1, %0
9388         st%M0.q %m0, %1"
9389   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media")
9390    (set_attr "length" "4,4,16,4,4")])
9391
9392 (define_expand "movv2si"
9393   [(set (match_operand:V2SI 0 "general_movdst_operand" "")
9394         (match_operand:V2SI 1 "general_movsrc_operand" ""))]
9395   "TARGET_SHMEDIA"
9396   "{ if (prepare_move_operands (operands, V2SImode)) DONE; }")
9397
9398 (define_insn "movv2si_i"
9399   [(set (match_operand:V2SI 0 "general_movdst_operand" "=r,r,r,rl,m")
9400         (match_operand:V2SI 1 "general_movsrc_operand" "r,JSU,nW,m,rl"))]
9401   "TARGET_SHMEDIA
9402    && (register_operand (operands[0], V2SImode)
9403        || register_operand (operands[1], V2SImode))"
9404   "@
9405         add     %1, r63, %0
9406         #
9407         #
9408         ld%M1.q %m1, %0
9409         st%M0.q %m0, %1"
9410   [(set_attr "type"   "arith_media,arith_media,*,load_media,store_media")
9411    (set_attr "length" "4,4,16,4,4")])
9412
9413 ;; Multimedia Intrinsics
9414
9415 (define_insn "absv2si2"
9416   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9417         (abs:V2SI (match_operand:V2SI 1 "arith_reg_operand" "r")))]
9418   "TARGET_SHMEDIA"
9419   "mabs.l       %1, %0"
9420   [(set_attr "type" "mcmp_media")])
9421
9422 (define_insn "absv4hi2"
9423   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9424         (abs:V4HI (match_operand:V4HI 1 "arith_reg_operand" "r")))]
9425   "TARGET_SHMEDIA"
9426   "mabs.w       %1, %0"
9427   [(set_attr "type" "mcmp_media")])
9428
9429 (define_insn "addv2si3"
9430   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9431         (plus:V2SI (match_operand:V2SI 1 "arith_reg_operand" "%r")
9432                    (match_operand:V2SI 2 "arith_reg_operand" "r")))]
9433   "TARGET_SHMEDIA"
9434   "madd.l       %1, %2, %0"
9435   [(set_attr "type" "arith_media")])
9436
9437 (define_insn "addv4hi3"
9438   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9439         (plus:V4HI (match_operand:V4HI 1 "arith_reg_operand" "%r")
9440                    (match_operand:V4HI 2 "arith_reg_operand" "r")))]
9441   "TARGET_SHMEDIA"
9442   "madd.w       %1, %2, %0"
9443   [(set_attr "type" "arith_media")])
9444
9445 (define_insn "ssaddv2si3"
9446   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9447         (ss_plus:V2SI (match_operand:V2SI 1 "arith_reg_operand" "%r")
9448                       (match_operand:V2SI 2 "arith_reg_operand" "r")))]
9449   "TARGET_SHMEDIA"
9450   "madds.l      %1, %2, %0"
9451   [(set_attr "type" "mcmp_media")])
9452
9453 (define_insn "usaddv8qi3"
9454   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
9455         (us_plus:V8QI (match_operand:V8QI 1 "arith_reg_operand" "%r")
9456                       (match_operand:V8QI 2 "arith_reg_operand" "r")))]
9457   "TARGET_SHMEDIA"
9458   "madds.ub     %1, %2, %0"
9459   [(set_attr "type" "mcmp_media")])
9460
9461 (define_insn "ssaddv4hi3"
9462   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9463         (ss_plus:V4HI (match_operand:V4HI 1 "arith_reg_operand" "%r")
9464                       (match_operand:V4HI 2 "arith_reg_operand" "r")))]
9465   "TARGET_SHMEDIA"
9466   "madds.w      %1, %2, %0"
9467   [(set_attr "type" "mcmp_media")])
9468
9469 (define_insn "negcmpeqv8qi"
9470   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
9471         (neg:V8QI (eq:V8QI (match_operand:V8QI 1 "arith_reg_or_0_operand" "%rU")
9472                            (match_operand:V8QI 2 "arith_reg_or_0_operand" "rU"))))]
9473   "TARGET_SHMEDIA"
9474   "mcmpeq.b     %N1, %N2, %0"
9475   [(set_attr "type" "mcmp_media")])
9476
9477 (define_insn "negcmpeqv2si"
9478   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9479         (neg:V2SI (eq:V2SI (match_operand:V2SI 1 "arith_reg_or_0_operand" "%rU")
9480                            (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU"))))]
9481   "TARGET_SHMEDIA"
9482   "mcmpeq.l     %N1, %N2, %0"
9483   [(set_attr "type" "mcmp_media")])
9484
9485 (define_insn "negcmpeqv4hi"
9486   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9487         (neg:V4HI (eq:V4HI (match_operand:V4HI 1 "arith_reg_or_0_operand" "%rU")
9488                            (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU"))))]
9489   "TARGET_SHMEDIA"
9490   "mcmpeq.w     %N1, %N2, %0"
9491   [(set_attr "type" "mcmp_media")])
9492
9493 (define_insn "negcmpgtuv8qi"
9494   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
9495         (neg:V8QI (gtu:V8QI (match_operand:V8QI 1 "arith_reg_or_0_operand" "%rU")
9496                             (match_operand:V8QI 2 "arith_reg_or_0_operand" "rU"))))]
9497   "TARGET_SHMEDIA"
9498   "mcmpgt.ub    %N1, %N2, %0"
9499   [(set_attr "type" "mcmp_media")])
9500
9501 (define_insn "negcmpgtv2si"
9502   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9503         (neg:V2SI (gt:V2SI (match_operand:V2SI 1 "arith_reg_or_0_operand" "%rU")
9504                            (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU"))))]
9505   "TARGET_SHMEDIA"
9506   "mcmpgt.l     %N1, %N2, %0"
9507   [(set_attr "type" "mcmp_media")])
9508
9509 (define_insn "negcmpgtv4hi"
9510   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9511         (neg:V4HI (gt:V4HI (match_operand:V4HI 1 "arith_reg_or_0_operand" "%rU")
9512                            (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU"))))]
9513   "TARGET_SHMEDIA"
9514   "mcmpgt.w     %N1, %N2, %0"
9515   [(set_attr "type" "mcmp_media")])
9516
9517 (define_insn "mcmv"
9518   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
9519         (ior:DI (and:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9520                         (match_operand:DI 2 "arith_reg_operand" "r"))
9521                 (and:DI (match_operand:DI 3 "arith_reg_operand" "0")
9522                         (not:DI (match_dup 2)))))]
9523   "TARGET_SHMEDIA"
9524   "mcmv %N1, %2, %0"
9525   [(set_attr "type" "arith_media")])
9526
9527 (define_insn "mcnvs_lw"
9528   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9529         (vec_concat:V4HI
9530          (ss_truncate:V2HI (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU"))
9531          (ss_truncate:V2HI (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU"))))]
9532   "TARGET_SHMEDIA"
9533   "mcnvs.lw     %N1, %N2, %0"
9534   [(set_attr "type" "mcmp_media")])
9535
9536 (define_insn "mcnvs_wb"
9537   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
9538         (vec_concat:V8QI
9539          (ss_truncate:V4QI (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU"))
9540          (ss_truncate:V4QI (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU"))))]
9541   "TARGET_SHMEDIA"
9542   "mcnvs.wb     %N1, %N2, %0"
9543   [(set_attr "type" "mcmp_media")])
9544
9545 (define_insn "mcnvs_wub"
9546   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
9547         (vec_concat:V8QI
9548          (us_truncate:V4QI (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU"))
9549          (us_truncate:V4QI (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU"))))]
9550   "TARGET_SHMEDIA"
9551   "mcnvs.wub    %N1, %N2, %0"
9552   [(set_attr "type" "mcmp_media")])
9553
9554 (define_insn "mextr_rl"
9555   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
9556         (ior:DI (lshiftrt:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9557                              (match_operand:HI 3 "mextr_bit_offset" "i"))
9558                (ashift:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU")
9559                           (match_operand:HI 4 "mextr_bit_offset" "i"))))]
9560   "TARGET_SHMEDIA && INTVAL (operands[3]) + INTVAL (operands[4]) == 64"
9561   "*
9562 {
9563   static char templ[16];
9564
9565   sprintf (templ, \"mextr%d\\t%%N1, %%N2, %%0\",
9566            (int) INTVAL (operands[3]) >> 3);
9567   return templ;
9568 }"
9569   [(set_attr "type" "arith_media")])
9570
9571 (define_insn "*mextr_lr"
9572   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
9573         (ior:DI (ashift:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9574                            (match_operand:HI 3 "mextr_bit_offset" "i"))
9575                (lshiftrt:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU")
9576                             (match_operand:HI 4 "mextr_bit_offset" "i"))))]
9577   "TARGET_SHMEDIA && INTVAL (operands[3]) + INTVAL (operands[4]) == 64"
9578   "*
9579 {
9580   static char templ[16];
9581
9582   sprintf (templ, \"mextr%d\\t%%N2, %%N1, %%0\",
9583            (int) INTVAL (operands[4]) >> 3);
9584   return templ;
9585 }"
9586   [(set_attr "type" "arith_media")])
9587
9588 ; mextrN can be modelled with vec_select / vec_concat, but the selection
9589 ; vector then varies depending on endianness.
9590 (define_expand "mextr1"
9591   [(match_operand:DI 0 "arith_reg_dest" "")
9592    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9593    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9594   "TARGET_SHMEDIA"
9595   "
9596 {
9597   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9598                            GEN_INT (1 * 8), GEN_INT (7 * 8)));
9599   DONE;
9600 }")
9601
9602 (define_expand "mextr2"
9603   [(match_operand:DI 0 "arith_reg_dest" "")
9604    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9605    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9606   "TARGET_SHMEDIA"
9607   "
9608 {
9609   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9610                            GEN_INT (2 * 8), GEN_INT (6 * 8)));
9611   DONE;
9612 }")
9613
9614 (define_expand "mextr3"
9615   [(match_operand:DI 0 "arith_reg_dest" "")
9616    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9617    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9618   "TARGET_SHMEDIA"
9619   "
9620 {
9621   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9622                            GEN_INT (3 * 8), GEN_INT (5 * 8)));
9623   DONE;
9624 }")
9625
9626 (define_expand "mextr4"
9627   [(match_operand:DI 0 "arith_reg_dest" "")
9628    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9629    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9630   "TARGET_SHMEDIA"
9631   "
9632 {
9633   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9634                            GEN_INT (4 * 8), GEN_INT (4 * 8)));
9635   DONE;
9636 }")
9637
9638 (define_expand "mextr5"
9639   [(match_operand:DI 0 "arith_reg_dest" "")
9640    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9641    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9642   "TARGET_SHMEDIA"
9643   "
9644 {
9645   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9646                            GEN_INT (5 * 8), GEN_INT (3 * 8)));
9647   DONE;
9648 }")
9649
9650 (define_expand "mextr6"
9651   [(match_operand:DI 0 "arith_reg_dest" "")
9652    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9653    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9654   "TARGET_SHMEDIA"
9655   "
9656 {
9657   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9658                            GEN_INT (6 * 8), GEN_INT (2 * 8)));
9659   DONE;
9660 }")
9661
9662 (define_expand "mextr7"
9663   [(match_operand:DI 0 "arith_reg_dest" "")
9664    (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
9665    (match_operand:DI 2 "arith_reg_or_0_operand" "rU")]
9666   "TARGET_SHMEDIA"
9667   "
9668 {
9669   emit_insn (gen_mextr_rl (operands[0], operands[1], operands[2],
9670                            GEN_INT (7 * 8), GEN_INT (1 * 8)));
9671   DONE;
9672 }")
9673
9674 (define_expand "mmacfx_wl"
9675   [(match_operand:V2SI 0 "arith_reg_dest" "")
9676    (match_operand:V2HI 1 "extend_reg_operand" "")
9677    (match_operand:V2HI 2 "extend_reg_operand" "")
9678    (match_operand:V2SI 3 "arith_reg_operand" "")]
9679   "TARGET_SHMEDIA"
9680   "
9681 {
9682   emit_insn (gen_mmacfx_wl_i (operands[0], operands[3],
9683                               operands[1], operands[2]));
9684   DONE;
9685 }")
9686
9687 (define_insn "mmacfx_wl_i"
9688   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9689         (ss_plus:V2SI
9690          (match_operand:V2SI 1 "arith_reg_operand" "0")
9691          (ss_truncate:V2SI
9692           (ashift:V2DI
9693            (sign_extend:V2DI
9694             (mult:V2SI
9695              (sign_extend:V2SI (match_operand:V2HI 2 "extend_reg_operand" "r"))
9696              (sign_extend:V2SI (match_operand:V2HI 3 "extend_reg_operand" "r"))))
9697            (const_int 1)))))]
9698   "TARGET_SHMEDIA"
9699   "mmacfx.wl    %2, %3, %0"
9700   [(set_attr "type" "mac_media")])
9701
9702 (define_expand "mmacnfx_wl"
9703   [(match_operand:V2SI 0 "arith_reg_dest" "")
9704    (match_operand:V2HI 1 "extend_reg_operand" "")
9705    (match_operand:V2HI 2 "extend_reg_operand" "")
9706    (match_operand:V2SI 3 "arith_reg_operand" "")]
9707   "TARGET_SHMEDIA"
9708   "
9709 {
9710   emit_insn (gen_mmacnfx_wl_i (operands[0], operands[3],
9711                                operands[1], operands[2]));
9712   DONE;
9713 }")
9714
9715 (define_insn "mmacnfx_wl_i"
9716   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9717         (ss_minus:V2SI
9718          (match_operand:V2SI 1 "arith_reg_operand" "0")
9719          (ss_truncate:V2SI
9720           (ashift:V2DI
9721            (sign_extend:V2DI
9722             (mult:V2SI
9723              (sign_extend:V2SI (match_operand:V2HI 2 "extend_reg_operand" "r"))
9724              (sign_extend:V2SI (match_operand:V2HI 3 "extend_reg_operand" "r"))))
9725            (const_int 1)))))]
9726   "TARGET_SHMEDIA"
9727   "mmacnfx.wl   %2, %3, %0"
9728   [(set_attr "type" "mac_media")])
9729
9730 (define_insn "mulv2si3"
9731   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9732         (mult:V2SI (match_operand:V2SI 1 "arith_reg_operand" "r")
9733                    (match_operand:V2SI 2 "arith_reg_operand" "r")))]
9734   "TARGET_SHMEDIA"
9735   "mmul.l       %1, %2, %0"
9736   [(set_attr "type" "d2mpy_media")])
9737
9738 (define_insn "mulv4hi3"
9739   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9740         (mult:V4HI (match_operand:V4HI 1 "arith_reg_operand" "r")
9741                    (match_operand:V4HI 2 "arith_reg_operand" "r")))]
9742   "TARGET_SHMEDIA"
9743   "mmul.w       %1, %2, %0"
9744   [(set_attr "type" "dmpy_media")])
9745
9746 (define_insn "mmulfx_l"
9747   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9748         (ss_truncate:V2SI
9749          (ashiftrt:V2DI
9750           (mult:V2DI
9751            (sign_extend:V2DI (match_operand:V2SI 1 "arith_reg_operand" "r"))
9752            (sign_extend:V2DI (match_operand:V2SI 2 "arith_reg_operand" "r")))
9753           (const_int 31))))]
9754   "TARGET_SHMEDIA"
9755   "mmulfx.l     %1, %2, %0"
9756   [(set_attr "type" "d2mpy_media")])
9757
9758 (define_insn "mmulfx_w"
9759   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9760         (ss_truncate:V4HI
9761          (ashiftrt:V4SI
9762           (mult:V4SI
9763            (sign_extend:V4SI (match_operand:V4HI 1 "arith_reg_operand" "r"))
9764            (sign_extend:V4SI (match_operand:V4HI 2 "arith_reg_operand" "r")))
9765           (const_int 15))))]
9766   "TARGET_SHMEDIA"
9767   "mmulfx.w     %1, %2, %0"
9768   [(set_attr "type" "dmpy_media")])
9769
9770 (define_insn "mmulfxrp_w"
9771   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9772         (ss_truncate:V4HI
9773          (ashiftrt:V4SI
9774           (plus:V4SI
9775            (mult:V4SI
9776             (sign_extend:V4SI (match_operand:V4HI 1 "arith_reg_operand" "r"))
9777             (sign_extend:V4SI (match_operand:V4HI 2 "arith_reg_operand" "r")))
9778            (const_int 16384))
9779           (const_int 15))))]
9780   "TARGET_SHMEDIA"
9781   "mmulfxrp.w   %1, %2, %0"
9782   [(set_attr "type" "dmpy_media")])
9783
9784 (define_expand "mmulhi_wl"
9785   [(match_operand:V2SI 0 "arith_reg_dest" "")
9786    (match_operand:V4HI 1 "arith_reg_operand" "")
9787    (match_operand:V4HI 2 "arith_reg_operand" "")]
9788   "TARGET_SHMEDIA"
9789   "
9790 {
9791   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mmul23_wl : gen_mmul01_wl)
9792              (operands[0], operands[1], operands[2]));
9793   DONE;
9794 }")
9795
9796 (define_expand "mmullo_wl"
9797   [(match_operand:V2SI 0 "arith_reg_dest" "")
9798    (match_operand:V4HI 1 "arith_reg_operand" "")
9799    (match_operand:V4HI 2 "arith_reg_operand" "")]
9800   "TARGET_SHMEDIA"
9801   "
9802 {
9803   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mmul01_wl : gen_mmul23_wl)
9804              (operands[0], operands[1], operands[2]));
9805   DONE;
9806 }")
9807
9808 (define_insn "mmul23_wl"
9809   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9810         (vec_select:V2SI
9811          (mult:V4SI
9812           (sign_extend:V4SI (match_operand:V4HI 1 "arith_reg_operand" "r"))
9813           (sign_extend:V4SI (match_operand:V4HI 2 "arith_reg_operand" "r")))
9814          (parallel [(const_int 2) (const_int 3)])))]
9815   "TARGET_SHMEDIA"
9816   "* return (TARGET_LITTLE_ENDIAN
9817              ? \"mmulhi.wl      %1, %2, %0\"
9818              : \"mmullo.wl      %1, %2, %0\");"
9819   [(set_attr "type" "dmpy_media")])
9820
9821 (define_insn "mmul01_wl"
9822   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9823         (vec_select:V2SI
9824          (mult:V4SI
9825           (sign_extend:V4SI (match_operand:V4HI 1 "arith_reg_operand" "r"))
9826           (sign_extend:V4SI (match_operand:V4HI 2 "arith_reg_operand" "r")))
9827          (parallel [(const_int 0) (const_int 1)])))]
9828   "TARGET_SHMEDIA"
9829   "* return (TARGET_LITTLE_ENDIAN
9830              ? \"mmullo.wl      %1, %2, %0\"
9831              : \"mmulhi.wl      %1, %2, %0\");"
9832   [(set_attr "type" "dmpy_media")])
9833
9834 (define_expand "mmulsum_wq"
9835   [(match_operand:DI 0 "arith_reg_dest" "")
9836    (match_operand:V4HI 1 "arith_reg_operand" "")
9837    (match_operand:V4HI 2 "arith_reg_operand" "")
9838    (match_operand:DI 3 "arith_reg_operand" "")]
9839   "TARGET_SHMEDIA"
9840   "
9841 {
9842   emit_insn (gen_mmulsum_wq_i (operands[0], operands[3],
9843                                operands[1], operands[2]));
9844   DONE;
9845 }")
9846
9847 (define_insn "mmulsum_wq_i"
9848   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
9849         (plus:DI (match_operand:DI 1 "arith_reg_operand" "0")
9850          (plus:DI
9851           (plus:DI
9852            (vec_select:DI
9853             (mult:V4DI
9854              (sign_extend:V4DI (match_operand:V4HI 2 "arith_reg_operand" "r"))
9855              (sign_extend:V4DI (match_operand:V4HI 3 "arith_reg_operand" "r")))
9856             (parallel [(const_int 0)]))
9857            (vec_select:DI (mult:V4DI (sign_extend:V4DI (match_dup 2))
9858                                      (sign_extend:V4DI (match_dup 3)))
9859                           (parallel [(const_int 1)])))
9860           (plus:DI
9861            (vec_select:DI (mult:V4DI (sign_extend:V4DI (match_dup 2))
9862                                      (sign_extend:V4DI (match_dup 3)))
9863                           (parallel [(const_int 2)]))
9864            (vec_select:DI (mult:V4DI (sign_extend:V4DI (match_dup 2))
9865                                      (sign_extend:V4DI (match_dup 3)))
9866                           (parallel [(const_int 3)]))))))]
9867   "TARGET_SHMEDIA"
9868   "mmulsum.wq   %2, %3, %0"
9869   [(set_attr "type" "mac_media")])
9870
9871 (define_expand "mperm_w"
9872   [(match_operand:V4HI 0 "arith_reg_dest" "=r")
9873    (match_operand:V4HI 1 "arith_reg_operand" "r")
9874    (match_operand:QI 2 "extend_reg_or_0_operand" "rU")]
9875   "TARGET_SHMEDIA"
9876   "
9877 {
9878   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mperm_w_little : gen_mperm_w_big)
9879              (operands[0], operands[1], operands[2]));
9880 }")
9881
9882 ; This use of vec_select isn't exactly correct according to rtl.texi
9883 ; (because not constant), but it seems a straightforward extension.
9884 (define_insn "mperm_w_little"
9885   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9886         (vec_select:V4HI
9887          (match_operand:V4HI 1 "arith_reg_operand" "r")
9888          (parallel
9889           [(zero_extract (match_operand:QI 2 "extend_reg_or_0_operand" "rU")
9890                          (const_int 2) (const_int 0))
9891            (zero_extract (match_dup 2) (const_int 2) (const_int 2))
9892            (zero_extract (match_dup 2) (const_int 2) (const_int 4))
9893            (zero_extract (match_dup 2) (const_int 2) (const_int 6))])))]
9894   "TARGET_SHMEDIA && TARGET_LITTLE_ENDIAN"
9895   "mperm.w      %1, %N2, %0"
9896   [(set_attr "type" "arith_media")])
9897
9898 (define_insn "mperm_w_big"
9899   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9900         (vec_select:V4HI
9901          (match_operand:V4HI 1 "arith_reg_operand" "r")
9902          (parallel
9903           [(zero_extract (not:QI (match_operand:QI 2
9904                                   "extend_reg_or_0_operand" "rU"))
9905                          (const_int 2) (const_int 0))
9906            (zero_extract (not:QI (match_dup 2)) (const_int 2) (const_int 2))
9907            (zero_extract (not:QI (match_dup 2)) (const_int 2) (const_int 4))
9908            (zero_extract (not:QI (match_dup 2)) (const_int 2) (const_int 6))])))]
9909   "TARGET_SHMEDIA && ! TARGET_LITTLE_ENDIAN"
9910   "mperm.w      %1, %N2, %0"
9911   [(set_attr "type" "arith_media")])
9912
9913 (define_insn "mperm_w0"
9914   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9915         (vec_duplicate:V4HI (truncate:HI (match_operand 1
9916                                           "trunc_hi_operand" "r"))))]
9917   "TARGET_SHMEDIA"
9918   "mperm.w      %1, r63, %0"
9919   [(set_attr "type" "arith_media")])
9920
9921 (define_expand "msad_ubq"
9922   [(match_operand:DI 0 "arith_reg_dest" "")
9923    (match_operand:V8QI 1 "arith_reg_or_0_operand" "")
9924    (match_operand:V8QI 2 "arith_reg_or_0_operand" "")
9925    (match_operand:DI 3 "arith_reg_operand" "")]
9926   "TARGET_SHMEDIA"
9927   "
9928 {
9929   emit_insn (gen_msad_ubq_i (operands[0], operands[3],
9930                              operands[1], operands[2]));
9931   DONE;
9932 }")
9933
9934 (define_insn "msad_ubq_i"
9935   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
9936         (plus:DI
9937          (plus:DI
9938           (plus:DI
9939            (plus:DI
9940             (match_operand:DI 1 "arith_reg_operand" "0")
9941             (abs:DI (vec_select:DI
9942                      (minus:V8DI
9943                       (zero_extend:V8DI
9944                        (match_operand:V8QI 2 "arith_reg_or_0_operand" "r"))
9945                       (zero_extend:V8DI
9946                        (match_operand:V8QI 3 "arith_reg_or_0_operand" "r")))
9947                      (parallel [(const_int 0)]))))
9948            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9949                                               (zero_extend:V8DI (match_dup 3)))
9950                                   (parallel [(const_int 1)]))))
9951           (plus:DI
9952            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9953                                               (zero_extend:V8DI (match_dup 3)))
9954                                   (parallel [(const_int 2)])))
9955            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9956                                               (zero_extend:V8DI (match_dup 3)))
9957                                   (parallel [(const_int 3)])))))
9958          (plus:DI
9959           (plus:DI
9960            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9961                                               (zero_extend:V8DI (match_dup 3)))
9962                                   (parallel [(const_int 4)])))
9963            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9964                                               (zero_extend:V8DI (match_dup 3)))
9965                                   (parallel [(const_int 5)]))))
9966           (plus:DI
9967            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9968                                               (zero_extend:V8DI (match_dup 3)))
9969                                   (parallel [(const_int 6)])))
9970            (abs:DI (vec_select:DI (minus:V8DI (zero_extend:V8DI (match_dup 2))
9971                                               (zero_extend:V8DI (match_dup 3)))
9972                                   (parallel [(const_int 7)])))))))]
9973   "TARGET_SHMEDIA"
9974   "msad.ubq     %N2, %N3, %0"
9975   [(set_attr "type" "mac_media")])
9976
9977 (define_insn "mshalds_l"
9978   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
9979         (ss_truncate:V2SI
9980          (ashift:V2DI
9981           (sign_extend:V2DI (match_operand:V2SI 1 "arith_reg_operand" "r"))
9982           (and:DI (match_operand:DI 2 "arith_reg_operand" "r")
9983                   (const_int 31)))))]
9984   "TARGET_SHMEDIA"
9985   "mshalds.l    %1, %2, %0"
9986   [(set_attr "type" "mcmp_media")])
9987
9988 (define_insn "mshalds_w"
9989   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
9990         (ss_truncate:V4HI
9991          (ashift:V4SI
9992           (sign_extend:V4SI (match_operand:V4HI 1 "arith_reg_operand" "r"))
9993           (and:DI (match_operand:DI 2 "arith_reg_operand" "r")
9994                   (const_int 15)))))]
9995   "TARGET_SHMEDIA"
9996   "mshalds.w    %1, %2, %0"
9997   [(set_attr "type" "mcmp_media")])
9998
9999 (define_insn "ashrv2si3"
10000   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10001         (ashiftrt:V2SI (match_operand:V2SI 1 "arith_reg_operand" "r")
10002                        (match_operand:DI 2 "arith_reg_operand" "r")))]
10003   "TARGET_SHMEDIA"
10004   "mshard.l     %1, %2, %0"
10005   [(set_attr "type" "arith_media")])
10006
10007 (define_insn "ashrv4hi3"
10008   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10009         (ashiftrt:V4HI (match_operand:V4HI 1 "arith_reg_operand" "r")
10010                        (match_operand:DI 2 "arith_reg_operand" "r")))]
10011   "TARGET_SHMEDIA"
10012   "mshard.w     %1, %2, %0"
10013   [(set_attr "type" "arith_media")])
10014
10015 (define_insn "mshards_q"
10016   [(set (match_operand:HI 0 "arith_reg_dest" "=r")
10017         (ss_truncate:HI
10018          (ashiftrt:DI (match_operand:DI 1 "arith_reg_operand" "r")
10019                       (match_operand:DI 2 "arith_reg_or_0_operand" "rU"))))]
10020   "TARGET_SHMEDIA"
10021   "mshards.q    %1, %N2, %0"
10022   [(set_attr "type" "mcmp_media")])
10023
10024 (define_expand "mshfhi_b"
10025   [(match_operand:V8QI 0 "arith_reg_dest" "")
10026    (match_operand:V8QI 1 "arith_reg_or_0_operand" "rU")
10027    (match_operand:V8QI 2 "arith_reg_or_0_operand" "rU")]
10028   "TARGET_SHMEDIA"
10029   "
10030 {
10031   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mshf4_b : gen_mshf0_b)
10032              (operands[0], operands[1], operands[2]));
10033   DONE;
10034 }")
10035
10036 (define_expand "mshflo_b"
10037   [(match_operand:V8QI 0 "arith_reg_dest" "")
10038    (match_operand:V8QI 1 "arith_reg_or_0_operand" "rU")
10039    (match_operand:V8QI 2 "arith_reg_or_0_operand" "rU")]
10040   "TARGET_SHMEDIA"
10041   "
10042 {
10043   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mshf0_b : gen_mshf4_b)
10044              (operands[0], operands[1], operands[2]));
10045   DONE;
10046 }")
10047
10048 (define_insn "mshf4_b"
10049   [(set
10050     (match_operand:V8QI 0 "arith_reg_dest" "=r")
10051     (vec_select:V8QI
10052      (vec_concat:V16QI (match_operand:V8QI 1 "arith_reg_or_0_operand" "rU")
10053                        (match_operand:V8QI 2 "arith_reg_or_0_operand" "rU"))
10054      (parallel [(const_int 4) (const_int 12) (const_int 5) (const_int 13)
10055                 (const_int 6) (const_int 14) (const_int 7) (const_int 15)])))]
10056   "TARGET_SHMEDIA"
10057   "* return (TARGET_LITTLE_ENDIAN
10058              ? \"mshfhi.b       %N1, %N2, %0\"
10059              : \"mshflo.b       %N1, %N2, %0\");"
10060   [(set_attr "type" "arith_media")])
10061
10062 (define_insn "mshf0_b"
10063   [(set
10064     (match_operand:V8QI 0 "arith_reg_dest" "=r")
10065     (vec_select:V8QI
10066      (vec_concat:V16QI (match_operand:V8QI 1 "arith_reg_or_0_operand" "rU")
10067                        (match_operand:V8QI 2 "arith_reg_or_0_operand" "rU"))
10068      (parallel [(const_int 0) (const_int 8) (const_int 1) (const_int 9)
10069                 (const_int 2) (const_int 10) (const_int 3) (const_int 11)])))]
10070   "TARGET_SHMEDIA"
10071   "* return (TARGET_LITTLE_ENDIAN
10072              ? \"mshflo.b       %N1, %N2, %0\"
10073              : \"mshfhi.b       %N1, %N2, %0\");"
10074   [(set_attr "type" "arith_media")])
10075
10076 (define_expand "mshfhi_l"
10077   [(match_operand:V2SI 0 "arith_reg_dest" "")
10078    (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU")
10079    (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU")]
10080   "TARGET_SHMEDIA"
10081   "
10082 {
10083   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mshf4_l : gen_mshf0_l)
10084              (operands[0], operands[1], operands[2]));
10085   DONE;
10086 }")
10087
10088 (define_expand "mshflo_l"
10089   [(match_operand:V2SI 0 "arith_reg_dest" "")
10090    (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU")
10091    (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU")]
10092   "TARGET_SHMEDIA"
10093   "
10094 {
10095   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mshf0_l : gen_mshf4_l)
10096              (operands[0], operands[1], operands[2]));
10097   DONE;
10098 }")
10099
10100 (define_insn "mshf4_l"
10101   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10102         (vec_select:V2SI
10103          (vec_concat:V4SI (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU")
10104                           (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU"))
10105          (parallel [(const_int 1) (const_int 3)])))]
10106   "TARGET_SHMEDIA"
10107   "* return (TARGET_LITTLE_ENDIAN
10108              ? \"mshfhi.l       %N1, %N2, %0\"
10109              : \"mshflo.l       %N1, %N2, %0\");"
10110   [(set_attr "type" "arith_media")])
10111
10112 (define_insn "mshf0_l"
10113   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10114         (vec_select:V2SI
10115          (vec_concat:V4SI (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU")
10116                           (match_operand:V2SI 2 "arith_reg_or_0_operand" "rU"))
10117          (parallel [(const_int 0) (const_int 2)])))]
10118   "TARGET_SHMEDIA"
10119   "* return (TARGET_LITTLE_ENDIAN
10120              ? \"mshflo.l       %N1, %N2, %0\"
10121              : \"mshfhi.l       %N1, %N2, %0\");"
10122   [(set_attr "type" "arith_media")])
10123
10124 (define_expand "mshfhi_w"
10125   [(match_operand:V4HI 0 "arith_reg_dest" "")
10126    (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU")
10127    (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU")]
10128   "TARGET_SHMEDIA"
10129   "
10130 {
10131   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mshf4_w : gen_mshf0_w)
10132              (operands[0], operands[1], operands[2]));
10133   DONE;
10134 }")
10135
10136 (define_expand "mshflo_w"
10137   [(match_operand:V4HI 0 "arith_reg_dest" "")
10138    (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU")
10139    (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU")]
10140   "TARGET_SHMEDIA"
10141   "
10142 {
10143   emit_insn ((TARGET_LITTLE_ENDIAN ? gen_mshf0_w : gen_mshf4_w)
10144              (operands[0], operands[1], operands[2]));
10145   DONE;
10146 }")
10147
10148 (define_insn "mshf4_w"
10149   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10150         (vec_select:V4HI
10151          (vec_concat:V8HI (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU")
10152                           (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU"))
10153          (parallel [(const_int 2) (const_int 6) (const_int 3) (const_int 7)])))]
10154   "TARGET_SHMEDIA"
10155   "* return (TARGET_LITTLE_ENDIAN
10156              ? \"mshfhi.w       %N1, %N2, %0\"
10157              : \"mshflo.w       %N1, %N2, %0\");"
10158   [(set_attr "type" "arith_media")])
10159
10160 (define_insn "mshf0_w"
10161   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10162         (vec_select:V4HI
10163          (vec_concat:V8HI (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU")
10164                           (match_operand:V4HI 2 "arith_reg_or_0_operand" "rU"))
10165          (parallel [(const_int 0) (const_int 4) (const_int 1) (const_int 5)])))]
10166   "TARGET_SHMEDIA"
10167   "* return (TARGET_LITTLE_ENDIAN
10168              ? \"mshflo.w       %N1, %N2, %0\"
10169              : \"mshfhi.w       %N1, %N2, %0\");"
10170   [(set_attr "type" "arith_media")])
10171
10172 (define_insn "mshflo_w_x"
10173   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10174         (vec_select:V4HI
10175          (vec_concat:V4HI (match_operand:V2HI 1 "extend_reg_or_0_operand" "rU")
10176                           (match_operand:V2HI 2 "extend_reg_or_0_operand" "rU"))
10177          (parallel [(const_int 0) (const_int 2) (const_int 1) (const_int 3)])))]
10178   "TARGET_SHMEDIA"
10179   "mshflo.w     %N1, %N2, %0"
10180   [(set_attr "type" "arith_media")])
10181
10182 /* These are useful to expand ANDs and as combiner patterns.  */
10183 (define_insn_and_split "mshfhi_l_di"
10184   [(set (match_operand:DI 0 "arith_reg_dest" "=r,f")
10185         (ior:DI (lshiftrt:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU,f")
10186                              (const_int 32))
10187                 (and:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU,?f")
10188                         (const_int -4294967296))))]
10189   "TARGET_SHMEDIA"
10190   "@
10191         mshfhi.l        %N1, %N2, %0
10192         #"
10193   "TARGET_SHMEDIA && reload_completed
10194    && ! GENERAL_REGISTER_P (true_regnum (operands[0]))"
10195   [(set (match_dup 3) (match_dup 4))
10196    (set (match_dup 5) (match_dup 6))]
10197   "
10198 {
10199   operands[3] = gen_lowpart (SImode, operands[0]);
10200   operands[4] = gen_highpart (SImode, operands[1]);
10201   operands[5] = gen_highpart (SImode, operands[0]);
10202   operands[6] = gen_highpart (SImode, operands[2]);
10203 }"
10204   [(set_attr "type" "arith_media")])
10205
10206 (define_insn "*mshfhi_l_di_rev"
10207   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10208         (ior:DI (and:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
10209                         (const_int -4294967296))
10210                 (lshiftrt:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU")
10211                              (const_int 32))))]
10212   "TARGET_SHMEDIA"
10213   "mshfhi.l     %N2, %N1, %0"
10214   [(set_attr "type" "arith_media")])
10215
10216 (define_split
10217   [(set (match_operand:DI 0 "arith_reg_dest" "")
10218         (ior:DI (zero_extend:DI (match_operand:SI 1
10219                                               "extend_reg_or_0_operand" ""))
10220                 (and:DI (match_operand:DI 2 "arith_reg_or_0_operand" "")
10221                         (const_int -4294967296))))
10222    (clobber (match_operand:DI 3 "arith_reg_dest" ""))]
10223   "TARGET_SHMEDIA"
10224   [(const_int 0)]
10225   "
10226 {
10227   emit_insn (gen_ashldi3_media (operands[3],
10228                                 simplify_gen_subreg (DImode, operands[1],
10229                                                      SImode, 0),
10230                                 GEN_INT (32)));
10231   emit_insn (gen_mshfhi_l_di (operands[0], operands[3], operands[2]));
10232   DONE;
10233 }")
10234
10235 (define_insn "mshflo_l_di"
10236   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10237         (ior:DI (and:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
10238                         (const_int 4294967295))
10239                 (ashift:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU")
10240                            (const_int 32))))]
10241                                 
10242   "TARGET_SHMEDIA"
10243   "mshflo.l     %N1, %N2, %0"
10244   [(set_attr "type" "arith_media")])
10245
10246 (define_insn "*mshflo_l_di_rev"
10247   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10248         (ior:DI (ashift:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
10249                            (const_int 32))
10250                 (and:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU")
10251                         (const_int 4294967295))))]
10252                                 
10253   "TARGET_SHMEDIA"
10254   "mshflo.l     %N2, %N1, %0"
10255   [(set_attr "type" "arith_media")])
10256
10257 ;; Combiner pattern for trampoline initialization.
10258 (define_insn_and_split "*double_shori"
10259   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10260         (ior:DI (ashift:DI (match_operand:DI 1 "arith_reg_operand" "0")
10261                            (const_int 32))
10262                 (match_operand:DI 2 "const_int_operand" "n")))]
10263   "TARGET_SHMEDIA
10264    && INTVAL (operands[2]) == trunc_int_for_mode (INTVAL (operands[2]), SImode)"
10265   "#"
10266   "rtx_equal_p (operands[0], operands[1])"
10267   [(const_int 0)]
10268   "
10269 {
10270   HOST_WIDE_INT v = INTVAL (operands[2]);
10271
10272   emit_insn (gen_shori_media (operands[0], operands[0],
10273              gen_int_mode (INTVAL (operands[2]) >> 16, HImode)));
10274   emit_insn (gen_shori_media (operands[0], operands[0],
10275                               gen_int_mode (v, HImode)));
10276   DONE;
10277 }")
10278
10279
10280 (define_insn "*mshflo_l_di_x"
10281   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10282         (ior:DI (zero_extend:DI (match_operand:SI 1 "extend_reg_or_0_operand"
10283                                  "rU"))
10284                 (ashift:DI (match_operand:DI 2 "arith_reg_or_0_operand" "rU")
10285                            (const_int 32))))]
10286                                 
10287   "TARGET_SHMEDIA"
10288   "mshflo.l     %N1, %N2, %0"
10289   [(set_attr "type" "arith_media")])
10290
10291 (define_insn_and_split "concat_v2sf"
10292   [(set (match_operand:V2SF 0 "register_operand" "=r,f,f?")
10293 ;;      (vec_concat:V2SF (match_operand:SF 1 "register_operand" "rU,0,f")
10294         (vec_concat:V2SF (match_operand:SF 1 "register_operand" "rU,f,f")
10295                          (match_operand:SF 2 "register_operand" "rU,f,f")))]
10296                                 
10297   "TARGET_SHMEDIA"
10298   "@
10299         mshflo.l        %N1, %N2, %0
10300         #
10301         #"
10302   "TARGET_SHMEDIA && reload_completed
10303    && ! GENERAL_REGISTER_P (true_regnum (operands[0]))"
10304   [(set (match_dup 3) (match_dup 1))
10305    (set (match_dup 4) (match_dup 2))]
10306   "
10307 {
10308   operands[3] = simplify_gen_subreg (SFmode, operands[0], V2SFmode, 0);
10309   operands[4] = simplify_gen_subreg (SFmode, operands[0], V2SFmode, 4);
10310 }"
10311   [(set_attr "type" "arith_media")])
10312
10313 (define_insn "*mshflo_l_di_x_rev"
10314   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10315         (ior:DI (ashift:DI (match_operand:DI 1 "arith_reg_or_0_operand" "rU")
10316                            (const_int 32))
10317                 (zero_extend:DI (match_operand:SI 2 "extend_reg_or_0_operand" "rU"))))]
10318                                 
10319   "TARGET_SHMEDIA"
10320   "mshflo.l     %N2, %N1, %0"
10321   [(set_attr "type" "arith_media")])
10322
10323 (define_insn "ashlv2si3"
10324   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10325         (ashift:V2SI (match_operand:V2SI 1 "arith_reg_operand" "r")
10326                      (match_operand:DI 2 "arith_reg_operand" "r")))]
10327   "TARGET_SHMEDIA"
10328   "mshlld.l     %1, %2, %0"
10329   [(set_attr "type" "arith_media")])
10330
10331 (define_insn "ashlv4hi3"
10332   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10333         (ashift:V4HI (match_operand:V4HI 1 "arith_reg_operand" "r")
10334                      (match_operand:DI 2 "arith_reg_operand" "r")))]
10335   "TARGET_SHMEDIA"
10336   "mshlld.w     %1, %2, %0"
10337   [(set_attr "type" "arith_media")])
10338
10339 (define_insn "lshrv2si3"
10340   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10341         (lshiftrt:V2SI (match_operand:V2SI 1 "arith_reg_operand" "r")
10342                        (match_operand:DI 2 "arith_reg_operand" "r")))]
10343   "TARGET_SHMEDIA"
10344   "mshlrd.l     %1, %2, %0"
10345   [(set_attr "type" "arith_media")])
10346
10347 (define_insn "lshrv4hi3"
10348   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10349         (lshiftrt:V4HI (match_operand:V4HI 1 "arith_reg_operand" "r")
10350                        (match_operand:DI 2 "arith_reg_operand" "r")))]
10351   "TARGET_SHMEDIA"
10352   "mshlrd.w     %1, %2, %0"
10353   [(set_attr "type" "arith_media")])
10354
10355 (define_insn "subv2si3"
10356   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10357         (minus:V2SI (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU")
10358                     (match_operand:V2SI 2 "arith_reg_operand" "r")))]
10359   "TARGET_SHMEDIA"
10360   "msub.l       %N1, %2, %0"
10361   [(set_attr "type" "arith_media")])
10362
10363 (define_insn "subv4hi3"
10364   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10365         (minus:V4HI (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU")
10366                     (match_operand:V4HI 2 "arith_reg_operand" "r")))]
10367   "TARGET_SHMEDIA"
10368   "msub.w       %N1, %2, %0"
10369   [(set_attr "type" "arith_media")])
10370
10371 (define_insn "sssubv2si3"
10372   [(set (match_operand:V2SI 0 "arith_reg_dest" "=r")
10373         (ss_minus:V2SI (match_operand:V2SI 1 "arith_reg_or_0_operand" "rU")
10374                        (match_operand:V2SI 2 "arith_reg_operand" "r")))]
10375   "TARGET_SHMEDIA"
10376   "msubs.l      %N1, %2, %0"
10377   [(set_attr "type" "mcmp_media")])
10378
10379 (define_insn "ussubv8qi3"
10380   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
10381         (us_minus:V8QI (match_operand:V8QI 1 "arith_reg_operand" "r")
10382                        (match_operand:V8QI 2 "arith_reg_operand" "r")))]
10383   "TARGET_SHMEDIA"
10384   "msubs.ub     %1, %2, %0"
10385   [(set_attr "type" "mcmp_media")])
10386
10387 (define_insn "sssubv4hi3"
10388   [(set (match_operand:V4HI 0 "arith_reg_dest" "=r")
10389         (ss_minus:V4HI (match_operand:V4HI 1 "arith_reg_or_0_operand" "rU")
10390                        (match_operand:V4HI 2 "arith_reg_operand" "r")))]
10391   "TARGET_SHMEDIA"
10392   "msubs.w      %N1, %2, %0"
10393   [(set_attr "type" "mcmp_media")])
10394
10395 ;; Floating Point Intrinsics
10396
10397 (define_insn "fcosa_s"
10398   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
10399         (unspec:SF [(match_operand:SI 1 "fp_arith_reg_operand" "f")]
10400                    UNSPEC_FCOSA))]
10401   "TARGET_SHMEDIA"
10402   "fcosa.s      %1, %0"
10403   [(set_attr "type" "atrans_media")])
10404
10405 (define_insn "fsina_s"
10406   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
10407         (unspec:SF [(match_operand:SI 1 "fp_arith_reg_operand" "f")]
10408                    UNSPEC_FSINA))]
10409   "TARGET_SHMEDIA"
10410   "fsina.s      %1, %0"
10411   [(set_attr "type" "atrans_media")])
10412
10413 (define_insn "fipr"
10414   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
10415         (plus:SF (plus:SF (vec_select:SF (mult:V4SF (match_operand:V4SF 1
10416                                                     "fp_arith_reg_operand" "f")
10417                                                    (match_operand:V4SF 2
10418                                                     "fp_arith_reg_operand" "f"))
10419                                          (parallel [(const_int 0)]))
10420                           (vec_select:SF (mult:V4SF (match_dup 1) (match_dup 2))
10421                                          (parallel [(const_int 1)])))
10422                  (plus:SF (vec_select:SF (mult:V4SF (match_dup 1) (match_dup 2))
10423                                          (parallel [(const_int 2)]))
10424                           (vec_select:SF (mult:V4SF (match_dup 1) (match_dup 2))
10425                                          (parallel [(const_int 3)])))))]
10426   "TARGET_SHMEDIA"
10427   "fipr %1, %2, %0"
10428   [(set_attr "type" "fparith_media")])
10429
10430 (define_insn "fsrra_s"
10431   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
10432         (unspec:SF [(match_operand:SF 1 "fp_arith_reg_operand" "f")]
10433                    UNSPEC_FSRRA))]
10434   "TARGET_SHMEDIA"
10435   "fsrra.s      %1, %0"
10436   [(set_attr "type" "atrans_media")])
10437
10438 (define_insn "ftrv"
10439   [(set (match_operand:V4SF 0 "fp_arith_reg_operand" "=f")
10440         (plus:V4SF
10441          (plus:V4SF
10442           (mult:V4SF
10443            (vec_select:V4SF (match_operand:V16SF 1 "fp_arith_reg_operand" "f")
10444                             (parallel [(const_int 0) (const_int 5)
10445                                        (const_int 10) (const_int 15)]))
10446            (match_operand:V4SF 2 "fp_arith_reg_operand" "f"))
10447           (mult:V4SF
10448            (vec_select:V4SF (match_dup 1)
10449                             (parallel [(const_int 4) (const_int 9)
10450                                        (const_int 14) (const_int 3)]))
10451            (vec_select:V4SF (match_dup 2)
10452                             (parallel [(const_int 1) (const_int 2)
10453                                       (const_int 3) (const_int 0)]))))
10454          (plus:V4SF
10455           (mult:V4SF
10456            (vec_select:V4SF (match_dup 1)
10457                             (parallel [(const_int 8) (const_int 13)
10458                                        (const_int 2) (const_int 7)]))
10459            (vec_select:V4SF (match_dup 2)
10460                             (parallel [(const_int 2) (const_int 3)
10461                                        (const_int 0) (const_int 1)])))
10462           (mult:V4SF
10463            (vec_select:V4SF (match_dup 1)
10464                             (parallel [(const_int 12) (const_int 1)
10465                                        (const_int 6) (const_int 11)]))
10466            (vec_select:V4SF (match_dup 2)
10467                             (parallel [(const_int 3) (const_int 0)
10468                                        (const_int 1) (const_int 2)]))))))]
10469   "TARGET_SHMEDIA"
10470   "ftrv %1, %2, %0"
10471   [(set_attr "type" "fparith_media")])
10472
10473 (define_insn "nsb"
10474   [(set (match_operand:QI 0 "arith_reg_dest" "=r")
10475         (unspec:QI [(match_operand:DI 1 "arith_reg_operand" "r")]
10476                    UNSPEC_NSB))]
10477   "TARGET_SHMEDIA"
10478   "nsb  %1, %0"
10479   [(set_attr "type" "arith_media")])
10480
10481 (define_insn "nsbsi"
10482   [(set (match_operand:SI 0 "arith_reg_dest" "=r")
10483         (zero_extend:SI
10484          (unspec:QI [(match_operand:DI 1 "arith_reg_operand" "r")]
10485                     UNSPEC_NSB)))]
10486   "TARGET_SHMEDIA"
10487   "nsb  %1, %0"
10488   [(set_attr "type" "arith_media")])
10489
10490 (define_insn "nsbdi"
10491   [(set (match_operand:DI 0 "arith_reg_dest" "=r")
10492         (zero_extend:DI
10493          (unspec:QI [(match_operand:DI 1 "arith_reg_operand" "r")]
10494                     UNSPEC_NSB)))]
10495   "TARGET_SHMEDIA"
10496   "nsb  %1, %0"
10497   [(set_attr "type" "arith_media")])
10498
10499 (define_expand "ffsdi2"
10500   [(set (match_operand:DI 0 "arith_reg_dest" "")
10501         (ffs:DI (match_operand:DI 1 "arith_reg_operand" "")))]
10502   "TARGET_SHMEDIA"
10503   "
10504 {
10505   rtx scratch = gen_reg_rtx (DImode);
10506   rtx last;
10507
10508   emit_insn (gen_adddi3 (scratch, operands[1], GEN_INT (-1)));
10509   emit_insn (gen_xordi3 (scratch, operands[1], scratch));
10510   emit_insn (gen_lshrdi3_media (scratch, scratch, const1_rtx));
10511   emit_insn (gen_nsbdi (scratch, scratch));
10512   emit_insn (gen_adddi3 (scratch, scratch, GEN_INT (-64)));
10513   emit_insn (gen_movdicc_false (scratch, operands[1], const0_rtx, scratch));
10514   last = emit_insn (gen_subdi3 (operands[0], const0_rtx, scratch));
10515   REG_NOTES (last)
10516     = gen_rtx_EXPR_LIST (REG_EQUAL,
10517                          gen_rtx_FFS (DImode, operands[0]), REG_NOTES (last));
10518   DONE;
10519 }")
10520
10521 (define_expand "ffssi2"
10522   [(set (match_operand:SI 0 "arith_reg_dest" "")
10523         (ffs:SI (match_operand:SI 1 "arith_reg_operand" "")))]
10524   "TARGET_SHMEDIA"
10525   "
10526 {
10527   rtx scratch = gen_reg_rtx (SImode);
10528   rtx discratch = gen_reg_rtx (DImode);
10529   rtx last;
10530
10531   emit_insn (gen_adddi3z_media (discratch, operands[1],
10532                                 force_reg (SImode, GEN_INT (-1))));
10533   emit_insn (gen_andcdi3 (discratch, discratch,
10534                           simplify_gen_subreg (DImode, operands[1],
10535                                                SImode, 0)));
10536   emit_insn (gen_nsbsi (scratch, discratch));
10537   last = emit_insn (gen_subsi3 (operands[0],
10538                                 force_reg (SImode, GEN_INT (-64)), scratch));
10539   REG_NOTES (last)
10540     = gen_rtx_EXPR_LIST (REG_EQUAL,
10541                          gen_rtx_FFS (SImode, operands[0]), REG_NOTES (last));
10542   DONE;
10543 }")
10544
10545 (define_insn "byterev"
10546   [(set (match_operand:V8QI 0 "arith_reg_dest" "=r")
10547         (vec_select:V8QI (match_operand:V8QI 1 "arith_reg_operand" "r")
10548                          (parallel [(const_int 7) (const_int 6) (const_int 5)
10549                                     (const_int 4) (const_int 3) (const_int 2)
10550                                     (const_int 1) (const_int 0)])))]
10551   "TARGET_SHMEDIA"
10552   "byterev      %1, %0"
10553   [(set_attr "type" "arith_media")])
10554
10555 ;; The following description  models the
10556 ;; SH4 pipeline using the DFA based scheduler. 
10557 ;; The DFA based description is better way to model 
10558 ;; a superscalar pipeline as compared to function unit
10559 ;; reservation model.   
10560 ;; 1. The function unit based model is oriented to describe at most one 
10561 ;;    unit reservation by each insn. It is difficult to model unit reservations in multiple 
10562 ;;    pipeline units by same insn. This can be done using DFA based description.
10563 ;; 2. The execution performance of DFA based scheduler does not depend on processor complexity.
10564 ;; 3. Writing all unit reservations for an instruction class is more natural description 
10565 ;;    of the pipeline and makes interface of the hazard recognizer simpler than the 
10566 ;;    old function unit based model.
10567 ;; 4. The DFA model is richer and is a part of greater overall framework of RCSP.
10568
10569
10570 ;; Two automata are defined to reduce number of states
10571 ;; which a single large automaton will have.(Factoring)
10572
10573 (define_automaton "inst_pipeline,fpu_pipe")
10574
10575 ;; This unit is basically the decode unit of the processor.
10576 ;; Since SH4 is a dual issue machine,it is as if there are two 
10577 ;; units so that any insn can be processed by either one
10578 ;; of the decoding unit.
10579
10580 (define_cpu_unit "pipe_01,pipe_02" "inst_pipeline")
10581
10582
10583 ;; The fixed point arithmetic calculator(?? EX Unit).
10584
10585 (define_cpu_unit  "int" "inst_pipeline")
10586
10587 ;; f1_1 and f1_2 are floating point units.Actually there is
10588 ;; a f1 unit which can overlap with other f1 unit but
10589 ;; not another F1 unit.It is as though there were two
10590 ;; f1 units.
10591
10592 (define_cpu_unit "f1_1,f1_2" "fpu_pipe")
10593
10594 ;; The floating point units.
10595
10596 (define_cpu_unit "F1,F2,F3,FS" "fpu_pipe")
10597
10598 ;; This is basically the MA unit of SH4
10599 ;; used in LOAD/STORE pipeline.
10600
10601 (define_cpu_unit "memory" "inst_pipeline")
10602
10603 ;; The address calculator used for branch instructions.
10604 ;; This will be reserved with "issue" of branch instructions
10605 ;; and this is to make sure that  no two branch instructions 
10606 ;; can be issued in parallel. 
10607
10608 (define_cpu_unit "pcr_addrcalc" "inst_pipeline")
10609
10610 ;; ----------------------------------------------------
10611 ;; This reservation is to simplify the dual issue description.
10612
10613 (define_reservation  "issue"  "pipe_01|pipe_02")
10614
10615 ;; This is to express the locking of D stage.
10616
10617 (define_reservation  "d_lock" "pipe_01+pipe_02")
10618
10619 ;; This is to simplify description where F1,F2,FS
10620 ;; are used simultaneously.
10621
10622 (define_reservation "fpu" "F1+F2+FS")
10623
10624 ;; This is to highlight the fact that f1 
10625 ;; cannot overlap with F1.
10626
10627 (exclusion_set  "f1_1,f1_2" "F1")
10628
10629 ;; Although reg moves have a latency of zero 
10630 ;; we need to highlight that they use D stage
10631 ;; for one cycle.
10632
10633 (define_insn_reservation "reg_mov" 0
10634                (eq_attr "type" "move,fmove")
10635               "issue")
10636
10637 ;; Other MT  group intructions(1 step operations)
10638 ;; Group:       MT
10639 ;; Latency:     1
10640 ;; Issue Rate:  1
10641
10642 (define_insn_reservation "mt" 1
10643                       (eq_attr "insn_class" "mt_group")
10644                       "issue,nothing")
10645
10646 ;; Fixed Point Arithmetic Instructions(1 step operations)
10647 ;; Group:       EX
10648 ;; Latency:     1
10649 ;; Issue Rate:  1
10650
10651 (define_insn_reservation "simple_arith" 1 
10652             (eq_attr "insn_class" "ex_group")
10653             "issue,int")
10654
10655 ;; Load Store instructions. (MOV.[BWL]@(d,GBR)
10656 ;; Group:       LS
10657 ;; Latency:     2
10658 ;; Issue Rate:  1
10659
10660 (define_insn_reservation "load_store" 2
10661        (eq_attr "type" "load,load_si,pcload,pcload_si,store")
10662        "issue,memory*2")
10663
10664 ;; Branch (BF,BF/S,BT,BT/S,BRA)
10665 ;; Group:       BR
10666 ;; Latency:     2 (or 1) Actually Observed to be 5/7
10667 ;; Issue Rate:  1
10668 ;; The latency is 1 when displacement is 0.
10669 ;; This reservation can be further broken into 2
10670 ;;    1. branch_zero : One with latency 1 and in the TEST 
10671 ;;       part it also checks for 0 (ZERO) displacement 
10672 ;;    2. branch: Latency 2.
10673
10674 (define_insn_reservation "branch_zero"  5
10675              (and (eq_attr "type" "cbranch")
10676                   (eq_attr "length" "2"))
10677              "(issue+pcr_addrcalc),pcr_addrcalc,nothing")
10678
10679 (define_insn_reservation "branch"  7
10680              (eq_attr "type" "cbranch")
10681              "(issue+pcr_addrcalc),pcr_addrcalc,nothing")
10682
10683 ;; Branch Far (JMP,RTS,BRAF)
10684 ;; Group:       CO
10685 ;; Latency:     3
10686 ;; Issue Rate:  2
10687 ;;    Since issue stage (D stage) is blocked for 2nd cycle, 
10688 ;;    cpu_unit  int  is reserved since it might be required for far
10689 ;;    address calculation.
10690
10691 (define_insn_reservation "branch_far" 12
10692          (and (eq_attr "type" "jump,return")
10693               (eq_attr "length" "6"))
10694          "d_lock*2,int+pcr_addrcalc,pcr_addrcalc")
10695
10696 ;; RTE
10697 ;; Group:       CO
10698 ;; atency:      5
10699 ;; Issue Rate:  5
10700 ;; this instruction can be executed in any of the pipelines 
10701 ;; and blocks the pipeline for next 4 stages.
10702
10703 (define_insn_reservation "return_from_exp" 5
10704           (eq_attr "type" "rte")
10705          "(issue+pcr_addrcalc),d_lock*4,int+pcr_addrcalc,nothing")
10706
10707 ;; OCBP, OCBWB
10708 ;; Group:       CO
10709 ;; Latency:     5
10710 ;; Issue Rate:  1
10711
10712 (define_insn_reservation "ocbwb"  5
10713           (eq_attr "insn_class" "cwb") 
10714            "issue,(int+memory),memory*5")
10715                 
10716 ;; LDS to PR,JSR
10717 ;; Group:       CO
10718 ;; Latency:     3
10719 ;; Issue Rate:  2
10720 ;; The SX stage is blocked for last 2 cycles.
10721
10722 (define_insn_reservation "lds_to_pr" 3 
10723           (eq_attr "type" "prset,call,sfunc") 
10724           "(issue+pcr_addrcalc),(issue+int+pcr_addrcalc),(int+pcr_addrcalc)*2")
10725
10726 ;; LDS.L to PR 
10727 ;; Group:       CO
10728 ;; Latency:     3
10729 ;; Issue Rate:  2
10730 ;; The SX unit is blocked for last 2 cycles.
10731  
10732 (define_insn_reservation "ldsmem_to_pr"  3
10733       (eq_attr "type" "pload") 
10734      "(issue+pcr_addrcalc),(issue+int+pcr_addrcalc),(int+memory+pcr_addrcalc),(int+pcr_addrcalc)")
10735
10736 ;; STS from PR
10737 ;; Group:       CO
10738 ;; Latency:     2
10739 ;; Issue Rate:  2
10740 ;; The SX unit in second and third cycles.
10741
10742 (define_insn_reservation "sts_from_pr" 2
10743         (eq_attr "type" "prget")
10744        "(issue+pcr_addrcalc),(pipe_01+int+pcr_addrcalc),(int+pcr_addrcalc),nothing")
10745
10746 ;; STS.L from PR
10747 ;; Group:       CO
10748 ;; Latency:     2
10749 ;; Issue Rate:  2
10750
10751 (define_insn_reservation "prload_mem" 2 
10752           (eq_attr "type" "pstore")
10753            "(issue+pcr_addrcalc),(pipe_01+int+pcr_addrcalc),(int+memory+pcr_addrcalc),memory")
10754
10755 ;; LDS to FPSCR
10756 ;; Group:       CO
10757 ;; Latency:     4
10758 ;; Issue Rate:  1
10759 ;; F1 is blocked for last three cycles. 
10760
10761 (define_insn_reservation "fpscr_store" 4
10762         (eq_attr "insn_class" "lds_to_fpscr")
10763        "issue,int,F1*3")
10764
10765 ;; LDS.L to FPSCR
10766 ;; Group:       CO
10767 ;; Latency:     1 / 4
10768 ;; Latency to update Rn is 1 and latency to update FPSCR is 4
10769 ;; Issue Rate:  1
10770 ;; F1 is blocked for last three cycles.
10771
10772 (define_insn_reservation "fpscr_store_mem" 4
10773         (eq_attr "insn_class"  "ldsmem_to_fpscr") 
10774         "issue,(int+memory),(F1+memory),F1*2")
10775
10776 \f
10777 ;; Fixed point multiplication (DMULS.L DMULU.L MUL.L MULS.W,MULU.W)
10778 ;; Group:       CO
10779 ;; Latency:     4 / 4
10780 ;; Issue Rate:  1
10781
10782 (define_insn_reservation "multi" 4
10783         (eq_attr "type" "smpy,dmpy")
10784         "issue,(issue+int+f1_1),(int+f1_1),(f1_1|f1_2)*2,F2,FS")
10785
10786
10787 ;; Single precision floating point computation FCMP/EQ,
10788 ;; FCP/GT, FADD, FLOAT, FMAC, FMUL, FSUB, FTRC, FRVHG, FSCHG
10789 ;; Group:       FE
10790 ;; Latency:     4
10791 ;; Issue Rate:  1
10792
10793 (define_insn_reservation "fp_arith"  4
10794               (eq_attr "type" "fp")
10795               "issue,F1,F2,FS")
10796
10797 ;; Single Precision FDIV/SQRT
10798 ;; Group:       FE
10799 ;; Latency:     12/13
10800 ;; Issue Rate:  1
10801
10802 (define_insn_reservation "fp_div" 13
10803                (eq_attr "type" "fdiv")
10804                "issue,F1+F3,F1+F2+F3,F3*7,F1+F3,F2,FS")
10805
10806 ;; Double Precision floating point computation
10807 ;; (FCNVDS, FCNVSD, FLOAT, FTRC)
10808 ;; Group:       FE
10809 ;; Latency:     (3,4)/5
10810 ;; Issue Rate:  1
10811
10812 (define_insn_reservation "dp_float" 5
10813          (eq_attr "type" "dfp_conv")
10814          "issue,F1,F1+F2,F2+FS,FS")
10815
10816 ;; Double-precision floating-point (FADD ,FMUL,FSUB) 
10817 ;; Group:       FE
10818 ;; Latency:     (7,8)/9
10819 ;; Issue Rate:  1
10820
10821 (define_insn_reservation "fp_double_arith" 9
10822         (eq_attr "type" "dfp_arith")
10823         "issue,F1,F1+F2,fpu*4,F2+FS,FS")
10824
10825 ;; Double-precision FCMP (FCMP/EQ,FCMP/GT) 
10826 ;; Group:       FE
10827 ;; Latency:     3/5
10828 ;; Issue Rate:  2
10829
10830 (define_insn_reservation "fp_double_cmp" 5 
10831         (eq_attr "type" "dfp_cmp")
10832         "issue,(issue+F1),F1+F2,F2+FS,FS")
10833
10834 ;; Double precision FDIV/SQRT
10835 ;; Group:       FE
10836 ;; Latency:     (24,25)/26
10837 ;; Issue Rate:  1
10838
10839 (define_insn_reservation "dp_div" 26
10840         (eq_attr "type" "dfdiv")
10841         "issue,F1+F3,F1+F2+F3,F2+F3+FS,F3*16,F1+F3,F1+F2+F3,fpu+F3,F2+FS,FS")
10842