OSDN Git Service

handle interrupt_handler correctly
[pf3gnuchains/gcc-fork.git] / gcc / config / sh / sh.md
1 ;;- Machine description for the Hitachi SH.
2 ;;  Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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       16)
74   (PR_REG       17)
75   (T_REG        18)
76   (GBR_REG      19)
77   (MACH_REG     20)
78   (MACL_REG     21)
79   (FPUL_REG     22)
80   (RAP_REG      23)
81
82   (FPSCR_REG    48)
83
84   (PIC_REG      12)
85   (FP_REG       14)
86   (SP_REG       15)
87
88   (R0_REG       0)
89   (R1_REG       1)
90   (R2_REG       2)
91   (R3_REG       3)
92   (R4_REG       4)
93   (R5_REG       5)
94   (R6_REG       6)
95
96   (DR0_REG      24)
97   (DR2_REG      26)
98   (DR4_REG      28)
99
100   (XD0_REG      40)
101
102   ;; These are used with unspec.
103   (UNSPEC_MOVA          1)
104   (UNSPEC_CASESI        2)
105   (UNSPEC_BBR           4)
106   (UNSPEC_SFUNC         5)
107   (UNSPEC_PIC           6)
108   (UNSPEC_GOT           7)
109   (UNSPEC_GOTOFF        8)
110   (UNSPEC_PLT           9)
111   (UNSPEC_CALLER        10)
112   (UNSPEC_ICACHE        12)
113
114   ;; These are used with unspec_volatile.
115   (UNSPECV_BLOCKAGE     0)
116   (UNSPECV_ALIGN        1)
117   (UNSPECV_CONST2       2)
118   (UNSPECV_CONST4       4)
119   (UNSPECV_CONST8       6)
120   (UNSPECV_WINDOW_END   10)
121   (UNSPECV_CONST_END    11)
122 ])  
123
124 ;; -------------------------------------------------------------------------
125 ;; Attributes
126 ;; -------------------------------------------------------------------------
127
128 ;; Target CPU.
129
130 (define_attr "cpu"
131  "sh1,sh2,sh3,sh3e,sh4"
132   (const (symbol_ref "sh_cpu_attr")))
133
134 (define_attr "endian" "big,little"
135  (const (if_then_else (symbol_ref "TARGET_LITTLE_ENDIAN")
136                       (const_string "little") (const_string "big"))))
137
138 ;; Indicate if the default fpu mode is single precision.
139 (define_attr "fpu_single" "yes,no"
140   (const (if_then_else (symbol_ref "TARGET_FPU_SINGLE")
141                          (const_string "yes") (const_string "no"))))
142
143 (define_attr "fmovd" "yes,no"
144   (const (if_then_else (symbol_ref "TARGET_FMOVD")
145                        (const_string "yes") (const_string "no"))))
146 ;; issues/clock
147 (define_attr "issues" "1,2"
148   (const (if_then_else (symbol_ref "TARGET_SUPERSCALAR") (const_string "2") (const_string "1"))))
149
150 ;; cbranch      conditional branch instructions
151 ;; jump         unconditional jumps
152 ;; arith        ordinary arithmetic
153 ;; arith3       a compound insn that behaves similarly to a sequence of
154 ;;              three insns of type arith
155 ;; arith3b      like above, but might end with a redirected branch
156 ;; load         from memory
157 ;; load_si      Likewise, SImode variant for general register.
158 ;; store        to memory
159 ;; move         register to register
160 ;; fmove        register to register, floating point
161 ;; smpy         word precision integer multiply
162 ;; dmpy         longword or doublelongword precision integer multiply
163 ;; return       rts
164 ;; pload        load of pr reg, which can't be put into delay slot of rts
165 ;; prset        copy register to pr reg, ditto
166 ;; pstore       store of pr reg, which can't be put into delay slot of jsr
167 ;; prget        copy pr to register, ditto
168 ;; pcload       pc relative load of constant value
169 ;; pcload_si    Likewise, SImode variant for general register.
170 ;; rte          return from exception
171 ;; sfunc        special function call with known used registers
172 ;; call         function call
173 ;; fp           floating point
174 ;; fdiv         floating point divide (or square root)
175 ;; gp_fpul      move between general purpose register and fpul
176 ;; dfp_arith, dfp_cmp,dfp_conv
177 ;; dfdiv        double precision floating point divide (or square root)
178 ;; nil          no-op move, will be deleted.
179
180 (define_attr "type"
181  "cbranch,jump,jump_ind,arith,arith3,arith3b,dyn_shift,other,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,nil"
182   (const_string "other"))
183
184 ;; Indicate what precision must be selected in fpscr for this insn, if any.
185
186 (define_attr "fp_mode" "single,double,none" (const_string "none"))
187
188 ; If a conditional branch destination is within -252..258 bytes away
189 ; from the instruction it can be 2 bytes long.  Something in the
190 ; range -4090..4100 bytes can be 6 bytes long.  All other conditional
191 ; branches are initially assumed to be 16 bytes long.
192 ; In machine_dependent_reorg, we split all branches that are longer than
193 ; 2 bytes.
194
195 ;; The maximum range used for SImode constant pool entries is 1018.  A final
196 ;; instruction can add 8 bytes while only being 4 bytes in size, thus we
197 ;; can have a total of 1022 bytes in the pool.  Add 4 bytes for a branch
198 ;; instruction around the pool table, 2 bytes of alignment before the table,
199 ;; and 30 bytes of alignment after the table.  That gives a maximum total
200 ;; pool size of 1058 bytes.
201 ;; Worst case code/pool content size ratio is 1:2 (using asms).
202 ;; Thus, in the worst case, there is one instruction in front of a maximum
203 ;; sized pool, and then there are 1052 bytes of pool for every 508 bytes of
204 ;; code.  For the last n bytes of code, there are 2n + 36 bytes of pool.
205 ;; If we have a forward branch, the initial table will be put after the
206 ;; unconditional branch.
207 ;;
208 ;; ??? We could do much better by keeping track of the actual pcloads within
209 ;; the branch range and in the pcload range in front of the branch range.
210
211 ;; ??? This looks ugly because genattrtab won't allow if_then_else or cond
212 ;; inside an le.
213 (define_attr "short_cbranch_p" "no,yes"
214   (cond [(ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
215          (const_string "no")
216          (leu (plus (minus (match_dup 0) (pc)) (const_int 252)) (const_int 506))
217          (const_string "yes")
218          (ne (symbol_ref "NEXT_INSN (PREV_INSN (insn)) != insn") (const_int 0))
219          (const_string "no")
220          (leu (plus (minus (match_dup 0) (pc)) (const_int 252)) (const_int 508))
221          (const_string "yes")
222          ] (const_string "no")))
223
224 (define_attr "med_branch_p" "no,yes"
225   (cond [(leu (plus (minus (match_dup 0) (pc)) (const_int 990))
226               (const_int 1988))
227          (const_string "yes")
228          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
229          (const_string "no")
230          (leu (plus (minus (match_dup 0) (pc)) (const_int 4092))
231               (const_int 8186))
232          (const_string "yes")
233          ] (const_string "no")))
234
235 (define_attr "med_cbranch_p" "no,yes"
236   (cond [(leu (plus (minus (match_dup 0) (pc)) (const_int 988))
237               (const_int 1986))
238          (const_string "yes")
239          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
240          (const_string "no")
241          (leu (plus (minus (match_dup 0) (pc)) (const_int 4090))
242                (const_int 8184))
243          (const_string "yes")
244          ] (const_string "no")))
245
246 (define_attr "braf_branch_p" "no,yes"
247   (cond [(ne (symbol_ref "! TARGET_SH2") (const_int 0))
248          (const_string "no")
249          (leu (plus (minus (match_dup 0) (pc)) (const_int 10330))
250               (const_int 20660))
251          (const_string "yes")
252          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
253          (const_string "no")
254          (leu (plus (minus (match_dup 0) (pc)) (const_int 32764))
255               (const_int 65530))
256          (const_string "yes")
257          ] (const_string "no")))
258
259 (define_attr "braf_cbranch_p" "no,yes"
260   (cond [(ne (symbol_ref "! TARGET_SH2") (const_int 0))
261          (const_string "no")
262          (leu (plus (minus (match_dup 0) (pc)) (const_int 10328))
263               (const_int 20658))
264          (const_string "yes")
265          (ne (symbol_ref "mdep_reorg_phase <= SH_FIXUP_PCLOAD") (const_int 0))
266          (const_string "no")
267          (leu (plus (minus (match_dup 0) (pc)) (const_int 32762))
268               (const_int 65528))
269          (const_string "yes")
270          ] (const_string "no")))
271
272 ; An unconditional jump in the range -4092..4098 can be 2 bytes long.
273 ; For wider ranges, we need a combination of a code and a data part.
274 ; If we can get a scratch register for a long range jump, the code
275 ; part can be 4 bytes long; otherwise, it must be 8 bytes long.
276 ; If the jump is in the range -32764..32770, the data part can be 2 bytes
277 ; long; otherwise, it must be 6 bytes long.
278
279 ; All other instructions are two bytes long by default.
280
281 ;; ??? This should use something like *branch_p (minus (match_dup 0) (pc)),
282 ;; but getattrtab doesn't understand this.
283 (define_attr "length" ""
284   (cond [(eq_attr "type" "cbranch")
285          (cond [(eq_attr "short_cbranch_p" "yes")
286                 (const_int 2)
287                 (eq_attr "med_cbranch_p" "yes")
288                 (const_int 6)
289                 (eq_attr "braf_cbranch_p" "yes")
290                 (const_int 12)
291 ;; ??? using pc is not computed transitively.
292                 (ne (match_dup 0) (match_dup 0))
293                 (const_int 14)
294                 (ne (symbol_ref ("flag_pic")) (const_int 0))
295                 (const_int 24)
296                 ] (const_int 16))
297          (eq_attr "type" "jump")
298          (cond [(eq_attr "med_branch_p" "yes")
299                 (const_int 2)
300                 (and (eq (symbol_ref "GET_CODE (PREV_INSN (insn))")
301                          (symbol_ref "INSN"))
302                      (eq (symbol_ref "INSN_CODE (PREV_INSN (insn))")
303                          (symbol_ref "code_for_indirect_jump_scratch")))
304                 (if_then_else (eq_attr "braf_branch_p" "yes")
305                               (const_int 6)
306                               (const_int 10))
307                 (eq_attr "braf_branch_p" "yes")
308                 (const_int 10)
309 ;; ??? using pc is not computed transitively.
310                 (ne (match_dup 0) (match_dup 0))
311                 (const_int 12)
312                 (ne (symbol_ref ("flag_pic")) (const_int 0))
313                 (const_int 22)
314                 ] (const_int 14))
315          ] (const_int 2)))
316
317 ;; (define_function_unit {name} {num-units} {n-users} {test}
318 ;;                       {ready-delay} {issue-delay} [{conflict-list}])
319
320 ;; Load and store instructions save a cycle if they are aligned on a
321 ;; four byte boundary.  Using a function unit for stores encourages
322 ;; gcc to separate load and store instructions by one instruction,
323 ;; which makes it more likely that the linker will be able to word
324 ;; align them when relaxing.
325
326 ;; Loads have a latency of two.
327 ;; However, call insns can have a delay slot, so that we want one more
328 ;; insn to be scheduled between the load of the function address and the call.
329 ;; This is equivalent to a latency of three.
330 ;; We cannot use a conflict list for this, because we need to distinguish
331 ;; between the actual call address and the function arguments.
332 ;; ADJUST_COST can only properly handle reductions of the cost, so we
333 ;; use a latency of three here.
334 ;; We only do this for SImode loads of general registers, to make the work
335 ;; for ADJUST_COST easier.
336 (define_function_unit "memory" 1 0
337   (and (eq_attr "issues" "1")
338        (eq_attr "type" "load_si,pcload_si"))
339   3 2)
340 (define_function_unit "memory" 1 0
341   (and (eq_attr "issues" "1")
342        (eq_attr "type" "load,pcload,pload,store,pstore"))
343   2 2)
344
345 (define_function_unit "int"    1 0
346   (and (eq_attr "issues" "1") (eq_attr "type" "arith3,arith3b")) 3 3)
347
348 (define_function_unit "int"    1 0
349   (and (eq_attr "issues" "1") (eq_attr "type" "dyn_shift")) 2 2)
350
351 (define_function_unit "int"    1 0
352   (and (eq_attr "issues" "1") (eq_attr "type" "!arith3,arith3b,dyn_shift")) 1 1)
353
354 ;; ??? These are approximations.
355 (define_function_unit "mpy"    1 0
356   (and (eq_attr "issues" "1") (eq_attr "type" "smpy")) 2 2)
357 (define_function_unit "mpy"    1 0
358   (and (eq_attr "issues" "1") (eq_attr "type" "dmpy")) 3 3)
359
360 (define_function_unit "fp"     1 0
361   (and (eq_attr "issues" "1") (eq_attr "type" "fp,fmove")) 2 1)
362 (define_function_unit "fp"     1 0
363   (and (eq_attr "issues" "1") (eq_attr "type" "fdiv")) 13 12)
364
365
366 ;; SH4 scheduling
367 ;; The SH4 is a dual-issue implementation, thus we have to multiply all
368 ;; costs by at least two.
369 ;; There will be single increments of the modeled that don't correspond
370 ;; to the actual target ;; whenever two insns to be issued depend one a
371 ;; single resource, and the scheduler picks to be the first one.
372 ;; If we multiplied the costs just by two, just two of these single
373 ;; increments would amount to an actual cycle.  By picking a larger
374 ;; factor, we can ameliorate the effect; However, we then have to make sure
375 ;; that only two insns are modeled as issued per actual cycle.
376 ;; Moreover, we need a way to specify the latency of insns that don't
377 ;; use an actual function unit.
378 ;; We use an 'issue' function unit to do that, and a cost factor of 10.
379
380 (define_function_unit "issue" 2 0
381   (and (eq_attr "issues" "2") (eq_attr "type" "!nil,arith3"))
382   10 10)
383
384 (define_function_unit "issue" 2 0
385   (and (eq_attr "issues" "2") (eq_attr "type" "arith3"))
386   30 30)
387
388 ;; There is no point in providing exact scheduling information about branches,
389 ;; because they are at the starts / ends of basic blocks anyways.
390
391 ;; Some insns cannot be issued before/after another insn in the same cycle,
392 ;; irrespective of the type of the other insn.
393
394 ;; default is dual-issue, but can't be paired with an insn that
395 ;; uses multiple function units.
396 (define_function_unit "single_issue"     1 0
397   (and (eq_attr "issues" "2")
398        (eq_attr "type" "!smpy,dmpy,pload,pstore,dfp_cmp,gp_fpul,call,sfunc,arith3,arith3b"))
399   1 10
400   [(eq_attr "type" "smpy,dmpy,pload,pstore,dfp_cmp,gp_fpul")])
401
402 (define_function_unit "single_issue"     1 0
403   (and (eq_attr "issues" "2")
404        (eq_attr "type" "smpy,dmpy,pload,pstore,dfp_cmp,gp_fpul"))
405   10 10
406   [(const_int 1)])
407
408 ;; arith3 insns are always pairable at the start, but not inecessarily at
409 ;; the end; however, there doesn;t seem to be a way to express that.
410 (define_function_unit "single_issue"     1 0
411   (and (eq_attr "issues" "2")
412        (eq_attr "type" "arith3"))
413   30 20
414   [(const_int 1)])
415
416 ;; arith3b insn are pairable at the end and have latency that prevents pairing
417 ;; with the following branch, but we don't want this latency be respected;
418 ;; When the following branch is immediately adjacent, we can redirect the
419 ;; internal branch, which is likly to be a larger win.
420 (define_function_unit "single_issue"     1 0
421   (and (eq_attr "issues" "2")
422        (eq_attr "type" "arith3b"))
423   20 20
424   [(const_int 1)])
425
426 ;; calls introduce a longisch delay that is likely to flush the pipelines.
427 (define_function_unit "single_issue"     1 0
428   (and (eq_attr "issues" "2")
429        (eq_attr "type" "call,sfunc"))
430   160 160
431   [(eq_attr "type" "!call") (eq_attr "type" "call")])
432
433 ;; Load and store instructions have no alignment peculiarities for the SH4,
434 ;; but they use the load-store unit, which they share with the fmove type
435 ;; insns (fldi[01]; fmov frn,frm; flds; fsts; fabs; fneg) .
436 ;; Loads have a latency of two.
437 ;; However, call insns can only paired with a preceding insn, and have
438 ;; a delay slot, so that we want two more insns to be scheduled between the
439 ;; load of the function address and the call.  This is equivalent to a
440 ;; latency of three.
441 ;; We cannot use a conflict list for this, because we need to distinguish
442 ;; between the actual call address and the function arguments.
443 ;; ADJUST_COST can only properly handle reductions of the cost, so we
444 ;; use a latency of three here, which gets multiplied by 10 to yield 30.
445 ;; We only do this for SImode loads of general registers, to make the work
446 ;; for ADJUST_COST easier.
447
448 ;; When specifying different latencies for different insns using the
449 ;; the same function unit, genattrtab.c assumes a 'FIFO constraint'
450 ;; so that the blockage is at least READY-COST (E) + 1 - READY-COST (C)
451 ;; for an executing insn E and a candidate insn C.
452 ;; Therefore, we define three different function units for load_store:
453 ;; load_store, load and load_si.
454
455 (define_function_unit "load_si" 1 0
456   (and (eq_attr "issues" "2")
457        (eq_attr "type" "load_si,pcload_si")) 30 10)
458 (define_function_unit "load" 1 0
459   (and (eq_attr "issues" "2")
460        (eq_attr "type" "load,pcload,pload")) 20 10)
461 (define_function_unit "load_store" 1 0
462   (and (eq_attr "issues" "2")
463        (eq_attr "type" "load_si,pcload_si,load,pcload,pload,store,pstore,fmove"))
464   10 10)
465
466 (define_function_unit "int"    1 0
467   (and (eq_attr "issues" "2") (eq_attr "type" "arith,dyn_shift")) 10 10)
468
469 ;; Again, we have to pretend a lower latency for the "int" unit to avoid a
470 ;; spurious FIFO constraint; the multiply instructions use the "int"
471 ;; unit actually only for two cycles.
472 (define_function_unit "int"    1 0
473   (and (eq_attr "issues" "2") (eq_attr "type" "smpy,dmpy")) 20 20)
474
475 ;; We use a fictous "mpy" unit to express the actual latency.
476 (define_function_unit "mpy"    1 0
477   (and (eq_attr "issues" "2") (eq_attr "type" "smpy,dmpy")) 40 20)
478
479 ;; Again, we have to pretend a lower latency for the "int" unit to avoid a
480 ;; spurious FIFO constraint.
481 (define_function_unit "int"     1 0
482   (and (eq_attr "issues" "2") (eq_attr "type" "gp_fpul")) 10 10)
483
484 ;; We use a fictous "gp_fpul" unit to express the actual latency.
485 (define_function_unit "gp_fpul"     1 0
486   (and (eq_attr "issues" "2") (eq_attr "type" "gp_fpul")) 20 10)
487
488 ;; ??? multiply uses the floating point unit, but with a two cycle delay.
489 ;; Thus, a simple single-precision fp operation could finish if issued in
490 ;; the very next cycle, but stalls when issued two or three cycles later.
491 ;; Similarily, a divide / sqrt can work without stalls if issued in
492 ;; the very next cycle, while it would have to block if issued two or
493 ;; three cycles later.
494 ;; There is no way to model this with gcc's function units.  This problem is
495 ;; actually mentioned in md.texi.  Tackling this problem requires first that
496 ;; it is possible to speak about the target in an open discussion.
497 ;; 
498 ;; However, simple double-precision operations always conflict.
499
500 (define_function_unit "fp"    1 0
501   (and (eq_attr "issues" "2") (eq_attr "type" "smpy,dmpy")) 40 40
502   [(eq_attr "type" "dfp_cmp,dfp_conv,dfp_arith")])
503
504 ;; The "fp" unit is for pipeline stages F1 and F2.
505
506 (define_function_unit "fp"     1 0
507   (and (eq_attr "issues" "2") (eq_attr "type" "fp")) 30 10)
508
509 ;; Again, we have to pretend a lower latency for the "fp" unit to avoid a
510 ;; spurious FIFO constraint; the bulk of the fdiv type insns executes in
511 ;; the F3 stage.
512 (define_function_unit "fp"     1 0
513   (and (eq_attr "issues" "2") (eq_attr "type" "fdiv")) 30 10)
514
515 ;; The "fdiv" function unit models the aggregate effect of the F1, F2 and F3
516 ;; pipeline stages on the pipelining of fdiv/fsqrt insns.
517 ;; We also use it to give the actual latency here.
518 ;; fsqrt is actually one cycle faster than fdiv (and the value used here),
519 ;; but that will hardly matter in practice for scheduling.
520 (define_function_unit "fdiv"     1 0
521   (and (eq_attr "issues" "2") (eq_attr "type" "fdiv")) 120 100)
522
523 ;; There is again a late use of the "fp" unit by [d]fdiv type insns
524 ;; that we can't express.
525
526 (define_function_unit "fp"     1 0
527   (and (eq_attr "issues" "2") (eq_attr "type" "dfp_cmp,dfp_conv")) 40 20)
528
529 (define_function_unit "fp"     1 0
530   (and (eq_attr "issues" "2") (eq_attr "type" "dfp_arith")) 80 60)
531
532 (define_function_unit "fp"     1 0
533   (and (eq_attr "issues" "2") (eq_attr "type" "dfdiv")) 230 10)
534
535 (define_function_unit "fdiv"     1 0
536   (and (eq_attr "issues" "2") (eq_attr "type" "dfdiv")) 230 210)
537
538 ; Definitions for filling branch delay slots.
539
540 (define_attr "needs_delay_slot" "yes,no" (const_string "no"))
541
542 ;; ??? This should be (nil) instead of (const_int 0)
543 (define_attr "hit_stack" "yes,no"
544         (cond [(eq (symbol_ref "find_regno_note (insn, REG_INC, SP_REG)")
545                    (const_int 0))
546                (const_string "no")]
547               (const_string "yes")))
548
549 (define_attr "interrupt_function" "no,yes"
550   (const (symbol_ref "current_function_interrupt")))
551
552 (define_attr "in_delay_slot" "yes,no"
553   (cond [(eq_attr "type" "cbranch") (const_string "no")
554          (eq_attr "type" "pcload,pcload_si") (const_string "no")
555          (eq_attr "needs_delay_slot" "yes") (const_string "no")
556          (eq_attr "length" "2") (const_string "yes")
557          ] (const_string "no")))
558
559 (define_attr "is_sfunc" ""
560   (if_then_else (eq_attr "type" "sfunc") (const_int 1) (const_int 0)))
561
562 (define_delay
563   (eq_attr "needs_delay_slot" "yes")
564   [(eq_attr "in_delay_slot" "yes") (nil) (nil)])
565
566 ;; On the SH and SH2, the rte instruction reads the return pc from the stack,
567 ;; and thus we can't put a pop instruction in its delay slot.
568 ;; ??? On the SH3, the rte instruction does not use the stack, so a pop
569 ;; instruction can go in the delay slot.
570
571 ;; Since a normal return (rts) implicitly uses the PR register,
572 ;; we can't allow PR register loads in an rts delay slot.
573
574 (define_delay
575   (eq_attr "type" "return")
576   [(and (eq_attr "in_delay_slot" "yes")
577         (ior (and (eq_attr "interrupt_function" "no")
578                   (eq_attr "type" "!pload,prset"))
579              (and (eq_attr "interrupt_function" "yes")
580                   (ior
581                    (ne (symbol_ref "TARGET_SH3") (const_int 0))
582                    (eq_attr "hit_stack" "no"))))) (nil) (nil)])
583
584 ;; Since a call implicitly uses the PR register, we can't allow
585 ;; a PR register store in a jsr delay slot.
586
587 (define_delay
588   (ior (eq_attr "type" "call") (eq_attr "type" "sfunc"))
589   [(and (eq_attr "in_delay_slot" "yes")
590         (eq_attr "type" "!pstore,prget")) (nil) (nil)])
591
592 ;; Say that we have annulled true branches, since this gives smaller and
593 ;; faster code when branches are predicted as not taken.
594
595 (define_delay
596   (and (eq_attr "type" "cbranch")
597        (ne (symbol_ref "TARGET_SH2") (const_int 0)))
598   [(eq_attr "in_delay_slot" "yes") (eq_attr "in_delay_slot" "yes") (nil)])
599 \f
600 ;; -------------------------------------------------------------------------
601 ;; SImode signed integer comparisons
602 ;; -------------------------------------------------------------------------
603
604 (define_insn ""
605   [(set (reg:SI T_REG)
606         (eq:SI (and:SI (match_operand:SI 0 "arith_reg_operand" "z,r")
607                        (match_operand:SI 1 "arith_operand" "L,r"))
608                (const_int 0)))]
609   ""
610   "tst  %1,%0")
611
612 ;; ??? Perhaps should only accept reg/constant if the register is reg 0.
613 ;; That would still allow reload to create cmpi instructions, but would
614 ;; perhaps allow forcing the constant into a register when that is better.
615 ;; Probably should use r0 for mem/imm compares, but force constant into a
616 ;; register for pseudo/imm compares.
617
618 (define_insn "cmpeqsi_t"
619   [(set (reg:SI T_REG)
620         (eq:SI (match_operand:SI 0 "arith_reg_operand" "r,z,r")
621                (match_operand:SI 1 "arith_operand" "N,rI,r")))]
622   ""
623   "@
624         tst     %0,%0
625         cmp/eq  %1,%0
626         cmp/eq  %1,%0")
627
628 (define_insn "cmpgtsi_t"
629   [(set (reg:SI T_REG)
630         (gt:SI (match_operand:SI 0 "arith_reg_operand" "r,r")
631                (match_operand:SI 1 "arith_reg_or_0_operand" "r,N")))]
632   ""
633   "@
634         cmp/gt  %1,%0
635         cmp/pl  %0")
636
637 (define_insn "cmpgesi_t"
638   [(set (reg:SI T_REG)
639         (ge:SI (match_operand:SI 0 "arith_reg_operand" "r,r")
640                (match_operand:SI 1 "arith_reg_or_0_operand" "r,N")))]
641   ""
642   "@
643         cmp/ge  %1,%0
644         cmp/pz  %0")
645 \f
646 ;; -------------------------------------------------------------------------
647 ;; SImode unsigned integer comparisons
648 ;; -------------------------------------------------------------------------
649
650 (define_insn "cmpgeusi_t"
651   [(set (reg:SI T_REG)
652         (geu:SI (match_operand:SI 0 "arith_reg_operand" "r")
653                 (match_operand:SI 1 "arith_reg_operand" "r")))]
654   ""
655   "cmp/hs       %1,%0")
656
657 (define_insn "cmpgtusi_t"
658   [(set (reg:SI T_REG)
659         (gtu:SI (match_operand:SI 0 "arith_reg_operand" "r")
660                 (match_operand:SI 1 "arith_reg_operand" "r")))]
661   ""
662   "cmp/hi       %1,%0")
663
664 ;; We save the compare operands in the cmpxx patterns and use them when
665 ;; we generate the branch.
666
667 (define_expand "cmpsi"
668   [(set (reg:SI T_REG)
669         (compare (match_operand:SI 0 "arith_operand" "")
670                  (match_operand:SI 1 "arith_operand" "")))]
671   ""
672   "
673 {
674   sh_compare_op0 = operands[0];
675   sh_compare_op1 = operands[1];
676   DONE;
677 }")
678 \f
679 ;; -------------------------------------------------------------------------
680 ;; DImode signed integer comparisons
681 ;; -------------------------------------------------------------------------
682
683 ;; ??? Could get better scheduling by splitting the initial test from the
684 ;; rest of the insn after reload.  However, the gain would hardly justify
685 ;; the sh.md size increase necessary to do that.
686
687 (define_insn ""
688   [(set (reg:SI T_REG)
689         (eq:SI (and:DI (match_operand:DI 0 "arith_reg_operand" "r")
690                        (match_operand:DI 1 "arith_operand" "r"))
691                (const_int 0)))]
692   ""
693   "* return output_branchy_insn (EQ, \"tst\\t%S1,%S0\;bf\\t%l9\;tst\\t%R1,%R0\",
694                                  insn, operands);"
695   [(set_attr "length" "6")
696    (set_attr "type" "arith3b")])
697
698 (define_insn "cmpeqdi_t"
699   [(set (reg:SI T_REG)
700         (eq:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
701                (match_operand:DI 1 "arith_reg_or_0_operand" "N,r")))]
702   ""
703   "@
704         tst     %S0,%S0\;bf     %,Ldi%=\;tst    %R0,%R0\\n%,Ldi%=:
705         cmp/eq  %S1,%S0\;bf     %,Ldi%=\;cmp/eq %R1,%R0\\n%,Ldi%=:"
706   [(set_attr "length" "6")
707    (set_attr "type" "arith3b")])
708
709 (define_split
710   [(set (reg:SI T_REG)
711         (eq:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
712                (match_operand:DI 1 "arith_reg_or_0_operand" "N,r")))]
713 ;; If we applied this split when not optimizing, it would only be
714 ;; applied during the machine-dependent reorg, when no new basic blocks
715 ;; may be created.
716   "reload_completed && optimize"
717   [(set (reg:SI T_REG) (eq:SI (match_dup 2) (match_dup 3)))
718    (set (pc) (if_then_else (eq (reg:SI T_REG) (const_int 0))
719                            (label_ref (match_dup 6))
720                            (pc)))
721    (set (reg:SI T_REG) (eq:SI (match_dup 4) (match_dup 5)))
722    (match_dup 6)]
723   "
724 {
725   operands[2]
726     = gen_rtx_REG (SImode,
727                    true_regnum (operands[0]) + (TARGET_LITTLE_ENDIAN ? 1 : 0));
728   operands[3]
729     = (operands[1] == const0_rtx
730        ? const0_rtx
731        : gen_rtx_REG (SImode,
732                       true_regnum (operands[1])
733                       + (TARGET_LITTLE_ENDIAN ? 1 : 0)));
734   operands[4] = gen_lowpart (SImode, operands[0]);
735   operands[5] = gen_lowpart (SImode, operands[1]);
736   operands[6] = gen_label_rtx ();
737 }")
738
739 (define_insn "cmpgtdi_t"
740   [(set (reg:SI T_REG)
741         (gt:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
742                (match_operand:DI 1 "arith_reg_or_0_operand" "r,N")))]
743   "TARGET_SH2"
744   "@
745         cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/gt\\t%S1,%S0\;cmp/hi\\t%R1,%R0\\n%,Ldi%=:
746         tst\\t%S0,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/pl\\t%S0\;cmp/hi\\t%S0,%R0\\n%,Ldi%=:"
747   [(set_attr "length" "8")
748    (set_attr "type" "arith3")])
749
750 (define_insn "cmpgedi_t"
751   [(set (reg:SI T_REG)
752         (ge:SI (match_operand:DI 0 "arith_reg_operand" "r,r")
753                (match_operand:DI 1 "arith_reg_or_0_operand" "r,N")))]
754   "TARGET_SH2"
755   "@
756         cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/ge\\t%S1,%S0\;cmp/hs\\t%R1,%R0\\n%,Ldi%=:
757         cmp/pz\\t%S0"
758   [(set_attr "length" "8,2")
759    (set_attr "type" "arith3,arith")])
760 \f
761 ;; -------------------------------------------------------------------------
762 ;; DImode unsigned integer comparisons
763 ;; -------------------------------------------------------------------------
764
765 (define_insn "cmpgeudi_t"
766   [(set (reg:SI T_REG)
767         (geu:SI (match_operand:DI 0 "arith_reg_operand" "r")
768                 (match_operand:DI 1 "arith_reg_operand" "r")))]
769   "TARGET_SH2"
770   "cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/hs\\t%S1,%S0\;cmp/hs\\t%R1,%R0\\n%,Ldi%=:"
771   [(set_attr "length" "8")
772    (set_attr "type" "arith3")])
773
774 (define_insn "cmpgtudi_t"
775   [(set (reg:SI T_REG)
776         (gtu:SI (match_operand:DI 0 "arith_reg_operand" "r")
777                 (match_operand:DI 1 "arith_reg_operand" "r")))]
778   "TARGET_SH2"
779   "cmp/eq\\t%S1,%S0\;bf{.|/}s\\t%,Ldi%=\;cmp/hi\\t%S1,%S0\;cmp/hi\\t%R1,%R0\\n%,Ldi%=:"
780   [(set_attr "length" "8")
781    (set_attr "type" "arith3")])
782
783 ;; We save the compare operands in the cmpxx patterns and use them when
784 ;; we generate the branch.
785
786 (define_expand "cmpdi"
787   [(set (reg:SI T_REG)
788         (compare (match_operand:DI 0 "arith_operand" "")
789                  (match_operand:DI 1 "arith_operand" "")))]
790   "TARGET_SH2"
791   "
792 {
793   sh_compare_op0 = operands[0];
794   sh_compare_op1 = operands[1];
795   DONE;
796 }")
797 \f
798 ;; -------------------------------------------------------------------------
799 ;; Addition instructions
800 ;; -------------------------------------------------------------------------
801
802 ;; ??? This should be a define expand.
803
804 (define_insn "adddi3"
805   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
806         (plus:DI (match_operand:DI 1 "arith_reg_operand" "%0")
807                  (match_operand:DI 2 "arith_reg_operand" "r")))
808    (clobber (reg:SI T_REG))]
809   ""
810   "#"
811   [(set_attr "length" "6")])
812
813 (define_split
814   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
815         (plus:DI (match_operand:DI 1 "arith_reg_operand" "%0")
816                  (match_operand:DI 2 "arith_reg_operand" "r")))
817    (clobber (reg:SI T_REG))]
818   "reload_completed"
819   [(const_int 0)]
820   "
821 {
822   rtx high0, high2, low0 = gen_lowpart (SImode, operands[0]);
823   high0 = gen_rtx_REG (SImode,
824                        true_regnum (operands[0])
825                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
826   high2 = gen_rtx_REG (SImode,
827                        true_regnum (operands[2])
828                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
829   emit_insn (gen_clrt ());
830   emit_insn (gen_addc (low0, low0, gen_lowpart (SImode, operands[2])));
831   emit_insn (gen_addc1 (high0, high0, high2));
832   DONE;
833 }")
834
835 (define_insn "addc"
836   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
837         (plus:SI (plus:SI (match_operand:SI 1 "arith_reg_operand" "0")
838                           (match_operand:SI 2 "arith_reg_operand" "r"))
839                  (reg:SI T_REG)))
840    (set (reg:SI T_REG)
841         (ltu:SI (plus:SI (match_dup 1) (match_dup 2)) (match_dup 1)))]
842   ""
843   "addc %2,%0"
844   [(set_attr "type" "arith")])
845
846 (define_insn "addc1"
847   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
848         (plus:SI (plus:SI (match_operand:SI 1 "arith_reg_operand" "0")
849                           (match_operand:SI 2 "arith_reg_operand" "r"))
850                  (reg:SI T_REG)))
851    (clobber (reg:SI T_REG))]
852   ""
853   "addc %2,%0"
854   [(set_attr "type" "arith")])
855
856 (define_insn "addsi3"
857   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
858         (plus:SI (match_operand:SI 1 "arith_operand" "%0")
859                  (match_operand:SI 2 "arith_operand" "rI")))]
860   ""
861   "add  %2,%0"
862   [(set_attr "type" "arith")])
863 \f
864 ;; -------------------------------------------------------------------------
865 ;; Subtraction instructions
866 ;; -------------------------------------------------------------------------
867
868 ;; ??? This should be a define expand.
869
870 (define_insn "subdi3"
871   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
872         (minus:DI (match_operand:DI 1 "arith_reg_operand" "0")
873                  (match_operand:DI 2 "arith_reg_operand" "r")))
874    (clobber (reg:SI T_REG))]
875   ""
876   "#"
877   [(set_attr "length" "6")])
878
879 (define_split
880   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
881         (minus:DI (match_operand:DI 1 "arith_reg_operand" "0")
882                   (match_operand:DI 2 "arith_reg_operand" "r")))
883    (clobber (reg:SI T_REG))]
884   "reload_completed"
885   [(const_int 0)]
886   "
887 {
888   rtx high0, high2, low0 = gen_lowpart (SImode, operands[0]);
889   high0 = gen_rtx_REG (SImode,
890                        true_regnum (operands[0])
891                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
892   high2 = gen_rtx_REG (SImode,
893                        true_regnum (operands[2])
894                        + (TARGET_LITTLE_ENDIAN ? 1 : 0));
895   emit_insn (gen_clrt ());
896   emit_insn (gen_subc (low0, low0, gen_lowpart (SImode, operands[2])));
897   emit_insn (gen_subc1 (high0, high0, high2));
898   DONE;
899 }")
900
901 (define_insn "subc"
902   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
903         (minus:SI (minus:SI (match_operand:SI 1 "arith_reg_operand" "0")
904                             (match_operand:SI 2 "arith_reg_operand" "r"))
905                   (reg:SI T_REG)))
906    (set (reg:SI T_REG)
907         (gtu:SI (minus:SI (match_dup 1) (match_dup 2)) (match_dup 1)))]
908   ""
909   "subc %2,%0"
910   [(set_attr "type" "arith")])
911
912 (define_insn "subc1"
913   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
914         (minus:SI (minus:SI (match_operand:SI 1 "arith_reg_operand" "0")
915                             (match_operand:SI 2 "arith_reg_operand" "r"))
916                   (reg:SI T_REG)))
917    (clobber (reg:SI T_REG))]
918   ""
919   "subc %2,%0"
920   [(set_attr "type" "arith")])
921
922 (define_insn "*subsi3_internal"
923   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
924         (minus:SI (match_operand:SI 1 "arith_reg_operand" "0")
925                   (match_operand:SI 2 "arith_reg_operand" "r")))]
926   ""
927   "sub  %2,%0"
928   [(set_attr "type" "arith")])
929
930 ;; Convert `constant - reg' to `neg rX; add rX, #const' since this
931 ;; will sometimes save one instruction.  Otherwise we might get
932 ;; `mov #const, rY; sub rY,rX; mov rX, rY' if the source and dest regs
933 ;; are the same.
934
935 (define_expand "subsi3"
936   [(set (match_operand:SI 0 "arith_reg_operand" "")
937         (minus:SI (match_operand:SI 1 "arith_operand" "")
938                   (match_operand:SI 2 "arith_reg_operand" "")))]
939   ""
940   "
941 {
942   if (GET_CODE (operands[1]) == CONST_INT)
943     {
944       emit_insn (gen_negsi2 (operands[0], operands[2]));
945       emit_insn (gen_addsi3 (operands[0], operands[0], operands[1]));
946       DONE;
947     }
948 }")
949 \f
950 ;; -------------------------------------------------------------------------
951 ;; Division instructions
952 ;; -------------------------------------------------------------------------
953
954 ;; We take advantage of the library routines which don't clobber as many
955 ;; registers as a normal function call would.
956
957 ;; The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
958 ;; also has an effect on the register that holds the address of the sfunc.
959 ;; To make this work, we have an extra dummy insns that shows the use
960 ;; of this register for reorg.
961
962 (define_insn "use_sfunc_addr"
963   [(set (reg:SI PR_REG)
964         (unspec [(match_operand:SI 0 "register_operand" "r")] UNSPEC_SFUNC))]
965   ""
966   ""
967   [(set_attr "length" "0")])
968
969 ;; We must use a pseudo-reg forced to reg 0 in the SET_DEST rather than
970 ;; hard register 0.  If we used hard register 0, then the next instruction
971 ;; would be a move from hard register 0 to a pseudo-reg.  If the pseudo-reg
972 ;; gets allocated to a stack slot that needs its address reloaded, then
973 ;; there is nothing to prevent reload from using r0 to reload the address.
974 ;; This reload would clobber the value in r0 we are trying to store.
975 ;; If we let reload allocate r0, then this problem can never happen.
976
977 (define_insn "udivsi3_i1"
978   [(set (match_operand:SI 0 "register_operand" "=z")
979         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
980    (clobber (reg:SI T_REG))
981    (clobber (reg:SI PR_REG))
982    (clobber (reg:SI R4_REG))
983    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
984   "! TARGET_SH4"
985   "jsr  @%1%#"
986   [(set_attr "type" "sfunc")
987    (set_attr "needs_delay_slot" "yes")])
988
989 (define_insn "udivsi3_i4"
990   [(set (match_operand:SI 0 "register_operand" "=y")
991         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
992    (clobber (reg:SI T_REG))
993    (clobber (reg:SI PR_REG))
994    (clobber (reg:DF DR0_REG))
995    (clobber (reg:DF DR2_REG))
996    (clobber (reg:DF DR4_REG))
997    (clobber (reg:SI R0_REG))
998    (clobber (reg:SI R1_REG))
999    (clobber (reg:SI R4_REG))
1000    (clobber (reg:SI R5_REG))
1001    (use (reg:PSI FPSCR_REG))
1002    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1003   "TARGET_SH4 && ! TARGET_FPU_SINGLE"
1004   "jsr  @%1%#"
1005   [(set_attr "type" "sfunc")
1006    (set_attr "fp_mode" "double")
1007    (set_attr "needs_delay_slot" "yes")])
1008
1009 (define_insn "udivsi3_i4_single"
1010   [(set (match_operand:SI 0 "register_operand" "=y")
1011         (udiv:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1012    (clobber (reg:SI T_REG))
1013    (clobber (reg:SI PR_REG))
1014    (clobber (reg:DF DR0_REG))
1015    (clobber (reg:DF DR2_REG))
1016    (clobber (reg:DF DR4_REG))
1017    (clobber (reg:SI R0_REG))
1018    (clobber (reg:SI R1_REG))
1019    (clobber (reg:SI R4_REG))
1020    (clobber (reg:SI R5_REG))
1021    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1022   "TARGET_HARD_SH4 && TARGET_FPU_SINGLE"
1023   "jsr  @%1%#"
1024   [(set_attr "type" "sfunc")
1025    (set_attr "needs_delay_slot" "yes")])
1026
1027 (define_expand "udivsi3"
1028   [(set (match_dup 3) (symbol_ref:SI "__udivsi3"))
1029    (set (reg:SI R4_REG) (match_operand:SI 1 "general_operand" ""))
1030    (set (reg:SI R5_REG) (match_operand:SI 2 "general_operand" ""))
1031    (parallel [(set (match_operand:SI 0 "register_operand" "")
1032                    (udiv:SI (reg:SI R4_REG)
1033                             (reg:SI R5_REG)))
1034               (clobber (reg:SI T_REG))
1035               (clobber (reg:SI PR_REG))
1036               (clobber (reg:SI R4_REG))
1037               (use (match_dup 3))])]
1038   ""
1039   "
1040 {
1041   rtx first, last;
1042
1043   operands[3] = gen_reg_rtx(SImode);
1044   /* Emit the move of the address to a pseudo outside of the libcall.  */
1045   if (TARGET_HARD_SH4 && TARGET_SH3E)
1046     {
1047       emit_move_insn (operands[3],
1048                       gen_rtx_SYMBOL_REF (SImode, \"__udivsi3_i4\"));
1049       if (TARGET_FPU_SINGLE)
1050         last = gen_udivsi3_i4_single (operands[0], operands[3]);
1051       else
1052         last = gen_udivsi3_i4 (operands[0], operands[3]);
1053     }
1054   else
1055     {
1056       emit_move_insn (operands[3],
1057                       gen_rtx_SYMBOL_REF (SImode, \"__udivsi3\"));
1058       last = gen_udivsi3_i1 (operands[0], operands[3]);
1059     }
1060   first = emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1061   emit_move_insn (gen_rtx_REG (SImode, 5), operands[2]);
1062   last = emit_insn (last);
1063   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1064      invariant code motion can move it.  */
1065   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1066   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1067   DONE;
1068 }")
1069
1070 (define_insn "divsi3_i1"
1071   [(set (match_operand:SI 0 "register_operand" "=z")
1072         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1073    (clobber (reg:SI T_REG))
1074    (clobber (reg:SI PR_REG))
1075    (clobber (reg:SI R1_REG))
1076    (clobber (reg:SI R2_REG))
1077    (clobber (reg:SI R3_REG))
1078    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1079   "! TARGET_SH4"
1080   "jsr  @%1%#"
1081   [(set_attr "type" "sfunc")
1082    (set_attr "needs_delay_slot" "yes")])
1083
1084 (define_insn "divsi3_i4"
1085   [(set (match_operand:SI 0 "register_operand" "=y")
1086         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1087    (clobber (reg:SI PR_REG))
1088    (clobber (reg:DF DR0_REG))
1089    (clobber (reg:DF DR2_REG))
1090    (use (reg:PSI FPSCR_REG))
1091    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1092   "TARGET_SH4 && ! TARGET_FPU_SINGLE"
1093   "jsr  @%1%#"
1094   [(set_attr "type" "sfunc")
1095    (set_attr "fp_mode" "double")
1096    (set_attr "needs_delay_slot" "yes")])
1097
1098 (define_insn "divsi3_i4_single"
1099   [(set (match_operand:SI 0 "register_operand" "=y")
1100         (div:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1101    (clobber (reg:SI PR_REG))
1102    (clobber (reg:DF DR0_REG))
1103    (clobber (reg:DF DR2_REG))
1104    (clobber (reg:SI R2_REG))
1105    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1106   "TARGET_HARD_SH4 && TARGET_FPU_SINGLE"
1107   "jsr  @%1%#"
1108   [(set_attr "type" "sfunc")
1109    (set_attr "needs_delay_slot" "yes")])
1110
1111 (define_expand "divsi3"
1112   [(set (match_dup 3) (symbol_ref:SI "__sdivsi3"))
1113    (set (reg:SI R4_REG) (match_operand:SI 1 "general_operand" ""))
1114    (set (reg:SI R5_REG) (match_operand:SI 2 "general_operand" ""))
1115    (parallel [(set (match_operand:SI 0 "register_operand" "")
1116                    (div:SI (reg:SI R4_REG)
1117                            (reg:SI R5_REG)))
1118               (clobber (reg:SI T_REG))
1119               (clobber (reg:SI PR_REG))
1120               (clobber (reg:SI R1_REG))
1121               (clobber (reg:SI R2_REG))
1122               (clobber (reg:SI R3_REG))
1123               (use (match_dup 3))])]
1124   ""
1125   "
1126 {
1127   rtx first, last;
1128
1129   operands[3] = gen_reg_rtx(SImode);
1130   /* Emit the move of the address to a pseudo outside of the libcall.  */
1131   if (TARGET_HARD_SH4 && TARGET_SH3E)
1132     {
1133       emit_move_insn (operands[3],
1134                       gen_rtx_SYMBOL_REF (SImode, \"__sdivsi3_i4\"));
1135       if (TARGET_FPU_SINGLE)
1136         last = gen_divsi3_i4_single (operands[0], operands[3]);
1137       else
1138         last = gen_divsi3_i4 (operands[0], operands[3]);
1139     }
1140   else
1141     {
1142       emit_move_insn (operands[3], gen_rtx_SYMBOL_REF (SImode, \"__sdivsi3\"));
1143       last = gen_divsi3_i1 (operands[0], operands[3]);
1144     }
1145   first = emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1146   emit_move_insn (gen_rtx_REG (SImode, 5), operands[2]);
1147   last = emit_insn (last);
1148   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1149      invariant code motion can move it.  */
1150   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1151   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1152   DONE;
1153 }")
1154 \f
1155 ;; -------------------------------------------------------------------------
1156 ;; Multiplication instructions
1157 ;; -------------------------------------------------------------------------
1158
1159 (define_insn "umulhisi3_i"
1160   [(set (reg:SI MACL_REG)
1161         (mult:SI (zero_extend:SI
1162                   (match_operand:HI 0 "arith_reg_operand" "r"))
1163                  (zero_extend:SI
1164                   (match_operand:HI 1 "arith_reg_operand" "r"))))]
1165   ""
1166   "mulu.w       %1,%0"
1167   [(set_attr "type" "smpy")])
1168
1169 (define_insn "mulhisi3_i"
1170   [(set (reg:SI MACL_REG)
1171         (mult:SI (sign_extend:SI
1172                   (match_operand:HI 0 "arith_reg_operand" "r"))
1173                  (sign_extend:SI
1174                   (match_operand:HI 1 "arith_reg_operand" "r"))))]
1175   ""
1176   "muls.w       %1,%0"
1177   [(set_attr "type" "smpy")])
1178
1179 (define_expand "mulhisi3"
1180   [(set (reg:SI MACL_REG)
1181         (mult:SI (sign_extend:SI
1182                   (match_operand:HI 1 "arith_reg_operand" ""))
1183                  (sign_extend:SI
1184                   (match_operand:HI 2 "arith_reg_operand" ""))))
1185    (set (match_operand:SI 0 "arith_reg_operand" "")
1186         (reg:SI MACL_REG))]
1187   ""
1188   "
1189 {
1190   rtx first, last;
1191
1192   first = emit_insn (gen_mulhisi3_i (operands[1], operands[2]));
1193   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACL_REG));
1194   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1195      invariant code motion can move it.  */
1196   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1197   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1198   DONE;
1199 }")
1200
1201 (define_expand "umulhisi3"
1202   [(set (reg:SI MACL_REG)
1203         (mult:SI (zero_extend:SI
1204                   (match_operand:HI 1 "arith_reg_operand" ""))
1205                  (zero_extend:SI
1206                   (match_operand:HI 2 "arith_reg_operand" ""))))
1207    (set (match_operand:SI 0 "arith_reg_operand" "")
1208         (reg:SI MACL_REG))]
1209   ""
1210   "
1211 {
1212   rtx first, last;
1213
1214   first = emit_insn (gen_umulhisi3_i (operands[1], operands[2]));
1215   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACL_REG));
1216   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1217      invariant code motion can move it.  */
1218   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1219   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1220   DONE;
1221 }")
1222
1223 ;; mulsi3 on the SH2 can be done in one instruction, on the SH1 we generate
1224 ;; a call to a routine which clobbers known registers.
1225
1226 (define_insn ""
1227   [(set (match_operand:SI 1 "register_operand" "=z")
1228         (mult:SI (reg:SI R4_REG) (reg:SI R5_REG)))
1229    (clobber (reg:SI MACL_REG))
1230    (clobber (reg:SI T_REG))
1231    (clobber (reg:SI PR_REG))
1232    (clobber (reg:SI R3_REG))
1233    (clobber (reg:SI R2_REG))
1234    (clobber (reg:SI R1_REG))
1235    (use (match_operand:SI 0 "arith_reg_operand" "r"))]
1236   ""
1237   "jsr  @%0%#"
1238   [(set_attr "type" "sfunc")
1239    (set_attr "needs_delay_slot" "yes")])
1240
1241 (define_expand "mulsi3_call"
1242   [(set (reg:SI R4_REG) (match_operand:SI 1 "general_operand" ""))
1243    (set (reg:SI R5_REG) (match_operand:SI 2 "general_operand" ""))
1244    (parallel[(set (match_operand:SI 0 "register_operand" "")
1245                   (mult:SI (reg:SI R4_REG)
1246                            (reg:SI R5_REG)))
1247              (clobber (reg:SI MACL_REG))
1248              (clobber (reg:SI T_REG))
1249              (clobber (reg:SI PR_REG))
1250              (clobber (reg:SI R3_REG))
1251              (clobber (reg:SI R2_REG))
1252              (clobber (reg:SI R1_REG))
1253              (use (match_operand:SI 3 "register_operand" ""))])]
1254   ""
1255   "")
1256
1257 (define_insn "mul_l"
1258   [(set (reg:SI MACL_REG)
1259         (mult:SI (match_operand:SI 0 "arith_reg_operand" "r")
1260                  (match_operand:SI 1 "arith_reg_operand" "r")))]
1261   "TARGET_SH2"
1262   "mul.l        %1,%0"
1263   [(set_attr "type" "dmpy")])
1264
1265 (define_expand "mulsi3"
1266   [(set (reg:SI MACL_REG)
1267         (mult:SI  (match_operand:SI 1 "arith_reg_operand" "")
1268                   (match_operand:SI 2 "arith_reg_operand" "")))
1269    (set (match_operand:SI 0 "arith_reg_operand" "")
1270         (reg:SI MACL_REG))]
1271   ""
1272   "
1273 {
1274   rtx first, last;
1275
1276   if (!TARGET_SH2)
1277     {
1278       /* The address must be set outside the libcall,
1279          since it goes into a pseudo.  */
1280       rtx sym = gen_rtx_SYMBOL_REF (SImode, \"__mulsi3\");
1281       rtx addr = force_reg (SImode, sym);
1282       rtx insns = gen_mulsi3_call (operands[0], operands[1],
1283                                    operands[2], addr);
1284       first = XVECEXP (insns, 0, 0);
1285       last = XVECEXP (insns, 0, XVECLEN (insns, 0) - 1);
1286       emit_insn (insns);
1287     }
1288   else
1289     {
1290       rtx macl = gen_rtx_REG (SImode, MACL_REG);
1291
1292       first = emit_insn (gen_mul_l (operands[1], operands[2]));
1293       /* consec_sets_giv can only recognize the first insn that sets a
1294          giv as the giv insn.  So we must tag this also with a REG_EQUAL
1295          note.  */
1296       last = emit_insn (gen_movsi_i ((operands[0]), macl));
1297     }
1298   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1299      invariant code motion can move it.  */
1300   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1301   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1302   DONE;
1303 }")
1304
1305 (define_insn "mulsidi3_i"
1306   [(set (reg:SI MACH_REG)
1307         (truncate:SI
1308          (lshiftrt:DI
1309           (mult:DI
1310            (sign_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1311            (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1312           (const_int 32))))
1313    (set (reg:SI MACL_REG)
1314         (mult:SI (match_dup 0)
1315                  (match_dup 1)))]
1316   "TARGET_SH2"
1317   "dmuls.l      %1,%0"
1318   [(set_attr "type" "dmpy")])
1319
1320 (define_insn "mulsidi3"
1321   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1322         (mult:DI
1323          (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r"))
1324          (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" "r"))))
1325    (clobber (reg:SI MACH_REG))
1326    (clobber (reg:SI MACL_REG))]
1327   "TARGET_SH2"
1328   "#")
1329
1330 (define_split
1331   [(set (match_operand:DI 0 "arith_reg_operand" "")
1332         (mult:DI
1333          (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
1334          (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" ""))))
1335    (clobber (reg:SI MACH_REG))
1336    (clobber (reg:SI MACL_REG))]
1337   "TARGET_SH2"
1338   [(const_int 0)]
1339   "
1340 {
1341   rtx low_dst = gen_lowpart (SImode, operands[0]);
1342   rtx high_dst = gen_highpart (SImode, operands[0]);
1343
1344   emit_insn (gen_mulsidi3_i (operands[1], operands[2]));
1345
1346   emit_move_insn (low_dst, gen_rtx_REG (SImode, MACL_REG));
1347   emit_move_insn (high_dst, gen_rtx_REG (SImode, MACH_REG));
1348   /* We need something to tag the possible REG_EQUAL notes on to.  */
1349   emit_move_insn (operands[0], operands[0]);
1350   DONE;
1351 }")
1352
1353 (define_insn "umulsidi3_i"
1354   [(set (reg:SI MACH_REG)
1355         (truncate:SI
1356          (lshiftrt:DI
1357           (mult:DI
1358            (zero_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1359            (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1360           (const_int 32))))
1361    (set (reg:SI MACL_REG)
1362         (mult:SI (match_dup 0)
1363                  (match_dup 1)))]
1364   "TARGET_SH2"
1365   "dmulu.l      %1,%0"
1366   [(set_attr "type" "dmpy")])
1367
1368 (define_insn "umulsidi3"
1369   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1370         (mult:DI
1371          (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r"))
1372          (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" "r"))))
1373    (clobber (reg:SI MACH_REG))
1374    (clobber (reg:SI MACL_REG))]
1375   "TARGET_SH2"
1376   "#")
1377
1378 (define_split
1379   [(set (match_operand:DI 0 "arith_reg_operand" "")
1380         (mult:DI (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
1381                  (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" ""))))
1382    (clobber (reg:SI MACH_REG))
1383    (clobber (reg:SI MACL_REG))]
1384   "TARGET_SH2"
1385   [(const_int 0)]
1386   "
1387 {
1388   rtx low_dst = gen_lowpart (SImode, operands[0]);
1389   rtx high_dst = gen_highpart (SImode, operands[0]);
1390
1391   emit_insn (gen_umulsidi3_i (operands[1], operands[2]));
1392
1393   emit_move_insn (low_dst, gen_rtx_REG (SImode, MACL_REG));
1394   emit_move_insn (high_dst, gen_rtx_REG (SImode, MACH_REG));
1395   /* We need something to tag the possible REG_EQUAL notes on to.  */
1396   emit_move_insn (operands[0], operands[0]);
1397   DONE;
1398 }")
1399
1400 (define_insn "smulsi3_highpart_i"
1401   [(set (reg:SI MACH_REG)
1402         (truncate:SI
1403          (lshiftrt:DI
1404           (mult:DI
1405            (sign_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1406            (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1407           (const_int 32))))
1408    (clobber (reg:SI MACL_REG))]
1409   "TARGET_SH2"
1410   "dmuls.l      %1,%0"
1411   [(set_attr "type" "dmpy")])
1412
1413 (define_expand "smulsi3_highpart"
1414   [(parallel
1415     [(set (reg:SI MACH_REG)
1416           (truncate:SI
1417            (lshiftrt:DI
1418             (mult:DI
1419              (sign_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
1420              (sign_extend:DI (match_operand:SI 2 "arith_reg_operand" "")))
1421             (const_int 32))))
1422     (clobber (reg:SI MACL_REG))])
1423    (set (match_operand:SI 0 "arith_reg_operand" "")
1424         (reg:SI MACH_REG))]
1425   "TARGET_SH2"
1426   "
1427 {
1428   rtx first, last;
1429
1430   first = emit_insn (gen_smulsi3_highpart_i (operands[1], operands[2]));
1431   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACH_REG));
1432   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1433      invariant code motion can move it.  */
1434   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1435   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1436   /* expand_binop can't find a suitable code in mul_highpart_optab to
1437      make a REG_EQUAL note from, so make one here.
1438      ??? Alternatively, we could put this at the calling site of expand_binop,
1439      i.e. expand_mult_highpart.  */
1440   REG_NOTES (last)
1441     = gen_rtx_EXPR_LIST (REG_EQUAL, copy_rtx (SET_SRC (single_set (first))),
1442                          REG_NOTES (last));
1443   DONE;
1444 }")
1445
1446 (define_insn "umulsi3_highpart_i"
1447   [(set (reg:SI MACH_REG)
1448         (truncate:SI
1449          (lshiftrt:DI
1450           (mult:DI
1451            (zero_extend:DI (match_operand:SI 0 "arith_reg_operand" "r"))
1452            (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" "r")))
1453           (const_int 32))))
1454    (clobber (reg:SI MACL_REG))]
1455   "TARGET_SH2"
1456   "dmulu.l      %1,%0"
1457   [(set_attr "type" "dmpy")])
1458
1459 (define_expand "umulsi3_highpart"
1460   [(parallel
1461     [(set (reg:SI MACH_REG)
1462           (truncate:SI
1463            (lshiftrt:DI
1464             (mult:DI
1465              (zero_extend:DI (match_operand:SI 1 "arith_reg_operand" ""))
1466              (zero_extend:DI (match_operand:SI 2 "arith_reg_operand" "")))
1467             (const_int 32))))
1468     (clobber (reg:SI MACL_REG))])
1469    (set (match_operand:SI 0 "arith_reg_operand" "")
1470         (reg:SI MACH_REG))]
1471   "TARGET_SH2"
1472   "
1473 {
1474   rtx first, last;
1475
1476   first = emit_insn (gen_umulsi3_highpart_i (operands[1], operands[2]));
1477   last = emit_move_insn (operands[0], gen_rtx_REG (SImode, MACH_REG));
1478   /* Wrap the sequence in REG_LIBCALL / REG_RETVAL notes so that loop
1479      invariant code motion can move it.  */
1480   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last, REG_NOTES (first));
1481   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
1482   DONE;
1483 }")
1484 \f
1485 ;; -------------------------------------------------------------------------
1486 ;; Logical operations
1487 ;; -------------------------------------------------------------------------
1488
1489 (define_insn ""
1490   [(set (match_operand:SI 0 "arith_reg_operand" "=r,z")
1491         (and:SI (match_operand:SI 1 "arith_reg_operand" "%0,0")
1492                 (match_operand:SI 2 "logical_operand" "r,L")))]
1493   ""
1494   "and  %2,%0"
1495   [(set_attr "type" "arith")])
1496
1497 ;; If the constant is 255, then emit a extu.b instruction instead of an
1498 ;; and, since that will give better code.
1499
1500 (define_expand "andsi3"
1501   [(set (match_operand:SI 0 "arith_reg_operand" "")
1502         (and:SI (match_operand:SI 1 "arith_reg_operand" "")
1503                 (match_operand:SI 2 "logical_operand" "")))]
1504   ""
1505   "
1506 {
1507   if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) == 255)
1508     {
1509       emit_insn (gen_zero_extendqisi2 (operands[0],
1510                                        gen_lowpart (QImode, operands[1])));
1511       DONE;
1512     }
1513 }")
1514
1515 (define_insn "iorsi3"
1516   [(set (match_operand:SI 0 "arith_reg_operand" "=r,z")
1517         (ior:SI (match_operand:SI 1 "arith_reg_operand" "%0,0")
1518                 (match_operand:SI 2 "logical_operand" "r,L")))]
1519   ""
1520   "or   %2,%0"
1521   [(set_attr "type" "arith")])
1522
1523 (define_insn "xorsi3"
1524   [(set (match_operand:SI 0 "arith_reg_operand" "=z,r")
1525         (xor:SI (match_operand:SI 1 "arith_reg_operand" "%0,0")
1526                 (match_operand:SI 2 "logical_operand" "L,r")))]
1527   ""
1528   "xor  %2,%0"
1529   [(set_attr "type" "arith")])
1530 \f
1531 ;; -------------------------------------------------------------------------
1532 ;; Shifts and rotates
1533 ;; -------------------------------------------------------------------------
1534
1535 (define_insn "rotlsi3_1"
1536   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1537         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "0")
1538                    (const_int 1)))
1539    (set (reg:SI T_REG)
1540         (lshiftrt:SI (match_dup 1) (const_int 31)))]
1541   ""
1542   "rotl %0"
1543   [(set_attr "type" "arith")])
1544
1545 (define_insn "rotlsi3_31"
1546   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1547         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "0")
1548                    (const_int 31)))
1549    (clobber (reg:SI T_REG))]
1550   ""
1551   "rotr %0"
1552   [(set_attr "type" "arith")])
1553
1554 (define_insn "rotlsi3_16"
1555   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1556         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "r")
1557                    (const_int 16)))]
1558   ""
1559   "swap.w       %1,%0"
1560   [(set_attr "type" "arith")])
1561
1562 (define_expand "rotlsi3"
1563   [(set (match_operand:SI 0 "arith_reg_operand" "")
1564         (rotate:SI (match_operand:SI 1 "arith_reg_operand" "")
1565                    (match_operand:SI 2 "immediate_operand" "")))]
1566   ""
1567   "
1568 {
1569   static char rot_tab[] = {
1570     000, 000, 000, 000, 000, 000, 010, 001,
1571     001, 001, 011, 013, 003, 003, 003, 003,
1572     003, 003, 003, 003, 003, 013, 012, 002,
1573     002, 002, 010, 000, 000, 000, 000, 000,
1574   };
1575
1576   int count, choice;
1577
1578   if (GET_CODE (operands[2]) != CONST_INT)
1579     FAIL;
1580   count = INTVAL (operands[2]);
1581   choice = rot_tab[count];
1582   if (choice & 010 && SH_DYNAMIC_SHIFT_COST <= 1)
1583     FAIL;
1584   choice &= 7;
1585   switch (choice)
1586     {
1587     case 0:
1588       emit_move_insn (operands[0], operands[1]);
1589       count -= (count & 16) * 2;
1590       break;
1591     case 3:
1592      emit_insn (gen_rotlsi3_16 (operands[0], operands[1]));
1593      count -= 16;
1594      break;
1595     case 1:
1596     case 2:
1597       {
1598         rtx parts[2];
1599         parts[0] = gen_reg_rtx (SImode);
1600         parts[1] = gen_reg_rtx (SImode);
1601         emit_insn (gen_rotlsi3_16 (parts[2-choice], operands[1]));
1602         parts[choice-1] = operands[1];
1603         emit_insn (gen_ashlsi3 (parts[0], parts[0], GEN_INT (8)));
1604         emit_insn (gen_lshrsi3 (parts[1], parts[1], GEN_INT (8)));
1605         emit_insn (gen_iorsi3 (operands[0], parts[0], parts[1]));
1606         count = (count & ~16) - 8;
1607       }
1608     }
1609
1610   for (; count > 0; count--)
1611     emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
1612   for (; count < 0; count++)
1613     emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
1614
1615   DONE;
1616 }")
1617
1618 (define_insn "*rotlhi3_8"
1619   [(set (match_operand:HI 0 "arith_reg_operand" "=r")
1620         (rotate:HI (match_operand:HI 1 "arith_reg_operand" "r")
1621                    (const_int 8)))]
1622   ""
1623   "swap.b       %1,%0"
1624   [(set_attr "type" "arith")])
1625
1626 (define_expand "rotlhi3"
1627   [(set (match_operand:HI 0 "arith_reg_operand" "")
1628         (rotate:HI (match_operand:HI 1 "arith_reg_operand" "")
1629                    (match_operand:HI 2 "immediate_operand" "")))]
1630   ""
1631   "
1632 {
1633   if (GET_CODE (operands[2]) != CONST_INT || INTVAL (operands[2]) != 8)
1634     FAIL;
1635 }")
1636
1637 ;;
1638 ;; shift left
1639
1640 ;; This pattern is used by init_expmed for computing the costs of shift
1641 ;; insns.
1642
1643 (define_insn_and_split "ashlsi3_std"
1644   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r,r,r")
1645         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "0,0,0,0")
1646                    (match_operand:SI 2 "nonmemory_operand" "r,M,K,?ri")))
1647    (clobber (match_scratch:SI 3 "=X,X,X,&r"))]
1648   "TARGET_SH3
1649    || (GET_CODE (operands[2]) == CONST_INT
1650        && CONST_OK_FOR_K (INTVAL (operands[2])))"
1651   "@
1652    shld %2,%0
1653    add  %0,%0
1654    shll%O2      %0
1655    #"
1656   "TARGET_SH3
1657    && GET_CODE (operands[2]) == CONST_INT
1658    && ! CONST_OK_FOR_K (INTVAL (operands[2]))"
1659   [(set (match_dup 3) (match_dup 2))
1660    (parallel
1661     [(set (match_dup 0) (ashift:SI (match_dup 1) (match_dup 3)))
1662      (clobber (match_dup 4))])]
1663   "operands[4] = gen_rtx_SCRATCH (SImode);"
1664   [(set_attr "length" "*,*,*,4")
1665    (set_attr "type" "dyn_shift,arith,arith,arith")])
1666
1667 (define_insn "ashlhi3_k"
1668   [(set (match_operand:HI 0 "arith_reg_operand" "=r,r")
1669         (ashift:HI (match_operand:HI 1 "arith_reg_operand" "0,0")
1670                    (match_operand:HI 2 "const_int_operand" "M,K")))]
1671   "CONST_OK_FOR_K (INTVAL (operands[2]))"
1672   "@
1673         add     %0,%0
1674         shll%O2 %0"
1675   [(set_attr "type" "arith")])
1676
1677 (define_insn "ashlsi3_n"
1678   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1679         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "0")
1680                    (match_operand:SI 2 "const_int_operand" "n")))
1681    (clobber (reg:SI T_REG))]
1682   "! sh_dynamicalize_shift_p (operands[2])"
1683   "#"
1684   [(set (attr "length")
1685         (cond [(eq (symbol_ref "shift_insns_rtx (insn)") (const_int 1))
1686                (const_string "2")
1687                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 2))
1688                (const_string "4")
1689                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 3))
1690                (const_string "6")]
1691               (const_string "8")))
1692    (set_attr "type" "arith")])
1693
1694 (define_split
1695   [(set (match_operand:SI 0 "arith_reg_operand" "")
1696         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "")
1697                    (match_operand:SI 2 "const_int_operand" "n")))
1698    (clobber (reg:SI T_REG))]
1699   ""
1700   [(use (reg:SI R0_REG))]
1701   "
1702 {
1703   gen_shifty_op (ASHIFT, operands);
1704   DONE;
1705 }")
1706
1707 (define_expand "ashlsi3"
1708   [(parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
1709                    (ashift:SI (match_operand:SI 1 "arith_reg_operand" "")
1710                               (match_operand:SI 2 "nonmemory_operand" "")))
1711               (clobber (reg:SI T_REG))])]
1712   ""
1713   "
1714 {
1715   if (GET_CODE (operands[2]) == CONST_INT
1716       && sh_dynamicalize_shift_p (operands[2]))
1717     operands[2] = force_reg (SImode, operands[2]);
1718   if (TARGET_SH3)
1719     {
1720       emit_insn (gen_ashlsi3_std (operands[0], operands[1], operands[2]));
1721       DONE;
1722     }
1723   if (! immediate_operand (operands[2], GET_MODE (operands[2])))
1724     FAIL;
1725 }")
1726
1727 (define_insn "ashlhi3"
1728   [(set (match_operand:HI 0 "arith_reg_operand" "=r")
1729         (ashift:HI (match_operand:HI 1 "arith_reg_operand" "0")
1730                    (match_operand:HI 2 "const_int_operand" "n")))
1731    (clobber (reg:SI T_REG))]
1732   ""
1733   "#"
1734   [(set (attr "length")
1735         (cond [(eq (symbol_ref "shift_insns_rtx (insn)") (const_int 1))
1736                (const_string "2")
1737                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 2))
1738                (const_string "4")]
1739               (const_string "6")))
1740    (set_attr "type" "arith")])
1741
1742 (define_split
1743   [(set (match_operand:HI 0 "arith_reg_operand" "")
1744         (ashift:HI (match_operand:HI 1 "arith_reg_operand" "")
1745                    (match_operand:HI 2 "const_int_operand" "n")))
1746    (clobber (reg:SI T_REG))]
1747   ""
1748   [(use (reg:SI R0_REG))]
1749   "
1750 {
1751   gen_shifty_hi_op (ASHIFT, operands);
1752   DONE;
1753 }")
1754
1755 ;
1756 ; arithmetic shift right
1757 ;
1758
1759 (define_insn "ashrsi3_k"
1760   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1761         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1762                      (match_operand:SI 2 "const_int_operand" "M")))
1763    (clobber (reg:SI T_REG))]
1764   "INTVAL (operands[2]) == 1"
1765   "shar %0"
1766   [(set_attr "type" "arith")])
1767
1768 ;; We can't do HImode right shifts correctly unless we start out with an
1769 ;; explicit zero / sign extension; doing that would result in worse overall
1770 ;; code, so just let the machine independent code widen the mode.
1771 ;; That's why we don't have ashrhi3_k / lshrhi3_k / lshrhi3_m / lshrhi3 .
1772
1773
1774 ;; ??? This should be a define expand.
1775
1776 (define_insn "ashrsi2_16"
1777   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1778         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "r")
1779                      (const_int 16)))]
1780   ""
1781   "#"
1782   [(set_attr "length" "4")])
1783
1784 (define_split
1785   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1786         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "r")
1787                      (const_int 16)))]
1788   ""
1789   [(set (match_dup 0) (rotate:SI (match_dup 1) (const_int 16)))
1790    (set (match_dup 0) (sign_extend:SI (match_dup 2)))]
1791   "operands[2] = gen_lowpart (HImode, operands[0]);")
1792
1793 ;; ??? This should be a define expand.
1794
1795 (define_insn "ashrsi2_31"
1796   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1797         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1798                      (const_int 31)))
1799    (clobber (reg:SI T_REG))]
1800   ""
1801   "#"
1802   [(set_attr "length" "4")])
1803
1804 (define_split
1805   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1806         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1807                      (const_int 31)))
1808    (clobber (reg:SI T_REG))]
1809   ""
1810   [(const_int 0)]
1811   "
1812 {
1813   emit_insn (gen_ashlsi_c (operands[0], operands[1]));
1814   emit_insn (gen_subc1 (operands[0], operands[0], operands[0]));
1815   DONE;
1816 }")
1817
1818 (define_insn "ashlsi_c"
1819   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1820         (ashift:SI (match_operand:SI 1 "arith_reg_operand" "0") (const_int 1)))
1821    (set (reg:SI T_REG)
1822         (lt:SI (match_dup 1) (const_int 0)))]
1823   ""
1824   "shll %0"
1825   [(set_attr "type" "arith")])
1826
1827 (define_insn "ashrsi3_d"
1828   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1829         (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1830                      (neg:SI (match_operand:SI 2 "arith_reg_operand" "r"))))]
1831   "TARGET_SH3"
1832   "shad %2,%0"
1833   [(set_attr "type" "dyn_shift")])
1834
1835 (define_insn "ashrsi3_n"
1836   [(set (reg:SI R4_REG)
1837         (ashiftrt:SI (reg:SI R4_REG)
1838                      (match_operand:SI 0 "const_int_operand" "i")))
1839    (clobber (reg:SI T_REG))
1840    (clobber (reg:SI PR_REG))
1841    (use (match_operand:SI 1 "arith_reg_operand" "r"))]
1842   ""
1843   "jsr  @%1%#"
1844   [(set_attr "type" "sfunc")
1845    (set_attr "needs_delay_slot" "yes")])
1846
1847 (define_expand "ashrsi3"
1848   [(parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
1849                    (ashiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
1850                                 (match_operand:SI 2 "nonmemory_operand" "")))
1851               (clobber (reg:SI T_REG))])]
1852   ""
1853   "if (expand_ashiftrt (operands)) DONE; else FAIL;")
1854
1855 ;; logical shift right
1856
1857 (define_insn "lshrsi3_d"
1858   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1859         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1860                      (neg:SI (match_operand:SI 2 "arith_reg_operand" "r"))))]
1861   "TARGET_SH3"
1862   "shld %2,%0"
1863   [(set_attr "type" "dyn_shift")])
1864
1865 ;;  Only the single bit shift clobbers the T bit.
1866
1867 (define_insn "lshrsi3_m"
1868   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1869         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1870                      (match_operand:SI 2 "const_int_operand" "M")))
1871    (clobber (reg:SI T_REG))]
1872   "CONST_OK_FOR_M (INTVAL (operands[2]))"
1873   "shlr %0"
1874   [(set_attr "type" "arith")])
1875
1876 (define_insn "lshrsi3_k"
1877   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1878         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1879                      (match_operand:SI 2 "const_int_operand" "K")))]
1880   "CONST_OK_FOR_K (INTVAL (operands[2]))
1881    && ! CONST_OK_FOR_M (INTVAL (operands[2]))"
1882   "shlr%O2      %0"
1883   [(set_attr "type" "arith")])
1884
1885 (define_insn "lshrsi3_n"
1886   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
1887         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
1888                      (match_operand:SI 2 "const_int_operand" "n")))
1889    (clobber (reg:SI T_REG))]
1890   "! sh_dynamicalize_shift_p (operands[2])"
1891   "#"
1892   [(set (attr "length")
1893         (cond [(eq (symbol_ref "shift_insns_rtx (insn)") (const_int 1))
1894                (const_string "2")
1895                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 2))
1896                (const_string "4")
1897                (eq (symbol_ref "shift_insns_rtx (insn)") (const_int 3))
1898                (const_string "6")]
1899               (const_string "8")))
1900    (set_attr "type" "arith")])
1901
1902 (define_split
1903   [(set (match_operand:SI 0 "arith_reg_operand" "")
1904         (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
1905                      (match_operand:SI 2 "const_int_operand" "n")))
1906    (clobber (reg:SI T_REG))]
1907   ""
1908   [(use (reg:SI R0_REG))]
1909   "
1910 {
1911   gen_shifty_op (LSHIFTRT, operands);
1912   DONE;
1913 }")
1914
1915 (define_expand "lshrsi3"
1916   [(parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
1917                    (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "")
1918                                 (match_operand:SI 2 "nonmemory_operand" "")))
1919               (clobber (reg:SI T_REG))])]
1920   ""
1921   "
1922 {
1923   if (GET_CODE (operands[2]) == CONST_INT
1924       && sh_dynamicalize_shift_p (operands[2]))
1925     operands[2] = force_reg (SImode, operands[2]);
1926   if (TARGET_SH3 && arith_reg_operand (operands[2], GET_MODE (operands[2])))
1927     {
1928       rtx count = copy_to_mode_reg (SImode, operands[2]);
1929       emit_insn (gen_negsi2 (count, count));
1930       emit_insn (gen_lshrsi3_d (operands[0], operands[1], count));
1931       DONE;
1932     }
1933   if (! immediate_operand (operands[2], GET_MODE (operands[2])))
1934     FAIL;
1935 }")
1936
1937 ;; ??? This should be a define expand.
1938
1939 (define_insn "ashldi3_k"
1940   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1941         (ashift:DI (match_operand:DI 1 "arith_reg_operand" "0")
1942                    (const_int 1)))
1943    (clobber (reg:SI T_REG))]
1944   ""
1945   "shll %R0\;rotcl      %S0"
1946   [(set_attr "length" "4")
1947    (set_attr "type" "arith")])
1948
1949 (define_expand "ashldi3"
1950   [(parallel [(set (match_operand:DI 0 "arith_reg_operand" "")
1951                    (ashift:DI (match_operand:DI 1 "arith_reg_operand" "")
1952                               (match_operand:DI 2 "immediate_operand" "")))
1953               (clobber (reg:SI T_REG))])]
1954   ""
1955   "{ if (GET_CODE (operands[2]) != CONST_INT
1956          || INTVAL (operands[2]) != 1) FAIL;} ")
1957
1958 ;; ??? This should be a define expand.
1959
1960 (define_insn "lshrdi3_k"
1961   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1962         (lshiftrt:DI (match_operand:DI 1 "arith_reg_operand" "0")
1963                      (const_int 1)))
1964    (clobber (reg:SI T_REG))]
1965   ""
1966   "shlr %S0\;rotcr      %R0"
1967   [(set_attr "length" "4")
1968    (set_attr "type" "arith")])
1969
1970 (define_expand "lshrdi3"
1971   [(parallel [(set (match_operand:DI 0 "arith_reg_operand" "")
1972                    (lshiftrt:DI (match_operand:DI 1 "arith_reg_operand" "")
1973                                (match_operand:DI 2 "immediate_operand" "")))
1974              (clobber (reg:SI T_REG))])]
1975   ""
1976   "{ if (GET_CODE (operands[2]) != CONST_INT
1977          || INTVAL (operands[2]) != 1) FAIL;} ")
1978
1979 ;; ??? This should be a define expand.
1980
1981 (define_insn "ashrdi3_k"
1982   [(set (match_operand:DI 0 "arith_reg_operand" "=r")
1983         (ashiftrt:DI (match_operand:DI 1 "arith_reg_operand" "0")
1984                      (const_int 1)))
1985    (clobber (reg:SI T_REG))]
1986   ""
1987   "shar %S0\;rotcr      %R0"
1988   [(set_attr "length" "4")
1989    (set_attr "type" "arith")])
1990
1991 (define_expand "ashrdi3"
1992   [(parallel [(set (match_operand:DI 0 "arith_reg_operand" "")
1993                    (ashiftrt:DI (match_operand:DI 1 "arith_reg_operand" "")
1994                                 (match_operand:DI 2 "immediate_operand" "")))
1995               (clobber (reg:SI T_REG))])]
1996   ""
1997   "{ if (GET_CODE (operands[2]) != CONST_INT
1998          || INTVAL (operands[2]) != 1) FAIL; } ")
1999
2000 ;; combined left/right shift
2001
2002 (define_split
2003   [(set (match_operand:SI 0 "register_operand" "")
2004         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
2005                            (match_operand:SI 2 "const_int_operand" "n"))
2006                 (match_operand:SI 3 "const_int_operand" "n")))]
2007   "(unsigned)INTVAL (operands[2]) < 32"
2008   [(use (reg:SI R0_REG))]
2009   "if (gen_shl_and (operands[0], operands[2], operands[3], operands[1])) FAIL;
2010    DONE;")
2011
2012 (define_split
2013   [(set (match_operand:SI 0 "register_operand" "")
2014         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
2015                            (match_operand:SI 2 "const_int_operand" "n"))
2016                 (match_operand:SI 3 "const_int_operand" "n")))
2017    (clobber (reg:SI T_REG))]
2018   "(unsigned)INTVAL (operands[2]) < 32"
2019   [(use (reg:SI R0_REG))]
2020   "if (gen_shl_and (operands[0], operands[2], operands[3], operands[1])) FAIL;
2021    DONE;")
2022
2023 (define_insn ""
2024   [(set (match_operand:SI 0 "register_operand" "=r")
2025         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
2026                            (match_operand:SI 2 "const_int_operand" "n"))
2027                 (match_operand:SI 3 "const_int_operand" "n")))
2028    (clobber (reg:SI T_REG))]
2029   "shl_and_kind (operands[2], operands[3], 0) == 1"
2030  "#"
2031   [(set (attr "length")
2032         (cond [(eq (symbol_ref "shl_and_length (insn)") (const_int 2))
2033                (const_string "4")
2034                (eq (symbol_ref "shl_and_length (insn)") (const_int 3))
2035                (const_string "6")
2036                (eq (symbol_ref "shl_and_length (insn)") (const_int 4))
2037                (const_string "8")
2038                (eq (symbol_ref "shl_and_length (insn)") (const_int 5))
2039                (const_string "10")
2040                (eq (symbol_ref "shl_and_length (insn)") (const_int 6))
2041                (const_string "12")
2042                (eq (symbol_ref "shl_and_length (insn)") (const_int 7))
2043                (const_string "14")
2044                (eq (symbol_ref "shl_and_length (insn)") (const_int 8))
2045                (const_string "16")]
2046               (const_string "18")))
2047    (set_attr "type" "arith")])
2048
2049 (define_insn ""
2050   [(set (match_operand:SI 0 "register_operand" "=z")
2051         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
2052                            (match_operand:SI 2 "const_int_operand" "n"))
2053                 (match_operand:SI 3 "const_int_operand" "n")))
2054    (clobber (reg:SI T_REG))]
2055   "shl_and_kind (operands[2], operands[3], 0) == 2"
2056  "#"
2057   [(set (attr "length")
2058         (cond [(eq (symbol_ref "shl_and_length (insn)") (const_int 2))
2059                (const_string "4")
2060                (eq (symbol_ref "shl_and_length (insn)") (const_int 3))
2061                (const_string "6")
2062                (eq (symbol_ref "shl_and_length (insn)") (const_int 4))
2063                (const_string "8")]
2064               (const_string "10")))
2065    (set_attr "type" "arith")])
2066
2067 ;; shift left / and combination with a scratch register: The combine pass
2068 ;; does not accept the individual instructions, even though they are
2069 ;; cheap.  But it needs a precise description so that it is usable after
2070 ;; reload.
2071 (define_insn "and_shl_scratch"
2072   [(set (match_operand:SI 0 "register_operand" "=r,&r")
2073         (lshiftrt:SI
2074          (ashift:SI
2075           (and:SI
2076            (lshiftrt:SI (match_operand:SI 1 "register_operand" "r,0")
2077                         (match_operand:SI 2 "const_int_operand" "N,n"))
2078            (match_operand:SI 3 "" "0,r"))
2079           (match_operand:SI 4 "const_int_operand" "n,n"))
2080          (match_operand:SI 5 "const_int_operand" "n,n")))
2081    (clobber (reg:SI T_REG))]
2082   ""
2083   "#"
2084   [(set (attr "length")
2085         (cond [(eq (symbol_ref "shl_and_scr_length (insn)") (const_int 2))
2086                (const_string "4")
2087                (eq (symbol_ref "shl_and_scr_length (insn)") (const_int 3))
2088                (const_string "6")
2089                (eq (symbol_ref "shl_and_scr_length (insn)") (const_int 4))
2090                (const_string "8")
2091                (eq (symbol_ref "shl_and_scr_length (insn)") (const_int 5))
2092                (const_string "10")]
2093               (const_string "12")))
2094    (set_attr "type" "arith")])
2095
2096 (define_split
2097   [(set (match_operand:SI 0 "register_operand" "=r,&r")
2098         (lshiftrt:SI
2099          (ashift:SI
2100           (and:SI
2101            (lshiftrt:SI (match_operand:SI 1 "register_operand" "r,0")
2102                         (match_operand:SI 2 "const_int_operand" "N,n"))
2103            (match_operand:SI 3 "register_operand" "0,r"))
2104           (match_operand:SI 4 "const_int_operand" "n,n"))
2105          (match_operand:SI 5 "const_int_operand" "n,n")))
2106    (clobber (reg:SI T_REG))]
2107   ""
2108   [(use (reg:SI R0_REG))]
2109   "
2110 {
2111   rtx and_source = operands[rtx_equal_p (operands[0], operands[1]) ? 3 : 1];
2112
2113   if (INTVAL (operands[2]))
2114     {
2115       gen_shifty_op (LSHIFTRT, operands);
2116     }
2117   emit_insn (gen_andsi3 (operands[0], operands[0], and_source));
2118   operands[2] = operands[4];
2119   gen_shifty_op (ASHIFT, operands);
2120   if (INTVAL (operands[5]))
2121     {
2122       operands[2] = operands[5];
2123       gen_shifty_op (LSHIFTRT, operands);
2124     }
2125   DONE;
2126 }")
2127
2128 ;; signed left/right shift combination.
2129 (define_split
2130   [(set (match_operand:SI 0 "register_operand" "=r")
2131         (sign_extract:SI
2132          (ashift:SI (match_operand:SI 1 "register_operand" "r")
2133                     (match_operand:SI 2 "const_int_operand" "n"))
2134          (match_operand:SI 3 "const_int_operand" "n")
2135          (const_int 0)))
2136    (clobber (reg:SI T_REG))]
2137   ""
2138   [(use (reg:SI R0_REG))]
2139   "if (gen_shl_sext (operands[0], operands[2], operands[3], operands[1])) FAIL;
2140    DONE;")
2141
2142 (define_insn "shl_sext_ext"
2143   [(set (match_operand:SI 0 "register_operand" "=r")
2144         (sign_extract:SI
2145          (ashift:SI (match_operand:SI 1 "register_operand" "0")
2146                     (match_operand:SI 2 "const_int_operand" "n"))
2147          (match_operand:SI 3 "const_int_operand" "n")
2148          (const_int 0)))
2149    (clobber (reg:SI T_REG))]
2150   "(unsigned)shl_sext_kind (operands[2], operands[3], 0) - 1 < 5"
2151   "#"
2152   [(set (attr "length")
2153         (cond [(eq (symbol_ref "shl_sext_length (insn)") (const_int 1))
2154                (const_string "2")
2155                (eq (symbol_ref "shl_sext_length (insn)") (const_int 2))
2156                (const_string "4")
2157                (eq (symbol_ref "shl_sext_length (insn)") (const_int 3))
2158                (const_string "6")
2159                (eq (symbol_ref "shl_sext_length (insn)") (const_int 4))
2160                (const_string "8")
2161                (eq (symbol_ref "shl_sext_length (insn)") (const_int 5))
2162                (const_string "10")
2163                (eq (symbol_ref "shl_sext_length (insn)") (const_int 6))
2164                (const_string "12")
2165                (eq (symbol_ref "shl_sext_length (insn)") (const_int 7))
2166                (const_string "14")
2167                (eq (symbol_ref "shl_sext_length (insn)") (const_int 8))
2168                (const_string "16")]
2169               (const_string "18")))
2170     (set_attr "type" "arith")])
2171
2172 (define_insn "shl_sext_sub"
2173   [(set (match_operand:SI 0 "register_operand" "=z")
2174         (sign_extract:SI
2175          (ashift:SI (match_operand:SI 1 "register_operand" "0")
2176                     (match_operand:SI 2 "const_int_operand" "n"))
2177          (match_operand:SI 3 "const_int_operand" "n")
2178          (const_int 0)))
2179    (clobber (reg:SI T_REG))]
2180   "(shl_sext_kind (operands[2], operands[3], 0) & ~1) == 6"
2181   "#"
2182   [(set (attr "length")
2183         (cond [(eq (symbol_ref "shl_sext_length (insn)") (const_int 3))
2184                (const_string "6")
2185                (eq (symbol_ref "shl_sext_length (insn)") (const_int 4))
2186                (const_string "8")
2187                (eq (symbol_ref "shl_sext_length (insn)") (const_int 5))
2188                (const_string "10")
2189                (eq (symbol_ref "shl_sext_length (insn)") (const_int 6))
2190                (const_string "12")]
2191               (const_string "14")))
2192     (set_attr "type" "arith")])
2193
2194 ;; These patterns are found in expansions of DImode shifts by 16, and
2195 ;; allow the xtrct instruction to be generated from C source.
2196
2197 (define_insn "xtrct_left"
2198   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2199         (ior:SI (ashift:SI (match_operand:SI 1 "arith_reg_operand" "r")
2200                            (const_int 16))
2201                 (lshiftrt:SI (match_operand:SI 2 "arith_reg_operand" "0")
2202                              (const_int 16))))]
2203   ""
2204   "xtrct        %1,%0"
2205   [(set_attr "type" "arith")])
2206
2207 (define_insn "xtrct_right"
2208   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2209         (ior:SI (lshiftrt:SI (match_operand:SI 1 "arith_reg_operand" "0")
2210                              (const_int 16))
2211                 (ashift:SI (match_operand:SI 2 "arith_reg_operand" "r")
2212                            (const_int 16))))]
2213   ""
2214   "xtrct        %2,%0"
2215   [(set_attr "type" "arith")])
2216 \f
2217 ;; -------------------------------------------------------------------------
2218 ;; Unary arithmetic
2219 ;; -------------------------------------------------------------------------
2220
2221 (define_insn "negc"
2222   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2223         (neg:SI (plus:SI (reg:SI T_REG)
2224                          (match_operand:SI 1 "arith_reg_operand" "r"))))
2225    (set (reg:SI T_REG)
2226         (ne:SI (ior:SI (reg:SI T_REG) (match_dup 1))
2227                (const_int 0)))]
2228   ""
2229   "negc %1,%0"
2230   [(set_attr "type" "arith")])
2231
2232 (define_expand "negdi2"
2233   [(set (match_operand:DI 0 "arith_reg_operand" "")
2234         (neg:DI (match_operand:DI 1 "arith_reg_operand" "")))
2235    (clobber (reg:SI T_REG))]
2236   ""
2237   "
2238 {
2239   int low_word = (TARGET_LITTLE_ENDIAN ? 0 : 1);
2240   int high_word = (TARGET_LITTLE_ENDIAN ? 1 : 0);
2241
2242   rtx low_src = operand_subword (operands[1], low_word, 0, DImode);
2243   rtx high_src = operand_subword (operands[1], high_word, 0, DImode);
2244
2245   rtx low_dst = operand_subword (operands[0], low_word, 1, DImode);
2246   rtx high_dst = operand_subword (operands[0], high_word, 1, DImode);
2247
2248   emit_insn (gen_clrt ());
2249   emit_insn (gen_negc (low_dst, low_src));
2250   emit_insn (gen_negc (high_dst, high_src));
2251   DONE;
2252 }")
2253
2254 (define_insn "negsi2"
2255   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2256         (neg:SI (match_operand:SI 1 "arith_reg_operand" "r")))]
2257   ""
2258   "neg  %1,%0"
2259   [(set_attr "type" "arith")])
2260
2261 (define_insn "one_cmplsi2"
2262   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2263         (not:SI (match_operand:SI 1 "arith_reg_operand" "r")))]
2264   ""
2265   "not  %1,%0"
2266   [(set_attr "type" "arith")])
2267 \f
2268 ;; -------------------------------------------------------------------------
2269 ;; Zero extension instructions
2270 ;; -------------------------------------------------------------------------
2271
2272 (define_insn "zero_extendhisi2"
2273   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2274         (zero_extend:SI (match_operand:HI 1 "arith_reg_operand" "r")))]
2275   ""
2276   "extu.w       %1,%0"
2277   [(set_attr "type" "arith")])
2278
2279 (define_insn "zero_extendqisi2"
2280   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
2281         (zero_extend:SI (match_operand:QI 1 "arith_reg_operand" "r")))]
2282   ""
2283   "extu.b       %1,%0"
2284   [(set_attr "type" "arith")])
2285
2286 (define_insn "zero_extendqihi2"
2287   [(set (match_operand:HI 0 "arith_reg_operand" "=r")
2288         (zero_extend:HI (match_operand:QI 1 "arith_reg_operand" "r")))]
2289   ""
2290   "extu.b       %1,%0"
2291   [(set_attr "type" "arith")])
2292 \f
2293 ;; -------------------------------------------------------------------------
2294 ;; Sign extension instructions
2295 ;; -------------------------------------------------------------------------
2296
2297 ;; ??? This should be a define expand.
2298 ;; ??? Or perhaps it should be dropped?
2299
2300 /* There is no point in defining extendsidi2; convert_move generates good
2301    code for that.  */
2302
2303 (define_insn "extendhisi2"
2304   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
2305         (sign_extend:SI (match_operand:HI 1 "general_movsrc_operand" "r,m")))]
2306   ""
2307   "@
2308         exts.w  %1,%0
2309         mov.w   %1,%0"
2310   [(set_attr "type" "arith,load")])
2311
2312 (define_insn "extendqisi2"
2313   [(set (match_operand:SI 0 "arith_reg_operand" "=r,r")
2314         (sign_extend:SI (match_operand:QI 1 "general_movsrc_operand" "r,m")))]
2315   ""
2316   "@
2317         exts.b  %1,%0
2318         mov.b   %1,%0"
2319   [(set_attr "type" "arith,load")])
2320
2321 (define_insn "extendqihi2"
2322   [(set (match_operand:HI 0 "arith_reg_operand" "=r,r")
2323         (sign_extend:HI (match_operand:QI 1 "general_movsrc_operand" "r,m")))]
2324   ""
2325   "@
2326         exts.b  %1,%0
2327         mov.b   %1,%0"
2328   [(set_attr "type" "arith,load")])
2329 \f
2330 ;; -------------------------------------------------------------------------
2331 ;; Move instructions
2332 ;; -------------------------------------------------------------------------
2333
2334 ;; define push and pop so it is easy for sh.c
2335
2336 (define_expand "push"
2337   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
2338         (match_operand:SI 0 "register_operand" "r,l,x"))]
2339   ""
2340   "")
2341
2342 (define_expand "pop"
2343   [(set (match_operand:SI 0 "register_operand" "=r,l,x")
2344         (mem:SI (post_inc:SI (reg:SI SP_REG))))]
2345   ""
2346   "")
2347
2348 (define_expand "push_e"
2349   [(parallel [(set (mem:SF (pre_dec:SI (reg:SI SP_REG)))
2350                    (match_operand:SF 0 "" ""))
2351               (use (reg:PSI FPSCR_REG))
2352               (clobber (scratch:SI))])]
2353   ""
2354   "")
2355
2356 (define_insn "push_fpul"
2357   [(set (mem:SF (pre_dec:SI (reg:SI SP_REG))) (reg:SF FPUL_REG))]
2358   "TARGET_SH3E"
2359   "sts.l        fpul,@-r15"
2360   [(set_attr "type" "store")
2361    (set_attr "hit_stack" "yes")])
2362
2363 ;; DFmode pushes for sh4 require a lot of what is defined for movdf_i4,
2364 ;; so use that.
2365 (define_expand "push_4"
2366   [(parallel [(set (mem:DF (pre_dec:SI (reg:SI SP_REG)))
2367                    (match_operand:DF 0 "" ""))
2368               (use (reg:PSI FPSCR_REG))
2369               (clobber (scratch:SI))])]
2370   ""
2371   "")
2372
2373 (define_expand "pop_e"
2374   [(parallel [(set (match_operand:SF 0 "" "")
2375               (mem:SF (post_inc:SI (reg:SI SP_REG))))
2376               (use (reg:PSI FPSCR_REG))
2377               (clobber (scratch:SI))])]
2378   ""
2379   "")
2380
2381 (define_insn "pop_fpul"
2382   [(set (reg:SF FPUL_REG) (mem:SF (post_inc:SI (reg:SI SP_REG))))]
2383   "TARGET_SH3E"
2384   "lds.l        @r15+,fpul"
2385   [(set_attr "type" "load")
2386    (set_attr "hit_stack" "yes")])
2387
2388 (define_expand "pop_4"
2389   [(parallel [(set (match_operand:DF 0 "" "")
2390                    (mem:DF (post_inc:SI (reg:SI SP_REG))))
2391               (use (reg:PSI FPSCR_REG))
2392               (clobber (scratch:SI))])]
2393   ""
2394   "")
2395
2396 ;; These two patterns can happen as the result of optimization, when
2397 ;; comparisons get simplified to a move of zero or 1 into the T reg.
2398 ;; They don't disappear completely, because the T reg is a fixed hard reg.
2399
2400 (define_insn "clrt"
2401   [(set (reg:SI T_REG) (const_int 0))]
2402   ""
2403   "clrt")
2404
2405 (define_insn "sett"
2406   [(set (reg:SI T_REG) (const_int 1))]
2407   ""
2408   "sett")
2409
2410 ;; t/r must come after r/r, lest reload will try to reload stuff like
2411 ;; (set (subreg:SI (mem:QI (plus:SI (reg:SI SP_REG) (const_int 12)) 0) 0)
2412 ;; (made from (set (subreg:SI (reg:QI ###) 0) ) into T.
2413 (define_insn "movsi_i"
2414   [(set (match_operand:SI 0 "general_movdst_operand" "=r,r,t,r,r,r,r,m,<,<,x,l,x,l,r")
2415         (match_operand:SI 1 "general_movsrc_operand" "Q,rI,r,mr,x,l,t,r,x,l,r,r,>,>,i"))]
2416   "
2417    ! TARGET_SH3E
2418    && (register_operand (operands[0], SImode)
2419        || register_operand (operands[1], SImode))"
2420   "@
2421         mov.l   %1,%0
2422         mov     %1,%0
2423         cmp/pl  %1
2424         mov.l   %1,%0
2425         sts     %1,%0
2426         sts     %1,%0
2427         movt    %0
2428         mov.l   %1,%0
2429         sts.l   %1,%0
2430         sts.l   %1,%0
2431         lds     %1,%0
2432         lds     %1,%0
2433         lds.l   %1,%0
2434         lds.l   %1,%0
2435         fake    %1,%0"
2436   [(set_attr "type" "pcload_si,move,*,load_si,move,prget,move,store,store,pstore,move,prset,load,pload,pcload_si")
2437    (set_attr "length" "*,*,*,*,*,*,*,*,*,*,*,*,*,*,*")])
2438
2439 ;; t/r must come after r/r, lest reload will try to reload stuff like
2440 ;; (subreg:SI (reg:SF FR14_REG) 0) into T (compiling stdlib/strtod.c -m3e -O2)
2441 ;; ??? This allows moves from macl to fpul to be recognized, but these moves
2442 ;; will require a reload.
2443 (define_insn "movsi_ie"
2444   [(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")
2445         (match_operand:SI 1 "general_movsrc_operand" "Q,rI,r,mr,x,l,t,r,x,l,r,r,>,>,>,y,i,r,y,y"))]
2446   "TARGET_SH3E
2447    && (register_operand (operands[0], SImode)
2448        || register_operand (operands[1], SImode))"
2449   "@
2450         mov.l   %1,%0
2451         mov     %1,%0
2452         cmp/pl  %1
2453         mov.l   %1,%0
2454         sts     %1,%0
2455         sts     %1,%0
2456         movt    %0
2457         mov.l   %1,%0
2458         sts.l   %1,%0
2459         sts.l   %1,%0
2460         lds     %1,%0
2461         lds     %1,%0
2462         lds.l   %1,%0
2463         lds.l   %1,%0
2464         lds.l   %1,%0
2465         sts.l   %1,%0
2466         fake    %1,%0
2467         lds     %1,%0
2468         sts     %1,%0
2469         ! move optimized away"
2470   [(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")
2471    (set_attr "length" "*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,0")])
2472
2473 (define_insn "movsi_i_lowpart"
2474   [(set (strict_low_part (match_operand:SI 0 "general_movdst_operand" "+r,r,r,r,r,r,m,r"))
2475         (match_operand:SI 1 "general_movsrc_operand" "Q,rI,mr,x,l,t,r,i"))]
2476    "register_operand (operands[0], SImode)
2477     || register_operand (operands[1], SImode)"
2478   "@
2479         mov.l   %1,%0
2480         mov     %1,%0
2481         mov.l   %1,%0
2482         sts     %1,%0
2483         sts     %1,%0
2484         movt    %0
2485         mov.l   %1,%0
2486         fake    %1,%0"
2487   [(set_attr "type" "pcload,move,load,move,prget,move,store,pcload")])
2488
2489 (define_expand "movsi"
2490   [(set (match_operand:SI 0 "general_movdst_operand" "")
2491         (match_operand:SI 1 "general_movsrc_operand" ""))]
2492   ""
2493   "{ if (prepare_move_operands (operands, SImode)) DONE; }")
2494
2495 (define_expand "ic_invalidate_line"
2496   [(parallel [(unspec_volatile [(match_operand:SI 0 "register_operand" "+r")
2497                                 (match_dup 1)] UNSPEC_ICACHE)
2498               (clobber (scratch:SI))])]
2499   "TARGET_HARD_SH4"
2500   "
2501 {
2502   operands[0] = force_reg (Pmode, operands[0]);
2503   operands[1] = force_reg (Pmode, GEN_INT (0xf0000008));
2504 }")
2505
2506 ;; The address %0 is assumed to be 4-aligned at least.  Thus, by ORing
2507 ;; 0xf0000008, we get the low-oder bits *1*00 (binary), which fits
2508 ;; the requirement *1*00 for associative address writes.  The alignment of
2509 ;; %0 implies that its least significant bit is cleared,
2510 ;; thus we clear the V bit of a matching entry if there is one.
2511 (define_insn "ic_invalidate_line_i"
2512   [(unspec_volatile [(match_operand:SI 0 "register_operand" "r")
2513                      (match_operand:SI 1 "register_operand" "r")]
2514                      UNSPEC_ICACHE)
2515    (clobber (match_scratch:SI 2 "=&r"))]
2516   "TARGET_HARD_SH4"
2517   "ocbwb\\t@%0\;extu.w\\t%0,%2\;or\\t%1,%2\;mov.l\\t%0,@%2"
2518   [(set_attr "length" "8")])
2519
2520 (define_insn "movqi_i"
2521   [(set (match_operand:QI 0 "general_movdst_operand" "=r,r,m,r,r,l")
2522         (match_operand:QI 1 "general_movsrc_operand"  "ri,m,r,t,l,r"))]
2523   "arith_reg_operand (operands[0], QImode)
2524    || arith_reg_operand (operands[1], QImode)"
2525   "@
2526         mov     %1,%0
2527         mov.b   %1,%0
2528         mov.b   %1,%0
2529         movt    %0
2530         sts     %1,%0
2531         lds     %1,%0"
2532  [(set_attr "type" "move,load,store,move,move,move")])
2533
2534 (define_expand "movqi"
2535   [(set (match_operand:QI 0 "general_operand" "")
2536         (match_operand:QI 1 "general_operand"  ""))]
2537   ""
2538   "{ if (prepare_move_operands (operands, QImode)) DONE; }")
2539
2540 (define_insn "movhi_i"
2541   [(set (match_operand:HI 0 "general_movdst_operand" "=r,r,r,r,m,r,l,r")
2542         (match_operand:HI 1 "general_movsrc_operand" "Q,rI,m,t,r,l,r,i"))]
2543   "arith_reg_operand (operands[0], HImode)
2544    || arith_reg_operand (operands[1], HImode)"
2545   "@
2546         mov.w   %1,%0
2547         mov     %1,%0
2548         mov.w   %1,%0
2549         movt    %0
2550         mov.w   %1,%0
2551         sts     %1,%0
2552         lds     %1,%0
2553         fake    %1,%0"
2554   [(set_attr "type" "pcload,move,load,move,store,move,move,pcload")])
2555
2556 (define_expand "movhi"
2557   [(set (match_operand:HI 0 "general_movdst_operand" "")
2558         (match_operand:HI 1 "general_movsrc_operand"  ""))]
2559   ""
2560   "{ if (prepare_move_operands (operands, HImode)) DONE; }")
2561
2562 ;; ??? This should be a define expand.
2563
2564 ;; x/r can be created by inlining/cse, e.g. for execute/961213-1.c
2565 ;; compiled with -m2 -ml -O3 -funroll-loops
2566 (define_insn ""
2567   [(set (match_operand:DI 0 "general_movdst_operand" "=r,r,r,m,r,r,r,*!x")
2568         (match_operand:DI 1 "general_movsrc_operand" "Q,r,m,r,I,i,x,r"))]
2569   "arith_reg_operand (operands[0], DImode)
2570    || arith_reg_operand (operands[1], DImode)"
2571   "* return output_movedouble (insn, operands, DImode);"
2572   [(set_attr "length" "4")
2573    (set_attr "type" "pcload,move,load,store,move,pcload,move,move")])
2574
2575 ;; If the output is a register and the input is memory or a register, we have
2576 ;; to be careful and see which word needs to be loaded first.  
2577
2578 (define_split
2579   [(set (match_operand:DI 0 "general_movdst_operand" "")
2580         (match_operand:DI 1 "general_movsrc_operand" ""))]
2581   "reload_completed"
2582   [(set (match_dup 2) (match_dup 3))
2583    (set (match_dup 4) (match_dup 5))]
2584   "
2585 {
2586   int regno;
2587
2588   if ((GET_CODE (operands[0]) == MEM
2589        && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
2590       || (GET_CODE (operands[1]) == MEM
2591           && GET_CODE (XEXP (operands[1], 0)) == POST_INC))
2592     FAIL;
2593
2594   if (GET_CODE (operands[0]) == REG)
2595     regno = REGNO (operands[0]);
2596   else if (GET_CODE (operands[0]) == SUBREG)
2597     regno = subreg_regno (operands[0]);
2598   else if (GET_CODE (operands[0]) == MEM)
2599     regno = -1;
2600
2601   if (regno == -1
2602       || ! refers_to_regno_p (regno, regno + 1, operands[1], 0))
2603     {
2604       operands[2] = operand_subword (operands[0], 0, 0, DImode);
2605       operands[3] = operand_subword (operands[1], 0, 0, DImode);
2606       operands[4] = operand_subword (operands[0], 1, 0, DImode);
2607       operands[5] = operand_subword (operands[1], 1, 0, DImode);
2608     }
2609   else
2610     {
2611       operands[2] = operand_subword (operands[0], 1, 0, DImode);
2612       operands[3] = operand_subword (operands[1], 1, 0, DImode);
2613       operands[4] = operand_subword (operands[0], 0, 0, DImode);
2614       operands[5] = operand_subword (operands[1], 0, 0, DImode);
2615     }
2616
2617   if (operands[2] == 0 || operands[3] == 0
2618       || operands[4] == 0 || operands[5] == 0)
2619     FAIL;
2620 }")
2621
2622 (define_expand "movdi"
2623   [(set (match_operand:DI 0 "general_movdst_operand" "")
2624         (match_operand:DI 1 "general_movsrc_operand" ""))]
2625   ""
2626   "{ if (prepare_move_operands (operands, DImode)) DONE; }")
2627
2628 ;; ??? This should be a define expand.
2629
2630 (define_insn "movdf_k"
2631   [(set (match_operand:DF 0 "general_movdst_operand" "=r,r,r,m")
2632         (match_operand:DF 1 "general_movsrc_operand" "r,FQ,m,r"))]
2633   "(! TARGET_SH4 || reload_completed
2634     /* ??? We provide some insn so that direct_{load,store}[DFmode] get set */
2635     || (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 3)
2636     || (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 3))
2637    && (arith_reg_operand (operands[0], DFmode)
2638        || arith_reg_operand (operands[1], DFmode))"
2639   "* return output_movedouble (insn, operands, DFmode);"
2640   [(set_attr "length" "4")
2641    (set_attr "type" "move,pcload,load,store")])
2642
2643 ;; All alternatives of movdf_i4 are split for ! TARGET_FMOVD.
2644 ;; However, the d/F/c/z alternative cannot be split directly; it is converted
2645 ;; with special code in machine_dependent_reorg into a load of the R0_REG and
2646 ;; the d/m/c/X alternative, which is split later into single-precision
2647 ;; instructions.  And when not optimizing, no splits are done before fixing
2648 ;; up pcloads, so we need usable length information for that.
2649 (define_insn "movdf_i4"
2650   [(set (match_operand:DF 0 "general_movdst_operand" "=d,r,d,d,m,r,r,m,!??r,!???d")
2651         (match_operand:DF 1 "general_movsrc_operand" "d,r,F,m,d,FQ,m,r,d,r"))
2652    (use (match_operand:PSI 2 "fpscr_operand" "c,c,c,c,c,c,c,c,c,c"))
2653    (clobber (match_scratch:SI 3 "=X,X,&z,X,X,X,X,X,X,X"))]
2654   "TARGET_SH4
2655    && (arith_reg_operand (operands[0], DFmode)
2656        || arith_reg_operand (operands[1], DFmode))"
2657   "@
2658         fmov    %1,%0
2659         #
2660         #
2661         fmov.d  %1,%0
2662         fmov.d  %1,%0
2663         #
2664         #
2665         #
2666         #
2667         #"
2668   [(set_attr_alternative "length"
2669      [(if_then_else (eq_attr "fmovd" "yes") (const_int 2) (const_int 4))
2670       (const_int 4)
2671       (if_then_else (eq_attr "fmovd" "yes") (const_int 4) (const_int 6))
2672       (if_then_else (eq_attr "fmovd" "yes") (const_int 2) (const_int 6))
2673       (if_then_else (eq_attr "fmovd" "yes") (const_int 2) (const_int 6))
2674       (const_int 4)
2675       (const_int 8) (const_int 8) ;; these need only 8 bytes for @(r0,rn)
2676       (const_int 8) (const_int 8)])
2677    (set_attr "type" "fmove,move,pcload,load,store,pcload,load,store,load,load")
2678    (set (attr "fp_mode") (if_then_else (eq_attr "fmovd" "yes")
2679                                            (const_string "double")
2680                                            (const_string "none")))])
2681
2682 ;; Moving DFmode between fp/general registers through memory
2683 ;; (the top of the stack) is faster than moving through fpul even for
2684 ;; little endian.  Because the type of an instruction is important for its
2685 ;; scheduling,  it is beneficial to split these operations, rather than
2686 ;; emitting them in one single chunk, even if this will expose a stack
2687 ;; use that will prevent scheduling of other stack accesses beyond this
2688 ;; instruction.
2689 (define_split
2690   [(set (match_operand:DF 0 "register_operand" "")
2691         (match_operand:DF 1 "register_operand" ""))
2692    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2693    (clobber (match_scratch:SI 3 "=X"))]
2694   "TARGET_SH4 && reload_completed
2695    && (true_regnum (operands[0]) < 16) != (true_regnum (operands[1]) < 16)"
2696   [(const_int 0)]
2697   "
2698 {
2699   rtx insn, tos;
2700
2701   tos = gen_rtx (MEM, DFmode, gen_rtx (PRE_DEC, Pmode, stack_pointer_rtx));
2702   insn = emit_insn (gen_movdf_i4 (tos, operands[1], operands[2]));
2703   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, stack_pointer_rtx, NULL_RTX);
2704   tos = gen_rtx (MEM, DFmode, gen_rtx (POST_INC, Pmode, stack_pointer_rtx));
2705   insn = emit_insn (gen_movdf_i4 (operands[0], tos, operands[2]));
2706   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, stack_pointer_rtx, NULL_RTX);
2707   DONE;
2708 }")
2709
2710 ;; local-alloc sometimes allocates scratch registers even when not required,
2711 ;; so we must be prepared to handle these.
2712
2713 ;; Remove the use and clobber from a movdf_i4 so that we can use movdf_k.
2714 (define_split
2715   [(set (match_operand:DF 0 "general_movdst_operand" "")
2716         (match_operand:DF 1 "general_movsrc_operand"  ""))
2717    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2718    (clobber (match_scratch:SI 3 "X"))]
2719   "TARGET_SH4
2720    && reload_completed
2721    && true_regnum (operands[0]) < 16
2722    && true_regnum (operands[1]) < 16"
2723   [(set (match_dup 0) (match_dup 1))]
2724   "
2725 {
2726   /* If this was a reg <-> mem operation with base + index reg addressing,
2727      we have to handle this in a special way.  */
2728   rtx mem = operands[0];
2729   int store_p = 1;
2730   if (! memory_operand (mem, DFmode))
2731     {
2732       mem = operands[1];
2733       store_p = 0;
2734     }
2735   if (GET_CODE (mem) == SUBREG && SUBREG_BYTE (mem) == 0)
2736     mem = SUBREG_REG (mem);
2737   if (GET_CODE (mem) == MEM)
2738     {
2739       rtx addr = XEXP (mem, 0);
2740       if (GET_CODE (addr) == PLUS
2741           && GET_CODE (XEXP (addr, 0)) == REG
2742           && GET_CODE (XEXP (addr, 1)) == REG)
2743         {
2744           int offset;
2745           rtx reg0 = gen_rtx (REG, Pmode, 0);
2746           rtx regop = operands[store_p], word0 ,word1;
2747
2748           if (GET_CODE (regop) == SUBREG)
2749             regop = alter_subreg (regop);
2750           if (REGNO (XEXP (addr, 0)) == REGNO (XEXP (addr, 1)))
2751             offset = 2;
2752           else
2753             offset = 4;
2754           mem = copy_rtx (mem);
2755           PUT_MODE (mem, SImode);
2756           word0 = alter_subreg (gen_rtx (SUBREG, SImode, regop, 0));
2757           word1 = alter_subreg (gen_rtx (SUBREG, SImode, regop, 4));
2758           if (store_p || ! refers_to_regno_p (REGNO (word0),
2759                                               REGNO (word0) + 1, addr, 0))
2760             {
2761               emit_insn (store_p
2762                          ? gen_movsi_ie (mem, word0)
2763                          : gen_movsi_ie (word0, mem));
2764               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (offset)));
2765               mem = copy_rtx (mem);
2766               emit_insn (store_p
2767                          ? gen_movsi_ie (mem, word1)
2768                          : gen_movsi_ie (word1, mem));
2769               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (-offset)));
2770             }
2771           else
2772             {
2773               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (offset)));
2774               emit_insn (gen_movsi_ie (word1, mem));
2775               emit_insn (gen_addsi3 (reg0, reg0, GEN_INT (-offset)));
2776               mem = copy_rtx (mem);
2777               emit_insn (gen_movsi_ie (word0, mem));
2778             }
2779           DONE;
2780         }
2781     }
2782 }")
2783
2784 ;; Split away the clobber of r0 after machine_dependent_reorg has fixed pcloads.
2785 (define_split
2786   [(set (match_operand:DF 0 "register_operand" "")
2787         (match_operand:DF 1 "memory_operand"  ""))
2788    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2789    (clobber (reg:SI R0_REG))]
2790   "TARGET_SH4 && reload_completed"
2791   [(parallel [(set (match_dup 0) (match_dup 1))
2792               (use (match_dup 2))
2793               (clobber (scratch:SI))])]
2794   "")
2795
2796 (define_expand "reload_indf"
2797   [(parallel [(set (match_operand:DF 0 "register_operand" "=f")
2798                    (match_operand:DF 1 "immediate_operand" "FQ"))
2799               (use (reg:PSI FPSCR_REG))
2800               (clobber (match_operand:SI 2 "register_operand" "=&z"))])]
2801   ""
2802   "")
2803
2804 (define_expand "reload_outdf"
2805   [(parallel [(set (match_operand:DF 0 "register_operand" "=r,f")
2806                    (match_operand:DF 1 "register_operand" "af,r"))
2807               (clobber (match_operand:SI 2 "register_operand" "=&y,y"))])]
2808   ""
2809   "")
2810
2811 ;; Simplify no-op moves.
2812 (define_split
2813   [(set (match_operand:SF 0 "register_operand" "")
2814         (match_operand:SF 1 "register_operand" ""))
2815    (use (match_operand:PSI 2 "fpscr_operand" ""))
2816    (clobber (match_scratch:SI 3 "X"))]
2817   "TARGET_SH3E && reload_completed
2818    && true_regnum (operands[0]) == true_regnum (operands[1])"
2819   [(set (match_dup 0) (match_dup 0))]
2820   "")
2821
2822 ;; fmovd substitute post-reload splits
2823 (define_split
2824   [(set (match_operand:DF 0 "register_operand" "")
2825         (match_operand:DF 1 "register_operand" ""))
2826    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2827    (clobber (match_scratch:SI 3 "X"))]
2828   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
2829    && FP_OR_XD_REGISTER_P (true_regnum (operands[0]))
2830    && FP_OR_XD_REGISTER_P (true_regnum (operands[1]))"
2831   [(const_int 0)]
2832   "
2833 {
2834   int dst = true_regnum (operands[0]), src = true_regnum (operands[1]);
2835   emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode, dst),
2836                            gen_rtx (REG, SFmode, src), operands[2]));
2837   emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode, dst + 1),
2838                            gen_rtx (REG, SFmode, src + 1), operands[2]));
2839   DONE;
2840 }")
2841
2842 (define_split
2843   [(set (match_operand:DF 0 "register_operand" "")
2844         (mem:DF (match_operand:SI 1 "register_operand" "")))
2845    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2846    (clobber (match_scratch:SI 3 "X"))]
2847   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
2848    && FP_OR_XD_REGISTER_P (true_regnum (operands[0]))
2849    && find_regno_note (insn, REG_DEAD, true_regnum (operands[1]))"
2850   [(const_int 0)]
2851   "
2852 {
2853   int regno = true_regnum (operands[0]);
2854   rtx insn;
2855   rtx mem2 = gen_rtx (MEM, SFmode, gen_rtx (POST_INC, Pmode, operands[1]));
2856
2857   insn = emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode,
2858                                            regno + !! TARGET_LITTLE_ENDIAN),
2859                                   mem2, operands[2]));
2860   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, operands[1], NULL_RTX);
2861   insn = emit_insn (gen_movsf_ie (gen_rtx (REG, SFmode,
2862                                            regno + ! TARGET_LITTLE_ENDIAN),
2863                                   gen_rtx (MEM, SFmode, operands[1]),
2864                                   operands[2]));
2865   DONE;
2866 }")
2867
2868 (define_split
2869   [(set (match_operand:DF 0 "register_operand" "")
2870         (match_operand:DF 1 "memory_operand" ""))
2871    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2872    (clobber (match_scratch:SI 3 "X"))]
2873   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
2874    && FP_OR_XD_REGISTER_P (true_regnum (operands[0]))"
2875   [(const_int 0)]
2876   "
2877 {
2878   int regno = true_regnum (operands[0]);
2879   rtx addr, insn, adjust = NULL_RTX;
2880   rtx mem2 = copy_rtx (operands[1]);
2881   rtx reg0 = gen_rtx_REG (SFmode, regno + !! TARGET_LITTLE_ENDIAN);
2882   rtx reg1 = gen_rtx_REG (SFmode, regno + ! TARGET_LITTLE_ENDIAN);
2883
2884   PUT_MODE (mem2, SFmode);
2885   operands[1] = copy_rtx (mem2);
2886   addr = XEXP (mem2, 0);
2887   if (GET_CODE (addr) != POST_INC)
2888     {
2889       /* If we have to modify the stack pointer, the value that we have
2890          read with post-increment might be modified by an interrupt,
2891          so write it back.  */
2892       if (REGNO (addr) == STACK_POINTER_REGNUM)
2893         adjust = gen_push_e (reg0);
2894       else
2895         adjust = gen_addsi3 (addr, addr, GEN_INT (-4));
2896       XEXP (mem2, 0) = addr = gen_rtx_POST_INC (SImode, addr);
2897     }
2898   addr = XEXP (addr, 0);
2899   insn = emit_insn (gen_movsf_ie (reg0, mem2, operands[2]));
2900   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, addr, NULL_RTX);
2901   insn = emit_insn (gen_movsf_ie (reg1, operands[1], operands[2]));
2902   if (adjust)
2903     emit_insn (adjust);
2904   else
2905     REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, addr, NULL_RTX);
2906   DONE;
2907 }")
2908
2909 (define_split
2910   [(set (match_operand:DF 0 "memory_operand" "")
2911         (match_operand:DF 1 "register_operand" ""))
2912    (use (match_operand:PSI 2 "fpscr_operand" "c"))
2913    (clobber (match_scratch:SI 3 "X"))]
2914   "TARGET_SH4 && ! TARGET_FMOVD && reload_completed
2915    && FP_OR_XD_REGISTER_P (true_regnum (operands[1]))"
2916   [(const_int 0)]
2917   "
2918 {
2919   int regno = true_regnum (operands[1]);
2920   rtx insn, addr, adjust = NULL_RTX;
2921
2922   operands[0] = copy_rtx (operands[0]);
2923   PUT_MODE (operands[0], SFmode);
2924   insn = emit_insn (gen_movsf_ie (operands[0],
2925                                   gen_rtx (REG, SFmode,
2926                                            regno + ! TARGET_LITTLE_ENDIAN),
2927                                   operands[2]));
2928   operands[0] = copy_rtx (operands[0]);
2929   addr = XEXP (operands[0], 0);
2930   if (GET_CODE (addr) != PRE_DEC)
2931     {
2932       adjust = gen_addsi3 (addr, addr, GEN_INT (4));
2933       emit_insn_before (adjust, insn);
2934       XEXP (operands[0], 0) = addr = gen_rtx (PRE_DEC, SImode, addr);
2935     }
2936   addr = XEXP (addr, 0);
2937   if (! adjust)
2938     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, addr, NULL_RTX);
2939   insn = emit_insn (gen_movsf_ie (operands[0],
2940                                   gen_rtx (REG, SFmode,
2941                                            regno + !! TARGET_LITTLE_ENDIAN),
2942                                   operands[2]));
2943   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, addr, NULL_RTX);
2944   DONE;
2945 }")
2946
2947 ;; If the output is a register and the input is memory or a register, we have
2948 ;; to be careful and see which word needs to be loaded first.  
2949
2950 (define_split
2951   [(set (match_operand:DF 0 "general_movdst_operand" "")
2952         (match_operand:DF 1 "general_movsrc_operand" ""))]
2953   "reload_completed"
2954   [(set (match_dup 2) (match_dup 3))
2955    (set (match_dup 4) (match_dup 5))]
2956   "
2957 {
2958   int regno;
2959
2960   if ((GET_CODE (operands[0]) == MEM
2961        && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
2962       || (GET_CODE (operands[1]) == MEM
2963           && GET_CODE (XEXP (operands[1], 0)) == POST_INC))
2964     FAIL;
2965
2966   if (GET_CODE (operands[0]) == REG)
2967     regno = REGNO (operands[0]);
2968   else if (GET_CODE (operands[0]) == SUBREG)
2969     regno = subreg_regno (operands[0]);
2970   else if (GET_CODE (operands[0]) == MEM)
2971     regno = -1;
2972
2973   if (regno == -1
2974       || ! refers_to_regno_p (regno, regno + 1, operands[1], 0))
2975     {
2976       operands[2] = operand_subword (operands[0], 0, 0, DFmode);
2977       operands[3] = operand_subword (operands[1], 0, 0, DFmode);
2978       operands[4] = operand_subword (operands[0], 1, 0, DFmode);
2979       operands[5] = operand_subword (operands[1], 1, 0, DFmode);
2980     }
2981   else
2982     {
2983       operands[2] = operand_subword (operands[0], 1, 0, DFmode);
2984       operands[3] = operand_subword (operands[1], 1, 0, DFmode);
2985       operands[4] = operand_subword (operands[0], 0, 0, DFmode);
2986       operands[5] = operand_subword (operands[1], 0, 0, DFmode);
2987     }
2988
2989   if (operands[2] == 0 || operands[3] == 0
2990       || operands[4] == 0 || operands[5] == 0)
2991     FAIL;
2992 }")
2993
2994 ;; If a base address generated by LEGITIMIZE_ADDRESS for SImode is
2995 ;; used only once, let combine add in the index again.
2996
2997 (define_split
2998   [(set (match_operand:SI 0 "register_operand" "")
2999         (match_operand:SI 1 "" ""))
3000    (clobber (match_operand 2 "register_operand" ""))]
3001   "! reload_in_progress && ! reload_completed"
3002   [(use (reg:SI R0_REG))]
3003   "
3004 {
3005   rtx addr, reg, const_int;
3006
3007   if (GET_CODE (operands[1]) != MEM)
3008     FAIL;
3009   addr = XEXP (operands[1], 0);
3010   if (GET_CODE (addr) != PLUS)
3011     FAIL;
3012   reg = XEXP (addr, 0);
3013   const_int = XEXP (addr, 1);
3014   if (! (BASE_REGISTER_RTX_P (reg) && INDEX_REGISTER_RTX_P (operands[2])
3015          && GET_CODE (const_int) == CONST_INT))
3016     FAIL;
3017   emit_move_insn (operands[2], const_int);
3018   emit_move_insn (operands[0],
3019                   change_address (operands[1], VOIDmode,
3020                                   gen_rtx_PLUS (SImode, reg, operands[2])));
3021   DONE;
3022 }")
3023
3024 (define_split
3025   [(set (match_operand:SI 1 "" "")
3026         (match_operand:SI 0 "register_operand" ""))
3027    (clobber (match_operand 2 "register_operand" ""))]
3028   "! reload_in_progress && ! reload_completed"
3029   [(use (reg:SI R0_REG))]
3030   "
3031 {
3032   rtx addr, reg, const_int;
3033
3034   if (GET_CODE (operands[1]) != MEM)
3035     FAIL;
3036   addr = XEXP (operands[1], 0);
3037   if (GET_CODE (addr) != PLUS)
3038     FAIL;
3039   reg = XEXP (addr, 0);
3040   const_int = XEXP (addr, 1);
3041   if (! (BASE_REGISTER_RTX_P (reg) && INDEX_REGISTER_RTX_P (operands[2])
3042          && GET_CODE (const_int) == CONST_INT))
3043     FAIL;
3044   emit_move_insn (operands[2], const_int);
3045   emit_move_insn (change_address (operands[1], VOIDmode,
3046                                   gen_rtx_PLUS (SImode, reg, operands[2])),
3047                   operands[0]);
3048   DONE;
3049 }")
3050
3051 (define_expand "movdf"
3052   [(set (match_operand:DF 0 "general_movdst_operand" "")
3053         (match_operand:DF 1 "general_movsrc_operand" ""))]
3054   ""
3055   "
3056 {
3057   if (prepare_move_operands (operands, DFmode)) DONE;
3058   if (TARGET_SH4)
3059     {
3060       emit_df_insn (gen_movdf_i4 (operands[0], operands[1], get_fpscr_rtx ()));
3061       DONE;
3062     }
3063 }")
3064
3065
3066 (define_insn "movsf_i"
3067   [(set (match_operand:SF 0 "general_movdst_operand" "=r,r,r,r,m,l,r")
3068         (match_operand:SF 1 "general_movsrc_operand"  "r,I,FQ,mr,r,r,l"))]
3069   "
3070    (! TARGET_SH3E
3071     /* ??? We provide some insn so that direct_{load,store}[SFmode] get set */
3072     || (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 3)
3073     || (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 3))
3074    && (arith_reg_operand (operands[0], SFmode)
3075        || arith_reg_operand (operands[1], SFmode))"
3076   "@
3077         mov     %1,%0
3078         mov     %1,%0
3079         mov.l   %1,%0
3080         mov.l   %1,%0
3081         mov.l   %1,%0
3082         lds     %1,%0
3083         sts     %1,%0"
3084   [(set_attr "type" "move,move,pcload,load,store,move,move")])
3085
3086 ;; We may not split the ry/yr/XX alternatives to movsi_ie, since
3087 ;; update_flow_info would not know where to put REG_EQUAL notes
3088 ;; when the destination changes mode.
3089 (define_insn "movsf_ie"
3090   [(set (match_operand:SF 0 "general_movdst_operand"
3091          "=f,r,f,f,fy,f,m,r,r,m,f,y,y,rf,r,y,<,y,y")
3092         (match_operand:SF 1 "general_movsrc_operand"
3093           "f,r,G,H,FQ,mf,f,FQ,mr,r,y,f,>,fr,y,r,y,>,y"))
3094    (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"))
3095    (clobber (match_scratch:SI 3 "=X,X,X,X,&z,X,X,X,X,X,X,X,X,y,X,X,X,X,X"))]
3096
3097   "TARGET_SH3E
3098    && (arith_reg_operand (operands[0], SFmode)
3099        || arith_reg_operand (operands[1], SFmode)
3100        || arith_reg_operand (operands[3], SImode)
3101        || (fpul_operand (operands[0], SFmode)
3102            && memory_operand (operands[1], SFmode)
3103            && GET_CODE (XEXP (operands[1], 0)) == POST_INC)
3104        || (fpul_operand (operands[1], SFmode)
3105            && memory_operand (operands[0], SFmode)
3106            && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC))"
3107   "@
3108         fmov    %1,%0
3109         mov     %1,%0
3110         fldi0   %0
3111         fldi1   %0
3112         #
3113         fmov.s  %1,%0
3114         fmov.s  %1,%0
3115         mov.l   %1,%0
3116         mov.l   %1,%0
3117         mov.l   %1,%0
3118         fsts    fpul,%0
3119         flds    %1,fpul
3120         lds.l   %1,%0
3121         #
3122         sts     %1,%0
3123         lds     %1,%0
3124         sts.l   %1,%0
3125         lds.l   %1,%0
3126         ! move optimized away"
3127   [(set_attr "type" "fmove,move,fmove,fmove,pcload,load,store,pcload,load,store,fmove,fmove,load,*,gp_fpul,gp_fpul,store,load,nil")
3128    (set_attr "length" "*,*,*,*,4,*,*,*,*,*,2,2,2,4,2,2,2,2,0")
3129    (set (attr "fp_mode") (if_then_else (eq_attr "fmovd" "yes")
3130                                            (const_string "single")
3131                                            (const_string "none")))])
3132
3133 (define_split
3134   [(set (match_operand:SF 0 "register_operand" "")
3135         (match_operand:SF 1 "register_operand" ""))
3136    (use (match_operand:PSI 2 "fpscr_operand" "c"))
3137    (clobber (reg:SI FPUL_REG))]
3138   ""
3139   [(parallel [(set (reg:SF FPUL_REG) (match_dup 1))
3140               (use (match_dup 2))
3141               (clobber (scratch:SI))])
3142    (parallel [(set (match_dup 0) (reg:SF FPUL_REG))
3143               (use (match_dup 2))
3144               (clobber (scratch:SI))])]
3145   "")
3146
3147 (define_expand "movsf"
3148   [(set (match_operand:SF 0 "general_movdst_operand" "")
3149         (match_operand:SF 1 "general_movsrc_operand" ""))]
3150   ""
3151   "
3152 {
3153   if (prepare_move_operands (operands, SFmode))
3154     DONE;
3155   if (TARGET_SH3E)
3156     {
3157       emit_sf_insn (gen_movsf_ie (operands[0], operands[1], get_fpscr_rtx ()));
3158       DONE;
3159     }
3160 }")
3161
3162 (define_insn "mov_nop"
3163   [(set (match_operand 0 "register_operand" "") (match_dup 0))]
3164   "TARGET_SH3E"
3165   ""
3166   [(set_attr "length" "0")
3167    (set_attr "type" "nil")])
3168
3169 (define_expand "reload_insf"
3170   [(parallel [(set (match_operand:SF 0 "register_operand" "=a")
3171                    (match_operand:SF 1 "immediate_operand" "FQ"))
3172               (use (reg:PSI FPSCR_REG))
3173               (clobber (match_operand:SI 2 "register_operand" "=&z"))])]
3174   ""
3175   "")
3176
3177 (define_expand "reload_insi"
3178   [(parallel [(set (match_operand:SF 0 "register_operand" "=y")
3179                    (match_operand:SF 1 "immediate_operand" "FQ"))
3180               (clobber (match_operand:SI 2 "register_operand" "=&z"))])]
3181   ""
3182   "")
3183
3184 (define_insn "*movsi_y"
3185   [(set (match_operand:SI 0 "register_operand" "=y,y")
3186         (match_operand:SI 1 "immediate_operand" "Qi,I"))
3187    (clobber (match_scratch:SI 2 "=&z,r"))]
3188   "TARGET_SH3E
3189    && (reload_in_progress || reload_completed)"
3190   "#"
3191   [(set_attr "length" "4")
3192    (set_attr "type" "pcload,move")])
3193
3194 (define_split
3195   [(set (match_operand:SI 0 "register_operand" "")
3196         (match_operand:SI 1 "immediate_operand" ""))
3197    (clobber (match_operand:SI 2 "register_operand" ""))]
3198   ""
3199   [(set (match_dup 2) (match_dup 1))
3200    (set (match_dup 0) (match_dup 2))]
3201   "")
3202
3203 (define_split
3204   [(set (match_operand:SI 0 "register_operand" "")
3205         (match_operand:SI 1 "memory_operand" ""))
3206    (clobber (reg:SI R0_REG))]
3207   ""
3208   [(set (match_dup 0) (match_dup 1))]
3209   "")
3210 \f
3211 ;; ------------------------------------------------------------------------
3212 ;; Define the real conditional branch instructions.
3213 ;; ------------------------------------------------------------------------
3214
3215 (define_insn "branch_true"
3216   [(set (pc) (if_then_else (ne (reg:SI T_REG) (const_int 0))
3217                            (label_ref (match_operand 0 "" ""))
3218                            (pc)))]
3219   ""
3220   "* return output_branch (1, insn, operands);"
3221   [(set_attr "type" "cbranch")])
3222
3223 (define_insn "branch_false"
3224   [(set (pc) (if_then_else (eq (reg:SI T_REG) (const_int 0))
3225                            (label_ref (match_operand 0 "" ""))
3226                            (pc)))]
3227   ""
3228   "* return output_branch (0, insn, operands);"
3229   [(set_attr "type" "cbranch")])
3230
3231 ;; Patterns to prevent reorg from re-combining a condbranch with a branch
3232 ;; which destination is too far away.
3233 ;; The const_int_operand is distinct for each branch target; it avoids
3234 ;; unwanted matches with redundant_insn.
3235 (define_insn "block_branch_redirect"
3236   [(set (pc) (unspec [(match_operand 0 "const_int_operand" "")] UNSPEC_BBR))]
3237   ""
3238   ""
3239   [(set_attr "length" "0")])
3240
3241 ;; This one has the additional purpose to record a possible scratch register
3242 ;; for the following branch.
3243 (define_insn "indirect_jump_scratch"
3244   [(set (match_operand 0 "register_operand" "=r")
3245         (unspec [(match_operand 1 "const_int_operand" "")] UNSPEC_BBR))]
3246   ""
3247   ""
3248   [(set_attr "length" "0")])
3249 \f
3250 ;; Conditional branch insns
3251
3252 (define_expand "beq"
3253   [(set (pc)
3254         (if_then_else (ne (reg:SI T_REG) (const_int 0))
3255                       (label_ref (match_operand 0 "" ""))
3256                       (pc)))]
3257   ""
3258   "from_compare (operands, EQ);")
3259
3260 (define_expand "bne"
3261   [(set (pc)
3262         (if_then_else (eq (reg:SI T_REG) (const_int 0))
3263                       (label_ref (match_operand 0 "" ""))
3264                       (pc)))]
3265   ""
3266   "from_compare (operands, EQ);")
3267
3268 (define_expand "bgt"
3269   [(set (pc)
3270         (if_then_else (ne (reg:SI T_REG) (const_int 0))
3271                       (label_ref (match_operand 0 "" ""))
3272                       (pc)))]
3273   ""
3274   "from_compare (operands, GT);")
3275
3276 (define_expand "blt"
3277   [(set (pc)
3278         (if_then_else (eq (reg:SI T_REG) (const_int 0))
3279                       (label_ref (match_operand 0 "" ""))
3280                       (pc)))]
3281   ""
3282   "
3283 {
3284   if (GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
3285     {
3286       rtx tmp = sh_compare_op0;
3287       sh_compare_op0 = sh_compare_op1;
3288       sh_compare_op1 = tmp;
3289       emit_insn (gen_bgt (operands[0]));
3290       DONE;
3291     }
3292   from_compare (operands, GE);
3293 }")
3294
3295 (define_expand "ble"
3296   [(set (pc)
3297         (if_then_else (eq (reg:SI T_REG) (const_int 0))
3298                       (label_ref (match_operand 0 "" ""))
3299                       (pc)))]
3300   ""
3301   "
3302 {
3303   if (TARGET_SH3E
3304       && TARGET_IEEE
3305       && GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
3306     {
3307       rtx tmp = sh_compare_op0;
3308       sh_compare_op0 = sh_compare_op1;
3309       sh_compare_op1 = tmp;
3310       emit_insn (gen_bge (operands[0]));
3311       DONE;
3312     }
3313   from_compare (operands, GT);
3314 }")
3315
3316 (define_expand "bge"
3317   [(set (pc)
3318         (if_then_else (ne (reg:SI T_REG) (const_int 0))
3319                       (label_ref (match_operand 0 "" ""))
3320                       (pc)))]
3321   ""
3322   "
3323 {
3324   if (TARGET_SH3E
3325       && ! TARGET_IEEE
3326       && GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
3327     {
3328       rtx tmp = sh_compare_op0;
3329       sh_compare_op0 = sh_compare_op1;
3330       sh_compare_op1 = tmp;
3331       emit_insn (gen_ble (operands[0]));
3332       DONE;
3333     }
3334   from_compare (operands, GE);
3335 }")
3336
3337 (define_expand "bgtu"
3338   [(set (pc)
3339         (if_then_else (ne (reg:SI T_REG) (const_int 0))
3340                       (label_ref (match_operand 0 "" ""))
3341                       (pc)))]
3342   ""
3343   "from_compare (operands, GTU); ")
3344
3345 (define_expand "bltu"
3346   [(set (pc)
3347         (if_then_else (eq (reg:SI T_REG) (const_int 0))
3348                       (label_ref (match_operand 0 "" ""))
3349                       (pc)))]
3350   ""
3351   "from_compare (operands, GEU);")
3352
3353 (define_expand "bgeu"
3354   [(set (pc)
3355         (if_then_else (ne (reg:SI T_REG) (const_int 0))
3356                       (label_ref (match_operand 0 "" ""))
3357                       (pc)))]
3358   ""
3359   "from_compare (operands, GEU);")
3360
3361 (define_expand "bleu"
3362   [(set (pc)
3363         (if_then_else (eq (reg:SI T_REG) (const_int 0))
3364                       (label_ref (match_operand 0 "" ""))
3365                       (pc)))]
3366   ""
3367   "from_compare (operands, GTU);")
3368 \f
3369 ;; ------------------------------------------------------------------------
3370 ;; Jump and linkage insns
3371 ;; ------------------------------------------------------------------------
3372
3373 (define_insn "jump"
3374   [(set (pc)
3375         (label_ref (match_operand 0 "" "")))]
3376   ""
3377   "*
3378 {
3379   /* The length is 16 if the delay slot is unfilled.  */
3380   if (get_attr_length(insn) > 4)
3381     return output_far_jump(insn, operands[0]);
3382   else
3383     return   \"bra      %l0%#\";
3384 }"
3385   [(set_attr "type" "jump")
3386    (set_attr "needs_delay_slot" "yes")])
3387
3388 (define_insn "calli"
3389   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
3390          (match_operand 1 "" ""))
3391    (use (reg:PSI FPSCR_REG))
3392    (clobber (reg:SI PR_REG))]
3393   ""
3394   "jsr  @%0%#"
3395   [(set_attr "type" "call")
3396    (set (attr "fp_mode")
3397         (if_then_else (eq_attr "fpu_single" "yes")
3398                       (const_string "single") (const_string "double")))
3399    (set_attr "needs_delay_slot" "yes")])
3400
3401 ;; This is a pc-rel call, using bsrf, for use with PIC.
3402
3403 (define_insn "calli_pcrel"
3404   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "r"))
3405          (match_operand 1 "" ""))
3406    (use (reg:PSI FPSCR_REG))
3407    (use (reg:SI PIC_REG))
3408    (use (match_operand 2 "" ""))
3409    (clobber (reg:SI PR_REG))]
3410   "TARGET_SH2"
3411   "bsrf %0\\n%O2:%#"
3412   [(set_attr "type" "call")
3413    (set (attr "fp_mode")
3414         (if_then_else (eq_attr "fpu_single" "yes")
3415                       (const_string "single") (const_string "double")))
3416    (set_attr "needs_delay_slot" "yes")])
3417
3418 (define_insn_and_split "call_pcrel"
3419   [(call (mem:SI (match_operand:SI 0 "symbol_ref_operand" ""))
3420          (match_operand 1 "" ""))
3421    (use (reg:PSI FPSCR_REG))
3422    (use (reg:SI PIC_REG))
3423    (clobber (reg:SI PR_REG))
3424    (clobber (match_scratch:SI 2 "=r"))]
3425   "TARGET_SH2"
3426   "#"
3427   "reload_completed"
3428   [(const_int 0)]
3429   "
3430 {
3431   rtx lab = gen_call_site ();
3432
3433   if (SYMBOL_REF_FLAG (operands[0]))
3434     emit_insn (gen_sym_label2reg (operands[2], operands[0], lab));
3435   else
3436     emit_insn (gen_symPLT_label2reg (operands[2], operands[0], lab));
3437   emit_call_insn (gen_calli_pcrel (operands[2], operands[1], lab));
3438   DONE;
3439 }"
3440   [(set_attr "type" "call")
3441    (set (attr "fp_mode")
3442         (if_then_else (eq_attr "fpu_single" "yes")
3443                       (const_string "single") (const_string "double")))
3444    (set_attr "needs_delay_slot" "yes")])
3445
3446 (define_insn "call_valuei"
3447   [(set (match_operand 0 "" "=rf")
3448         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
3449               (match_operand 2 "" "")))
3450    (use (reg:PSI FPSCR_REG))
3451    (clobber (reg:SI PR_REG))]
3452   ""
3453   "jsr  @%1%#"
3454   [(set_attr "type" "call")
3455    (set (attr "fp_mode")
3456         (if_then_else (eq_attr "fpu_single" "yes")
3457                       (const_string "single") (const_string "double")))
3458    (set_attr "needs_delay_slot" "yes")])
3459
3460 (define_insn "call_valuei_pcrel"
3461   [(set (match_operand 0 "" "=rf")
3462         (call (mem:SI (match_operand:SI 1 "arith_reg_operand" "r"))
3463               (match_operand 2 "" "")))
3464    (use (reg:PSI FPSCR_REG))
3465    (use (reg:SI PIC_REG))
3466    (use (match_operand 3 "" ""))
3467    (clobber (reg:SI PR_REG))]
3468   "TARGET_SH2"
3469   "bsrf %1\\n%O3:%#"
3470   [(set_attr "type" "call")
3471    (set (attr "fp_mode")
3472         (if_then_else (eq_attr "fpu_single" "yes")
3473                       (const_string "single") (const_string "double")))
3474    (set_attr "needs_delay_slot" "yes")])
3475
3476 (define_insn_and_split "call_value_pcrel"
3477   [(set (match_operand 0 "" "=rf")
3478         (call (mem:SI (match_operand:SI 1 "symbol_ref_operand" ""))
3479               (match_operand 2 "" "")))
3480    (use (reg:PSI FPSCR_REG))
3481    (use (reg:SI PIC_REG))
3482    (clobber (reg:SI PR_REG))
3483    (clobber (match_scratch:SI 3 "=r"))]
3484   "TARGET_SH2"
3485   "#"
3486   "reload_completed"
3487   [(const_int 0)]
3488   "
3489 {
3490   rtx lab = gen_call_site ();
3491
3492   if (SYMBOL_REF_FLAG (operands[1]))
3493     emit_insn (gen_sym_label2reg (operands[3], operands[1], lab));
3494   else
3495     emit_insn (gen_symPLT_label2reg (operands[3], operands[1], lab));
3496   emit_call_insn (gen_call_valuei_pcrel (operands[0], operands[3],
3497                                          operands[2], lab));
3498   DONE;
3499 }"
3500   [(set_attr "type" "call")
3501    (set (attr "fp_mode")
3502         (if_then_else (eq_attr "fpu_single" "yes")
3503                       (const_string "single") (const_string "double")))
3504    (set_attr "needs_delay_slot" "yes")])
3505
3506 (define_expand "call"
3507   [(parallel [(call (mem:SI (match_operand 0 "arith_reg_operand" ""))
3508                             (match_operand 1 "" ""))
3509               (use (reg:PSI FPSCR_REG))
3510               (clobber (reg:SI PR_REG))])]
3511   ""
3512   "
3513 {
3514   if (flag_pic && TARGET_SH2
3515       && GET_CODE (operands[0]) == MEM
3516       && GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF)
3517     {
3518       emit_call_insn (gen_call_pcrel (XEXP (operands[0], 0), operands[1]));
3519       DONE;
3520     }
3521   else
3522     operands[0] = force_reg (SImode, XEXP (operands[0], 0));
3523 }")
3524
3525 (define_expand "call_value"
3526   [(parallel [(set (match_operand 0 "arith_reg_operand" "")
3527                    (call (mem:SI (match_operand 1 "arith_reg_operand" ""))
3528                                  (match_operand 2 "" "")))
3529               (use (reg:PSI FPSCR_REG))
3530               (clobber (reg:SI PR_REG))])]
3531   ""
3532   "
3533 {
3534   if (flag_pic && TARGET_SH2
3535       && GET_CODE (operands[1]) == MEM
3536       && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF)
3537     {
3538       emit_call_insn (gen_call_value_pcrel (operands[0], XEXP (operands[1], 0),
3539                                             operands[2]));
3540       DONE;
3541     }
3542   else
3543     operands[1] = force_reg (SImode, XEXP (operands[1], 0));
3544 }")
3545
3546 (define_insn "sibcalli"
3547   [(call (mem:SI (match_operand:SI 0 "register_operand" "k"))
3548          (match_operand 1 "" ""))
3549    (use (reg:PSI FPSCR_REG))
3550    (return)]
3551   ""
3552   "jmp  @%0%#"
3553   [(set_attr "needs_delay_slot" "yes")
3554    (set (attr "fp_mode")
3555         (if_then_else (eq_attr "fpu_single" "yes")
3556                       (const_string "single") (const_string "double")))
3557    (set_attr "type" "jump_ind")])
3558
3559 (define_insn "sibcalli_pcrel"
3560   [(call (mem:SI (match_operand:SI 0 "arith_reg_operand" "k"))
3561          (match_operand 1 "" ""))
3562    (use (match_operand 2 "" ""))
3563    (use (reg:PSI FPSCR_REG))
3564    (return)]
3565   "TARGET_SH2"
3566   "braf %0\\n%O2:%#"
3567   [(set_attr "needs_delay_slot" "yes")
3568    (set (attr "fp_mode")
3569         (if_then_else (eq_attr "fpu_single" "yes")
3570                       (const_string "single") (const_string "double")))
3571    (set_attr "type" "jump_ind")])
3572
3573 (define_insn_and_split "sibcall_pcrel"
3574   [(call (mem:SI (match_operand:SI 0 "symbol_ref_operand" ""))
3575          (match_operand 1 "" ""))
3576    (use (reg:PSI FPSCR_REG))
3577    (clobber (match_scratch:SI 2 "=k"))
3578    (return)]
3579   "TARGET_SH2"
3580   "#"
3581   "reload_completed"
3582   [(const_int 0)]
3583   "
3584 {
3585   rtx lab = gen_call_site ();
3586   rtx call_insn;
3587
3588   emit_insn (gen_sym_label2reg (operands[2], operands[0], lab));
3589   call_insn = emit_call_insn (gen_sibcalli_pcrel (operands[2], operands[1],
3590                                                   lab));
3591   SIBLING_CALL_P (call_insn) = 1;
3592   DONE;
3593 }"
3594   [(set_attr "needs_delay_slot" "yes")
3595    (set (attr "fp_mode")
3596         (if_then_else (eq_attr "fpu_single" "yes")
3597                       (const_string "single") (const_string "double")))
3598    (set_attr "type" "jump_ind")])
3599
3600 (define_expand "sibcall"
3601   [(parallel
3602     [(call (mem:SI (match_operand 0 "arith_reg_operand" ""))
3603            (match_operand 1 "" ""))
3604      (use (reg:PSI FPSCR_REG))
3605      (return)])]
3606   ""
3607   "
3608 {
3609   if (flag_pic && TARGET_SH2
3610       && GET_CODE (operands[0]) == MEM
3611       && GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
3612       /* The PLT needs the PIC register, but the epilogue would have
3613          to restore it, so we can only use PC-relative PIC calls for
3614          static functions.  */
3615       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
3616     {
3617       emit_call_insn (gen_sibcall_pcrel (XEXP (operands[0], 0), operands[1]));
3618       DONE;
3619     }
3620   else
3621     operands[0] = force_reg (SImode, XEXP (operands[0], 0));
3622 }")
3623
3624 (define_expand "sibcall_value"
3625   [(set (match_operand 0 "" "")
3626         (call (match_operand 1 "" "")
3627               (match_operand 2 "" "")))]
3628   ""
3629   "
3630 {
3631   emit_call_insn (gen_sibcall (operands[1], operands[2]));
3632   DONE;
3633 }")
3634
3635 (define_expand "sibcall_epilogue"
3636   [(return)]
3637   ""
3638   "
3639 {
3640   sh_expand_epilogue ();
3641   DONE;
3642 }")
3643
3644 (define_insn "indirect_jump"
3645   [(set (pc)
3646         (match_operand:SI 0 "arith_reg_operand" "r"))]
3647   ""
3648   "jmp  @%0%#"
3649   [(set_attr "needs_delay_slot" "yes")
3650    (set_attr "type" "jump_ind")])
3651
3652 ;; The use of operand 1 / 2 helps us distinguish case table jumps
3653 ;; which can be present in structured code from indirect jumps which can not
3654 ;; be present in structured code.  This allows -fprofile-arcs to work.
3655
3656 ;; For SH1 processors.
3657 (define_insn "casesi_jump_1"
3658   [(set (pc)
3659         (match_operand:SI 0 "register_operand" "r"))
3660    (use (label_ref (match_operand 1 "" "")))]
3661   ""
3662   "jmp  @%0%#"
3663   [(set_attr "needs_delay_slot" "yes")
3664    (set_attr "type" "jump_ind")])
3665
3666 ;; For all later processors.
3667 (define_insn "casesi_jump_2"
3668   [(set (pc) (plus:SI (match_operand:SI 0 "register_operand" "r")
3669                       (label_ref (match_operand 1 "" ""))))
3670    (use (label_ref (match_operand 2 "" "")))]
3671   "TARGET_SH2
3672    && (! INSN_UID (operands[1]) || prev_real_insn (operands[1]) == insn)"
3673   "braf %0%#"
3674   [(set_attr "needs_delay_slot" "yes")
3675    (set_attr "type" "jump_ind")])
3676
3677 ;; Call subroutine returning any type.
3678 ;; ??? This probably doesn't work.
3679
3680 (define_expand "untyped_call"
3681   [(parallel [(call (match_operand 0 "" "")
3682                     (const_int 0))
3683               (match_operand 1 "" "")
3684               (match_operand 2 "" "")])]
3685   "TARGET_SH3E"
3686   "
3687 {
3688   int i;
3689
3690   emit_call_insn (gen_call (operands[0], const0_rtx));
3691
3692   for (i = 0; i < XVECLEN (operands[2], 0); i++)
3693     {
3694       rtx set = XVECEXP (operands[2], 0, i);
3695       emit_move_insn (SET_DEST (set), SET_SRC (set));
3696     }
3697
3698   /* The optimizer does not know that the call sets the function value
3699      registers we stored in the result block.  We avoid problems by
3700      claiming that all hard registers are used and clobbered at this
3701      point.  */
3702   emit_insn (gen_blockage ());
3703
3704   DONE;
3705 }")
3706 \f
3707 ;; ------------------------------------------------------------------------
3708 ;; Misc insns
3709 ;; ------------------------------------------------------------------------
3710
3711 (define_insn "dect"
3712   [(set (reg:SI T_REG)
3713         (eq:SI (match_operand:SI 0 "arith_reg_operand" "+r") (const_int 1)))
3714    (set (match_dup 0) (plus:SI (match_dup 0) (const_int -1)))]
3715   "TARGET_SH2"
3716   "dt   %0"
3717   [(set_attr "type" "arith")])
3718
3719 (define_insn "nop"
3720   [(const_int 0)]
3721   ""
3722   "nop")
3723
3724 ;; Load address of a label. This is only generated by the casesi expand,
3725 ;; and by machine_dependent_reorg (fixing up fp moves).
3726 ;; This must use unspec, because this only works for labels that are
3727 ;; within range,
3728
3729 (define_insn "mova"
3730   [(set (reg:SI R0_REG)
3731         (unspec [(label_ref (match_operand 0 "" ""))] UNSPEC_MOVA))]
3732   ""
3733   "mova %O0,r0"
3734   [(set_attr "in_delay_slot" "no")
3735    (set_attr "type" "arith")])
3736
3737 ;; machine_dependent_reorg() will make this a `mova'.
3738 (define_insn "mova_const"
3739   [(set (reg:SI R0_REG)
3740         (unspec [(match_operand 0 "immediate_operand" "i")] UNSPEC_MOVA))]
3741   ""
3742   "#"
3743   [(set_attr "in_delay_slot" "no")
3744    (set_attr "type" "arith")])
3745
3746 (define_expand "GOTaddr2picreg"
3747   [(set (reg:SI R0_REG)
3748         (unspec [(const (unspec [(match_dup 1)] UNSPEC_PIC))]
3749                 UNSPEC_MOVA))
3750    (set (match_dup 0) (const (unspec [(match_dup 1)] UNSPEC_PIC)))
3751    (set (match_dup 0) (plus:SI (match_dup 0) (reg:SI R0_REG)))]
3752   "" "
3753 {
3754   operands[0] = pic_offset_table_rtx;
3755   operands[1] = gen_rtx_SYMBOL_REF (VOIDmode, GOT_SYMBOL_NAME);
3756 }
3757 ")
3758
3759 (define_expand "builtin_setjmp_receiver"
3760   [(match_operand 0 "" "")]
3761   "flag_pic"
3762   "
3763 {
3764   emit_insn (gen_GOTaddr2picreg ());
3765   DONE;
3766 }")
3767
3768 (define_expand "call_site"
3769   [(unspec [(match_dup 0)] UNSPEC_CALLER)]
3770   ""
3771   "
3772 {
3773   static HOST_WIDE_INT i = 0;
3774   operands[0] = GEN_INT (i);
3775   i++;
3776 }")
3777
3778 (define_expand "sym_label2reg"
3779   [(set (match_operand:SI 0 "" "")
3780         (const (minus:SI
3781                 (const (unspec [(match_operand:SI 1 "" "")] UNSPEC_PIC))
3782                 (const (plus:SI
3783                         (match_operand:SI 2 "" "")
3784                         (const_int 2))))))]
3785   "" "")
3786
3787 (define_expand "symGOT2reg"
3788   [(set (match_operand:SI 0 "" "")
3789         (const (unspec [(match_operand:SI 1 "" "")] UNSPEC_GOT)))
3790   (set (match_dup 0) (plus:SI (match_dup 0) (match_dup 2)))
3791   (set (match_dup 0) (mem:SI (match_dup 0)))]
3792   ""
3793   "
3794 {
3795   operands[2] = pic_offset_table_rtx;
3796 }")
3797
3798 (define_expand "symGOTOFF2reg"
3799   [(set (match_operand:SI 0 "" "")
3800         (const (unspec [(match_operand:SI 1 "" "")] UNSPEC_GOTOFF)))
3801   (set (match_dup 0) (plus:SI (match_dup 0) (match_dup 2)))]
3802   ""
3803   "
3804 {
3805   operands[2] = pic_offset_table_rtx;
3806 }")
3807
3808 (define_expand "symPLT_label2reg"
3809   [(set (match_operand:SI 0 "" "")
3810         (const (minus:SI
3811                 (const (plus:SI
3812                         (unspec [(match_operand:SI 1 "" "")] UNSPEC_PLT)
3813                         (pc)))
3814                 (const (plus:SI
3815                         (match_operand:SI 2 "" "")
3816                         (const_int 2))))))
3817    ;; Even though the PIC register is not really used by the call
3818    ;; sequence in which this is expanded, the PLT code assumes the PIC
3819    ;; register is set, so we must not skip its initialization.  Since
3820    ;; we only use this expand as part of calling sequences, and never
3821    ;; to take the address of a function, this is the best point to
3822    ;; insert the (use).  Using the PLT to take the address of a
3823    ;; function would be wrong, not only because the PLT entry could
3824    ;; then be called from a function that doesn't initialize the PIC
3825    ;; register to the proper GOT, but also because pointers to the
3826    ;; same function might not compare equal, should they be set by
3827    ;; different shared libraries.
3828    (use (reg:SI PIC_REG))]
3829   ""
3830   "")
3831
3832 ;; case instruction for switch statements.
3833
3834 ;; Operand 0 is index
3835 ;; operand 1 is the minimum bound
3836 ;; operand 2 is the maximum bound - minimum bound + 1
3837 ;; operand 3 is CODE_LABEL for the table;
3838 ;; operand 4 is the CODE_LABEL to go to if index out of range.
3839
3840 (define_expand "casesi"
3841   [(match_operand:SI 0 "arith_reg_operand" "")
3842    (match_operand:SI 1 "arith_reg_operand" "")
3843    (match_operand:SI 2 "arith_reg_operand" "")
3844    (match_operand 3 "" "") (match_operand 4 "" "")]
3845   ""
3846   "
3847 {
3848   rtx reg = gen_reg_rtx (SImode);
3849   rtx reg2 = gen_reg_rtx (SImode);
3850   operands[1] = copy_to_mode_reg (SImode, operands[1]);
3851   operands[2] = copy_to_mode_reg (SImode, operands[2]);
3852   /* If optimizing, casesi_worker depends on the mode of the instruction
3853      before label it 'uses' - operands[3].  */
3854   emit_insn (gen_casesi_0 (operands[0], operands[1], operands[2], operands[4],
3855                            reg));
3856   emit_insn (gen_casesi_worker_0 (reg2, reg, operands[3]));
3857   if (TARGET_SH2)
3858     emit_jump_insn (gen_casesi_jump_2 (reg2, gen_label_rtx (), operands[3]));
3859   else
3860     emit_jump_insn (gen_casesi_jump_1 (reg2, operands[3]));
3861   /* For SH2 and newer, the ADDR_DIFF_VEC is not actually relative to
3862      operands[3], but to lab.  We will fix this up in
3863      machine_dependent_reorg.  */
3864   emit_barrier ();
3865   DONE;
3866 }")
3867
3868 (define_expand "casesi_0"
3869   [(set (match_operand:SI 4 "" "") (match_operand:SI 0 "arith_reg_operand" ""))
3870    (set (match_dup 4) (minus:SI (match_dup 4)
3871                                 (match_operand:SI 1 "arith_operand" "")))
3872    (set (reg:SI T_REG)
3873         (gtu:SI (match_dup 4)
3874                 (match_operand:SI 2 "arith_reg_operand" "")))
3875    (set (pc)
3876         (if_then_else (ne (reg:SI T_REG)
3877                           (const_int 0))
3878                       (label_ref (match_operand 3 "" ""))
3879                       (pc)))]
3880   ""
3881   "")
3882
3883 ;; ??? reload might clobber r0 if we use it explicitly in the RTL before
3884 ;; reload; using a R0_REGS pseudo reg is likely to give poor code.
3885 ;; So we keep the use of r0 hidden in a R0_REGS clobber until after reload.
3886
3887 (define_insn "casesi_worker_0"
3888   [(set (match_operand:SI 0 "register_operand" "=r,r")
3889         (unspec [(match_operand 1 "register_operand" "0,r")
3890                  (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
3891    (clobber (match_scratch:SI 3 "=X,1"))
3892    (clobber (match_scratch:SI 4 "=&z,z"))]
3893   ""
3894   "#")
3895
3896 (define_split
3897   [(set (match_operand:SI 0 "register_operand" "")
3898         (unspec [(match_operand 1 "register_operand" "")
3899                  (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
3900    (clobber (match_scratch:SI 3 ""))
3901    (clobber (match_scratch:SI 4 ""))]
3902   "! TARGET_SH2 && reload_completed"
3903   [(set (reg:SI R0_REG) (unspec [(label_ref (match_dup 2))] UNSPEC_MOVA))
3904    (parallel [(set (match_dup 0)
3905               (unspec [(reg:SI R0_REG) (match_dup 1)
3906                        (label_ref (match_dup 2))] UNSPEC_CASESI))
3907               (clobber (match_dup 3))])
3908    (set (match_dup 0) (plus:SI (match_dup 0) (reg:SI R0_REG)))]
3909   "LABEL_NUSES (operands[2])++;")
3910
3911 (define_split
3912   [(set (match_operand:SI 0 "register_operand" "")
3913         (unspec [(match_operand 1 "register_operand" "")
3914                  (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
3915    (clobber (match_scratch:SI 3 ""))
3916    (clobber (match_scratch:SI 4 ""))]
3917   "TARGET_SH2 && reload_completed"
3918   [(set (reg:SI R0_REG) (unspec [(label_ref (match_dup 2))] UNSPEC_MOVA))
3919    (parallel [(set (match_dup 0)
3920               (unspec [(reg:SI R0_REG) (match_dup 1)
3921                        (label_ref (match_dup 2))] UNSPEC_CASESI))
3922               (clobber (match_dup 3))])]
3923   "LABEL_NUSES (operands[2])++;")
3924
3925 (define_insn "*casesi_worker"
3926   [(set (match_operand:SI 0 "register_operand" "=r,r")
3927         (unspec [(reg:SI R0_REG) (match_operand 1 "register_operand" "0,r")
3928                  (label_ref (match_operand 2 "" ""))] UNSPEC_CASESI))
3929    (clobber (match_scratch:SI 3 "=X,1"))]
3930   ""
3931   "*
3932 {
3933   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
3934
3935   if (GET_CODE (diff_vec) != ADDR_DIFF_VEC)
3936     abort ();
3937
3938   switch (GET_MODE (diff_vec))
3939     {
3940     case SImode:
3941       return \"shll2    %1\;mov.l       @(r0,%1),%0\";
3942     case HImode:
3943       return \"add      %1,%1\;mov.w    @(r0,%1),%0\";
3944     case QImode:
3945       if (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned)
3946         return \"mov.b  @(r0,%1),%0\;extu.b     %0,%0\";
3947       return \"mov.b    @(r0,%1),%0\";
3948     default:
3949       abort ();
3950     }
3951 }"
3952   [(set_attr "length" "4")])
3953
3954 (define_expand "return"
3955   [(return)]
3956   "reload_completed && ! sh_need_epilogue ()"
3957   "")
3958
3959 (define_insn "*return_i"
3960   [(return)]
3961   "reload_completed"
3962   "%@   %#"
3963   [(set_attr "type" "return")
3964    (set_attr "needs_delay_slot" "yes")])
3965
3966 (define_expand "prologue"
3967   [(const_int 0)]
3968   ""
3969   "sh_expand_prologue (); DONE;")
3970
3971 (define_expand "epilogue"
3972   [(return)]
3973   ""
3974   "sh_expand_epilogue ();")
3975
3976 (define_insn "blockage"
3977   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)]
3978   ""
3979   ""
3980   [(set_attr "length" "0")])
3981 \f
3982 ;; ------------------------------------------------------------------------
3983 ;; Scc instructions
3984 ;; ------------------------------------------------------------------------
3985
3986 (define_insn "movt"
3987   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
3988         (eq:SI (reg:SI T_REG) (const_int 1)))]
3989   ""
3990   "movt %0"
3991   [(set_attr "type" "arith")])
3992
3993 (define_expand "seq"
3994   [(set (match_operand:SI 0 "arith_reg_operand" "")
3995         (match_dup 1))]
3996   ""
3997   "operands[1] = prepare_scc_operands (EQ);")
3998
3999 (define_expand "slt"
4000   [(set (match_operand:SI 0 "arith_reg_operand" "")
4001         (match_dup 1))]
4002   ""
4003   "operands[1] = prepare_scc_operands (LT);")
4004
4005 (define_expand "sle"
4006   [(match_operand:SI 0 "arith_reg_operand" "")]
4007   ""
4008   "
4009 {
4010   rtx tmp = sh_compare_op0;
4011   sh_compare_op0 = sh_compare_op1;
4012   sh_compare_op1 = tmp;
4013   emit_insn (gen_sge (operands[0]));
4014   DONE;
4015 }")
4016
4017 (define_expand "sgt"
4018   [(set (match_operand:SI 0 "arith_reg_operand" "")
4019         (match_dup 1))]
4020   ""
4021   "operands[1] = prepare_scc_operands (GT);")
4022
4023 (define_expand "sge"
4024   [(set (match_operand:SI 0 "arith_reg_operand" "")
4025         (match_dup 1))]
4026   ""
4027   "
4028 {
4029   if (GET_MODE_CLASS (GET_MODE (sh_compare_op0)) == MODE_FLOAT)
4030     {
4031       if (TARGET_IEEE)
4032         {
4033           rtx lab = gen_label_rtx ();
4034           prepare_scc_operands (EQ);
4035           emit_jump_insn (gen_branch_true (lab));
4036           prepare_scc_operands (GT);
4037           emit_label (lab);
4038           emit_insn (gen_movt (operands[0]));
4039         }
4040       else
4041         emit_insn (gen_movnegt (operands[0], prepare_scc_operands (LT)));
4042       DONE;
4043     }
4044   operands[1] = prepare_scc_operands (GE);
4045 }")
4046
4047 (define_expand "sgtu"
4048   [(set (match_operand:SI 0 "arith_reg_operand" "")
4049         (match_dup 1))]
4050   ""
4051   "operands[1] = prepare_scc_operands (GTU);")
4052
4053 (define_expand "sltu"
4054   [(set (match_operand:SI 0 "arith_reg_operand" "")
4055         (match_dup 1))]
4056   ""
4057   "operands[1] = prepare_scc_operands (LTU);")
4058
4059 (define_expand "sleu"
4060   [(set (match_operand:SI 0 "arith_reg_operand" "")
4061         (match_dup 1))]
4062   ""
4063   "operands[1] = prepare_scc_operands (LEU);")
4064
4065 (define_expand "sgeu"
4066   [(set (match_operand:SI 0 "arith_reg_operand" "")
4067         (match_dup 1))]
4068   ""
4069   "operands[1] = prepare_scc_operands (GEU);")
4070
4071 ;; sne moves the complement of the T reg to DEST like this:
4072 ;;      cmp/eq ...
4073 ;;      mov    #-1,temp
4074 ;;      negc   temp,dest
4075 ;;   This is better than xoring compare result with 1 because it does
4076 ;;   not require r0 and further, the -1 may be CSE-ed or lifted out of a
4077 ;;   loop.
4078
4079 (define_expand "sne"
4080   [(set (match_dup 2) (const_int -1))
4081    (parallel [(set (match_operand:SI 0 "arith_reg_operand" "")
4082                    (neg:SI (plus:SI (match_dup 1)
4083                                     (match_dup 2))))
4084               (set (reg:SI T_REG)
4085                    (ne:SI (ior:SI (match_dup 1) (match_dup 2))
4086                           (const_int 0)))])]  
4087   ""
4088   "
4089 {
4090    operands[1] = prepare_scc_operands (EQ);
4091    operands[2] = gen_reg_rtx (SImode);
4092 }")
4093
4094 ;; Use the same trick for FP sle / sge
4095 (define_expand "movnegt"
4096   [(set (match_dup 2) (const_int -1))
4097    (parallel [(set (match_operand 0 "" "")
4098                    (neg:SI (plus:SI (match_dup 1)
4099                                     (match_dup 2))))
4100               (set (reg:SI T_REG)
4101                    (ne:SI (ior:SI (match_operand 1 "" "") (match_dup 2))
4102                           (const_int 0)))])]  
4103   ""
4104   "operands[2] = gen_reg_rtx (SImode);")
4105
4106 ;; Recognize mov #-1/negc/neg sequence, and change it to movt/add #-1.
4107 ;; This prevents a regression that occurred when we switched from xor to
4108 ;; mov/neg for sne.
4109
4110 (define_split
4111   [(set (match_operand:SI 0 "arith_reg_operand" "")
4112         (plus:SI (reg:SI T_REG)
4113                  (const_int -1)))]
4114   ""
4115   [(set (match_dup 0) (eq:SI (reg:SI T_REG) (const_int 1)))
4116    (set (match_dup 0) (plus:SI (match_dup 0) (const_int -1)))]
4117   "")
4118
4119 ;; -------------------------------------------------------------------------
4120 ;; Instructions to cope with inline literal tables
4121 ;; -------------------------------------------------------------------------
4122
4123 ; 2 byte integer in line
4124
4125 (define_insn "consttable_2"
4126  [(unspec_volatile [(match_operand:SI 0 "general_operand" "=g")
4127                     (match_operand 1 "" "")]
4128                    UNSPECV_CONST2)]
4129  ""
4130  "*
4131 {
4132   if (operands[1] != const0_rtx)
4133     assemble_integer (operands[0], 2, 1);
4134   return \"\";
4135 }"
4136  [(set_attr "length" "2")
4137  (set_attr "in_delay_slot" "no")])
4138
4139 ; 4 byte integer in line
4140
4141 (define_insn "consttable_4"
4142  [(unspec_volatile [(match_operand:SI 0 "general_operand" "=g")
4143                     (match_operand 1 "" "")]
4144                    UNSPECV_CONST4)]
4145  ""
4146  "*
4147 {
4148   if (operands[1] != const0_rtx)
4149     assemble_integer (operands[0], 4, 1);
4150   return \"\";
4151 }"
4152  [(set_attr "length" "4")
4153   (set_attr "in_delay_slot" "no")])
4154
4155 ; 8 byte integer in line
4156
4157 (define_insn "consttable_8"
4158  [(unspec_volatile [(match_operand:SI 0 "general_operand" "=g")
4159                     (match_operand 1 "" "")]
4160                    UNSPECV_CONST8)]
4161  ""
4162  "*
4163 {
4164   if (operands[1] != const0_rtx)
4165     assemble_integer (operands[0], 8, 1);
4166   return \"\";
4167 }"
4168  [(set_attr "length" "8")
4169   (set_attr "in_delay_slot" "no")])
4170
4171 ; 4 byte floating point
4172
4173 (define_insn "consttable_sf"
4174  [(unspec_volatile [(match_operand:SF 0 "general_operand" "=g")
4175                     (match_operand 1 "" "")]
4176                    UNSPECV_CONST4)]
4177  ""
4178  "*
4179 {
4180   if (operands[1] != const0_rtx)
4181     {
4182       union real_extract u;
4183       memcpy (&u, &CONST_DOUBLE_LOW (operands[0]), sizeof u);
4184       assemble_real (u.d, SFmode);
4185     }
4186   return \"\";
4187 }"
4188  [(set_attr "length" "4")
4189   (set_attr "in_delay_slot" "no")])
4190
4191 ; 8 byte floating point
4192
4193 (define_insn "consttable_df"
4194  [(unspec_volatile [(match_operand:DF 0 "general_operand" "=g")
4195                     (match_operand 1 "" "")]
4196                    UNSPECV_CONST8)]
4197  ""
4198  "*
4199 {
4200   if (operands[1] != const0_rtx)
4201     {
4202       union real_extract u;
4203       memcpy (&u, &CONST_DOUBLE_LOW (operands[0]), sizeof u);
4204       assemble_real (u.d, DFmode);
4205     }
4206   return \"\";
4207 }"
4208  [(set_attr "length" "8")
4209   (set_attr "in_delay_slot" "no")])
4210
4211 ;; Alignment is needed for some constant tables; it may also be added for
4212 ;; Instructions at the start of loops, or after unconditional branches.
4213 ;; ??? We would get more accurate lengths if we did instruction
4214 ;; alignment based on the value of INSN_CURRENT_ADDRESS; the approach used
4215 ;; here is too conservative.
4216
4217 ; align to a two byte boundary
4218
4219 (define_expand "align_2"
4220  [(unspec_volatile [(const_int 1)] UNSPECV_ALIGN)]
4221  ""
4222  "")
4223
4224 ; align to a four byte boundary
4225 ;; align_4 and align_log are instructions for the starts of loops, or
4226 ;; after unconditional branches, which may take up extra room.
4227
4228 (define_expand "align_4"
4229  [(unspec_volatile [(const_int 2)] UNSPECV_ALIGN)]
4230  ""
4231  "")
4232
4233 ; align to a cache line boundary
4234
4235 (define_insn "align_log"
4236  [(unspec_volatile [(match_operand 0 "const_int_operand" "")] UNSPECV_ALIGN)]
4237  ""
4238  ""
4239  [(set_attr "length" "0")
4240   (set_attr "in_delay_slot" "no")])
4241
4242 ; emitted at the end of the literal table, used to emit the
4243 ; 32bit branch labels if needed.
4244
4245 (define_insn "consttable_end"
4246   [(unspec_volatile [(const_int 0)] UNSPECV_CONST_END)]
4247   ""
4248   "* return output_jump_label_table ();"
4249   [(set_attr "in_delay_slot" "no")])
4250
4251 ; emitted at the end of the window in the literal table.
4252
4253 (define_insn "consttable_window_end"
4254   [(unspec_volatile [(match_operand 0 "" "")] UNSPECV_WINDOW_END)]
4255   ""
4256   ""
4257   [(set_attr "length" "0")
4258    (set_attr "in_delay_slot" "no")])
4259
4260 ;; -------------------------------------------------------------------------
4261 ;; Misc
4262 ;; -------------------------------------------------------------------------
4263
4264 ;; String/block move insn.
4265
4266 (define_expand "movstrsi"
4267   [(parallel [(set (mem:BLK (match_operand:BLK 0 "" ""))
4268                    (mem:BLK (match_operand:BLK 1 "" "")))
4269               (use (match_operand:SI 2 "nonmemory_operand" ""))
4270               (use (match_operand:SI 3 "immediate_operand" ""))
4271               (clobber (reg:SI PR_REG))
4272               (clobber (reg:SI R4_REG))
4273               (clobber (reg:SI R5_REG))
4274               (clobber (reg:SI R0_REG))])]
4275   ""
4276   "
4277 {
4278   if(expand_block_move (operands))
4279      DONE;
4280   else FAIL;
4281 }")
4282
4283 (define_insn "block_move_real"
4284   [(parallel [(set (mem:BLK (reg:SI R4_REG))
4285                    (mem:BLK (reg:SI R5_REG)))
4286               (use (match_operand:SI 0 "arith_reg_operand" "r"))
4287               (clobber (reg:SI PR_REG))
4288               (clobber (reg:SI R0_REG))])]
4289   "! TARGET_HARD_SH4"
4290   "jsr  @%0%#"
4291   [(set_attr "type" "sfunc")
4292    (set_attr "needs_delay_slot" "yes")])
4293
4294 (define_insn "block_lump_real"
4295   [(parallel [(set (mem:BLK (reg:SI R4_REG))
4296                    (mem:BLK (reg:SI R5_REG)))
4297               (use (match_operand:SI 0 "arith_reg_operand" "r"))
4298               (use (reg:SI R6_REG))
4299               (clobber (reg:SI PR_REG))
4300               (clobber (reg:SI T_REG))
4301               (clobber (reg:SI R4_REG))
4302               (clobber (reg:SI R5_REG))
4303               (clobber (reg:SI R6_REG))
4304               (clobber (reg:SI R0_REG))])]
4305   "! TARGET_HARD_SH4"
4306   "jsr  @%0%#"
4307   [(set_attr "type" "sfunc")
4308    (set_attr "needs_delay_slot" "yes")])
4309
4310 (define_insn "block_move_real_i4"
4311   [(parallel [(set (mem:BLK (reg:SI R4_REG))
4312                    (mem:BLK (reg:SI R5_REG)))
4313               (use (match_operand:SI 0 "arith_reg_operand" "r"))
4314               (clobber (reg:SI PR_REG))
4315               (clobber (reg:SI R0_REG))
4316               (clobber (reg:SI R1_REG))
4317               (clobber (reg:SI R2_REG))])]
4318   "TARGET_HARD_SH4"
4319   "jsr  @%0%#"
4320   [(set_attr "type" "sfunc")
4321    (set_attr "needs_delay_slot" "yes")])
4322
4323 (define_insn "block_lump_real_i4"
4324   [(parallel [(set (mem:BLK (reg:SI R4_REG))
4325                    (mem:BLK (reg:SI R5_REG)))
4326               (use (match_operand:SI 0 "arith_reg_operand" "r"))
4327               (use (reg:SI R6_REG))
4328               (clobber (reg:SI PR_REG))
4329               (clobber (reg:SI T_REG))
4330               (clobber (reg:SI R4_REG))
4331               (clobber (reg:SI R5_REG))
4332               (clobber (reg:SI R6_REG))
4333               (clobber (reg:SI R0_REG))
4334               (clobber (reg:SI R1_REG))
4335               (clobber (reg:SI R2_REG))
4336               (clobber (reg:SI R3_REG))])]
4337   "TARGET_HARD_SH4"
4338   "jsr  @%0%#"
4339   [(set_attr "type" "sfunc")
4340    (set_attr "needs_delay_slot" "yes")])
4341 \f
4342 ;; -------------------------------------------------------------------------
4343 ;; Floating point instructions.
4344 ;; -------------------------------------------------------------------------
4345
4346 ;; ??? All patterns should have a type attribute.
4347
4348 (define_expand "fpu_switch0"
4349   [(set (match_operand:SI 0 "" "") (match_dup 2))
4350    (set (match_dup 1) (mem:PSI (match_dup 0)))]
4351   "TARGET_SH4"
4352   "
4353 {
4354   operands[1] = get_fpscr_rtx ();
4355   operands[2] = gen_rtx_SYMBOL_REF (SImode, \"__fpscr_values\");
4356   if (flag_pic)
4357     operands[2] = legitimize_pic_address (operands[2], SImode,
4358                                           no_new_pseudos ? operands[0] : 0);
4359 }")
4360
4361 (define_expand "fpu_switch1"
4362   [(set (match_operand:SI 0 "" "") (match_dup 2))
4363    (set (match_dup 3) (plus:SI (match_dup 0) (const_int 4)))
4364    (set (match_dup 1) (mem:PSI (match_dup 3)))]
4365   "TARGET_SH4"
4366   "
4367 {
4368   operands[1] = get_fpscr_rtx ();
4369   operands[2] = gen_rtx_SYMBOL_REF (SImode, \"__fpscr_values\");
4370   if (flag_pic)
4371     operands[2] = legitimize_pic_address (operands[2], SImode,
4372                                           no_new_pseudos ? operands[0] : 0);
4373   operands[3] = no_new_pseudos ? operands[0] : gen_reg_rtx (SImode);
4374 }")
4375
4376 (define_expand "movpsi"
4377   [(set (match_operand:PSI 0 "register_operand" "")
4378         (match_operand:PSI 1 "general_movsrc_operand" ""))]
4379   "TARGET_SH4"
4380   "")
4381
4382 ;; The c / m alternative is a fake to guide reload to load directly into
4383 ;; fpscr, since reload doesn't know how to use post-increment.
4384 ;; GO_IF_LEGITIMATE_ADDRESS guards about bogus addresses before reload,
4385 ;; SECONDARY_INPUT_RELOAD_CLASS does this during reload, and the insn's
4386 ;; predicate after reload.
4387 ;; The gp_fpul type for r/!c might look a bit odd, but it actually schedules
4388 ;; like a gpr <-> fpul move.
4389 (define_insn "fpu_switch"
4390   [(set (match_operand:PSI 0 "register_operand" "=c,c,r,c,c,r,m,r")
4391         (match_operand:PSI 1 "general_movsrc_operand" "c,>,m,m,r,r,r,!c"))]
4392   "TARGET_SH4
4393    && (! reload_completed
4394        || true_regnum (operands[0]) != FPSCR_REG
4395        || GET_CODE (operands[1]) != MEM
4396        || GET_CODE (XEXP (operands[1], 0)) != PLUS)"
4397   "@
4398         ! precision stays the same
4399         lds.l   %1,fpscr
4400         mov.l   %1,%0
4401         #
4402         lds     %1,fpscr
4403         mov     %1,%0
4404         mov.l   %1,%0
4405         sts     fpscr,%0"
4406   [(set_attr "length" "0,2,2,4,2,2,2,2")
4407    (set_attr "type" "dfp_conv,dfp_conv,load,dfp_conv,dfp_conv,move,store,gp_fpul")])
4408
4409 (define_split
4410   [(set (reg:PSI FPSCR_REG)
4411         (mem:PSI (match_operand:SI 0 "register_operand" "r")))]
4412   "TARGET_SH4 && find_regno_note (insn, REG_DEAD, true_regnum (operands[0]))"
4413   [(set (match_dup 0) (match_dup 0))]
4414   "
4415 {
4416   rtx insn = emit_insn (gen_fpu_switch (get_fpscr_rtx (),
4417                                         gen_rtx (MEM, PSImode,
4418                                                  gen_rtx (POST_INC, Pmode,
4419                                                           operands[0]))));
4420   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, operands[0], NULL_RTX);
4421 }")
4422
4423 (define_split
4424   [(set (reg:PSI FPSCR_REG)
4425         (mem:PSI (match_operand:SI 0 "register_operand" "r")))]
4426   "TARGET_SH4"
4427   [(set (match_dup 0) (plus:SI (match_dup 0) (const_int -4)))]
4428   "
4429 {
4430   rtx insn = emit_insn (gen_fpu_switch (get_fpscr_rtx (),
4431                                         gen_rtx (MEM, PSImode,
4432                                                  gen_rtx (POST_INC, Pmode,
4433                                                           operands[0]))));
4434   REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, operands[0], NULL_RTX);
4435 }")
4436
4437 ;; ??? This uses the fp unit, but has no type indicating that.
4438 ;; If we did that, this would either give a bogus latency or introduce
4439 ;; a bogus FIFO constraint.
4440 ;; Since this insn is currently only used for prologues/epilogues,
4441 ;; it is probably best to claim no function unit, which matches the
4442 ;; current setting.
4443 (define_insn "toggle_sz"
4444   [(set (reg:PSI FPSCR_REG)
4445         (xor:PSI (reg:PSI FPSCR_REG) (const_int 1048576)))]
4446   "TARGET_SH4"
4447   "fschg")
4448
4449 (define_expand "addsf3"
4450   [(match_operand:SF 0 "arith_reg_operand" "")
4451    (match_operand:SF 1 "arith_reg_operand" "")
4452    (match_operand:SF 2 "arith_reg_operand" "")]
4453   "TARGET_SH3E"
4454   "{ expand_sf_binop (&gen_addsf3_i, operands); DONE; }")
4455
4456 (define_insn "addsf3_i"
4457   [(set (match_operand:SF 0 "arith_reg_operand" "=f")
4458         (plus:SF (match_operand:SF 1 "arith_reg_operand" "%0")
4459                  (match_operand:SF 2 "arith_reg_operand" "f")))
4460    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4461   "TARGET_SH3E"
4462   "fadd %2,%0"
4463   [(set_attr "type" "fp")
4464    (set_attr "fp_mode" "single")])
4465
4466 (define_expand "subsf3"
4467   [(match_operand:SF 0 "fp_arith_reg_operand" "")
4468    (match_operand:SF 1 "fp_arith_reg_operand" "")
4469    (match_operand:SF 2 "fp_arith_reg_operand" "")]
4470   "TARGET_SH3E"
4471   "{ expand_sf_binop (&gen_subsf3_i, operands); DONE; }")
4472
4473 (define_insn "subsf3_i"
4474   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4475         (minus:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")
4476                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))
4477    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4478   "TARGET_SH3E"
4479   "fsub %2,%0"
4480   [(set_attr "type" "fp")
4481    (set_attr "fp_mode" "single")])
4482
4483 ;; Unfortunately, the combiner is unable to cope with the USE of the FPSCR
4484 ;; register in feeding fp instructions.  Thus, we cannot generate fmac for
4485 ;; mixed-precision SH4 targets.  To allow it to be still generated for the
4486 ;; SH3E, we use a separate insn for SH3E mulsf3.
4487
4488 (define_expand "mulsf3"
4489   [(match_operand:SF 0 "fp_arith_reg_operand" "")
4490    (match_operand:SF 1 "fp_arith_reg_operand" "")
4491    (match_operand:SF 2 "fp_arith_reg_operand" "")]
4492   "TARGET_SH3E"
4493   "
4494 {
4495   if (TARGET_SH4)
4496     expand_sf_binop (&gen_mulsf3_i4, operands);
4497   else
4498     emit_insn (gen_mulsf3_ie (operands[0], operands[1], operands[2]));
4499   DONE;
4500 }")
4501
4502 (define_insn "mulsf3_i4"
4503   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4504         (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%0")
4505                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))
4506    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4507   "TARGET_SH3E"
4508   "fmul %2,%0"
4509   [(set_attr "type" "fp")
4510    (set_attr "fp_mode" "single")])
4511
4512 (define_insn "mulsf3_ie"
4513   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4514         (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%0")
4515                  (match_operand:SF 2 "fp_arith_reg_operand" "f")))]
4516   "TARGET_SH3E && ! TARGET_SH4"
4517   "fmul %2,%0"
4518   [(set_attr "type" "fp")])
4519
4520 (define_insn "*macsf3"
4521   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4522         (plus:SF (mult:SF (match_operand:SF 1 "fp_arith_reg_operand" "%w")
4523                           (match_operand:SF 2 "fp_arith_reg_operand" "f"))
4524                  (match_operand:SF 3 "arith_reg_operand" "0")))
4525    (use (match_operand:PSI 4 "fpscr_operand" "c"))]
4526   "TARGET_SH3E && ! TARGET_SH4"
4527   "fmac fr0,%2,%0"
4528   [(set_attr "type" "fp")
4529    (set_attr "fp_mode" "single")])
4530
4531 (define_expand "divsf3"
4532   [(match_operand:SF 0 "arith_reg_operand" "")
4533    (match_operand:SF 1 "arith_reg_operand" "")
4534    (match_operand:SF 2 "arith_reg_operand" "")]
4535   "TARGET_SH3E"
4536   "{ expand_sf_binop (&gen_divsf3_i, operands); DONE; }")
4537
4538 (define_insn "divsf3_i"
4539   [(set (match_operand:SF 0 "arith_reg_operand" "=f")
4540         (div:SF (match_operand:SF 1 "arith_reg_operand" "0")
4541                  (match_operand:SF 2 "arith_reg_operand" "f")))
4542    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4543   "TARGET_SH3E"
4544   "fdiv %2,%0"
4545   [(set_attr "type" "fdiv")
4546    (set_attr "fp_mode" "single")])
4547
4548 (define_expand "floatsisf2"
4549   [(set (match_operand:SF 0 "fp_arith_reg_operand" "")
4550         (float:SF (match_operand:SI 1 "fpul_operand" "")))]
4551   "TARGET_SH3E"
4552   "
4553 {
4554   if (TARGET_SH4)
4555     {
4556       emit_sf_insn (gen_floatsisf2_i4 (operands[0], operands[1], get_fpscr_rtx ()));
4557       DONE;
4558     }
4559 }")
4560
4561 (define_insn "floatsisf2_i4"
4562   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4563         (float:SF (match_operand:SI 1 "fpul_operand" "y")))
4564    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4565   "TARGET_SH4"
4566   "float        %1,%0"
4567   [(set_attr "type" "fp")
4568    (set_attr "fp_mode" "single")])
4569
4570 (define_insn "*floatsisf2_ie"
4571   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4572         (float:SF (match_operand:SI 1 "fpul_operand" "y")))]
4573   "TARGET_SH3E && ! TARGET_SH4"
4574   "float        %1,%0"
4575   [(set_attr "type" "fp")])
4576
4577 (define_expand "fix_truncsfsi2"
4578   [(set (match_operand:SI 0 "fpul_operand" "=y")
4579         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
4580   "TARGET_SH3E"
4581   "
4582 {
4583   if (TARGET_SH4)
4584     {
4585       emit_sf_insn (gen_fix_truncsfsi2_i4 (operands[0], operands[1], get_fpscr_rtx ()));
4586       DONE;
4587     }
4588 }")
4589
4590 (define_insn "fix_truncsfsi2_i4"
4591   [(set (match_operand:SI 0 "fpul_operand" "=y")
4592         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))
4593    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4594   "TARGET_SH4"
4595   "ftrc %1,%0"
4596   [(set_attr "type" "fp")
4597    (set_attr "fp_mode" "single")])
4598
4599 ;; ??? This pattern is used nowhere.  fix_truncsfsi2 always expands to
4600 ;; fix_truncsfsi2_i4.
4601 ;; (define_insn "fix_truncsfsi2_i4_2"
4602 ;;  [(set (match_operand:SI 0 "arith_reg_operand" "=r")
4603 ;;      (fix:SI (match_operand:SF 1 "arith_reg_operand" "f")))
4604 ;;   (use (reg:PSI FPSCR_REG))
4605 ;;   (clobber (reg:SI FPUL_REG))]
4606 ;;  "TARGET_SH4"
4607 ;;  "#"
4608 ;;  [(set_attr "length" "4")
4609 ;;   (set_attr "fp_mode" "single")])
4610
4611 ;;(define_split
4612 ;;  [(set (match_operand:SI 0 "arith_reg_operand" "=r")
4613 ;;      (fix:SI (match_operand:SF 1 "arith_reg_operand" "f")))
4614 ;;   (use (match_operand:PSI 2 "fpscr_operand" "c"))
4615 ;;   (clobber (reg:SI FPUL_REG))]
4616 ;;  "TARGET_SH4"
4617 ;;  [(parallel [(set (reg:SI FPUL_REG) (fix:SI (match_dup 1)))
4618 ;;            (use (match_dup 2))])
4619 ;;   (set (match_dup 0) (reg:SI FPUL_REG))])
4620
4621 (define_insn "*fixsfsi"
4622   [(set (match_operand:SI 0 "fpul_operand" "=y")
4623         (fix:SI (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
4624   "TARGET_SH3E && ! TARGET_SH4"
4625   "ftrc %1,%0"
4626   [(set_attr "type" "fp")])
4627
4628 (define_insn "cmpgtsf_t"
4629   [(set (reg:SI T_REG)
4630         (gt:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
4631                (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
4632   "TARGET_SH3E && ! TARGET_SH4"
4633   "fcmp/gt      %1,%0"
4634   [(set_attr "type" "fp")
4635    (set_attr "fp_mode" "single")])
4636
4637 (define_insn "cmpeqsf_t"
4638   [(set (reg:SI T_REG)
4639         (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
4640                (match_operand:SF 1 "fp_arith_reg_operand" "f")))]
4641   "TARGET_SH3E && ! TARGET_SH4"
4642   "fcmp/eq      %1,%0"
4643   [(set_attr "type" "fp")
4644    (set_attr "fp_mode" "single")])
4645
4646 (define_insn "ieee_ccmpeqsf_t"
4647   [(set (reg:SI T_REG)
4648         (ior:SI (reg:SI T_REG)
4649                 (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
4650                        (match_operand:SF 1 "fp_arith_reg_operand" "f"))))]
4651   "TARGET_SH3E && TARGET_IEEE && ! TARGET_SH4"
4652   "* return output_ieee_ccmpeq (insn, operands);"
4653   [(set_attr "length" "4")])
4654
4655
4656 (define_insn "cmpgtsf_t_i4"
4657   [(set (reg:SI T_REG)
4658         (gt:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
4659                (match_operand:SF 1 "fp_arith_reg_operand" "f")))
4660    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4661   "TARGET_SH4"
4662   "fcmp/gt      %1,%0"
4663   [(set_attr "type" "fp")
4664    (set_attr "fp_mode" "single")])
4665
4666 (define_insn "cmpeqsf_t_i4"
4667   [(set (reg:SI T_REG)
4668         (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
4669                (match_operand:SF 1 "fp_arith_reg_operand" "f")))
4670    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4671   "TARGET_SH4"
4672   "fcmp/eq      %1,%0"
4673   [(set_attr "type" "fp")
4674    (set_attr "fp_mode" "single")])
4675
4676 (define_insn "*ieee_ccmpeqsf_t_4"
4677   [(set (reg:SI T_REG)
4678         (ior:SI (reg:SI T_REG)
4679                 (eq:SI (match_operand:SF 0 "fp_arith_reg_operand" "f")
4680                        (match_operand:SF 1 "fp_arith_reg_operand" "f"))))
4681    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4682   "TARGET_IEEE && TARGET_SH4"
4683   "* return output_ieee_ccmpeq (insn, operands);"
4684   [(set_attr "length" "4")
4685    (set_attr "fp_mode" "single")])
4686
4687 (define_expand "cmpsf"
4688   [(set (reg:SI T_REG)
4689         (compare (match_operand:SF 0 "arith_operand" "")
4690                  (match_operand:SF 1 "arith_operand" "")))]
4691   "TARGET_SH3E"
4692   "
4693 {
4694   sh_compare_op0 = operands[0];
4695   sh_compare_op1 = operands[1];
4696   DONE;
4697 }")
4698
4699 (define_expand "negsf2"
4700   [(match_operand:SF 0 "fp_arith_reg_operand" "")
4701    (match_operand:SF 1 "fp_arith_reg_operand" "")]
4702   "TARGET_SH3E"
4703   "{ expand_sf_unop (&gen_negsf2_i, operands); DONE; }")
4704
4705 (define_insn "negsf2_i"
4706   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4707         (neg:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")))
4708    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4709   "TARGET_SH3E"
4710   "fneg %0"
4711   [(set_attr "type" "fmove")
4712    (set_attr "fp_mode" "single")])
4713
4714 (define_expand "sqrtsf2"
4715   [(match_operand:SF 0 "fp_arith_reg_operand" "")
4716    (match_operand:SF 1 "fp_arith_reg_operand" "")]
4717   "TARGET_SH3E"
4718   "{ expand_sf_unop (&gen_sqrtsf2_i, operands); DONE; }")
4719
4720 (define_insn "sqrtsf2_i"
4721   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4722         (sqrt:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")))
4723    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4724   "TARGET_SH3E"
4725   "fsqrt        %0"
4726   [(set_attr "type" "fdiv")
4727    (set_attr "fp_mode" "single")])
4728
4729 (define_expand "abssf2"
4730   [(match_operand:SF 0 "fp_arith_reg_operand" "")
4731    (match_operand:SF 1 "fp_arith_reg_operand" "")]
4732   "TARGET_SH3E"
4733   "{ expand_sf_unop (&gen_abssf2_i, operands); DONE; }")
4734
4735 (define_insn "abssf2_i"
4736   [(set (match_operand:SF 0 "fp_arith_reg_operand" "=f")
4737         (abs:SF (match_operand:SF 1 "fp_arith_reg_operand" "0")))
4738    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4739   "TARGET_SH3E"
4740   "fabs %0"
4741   [(set_attr "type" "fmove")
4742    (set_attr "fp_mode" "single")])
4743
4744 (define_expand "adddf3"
4745   [(match_operand:DF 0 "fp_arith_reg_operand" "")
4746    (match_operand:DF 1 "fp_arith_reg_operand" "")
4747    (match_operand:DF 2 "fp_arith_reg_operand" "")]
4748   "TARGET_SH4"
4749   "{ expand_df_binop (&gen_adddf3_i, operands); DONE; }")
4750
4751 (define_insn "adddf3_i"
4752   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
4753         (plus:DF (match_operand:DF 1 "fp_arith_reg_operand" "%0")
4754                  (match_operand:DF 2 "fp_arith_reg_operand" "f")))
4755    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4756   "TARGET_SH4"
4757   "fadd %2,%0"
4758   [(set_attr "type" "dfp_arith")
4759    (set_attr "fp_mode" "double")])
4760
4761 (define_expand "subdf3"
4762   [(match_operand:DF 0 "fp_arith_reg_operand" "")
4763    (match_operand:DF 1 "fp_arith_reg_operand" "")
4764    (match_operand:DF 2 "fp_arith_reg_operand" "")]
4765   "TARGET_SH4"
4766   "{ expand_df_binop (&gen_subdf3_i, operands); DONE; }")
4767
4768 (define_insn "subdf3_i"
4769   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
4770         (minus:DF (match_operand:DF 1 "fp_arith_reg_operand" "0")
4771                   (match_operand:DF 2 "fp_arith_reg_operand" "f")))
4772    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4773   "TARGET_SH4"
4774   "fsub %2,%0"
4775   [(set_attr "type" "dfp_arith")
4776    (set_attr "fp_mode" "double")])
4777
4778 (define_expand "muldf3"
4779   [(match_operand:DF 0 "fp_arith_reg_operand" "")
4780    (match_operand:DF 1 "fp_arith_reg_operand" "")
4781    (match_operand:DF 2 "fp_arith_reg_operand" "")]
4782   "TARGET_SH4"
4783   "{ expand_df_binop (&gen_muldf3_i, operands); DONE; }")
4784
4785 (define_insn "muldf3_i"
4786   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
4787         (mult:DF (match_operand:DF 1 "fp_arith_reg_operand" "%0")
4788                  (match_operand:DF 2 "fp_arith_reg_operand" "f")))
4789    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4790   "TARGET_SH4"
4791   "fmul %2,%0"
4792   [(set_attr "type" "dfp_arith")
4793    (set_attr "fp_mode" "double")])
4794
4795 (define_expand "divdf3"
4796   [(match_operand:DF 0 "fp_arith_reg_operand" "")
4797    (match_operand:DF 1 "fp_arith_reg_operand" "")
4798    (match_operand:DF 2 "fp_arith_reg_operand" "")]
4799   "TARGET_SH4"
4800   "{ expand_df_binop (&gen_divdf3_i, operands); DONE; }")
4801
4802 (define_insn "divdf3_i"
4803   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
4804         (div:DF (match_operand:DF 1 "fp_arith_reg_operand" "0")
4805                 (match_operand:DF 2 "fp_arith_reg_operand" "f")))
4806    (use (match_operand:PSI 3 "fpscr_operand" "c"))]
4807   "TARGET_SH4"
4808   "fdiv %2,%0"
4809   [(set_attr "type" "dfdiv")
4810    (set_attr "fp_mode" "double")])
4811
4812 (define_expand "floatsidf2"
4813   [(match_operand:DF 0 "fp_arith_reg_operand" "")
4814    (match_operand:SI 1 "fpul_operand" "")]
4815   "TARGET_SH4"
4816   "
4817 {
4818   emit_df_insn (gen_floatsidf2_i (operands[0], operands[1], get_fpscr_rtx ()));
4819   DONE;
4820 }")
4821
4822 (define_insn "floatsidf2_i"
4823   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
4824         (float:DF (match_operand:SI 1 "fpul_operand" "y")))
4825    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4826   "TARGET_SH4"
4827   "float        %1,%0"
4828   [(set_attr "type" "dfp_conv")
4829    (set_attr "fp_mode" "double")])
4830
4831 (define_expand "fix_truncdfsi2"
4832   [(match_operand:SI 0 "fpul_operand" "")
4833    (match_operand:DF 1 "fp_arith_reg_operand" "")]
4834   "TARGET_SH4"
4835   "
4836 {
4837   emit_df_insn (gen_fix_truncdfsi2_i (operands[0], operands[1], get_fpscr_rtx ()));
4838   DONE;
4839 }")
4840
4841 (define_insn "fix_truncdfsi2_i"
4842   [(set (match_operand:SI 0 "fpul_operand" "=y")
4843         (fix:SI (match_operand:DF 1 "fp_arith_reg_operand" "f")))
4844    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4845   "TARGET_SH4"
4846   "ftrc %1,%0"
4847   [(set_attr "type" "dfp_conv")
4848    (set_attr "fp_mode" "double")])
4849
4850 ;; ??? This pattern is used nowhere.  fix_truncdfsi2 always expands to
4851 ;; fix_truncdfsi2_i.
4852 ;; (define_insn "fix_truncdfsi2_i4"
4853 ;;   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
4854 ;;      (fix:SI (match_operand:DF 1 "arith_reg_operand" "f")))
4855 ;;    (use (match_operand:PSI 2 "fpscr_operand" "c"))
4856 ;;    (clobber (reg:SI FPUL_REG))]
4857 ;;   "TARGET_SH4"
4858 ;;   "#"
4859 ;;   [(set_attr "length" "4")
4860 ;;    (set_attr "fp_mode" "double")])
4861 ;; 
4862 ;; (define_split
4863 ;;   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
4864 ;;      (fix:SI (match_operand:DF 1 "arith_reg_operand" "f")))
4865 ;;    (use (match_operand:PSI 2 "fpscr_operand" "c"))
4866 ;;    (clobber (reg:SI FPUL_REG))]
4867 ;;   "TARGET_SH4"
4868 ;;   [(parallel [(set (reg:SI FPUL_REG) (fix:SI (match_dup 1)))
4869 ;;            (use (match_dup 2))])
4870 ;;    (set (match_dup 0) (reg:SI FPUL_REG))])
4871
4872 (define_insn "cmpgtdf_t"
4873   [(set (reg:SI T_REG)
4874         (gt:SI (match_operand:DF 0 "arith_reg_operand" "f")
4875                (match_operand:DF 1 "arith_reg_operand" "f")))
4876    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4877   "TARGET_SH4"
4878   "fcmp/gt      %1,%0"
4879   [(set_attr "type" "dfp_cmp")
4880    (set_attr "fp_mode" "double")])
4881
4882 (define_insn "cmpeqdf_t"
4883   [(set (reg:SI T_REG)
4884         (eq:SI (match_operand:DF 0 "arith_reg_operand" "f")
4885                (match_operand:DF 1 "arith_reg_operand" "f")))
4886    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4887   "TARGET_SH4"
4888   "fcmp/eq      %1,%0"
4889   [(set_attr "type" "dfp_cmp")
4890    (set_attr "fp_mode" "double")])
4891
4892 (define_insn "*ieee_ccmpeqdf_t"
4893   [(set (reg:SI T_REG)
4894         (ior:SI (reg:SI T_REG)
4895                 (eq:SI (match_operand:DF 0 "arith_reg_operand" "f")
4896                        (match_operand:DF 1 "arith_reg_operand" "f"))))
4897    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4898   "TARGET_IEEE && TARGET_SH4"
4899   "* return output_ieee_ccmpeq (insn, operands);"
4900   [(set_attr "length" "4")
4901    (set_attr "fp_mode" "double")])
4902    
4903 (define_expand "cmpdf"
4904   [(set (reg:SI T_REG)
4905         (compare (match_operand:DF 0 "arith_operand" "")
4906                  (match_operand:DF 1 "arith_operand" "")))]
4907   "TARGET_SH4"
4908   "
4909 {
4910   sh_compare_op0 = operands[0];
4911   sh_compare_op1 = operands[1];
4912   DONE;
4913 }")
4914
4915 (define_expand "negdf2"
4916   [(match_operand:DF 0 "arith_reg_operand" "")
4917    (match_operand:DF 1 "arith_reg_operand" "")]
4918   "TARGET_SH4"
4919   "{ expand_df_unop (&gen_negdf2_i, operands); DONE; }")
4920
4921 (define_insn "negdf2_i"
4922   [(set (match_operand:DF 0 "arith_reg_operand" "=f")
4923         (neg:DF (match_operand:DF 1 "arith_reg_operand" "0")))
4924    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4925   "TARGET_SH4"
4926   "fneg %0"
4927   [(set_attr "type" "fmove")
4928    (set_attr "fp_mode" "double")])
4929
4930 (define_expand "sqrtdf2"
4931   [(match_operand:DF 0 "arith_reg_operand" "")
4932    (match_operand:DF 1 "arith_reg_operand" "")]
4933   "TARGET_SH4"
4934   "{ expand_df_unop (&gen_sqrtdf2_i, operands); DONE; }")
4935
4936 (define_insn "sqrtdf2_i"
4937   [(set (match_operand:DF 0 "arith_reg_operand" "=f")
4938         (sqrt:DF (match_operand:DF 1 "arith_reg_operand" "0")))
4939    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4940   "TARGET_SH4"
4941   "fsqrt        %0"
4942   [(set_attr "type" "dfdiv")
4943    (set_attr "fp_mode" "double")])
4944
4945 (define_expand "absdf2"
4946   [(match_operand:DF 0 "arith_reg_operand" "")
4947    (match_operand:DF 1 "arith_reg_operand" "")]
4948   "TARGET_SH4"
4949   "{ expand_df_unop (&gen_absdf2_i, operands); DONE; }")
4950
4951 (define_insn "absdf2_i"
4952   [(set (match_operand:DF 0 "arith_reg_operand" "=f")
4953         (abs:DF (match_operand:DF 1 "arith_reg_operand" "0")))
4954    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4955   "TARGET_SH4"
4956   "fabs %0"
4957   [(set_attr "type" "fmove")
4958    (set_attr "fp_mode" "double")])
4959
4960 (define_expand "extendsfdf2"
4961   [(match_operand:DF 0 "fp_arith_reg_operand" "")
4962    (match_operand:SF 1 "fpul_operand" "")]
4963   "TARGET_SH4"
4964   "
4965 {
4966   emit_df_insn (gen_extendsfdf2_i4 (operands[0], operands[1], get_fpscr_rtx ()));
4967   DONE;
4968 }")
4969
4970 (define_insn "extendsfdf2_i4"
4971   [(set (match_operand:DF 0 "fp_arith_reg_operand" "=f")
4972         (float_extend:DF (match_operand:SF 1 "fpul_operand" "y")))
4973    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4974   "TARGET_SH4"
4975   "fcnvsd  %1,%0"
4976   [(set_attr "type" "fp")
4977    (set_attr "fp_mode" "double")])
4978
4979 (define_expand "truncdfsf2"
4980   [(match_operand:SF 0 "fpul_operand" "")
4981    (match_operand:DF 1 "fp_arith_reg_operand" "")]
4982   "TARGET_SH4"
4983   "
4984 {
4985   emit_df_insn (gen_truncdfsf2_i4 (operands[0], operands[1], get_fpscr_rtx ()));
4986   DONE;
4987 }")
4988
4989 (define_insn "truncdfsf2_i4"
4990   [(set (match_operand:SF 0 "fpul_operand" "=y")
4991         (float_truncate:SF (match_operand:DF 1 "fp_arith_reg_operand" "f")))
4992    (use (match_operand:PSI 2 "fpscr_operand" "c"))]
4993   "TARGET_SH4"
4994   "fcnvds  %1,%0"
4995   [(set_attr "type" "fp")
4996    (set_attr "fp_mode" "double")])
4997 \f
4998 ;; Bit field extract patterns.  These give better code for packed bitfields,
4999 ;; because they allow auto-increment addresses to be generated.
5000
5001 (define_expand "insv"
5002   [(set (zero_extract:SI (match_operand:QI 0 "memory_operand" "")
5003                          (match_operand:SI 1 "immediate_operand" "")
5004                          (match_operand:SI 2 "immediate_operand" ""))
5005         (match_operand:SI 3 "general_operand" ""))]
5006   "! TARGET_LITTLE_ENDIAN"
5007   "
5008 {
5009   rtx addr_target, orig_address, shift_reg;
5010   HOST_WIDE_INT size;
5011
5012   /* ??? expmed doesn't care for non-register predicates.  */
5013   if (! memory_operand (operands[0], VOIDmode)
5014       || ! immediate_operand (operands[1], VOIDmode)
5015       || ! immediate_operand (operands[2], VOIDmode)
5016       || ! general_operand (operands[3], VOIDmode))
5017     FAIL;
5018   /* If this isn't a 16 / 24 / 32 bit field, or if
5019      it doesn't start on a byte boundary, then fail.  */
5020   size = INTVAL (operands[1]);
5021   if (size < 16 || size > 32 || size % 8 != 0
5022       || (INTVAL (operands[2]) % 8) != 0)
5023     FAIL;
5024
5025   size /= 8;
5026   orig_address = XEXP (operands[0], 0);
5027   shift_reg = gen_reg_rtx (SImode);
5028   emit_insn (gen_movsi (shift_reg, operands[3]));
5029   addr_target = copy_addr_to_reg (plus_constant (orig_address, size - 1));
5030
5031   operands[0] = change_address (operands[0], QImode, addr_target);
5032   emit_insn (gen_movqi (operands[0], gen_rtx_SUBREG (QImode, shift_reg, 0)));
5033
5034   while (size -= 1)
5035     {
5036       emit_insn (gen_lshrsi3_k (shift_reg, shift_reg, GEN_INT (8)));
5037       emit_insn (gen_addsi3 (addr_target, addr_target, GEN_INT (-1)));
5038       emit_insn (gen_movqi (operands[0],
5039                             gen_rtx_SUBREG (QImode, shift_reg, 0)));
5040     }
5041
5042   DONE;
5043 }")
5044 \f
5045 ;; -------------------------------------------------------------------------
5046 ;; Peepholes
5047 ;; -------------------------------------------------------------------------
5048
5049 ;; This matches cases where a stack pointer increment at the start of the
5050 ;; epilogue combines with a stack slot read loading the return value.
5051
5052 (define_peephole
5053   [(set (match_operand:SI 0 "arith_reg_operand" "")
5054         (mem:SI (match_operand:SI 1 "arith_reg_operand" "")))
5055    (set (match_dup 1) (plus:SI (match_dup 1) (const_int 4)))]
5056   "REGNO (operands[1]) != REGNO (operands[0])"
5057   "mov.l        @%1+,%0")
5058
5059 ;; See the comment on the dt combiner pattern above.
5060
5061 (define_peephole
5062   [(set (match_operand:SI 0 "arith_reg_operand" "=r")
5063         (plus:SI (match_dup 0)
5064                  (const_int -1)))
5065    (set (reg:SI T_REG)
5066         (eq:SI (match_dup 0)
5067                (const_int 0)))]
5068   "TARGET_SH2"
5069   "dt   %0")
5070
5071 ;; These convert sequences such as `mov #k,r0; add r15,r0; mov.l @r0,rn'
5072 ;; to `mov #k,r0; mov.l @(r0,r15),rn'.  These sequences are generated by
5073 ;; reload when the constant is too large for a reg+offset address.
5074
5075 ;; ??? We would get much better code if this was done in reload.  This would
5076 ;; require modifying find_reloads_address to recognize that if the constant
5077 ;; is out-of-range for an immediate add, then we get better code by reloading
5078 ;; the constant into a register than by reloading the sum into a register,
5079 ;; since the former is one instruction shorter if the address does not need
5080 ;; to be offsettable.  Unfortunately this does not work, because there is
5081 ;; only one register, r0, that can be used as an index register.  This register
5082 ;; is also the function return value register.  So, if we try to force reload
5083 ;; to use double-reg addresses, then we end up with some instructions that
5084 ;; need to use r0 twice.  The only way to fix this is to change the calling
5085 ;; convention so that r0 is not used to return values.
5086
5087 (define_peephole
5088   [(set (match_operand:SI 0 "register_operand" "=r")
5089         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5090    (set (mem:SI (match_dup 0))
5091         (match_operand:SI 2 "general_movsrc_operand" ""))]
5092   "REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
5093   "mov.l        %2,@(%0,%1)")
5094
5095 (define_peephole
5096   [(set (match_operand:SI 0 "register_operand" "=r")
5097         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5098    (set (match_operand:SI 2 "general_movdst_operand" "")
5099         (mem:SI (match_dup 0)))]
5100   "REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
5101   "mov.l        @(%0,%1),%2")
5102
5103 (define_peephole
5104   [(set (match_operand:SI 0 "register_operand" "=r")
5105         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5106    (set (mem:HI (match_dup 0))
5107         (match_operand:HI 2 "general_movsrc_operand" ""))]
5108   "REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
5109   "mov.w        %2,@(%0,%1)")
5110
5111 (define_peephole
5112   [(set (match_operand:SI 0 "register_operand" "=r")
5113         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5114    (set (match_operand:HI 2 "general_movdst_operand" "")
5115         (mem:HI (match_dup 0)))]
5116   "REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
5117   "mov.w        @(%0,%1),%2")
5118
5119 (define_peephole
5120   [(set (match_operand:SI 0 "register_operand" "=r")
5121         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5122    (set (mem:QI (match_dup 0))
5123         (match_operand:QI 2 "general_movsrc_operand" ""))]
5124   "REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
5125   "mov.b        %2,@(%0,%1)")
5126
5127 (define_peephole
5128   [(set (match_operand:SI 0 "register_operand" "=r")
5129         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5130    (set (match_operand:QI 2 "general_movdst_operand" "")
5131         (mem:QI (match_dup 0)))]
5132   "REGNO (operands[0]) == 0 && reg_unused_after (operands[0], insn)"
5133   "mov.b        @(%0,%1),%2")
5134
5135 (define_peephole
5136   [(set (match_operand:SI 0 "register_operand" "=r")
5137         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5138    (set (mem:SF (match_dup 0))
5139         (match_operand:SF 2 "general_movsrc_operand" ""))]
5140   "REGNO (operands[0]) == 0
5141    && ((GET_CODE (operands[2]) == REG && REGNO (operands[2]) < 16)
5142        || (GET_CODE (operands[2]) == SUBREG
5143            && REGNO (SUBREG_REG (operands[2])) < 16))
5144    && reg_unused_after (operands[0], insn)"
5145   "mov.l        %2,@(%0,%1)")
5146
5147 (define_peephole
5148   [(set (match_operand:SI 0 "register_operand" "=r")
5149         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5150    (set (match_operand:SF 2 "general_movdst_operand" "")
5151
5152         (mem:SF (match_dup 0)))]
5153   "REGNO (operands[0]) == 0
5154    && ((GET_CODE (operands[2]) == REG && REGNO (operands[2]) < 16)
5155        || (GET_CODE (operands[2]) == SUBREG
5156            && REGNO (SUBREG_REG (operands[2])) < 16))
5157    && reg_unused_after (operands[0], insn)"
5158   "mov.l        @(%0,%1),%2")
5159
5160 (define_peephole
5161   [(set (match_operand:SI 0 "register_operand" "=r")
5162         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5163    (set (mem:SF (match_dup 0))
5164         (match_operand:SF 2 "general_movsrc_operand" ""))]
5165   "REGNO (operands[0]) == 0
5166    && ((GET_CODE (operands[2]) == REG
5167         && FP_OR_XD_REGISTER_P (REGNO (operands[2])))
5168        || (GET_CODE (operands[2]) == SUBREG
5169            && FP_OR_XD_REGISTER_P (REGNO (SUBREG_REG (operands[2])))))
5170    && reg_unused_after (operands[0], insn)"
5171   "fmov{.s|}    %2,@(%0,%1)")
5172
5173 (define_peephole
5174   [(set (match_operand:SI 0 "register_operand" "=r")
5175         (plus:SI (match_dup 0) (match_operand:SI 1 "register_operand" "r")))
5176    (set (match_operand:SF 2 "general_movdst_operand" "")
5177
5178         (mem:SF (match_dup 0)))]
5179   "REGNO (operands[0]) == 0
5180    && ((GET_CODE (operands[2]) == REG
5181         && FP_OR_XD_REGISTER_P (REGNO (operands[2])))
5182        || (GET_CODE (operands[2]) == SUBREG
5183            && FP_OR_XD_REGISTER_P (REGNO (SUBREG_REG (operands[2])))))
5184    && reg_unused_after (operands[0], insn)"
5185   "fmov{.s|}    @(%0,%1),%2")
5186
5187 ;; Switch to a new stack with its address in sp_switch (a SYMBOL_REF).  */
5188 (define_insn "sp_switch_1"
5189   [(const_int 1)]
5190   ""
5191   "*
5192 {
5193   rtx xoperands[1];
5194
5195   xoperands[0] = sp_switch;
5196   output_asm_insn (\"mov.l r0,@-r15\;mov.l %0,r0\", xoperands);
5197   output_asm_insn (\"mov.l @r0,r0\;mov.l r15,@-r0\", xoperands);
5198   return \"mov r0,r15\";
5199 }"
5200   [(set_attr "length" "10")])
5201
5202 ;; Switch back to the original stack for interrupt functions with the
5203 ;; sp_switch attribute.  */
5204 (define_insn "sp_switch_2"
5205   [(const_int 2)]
5206   ""
5207   "mov.l @r15+,r15\;mov.l @r15+,r0"
5208   [(set_attr "length" "4")])