OSDN Git Service

Fix alpha-x-m32r-elf bugs.
[pf3gnuchains/gcc-fork.git] / gcc / config / m32r / m32r.md
1 ;; Machine description of the Mitsubishi M32R cpu for GNU C compiler
2 ;; Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU CC.
5
6 ;; GNU CC is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2, or (at your option)
9 ;; any later version.
10
11 ;; GNU CC is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU CC; see the file COPYING.  If not, write to
18 ;; the Free Software Foundation, 59 Temple Place - Suite 330,
19 ;; Boston, MA 02111-1307, USA.
20
21 ;; See file "rtl.def" for documentation on define_insn, match_*, et. al.
22
23 ;; unspec usage
24 ;; 0 - blockage
25 ;; 1 - flush_icache
26 ;; 2 - load_sda_base
27 ;; 3 - setting carry in addx/subx instructions.
28 \f
29 ;; Insn type.  Used to default other attribute values.
30 (define_attr "type"
31   "int2,int4,load2,load4,load8,store2,store4,store8,shift2,shift4,mul2,div4,uncond_branch,branch,call,multi,misc"
32   (const_string "misc"))
33
34 ;; Length in bytes.
35 (define_attr "length" ""
36   (cond [(eq_attr "type" "int2,load2,store2,shift2,mul2")
37          (const_int 2)
38
39          (eq_attr "type" "int4,load4,store4,shift4,div4")
40          (const_int 4)
41
42          (eq_attr "type" "multi")
43          (const_int 8)
44
45          (eq_attr "type" "uncond_branch,branch,call")
46          (const_int 4)]
47
48          (const_int 4)))
49
50 ;; The length here is the length of a single asm.  Unfortunately it might be
51 ;; 2 or 4 so we must allow for 4.  That's ok though.
52 (define_asm_attributes
53   [(set_attr "length" "4")
54    (set_attr "type" "multi")])
55
56
57 ;; Whether an instruction is 16-bit or 32-bit
58 (define_attr "insn_size" "short,long"
59   (if_then_else (eq_attr "type" "int2,load2,store2,shift2,mul2")
60                 (const_string "short")
61                 (const_string "long")))
62
63 (define_attr "debug" "no,yes"
64   (const (symbol_ref "(TARGET_DEBUG != 0)")))
65
66 (define_attr "opt_size" "no,yes"
67   (const (symbol_ref "(optimize_size != 0)")))
68
69 (define_attr "m32r" "no,yes"
70   (const (symbol_ref "(TARGET_M32R != 0)")))
71
72
73
74 \f
75 ;; ::::::::::::::::::::
76 ;; ::
77 ;; :: Function Units
78 ;; ::
79 ;; ::::::::::::::::::::
80
81 ;; On most RISC machines, there are instructions whose results are not
82 ;; available for a specific number of cycles.  Common cases are instructions
83 ;; that load data from memory.  On many machines, a pipeline stall will result
84 ;; if the data is referenced too soon after the load instruction.
85
86 ;; In addition, many newer microprocessors have multiple function units,
87 ;; usually one for integer and one for floating point, and often will incur
88 ;; pipeline stalls when a result that is needed is not yet ready.
89
90 ;; The descriptions in this section allow the specification of how much time
91 ;; must elapse between the execution of an instruction and the time when its
92 ;; result is used.  It also allows specification of when the execution of an
93 ;; instruction will delay execution of similar instructions due to function
94 ;; unit conflicts.
95
96 ;; For the purposes of the specifications in this section, a machine is divided
97 ;; into "function units", each of which execute a specific class of
98 ;; instructions in first-in-first-out order.  Function units that accept one
99 ;; instruction each cycle and allow a result to be used in the succeeding
100 ;; instruction (usually via forwarding) need not be specified.  Classic RISC
101 ;; microprocessors will normally have a single function unit, which we can call
102 ;; `memory'.  The newer "superscalar" processors will often have function units
103 ;; for floating point operations, usually at least a floating point adder and
104 ;; multiplier.
105
106 ;; Each usage of a function units by a class of insns is specified with a
107 ;; `define_function_unit' expression, which looks like this:
108
109 ;; (define_function_unit NAME MULTIPLICITY SIMULTANEITY TEST READY-DELAY
110 ;;   ISSUE-DELAY [CONFLICT-LIST])
111
112 ;; NAME is a string giving the name of the function unit.
113
114 ;; MULTIPLICITY is an integer specifying the number of identical units in the
115 ;; processor.  If more than one unit is specified, they will be scheduled
116 ;; independently.  Only truly independent units should be counted; a pipelined
117 ;; unit should be specified as a single unit.  (The only common example of a
118 ;; machine that has multiple function units for a single instruction class that
119 ;; are truly independent and not pipelined are the two multiply and two
120 ;; increment units of the CDC 6600.)
121
122 ;; SIMULTANEITY specifies the maximum number of insns that can be executing in
123 ;; each instance of the function unit simultaneously or zero if the unit is
124 ;; pipelined and has no limit.
125
126 ;; All `define_function_unit' definitions referring to function unit NAME must
127 ;; have the same name and values for MULTIPLICITY and SIMULTANEITY.
128
129 ;; TEST is an attribute test that selects the insns we are describing in this
130 ;; definition.  Note that an insn may use more than one function unit and a
131 ;; function unit may be specified in more than one `define_function_unit'.
132
133 ;; READY-DELAY is an integer that specifies the number of cycles after which
134 ;; the result of the instruction can be used without introducing any stalls.
135
136 ;; ISSUE-DELAY is an integer that specifies the number of cycles after the
137 ;; instruction matching the TEST expression begins using this unit until a
138 ;; subsequent instruction can begin.  A cost of N indicates an N-1 cycle delay.
139 ;; A subsequent instruction may also be delayed if an earlier instruction has a
140 ;; longer READY-DELAY value.  This blocking effect is computed using the
141 ;; SIMULTANEITY, READY-DELAY, ISSUE-DELAY, and CONFLICT-LIST terms.  For a
142 ;; normal non-pipelined function unit, SIMULTANEITY is one, the unit is taken
143 ;; to block for the READY-DELAY cycles of the executing insn, and smaller
144 ;; values of ISSUE-DELAY are ignored.
145
146 ;; CONFLICT-LIST is an optional list giving detailed conflict costs for this
147 ;; unit.  If specified, it is a list of condition test expressions to be
148 ;; applied to insns chosen to execute in NAME following the particular insn
149 ;; matching TEST that is already executing in NAME.  For each insn in the list,
150 ;; ISSUE-DELAY specifies the conflict cost; for insns not in the list, the cost
151 ;; is zero.  If not specified, CONFLICT-LIST defaults to all instructions that
152 ;; use the function unit.
153
154 ;; Typical uses of this vector are where a floating point function unit can
155 ;; pipeline either single- or double-precision operations, but not both, or
156 ;; where a memory unit can pipeline loads, but not stores, etc.
157
158 ;; As an example, consider a classic RISC machine where the result of a load
159 ;; instruction is not available for two cycles (a single "delay" instruction is
160 ;; required) and where only one load instruction can be executed
161 ;; simultaneously.  This would be specified as:
162
163 ;; (define_function_unit "memory" 1 1 (eq_attr "type" "load") 2 0)
164
165 ;; For the case of a floating point function unit that can pipeline
166 ;; either single or double precision, but not both, the following could be
167 ;; specified:
168 ;;
169 ;; (define_function_unit "fp" 1 0
170 ;;   (eq_attr "type" "sp_fp") 4 4
171 ;;   [(eq_attr "type" "dp_fp")])
172 ;;
173 ;; (define_function_unit "fp" 1 0
174 ;;   (eq_attr "type" "dp_fp") 4 4
175 ;;   [(eq_attr "type" "sp_fp")])
176
177 ;; Note: The scheduler attempts to avoid function unit conflicts and uses all
178 ;; the specifications in the `define_function_unit' expression.  It has
179 ;; recently come to our attention that these specifications may not allow
180 ;; modeling of some of the newer "superscalar" processors that have insns using
181 ;; multiple pipelined units.  These insns will cause a potential conflict for
182 ;; the second unit used during their execution and there is no way of
183 ;; representing that conflict.  We welcome any examples of how function unit
184 ;; conflicts work in such processors and suggestions for their representation.
185
186 ;; Function units of the M32R
187 ;; Units that take one cycle do not need to be specified.
188
189 ;; (define_function_unit {name} {multiplicity} {simulataneity} {test}
190 ;;                       {ready-delay} {issue-delay} [{conflict-list}])
191
192 ;; Hack to get GCC to better pack the instructions.
193 ;; We pretend there is a separate long function unit that conflicts with
194 ;; both the left and right 16 bit insn slots.
195
196 (define_function_unit "short" 2 2
197   (and (eq_attr "m32r" "yes")
198        (and (eq_attr "insn_size" "short")
199             (eq_attr "type" "!load2")))
200   1 0
201   [(eq_attr "insn_size" "long")])
202
203 (define_function_unit "short" 2 2       ;; load delay of 1 clock for mem execution + 1 clock for WB
204   (and (eq_attr "m32r" "yes")
205        (eq_attr "type" "load2"))
206   3 0
207   [(eq_attr "insn_size" "long")])
208
209 (define_function_unit "long" 1 1
210   (and (eq_attr "m32r" "yes")
211        (and (eq_attr "insn_size" "long")
212             (eq_attr "type" "!load4,load8")))
213   1 0
214   [(eq_attr "insn_size" "short")])
215
216 (define_function_unit "long" 1 1        ;; load delay of 1 clock for mem execution + 1 clock for WB
217   (and (eq_attr "m32r" "yes")
218        (and (eq_attr "insn_size" "long")
219             (eq_attr "type" "load4,load8")))
220   3 0
221   [(eq_attr "insn_size" "short")])
222
223
224 \f
225 ;; Instruction grouping
226
227 \f
228 ;; Expand prologue as RTL
229 (define_expand "prologue"
230   [(const_int 1)]
231   ""
232   "
233 {
234   m32r_expand_prologue ();
235   DONE;
236 }")
237
238 \f
239 ;; Move instructions.
240 ;;
241 ;; For QI and HI moves, the register must contain the full properly
242 ;; sign-extended value.  nonzero_bits assumes this [otherwise
243 ;; SHORT_IMMEDIATES_SIGN_EXTEND must be used, but the comment for it
244 ;; says it's a kludge and the .md files should be fixed instead].
245
246 (define_expand "movqi"
247   [(set (match_operand:QI 0 "general_operand" "")
248         (match_operand:QI 1 "general_operand" ""))]
249   ""
250   "
251 {
252   /* Everything except mem = const or mem = mem can be done easily.
253      Objects in the small data area are handled too.  */
254
255   if (GET_CODE (operands[0]) == MEM)
256     operands[1] = force_reg (QImode, operands[1]);
257 }")
258
259 (define_insn "*movqi_insn"
260   [(set (match_operand:QI 0 "move_dest_operand" "=r,r,r,r,r,T,m")
261         (match_operand:QI 1 "move_src_operand" "r,I,JQR,T,m,r,r"))]
262   "register_operand (operands[0], QImode) || register_operand (operands[1], QImode)"
263   "@
264    mv %0,%1
265    ldi %0,%#%1
266    ldi %0,%#%1
267    ldub %0,%1
268    ldub %0,%1
269    stb %1,%0
270    stb %1,%0"
271   [(set_attr "type" "int2,int2,int4,load2,load4,store2,store4")
272    (set_attr "length" "2,2,4,2,4,2,4")])
273
274 (define_expand "movhi"
275   [(set (match_operand:HI 0 "general_operand" "")
276         (match_operand:HI 1 "general_operand" ""))]
277   ""
278   "
279 {
280   /* Everything except mem = const or mem = mem can be done easily.  */
281
282   if (GET_CODE (operands[0]) == MEM)
283     operands[1] = force_reg (HImode, operands[1]);
284 }")
285
286 (define_insn "*movhi_insn"
287   [(set (match_operand:HI 0 "move_dest_operand" "=r,r,r,r,r,r,T,m")
288         (match_operand:HI 1 "move_src_operand" "r,I,JQR,K,T,m,r,r"))]
289   "register_operand (operands[0], HImode) || register_operand (operands[1], HImode)"
290   "@
291    mv %0,%1
292    ldi %0,%#%1
293    ldi %0,%#%1
294    ld24 %0,%#%1
295    lduh %0,%1
296    lduh %0,%1
297    sth %1,%0
298    sth %1,%0"
299   [(set_attr "type" "int2,int2,int4,int4,load2,load4,store2,store4")
300    (set_attr "length" "2,2,4,4,2,4,2,4")])
301
302 (define_expand "movsi_push"
303   [(set (mem:SI (pre_dec:SI (match_operand:SI 0 "register_operand" "")))
304         (match_operand:SI 1 "register_operand" ""))]
305   ""
306   "")
307
308 (define_expand "movsi_pop"
309   [(set (match_operand:SI 0 "register_operand" "")
310         (mem:SI (post_inc:SI (match_operand:SI 1 "register_operand" ""))))]
311   ""
312   "")
313
314 (define_expand "movsi"
315   [(set (match_operand:SI 0 "general_operand" "")
316         (match_operand:SI 1 "general_operand" ""))]
317   ""
318   "
319 {
320   /* Everything except mem = const or mem = mem can be done easily.  */
321
322   if (GET_CODE (operands[0]) == MEM)
323     operands[1] = force_reg (SImode, operands[1]);
324
325   /* Small Data Area reference?  */
326   if (small_data_operand (operands[1], SImode))
327     {
328       emit_insn (gen_movsi_sda (operands[0], operands[1]));
329       DONE;
330     }
331
332   /* If medium or large code model, symbols have to be loaded with
333      seth/add3.  */
334   if (addr32_operand (operands[1], SImode))
335     {
336       emit_insn (gen_movsi_addr32 (operands[0], operands[1]));
337       DONE;
338     }
339 }")
340
341 ;; ??? Do we need a const_double constraint here for large unsigned values?
342 (define_insn "*movsi_insn"
343   [(set (match_operand:SI 0 "move_dest_operand" "=r,r,r,r,r,r,r,r,r,T,U,m")
344         (match_operand:SI 1 "move_src_operand" "r,I,J,MQ,L,n,T,U,m,r,r,r"))]
345   "register_operand (operands[0], SImode) || register_operand (operands[1], SImode)"
346   "*
347 {
348   if (GET_CODE (operands[0]) == REG || GET_CODE (operands[1]) == SUBREG)
349     {
350       switch (GET_CODE (operands[1]))
351         {
352           HOST_WIDE_INT value;
353
354           default:
355             break;
356
357           case REG:
358           case SUBREG:
359             return \"mv %0,%1\";
360
361           case MEM:
362             return \"ld %0,%1\";
363
364           case CONST_INT:
365             value = INTVAL (operands[1]);
366             if (INT16_P (value))
367               return \"ldi %0,%#%1\\t; %X1\";
368
369             if (UINT24_P (value))
370               return \"ld24 %0,%#%1\\t; %X1\";
371
372             if (UPPER16_P (value))
373               return \"seth %0,%#%T1\\t; %X1\";
374
375             return \"#\";
376
377           case CONST:
378           case SYMBOL_REF:
379           case LABEL_REF:
380             if (TARGET_ADDR24)
381               return \"ld24 %0,%#%1\";
382
383             return \"#\";
384         }
385     }
386
387   else if (GET_CODE (operands[0]) == MEM
388            && (GET_CODE (operands[1]) == REG || GET_CODE (operands[1]) == SUBREG))
389     return \"st %1,%0\";
390
391   fatal_insn (\"bad movsi insn\", insn);
392 }"
393   [(set_attr "type" "int2,int2,int4,int4,int4,multi,load2,load2,load4,store2,store2,store4")
394    (set_attr "length" "2,2,4,4,4,8,2,2,4,2,2,4")])
395
396 ; Try to use a four byte / two byte pair for constants not loadable with
397 ; ldi, ld24, seth.
398
399 (define_split
400  [(set (match_operand:SI 0 "register_operand" "")
401        (match_operand:SI 1 "two_insn_const_operand" ""))]
402   ""
403   [(set (match_dup 0) (match_dup 2))
404    (set (match_dup 0) (ior:SI (match_dup 0) (match_dup 3)))]
405   "
406 {
407   unsigned HOST_WIDE_INT val = INTVAL (operands[1]);
408   unsigned HOST_WIDE_INT tmp;
409   int shift;
410
411   /* In all cases we will emit two instructions.  However we try to
412      use 2 byte instructions wherever possible.  We can assume the
413      constant isn't loadable with any of ldi, ld24, or seth.  */
414
415   /* See if we can load a 24 bit unsigned value and invert it.  */
416   if (UINT24_P (~ val))
417     {
418       emit_insn (gen_movsi (operands[0], GEN_INT (~ val)));
419       emit_insn (gen_one_cmplsi2 (operands[0], operands[0]));
420       DONE;
421     }
422
423   /* See if we can load a 24 bit unsigned value and shift it into place.
424      0x01fffffe is just beyond ld24's range.  */
425   for (shift = 1, tmp = 0x01fffffe;
426        shift < 8;
427        ++shift, tmp <<= 1)
428     {
429       if ((val & ~tmp) == 0)
430         {
431           emit_insn (gen_movsi (operands[0], GEN_INT (val >> shift)));
432           emit_insn (gen_ashlsi3 (operands[0], operands[0], GEN_INT (shift)));
433           DONE;
434         }
435     }
436
437   /* Can't use any two byte insn, fall back to seth/or3.  Use ~0xffff instead
438      of 0xffff0000, since the later fails on a 64-bit host.  */
439   operands[2] = GEN_INT ((val) & ~0xffff);
440   operands[3] = GEN_INT ((val) & 0xffff);
441 }")
442
443 (define_split
444   [(set (match_operand:SI 0 "register_operand" "")
445         (match_operand:SI 1 "seth_add3_operand" "i"))]
446   "TARGET_ADDR32"
447   [(set (match_dup 0)
448         (high:SI (match_dup 1)))
449    (set (match_dup 0)
450         (lo_sum:SI (match_dup 0)
451                    (match_dup 1)))]
452   "")
453
454 ;; Small data area support.
455 ;; The address of _SDA_BASE_ is loaded into a register and all objects in
456 ;; the small data area are indexed off that.  This is done for each reference
457 ;; but cse will clean things up for us.  We let the compiler choose the
458 ;; register to use so we needn't allocate (and maybe even fix) a special
459 ;; register to use.  Since the load and store insns have a 16 bit offset the
460 ;; total size of the data area can be 64K.  However, if the data area lives
461 ;; above 16M (24 bits), _SDA_BASE_ will have to be loaded with seth/add3 which
462 ;; would then yield 3 instructions to reference an object [though there would
463 ;; be no net loss if two or more objects were referenced].  The 3 insns can be
464 ;; reduced back to 2 if the size of the small data area were reduced to 32K
465 ;; [then seth + ld/st would work for any object in the area].  Doing this
466 ;; would require special handling of _SDA_BASE_ (its value would be
467 ;; (.sdata + 32K) & 0xffff0000) and reloc computations would be different
468 ;; [I think].  What to do about this is deferred until later and for now we
469 ;; require .sdata to be in the first 16M.
470
471 (define_expand "movsi_sda"
472   [(set (match_dup 2)
473         (unspec [(const_int 0)] 2))
474    (set (match_operand:SI 0 "register_operand" "")
475         (lo_sum:SI (match_dup 2)
476                    (match_operand:SI 1 "small_data_operand" "")))]
477   ""
478   "
479 {
480   if (reload_in_progress || reload_completed)
481     operands[2] = operands[0];
482   else
483     operands[2] = gen_reg_rtx (SImode);
484 }")
485
486 (define_insn "*load_sda_base"
487   [(set (match_operand:SI 0 "register_operand" "=r")
488         (unspec [(const_int 0)] 2))]
489   ""
490   "ld24 %0,#_SDA_BASE_"
491   [(set_attr "type" "int4")
492    (set_attr "length" "4")])
493
494 ;; 32 bit address support.
495
496 (define_expand "movsi_addr32"
497   [(set (match_dup 2)
498         ; addr32_operand isn't used because it's too restrictive,
499         ; seth_add3_operand is more general and thus safer.
500         (high:SI (match_operand:SI 1 "seth_add3_operand" "")))
501    (set (match_operand:SI 0 "register_operand" "")
502         (lo_sum:SI (match_dup 2) (match_dup 1)))]
503   ""
504   "
505 {
506   if (reload_in_progress || reload_completed)
507     operands[2] = operands[0];
508   else
509     operands[2] = gen_reg_rtx (SImode);
510 }")
511
512 (define_insn "set_hi_si"
513   [(set (match_operand:SI 0 "register_operand" "=r")
514         (high:SI (match_operand 1 "symbolic_operand" "")))]
515   ""
516   "seth %0,%#shigh(%1)"
517   [(set_attr "type" "int4")
518    (set_attr "length" "4")])
519
520 (define_insn "lo_sum_si"
521   [(set (match_operand:SI 0 "register_operand" "=r")
522         (lo_sum:SI (match_operand:SI 1 "register_operand" "r")
523                    (match_operand:SI 2 "immediate_operand" "in")))]
524   ""
525   "add3 %0,%1,%#%B2"
526   [(set_attr "type" "int4")
527    (set_attr "length" "4")])
528
529 (define_expand "movdi"
530   [(set (match_operand:DI 0 "general_operand" "")
531         (match_operand:DI 1 "general_operand" ""))]
532   ""
533   "
534 {
535   /* Everything except mem = const or mem = mem can be done easily.  */
536
537   if (GET_CODE (operands[0]) == MEM)
538     operands[1] = force_reg (DImode, operands[1]);
539 }")
540
541 (define_insn "*movdi_insn"
542   [(set (match_operand:DI 0 "move_dest_operand" "=r,r,r,r,m")
543         (match_operand:DI 1 "move_double_src_operand" "r,nG,F,m,r"))]
544   "register_operand (operands[0], DImode) || register_operand (operands[1], DImode)"
545   "#"
546   [(set_attr "type" "multi,multi,multi,load8,store8")
547    (set_attr "length" "4,4,16,6,6")])
548
549 (define_split
550   [(set (match_operand:DI 0 "move_dest_operand" "")
551         (match_operand:DI 1 "move_double_src_operand" ""))]
552   "reload_completed"
553   [(match_dup 2)]
554   "operands[2] = gen_split_move_double (operands);")
555 \f
556 ;; Floating point move insns.
557
558 (define_expand "movsf"
559   [(set (match_operand:SF 0 "general_operand" "")
560         (match_operand:SF 1 "general_operand" ""))]
561   ""
562   "
563 {
564   /* Everything except mem = const or mem = mem can be done easily.  */
565
566   if (GET_CODE (operands[0]) == MEM)
567     operands[1] = force_reg (SFmode, operands[1]);
568 }")
569
570 (define_insn "*movsf_insn"
571   [(set (match_operand:SF 0 "move_dest_operand" "=r,r,r,r,T,m")
572         (match_operand:SF 1 "move_src_operand" "r,F,T,m,r,r"))]
573   "register_operand (operands[0], SFmode) || register_operand (operands[1], SFmode)"
574   "@
575    mv %0,%1
576    #
577    ld %0,%1
578    ld %0,%1
579    st %1,%0
580    st %1,%0"
581   ;; ??? Length of alternative 1 is either 2, 4 or 8.
582   [(set_attr "type" "int2,multi,load2,load4,store2,store4")
583    (set_attr "length" "2,8,2,4,2,4")])
584
585 (define_split
586   [(set (match_operand:SF 0 "register_operand" "")
587         (match_operand:SF 1 "const_double_operand" ""))]
588   "reload_completed"
589   [(set (match_dup 2) (match_dup 3))]
590   "
591 {
592   long l;
593   REAL_VALUE_TYPE rv;
594
595   REAL_VALUE_FROM_CONST_DOUBLE (rv, operands[1]);
596   REAL_VALUE_TO_TARGET_SINGLE (rv, l);
597
598   operands[2] = operand_subword (operands[0], 0, 0, SFmode);
599   operands[3] = GEN_INT (l);
600 }")
601
602 (define_expand "movdf"
603   [(set (match_operand:DF 0 "general_operand" "")
604         (match_operand:DF 1 "general_operand" ""))]
605   ""
606   "
607 {
608   /* Everything except mem = const or mem = mem can be done easily.  */
609
610   if (GET_CODE (operands[0]) == MEM)
611     operands[1] = force_reg (DFmode, operands[1]);
612 }")
613
614 (define_insn "*movdf_insn"
615   [(set (match_operand:DF 0 "move_dest_operand" "=r,r,r,m")
616         (match_operand:DF 1 "move_double_src_operand" "r,F,m,r"))]
617   "register_operand (operands[0], DFmode) || register_operand (operands[1], DFmode)"
618   "#"
619   [(set_attr "type" "multi,multi,load8,store8")
620    (set_attr "length" "4,16,6,6")])
621
622 (define_split
623   [(set (match_operand:DF 0 "move_dest_operand" "")
624         (match_operand:DF 1 "move_double_src_operand" ""))]
625   "reload_completed"
626   [(match_dup 2)]
627   "operands[2] = gen_split_move_double (operands);")
628 \f
629 ;; Zero extension instructions.
630
631 (define_insn "zero_extendqihi2"
632   [(set (match_operand:HI 0 "register_operand" "=r,r,r")
633         (zero_extend:HI (match_operand:QI 1 "nonimmediate_operand" "r,T,m")))]
634   ""
635   "@
636    and3 %0,%1,%#255
637    ldub %0,%1
638    ldub %0,%1"
639   [(set_attr "type" "int4,load2,load4")
640    (set_attr "length" "4,2,4")])
641
642 (define_insn "zero_extendqisi2"
643   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
644         (zero_extend:SI (match_operand:QI 1 "nonimmediate_operand" "r,T,m")))]
645   ""
646   "@
647    and3 %0,%1,%#255
648    ldub %0,%1
649    ldub %0,%1"
650   [(set_attr "type" "int4,load2,load4")
651    (set_attr "length" "4,2,4")])
652
653 (define_insn "zero_extendhisi2"
654   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
655         (zero_extend:SI (match_operand:HI 1 "nonimmediate_operand" "r,T,m")))]
656   ""
657   "@
658    and3 %0,%1,%#65535
659    lduh %0,%1
660    lduh %0,%1"
661   [(set_attr "type" "int4,load2,load4")
662    (set_attr "length" "4,2,4")])
663 \f
664 ;; Sign extension instructions.
665 ;; ??? See v850.md.
666
667 ;; These patterns originally accepted general_operands, however, slightly
668 ;; better code is generated by only accepting register_operands, and then
669 ;; letting combine generate the lds[hb] insns.
670 ;; [This comment copied from sparc.md, I think.]
671
672 (define_expand "extendqihi2"
673   [(set (match_operand:HI 0 "register_operand" "")
674         (sign_extend:HI (match_operand:QI 1 "register_operand" "")))]
675   ""
676   "
677 {
678   rtx temp = gen_reg_rtx (SImode);
679   rtx shift_24 = GEN_INT (24);
680   int op1_subword = 0;
681   int op0_subword = 0;
682
683   if (GET_CODE (operand1) == SUBREG)
684     {
685       op1_subword = SUBREG_WORD (operand1);
686       operand1 = XEXP (operand1, 0);
687     }
688   if (GET_CODE (operand0) == SUBREG)
689     {
690       op0_subword = SUBREG_WORD (operand0);
691       operand0 = XEXP (operand0, 0);
692     }
693   emit_insn (gen_ashlsi3 (temp, gen_rtx (SUBREG, SImode, operand1,
694                                          op1_subword),
695                           shift_24));
696   if (GET_MODE (operand0) != SImode)
697     operand0 = gen_rtx (SUBREG, SImode, operand0, op0_subword);
698   emit_insn (gen_ashrsi3 (operand0, temp, shift_24));
699   DONE;
700 }")
701
702 (define_insn "*sign_extendqihi2_insn"
703   [(set (match_operand:HI 0 "register_operand" "=r,r")
704         (sign_extend:HI (match_operand:QI 1 "memory_operand" "T,m")))]
705   ""
706   "ldb %0,%1"
707   [(set_attr "type" "load2,load4")
708    (set_attr "length" "2,4")])
709
710 (define_expand "extendqisi2"
711   [(set (match_operand:SI 0 "register_operand" "")
712         (sign_extend:SI (match_operand:QI 1 "register_operand" "")))]
713   ""
714   "
715 {
716   rtx temp = gen_reg_rtx (SImode);
717   rtx shift_24 = GEN_INT (24);
718   int op1_subword = 0;
719
720   if (GET_CODE (operand1) == SUBREG)
721     {
722       op1_subword = SUBREG_WORD (operand1);
723       operand1 = XEXP (operand1, 0);
724     }
725
726   emit_insn (gen_ashlsi3 (temp, gen_rtx (SUBREG, SImode, operand1,
727                                          op1_subword),
728                           shift_24));
729   emit_insn (gen_ashrsi3 (operand0, temp, shift_24));
730   DONE;
731 }")
732
733 (define_insn "*sign_extendqisi2_insn"
734   [(set (match_operand:SI 0 "register_operand" "=r,r")
735         (sign_extend:SI (match_operand:QI 1 "memory_operand" "T,m")))]
736   ""
737   "ldb %0,%1"
738   [(set_attr "type" "load2,load4")
739    (set_attr "length" "2,4")])
740
741 (define_expand "extendhisi2"
742   [(set (match_operand:SI 0 "register_operand" "")
743         (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
744   ""
745   "
746 {
747   rtx temp = gen_reg_rtx (SImode);
748   rtx shift_16 = GEN_INT (16);
749   int op1_subword = 0;
750
751   if (GET_CODE (operand1) == SUBREG)
752     {
753       op1_subword = SUBREG_WORD (operand1);
754       operand1 = XEXP (operand1, 0);
755     }
756
757   emit_insn (gen_ashlsi3 (temp, gen_rtx (SUBREG, SImode, operand1,
758                                          op1_subword),
759                           shift_16));
760   emit_insn (gen_ashrsi3 (operand0, temp, shift_16));
761   DONE;
762 }")
763
764 (define_insn "*sign_extendhisi2_insn"
765   [(set (match_operand:SI 0 "register_operand" "=r,r")
766         (sign_extend:SI (match_operand:HI 1 "memory_operand" "T,m")))]
767   ""
768   "ldh %0,%1"
769   [(set_attr "type" "load2,load4")
770    (set_attr "length" "2,4")])
771 \f
772 ;; Arithmetic instructions.
773
774 ; ??? Adding an alternative to split add3 of small constants into two
775 ; insns yields better instruction packing but slower code.  Adds of small
776 ; values is done a lot.
777
778 (define_insn "addsi3"
779   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
780         (plus:SI (match_operand:SI 1 "register_operand" "%0,0,r")
781                  (match_operand:SI 2 "nonmemory_operand" "r,I,J")))]
782   ""
783   "@
784    add %0,%2
785    addi %0,%#%2
786    add3 %0,%1,%#%2"
787   [(set_attr "type" "int2,int2,int4")
788    (set_attr "length" "2,2,4")])
789
790 ;(define_split
791 ;  [(set (match_operand:SI 0 "register_operand" "")
792 ;       (plus:SI (match_operand:SI 1 "register_operand" "")
793 ;                (match_operand:SI 2 "int8_operand" "")))]
794 ;  "reload_completed
795 ;   && REGNO (operands[0]) != REGNO (operands[1])
796 ;   && INT8_P (INTVAL (operands[2]))
797 ;   && INTVAL (operands[2]) != 0"
798 ;  [(set (match_dup 0) (match_dup 1))
799 ;   (set (match_dup 0) (plus:SI (match_dup 0) (match_dup 2)))]
800 ;  "")
801
802 (define_insn "adddi3"
803   [(set (match_operand:DI 0 "register_operand" "=r")
804         (plus:DI (match_operand:DI 1 "register_operand" "%0")
805                  (match_operand:DI 2 "register_operand" "r")))
806    (clobber (reg:SI 17))]
807   ""
808   "#"
809   [(set_attr "type" "multi")
810    (set_attr "length" "6")])
811
812 ;; ??? The cmp clears the condition bit.  Can we speed up somehow?
813 (define_split
814   [(set (match_operand:DI 0 "register_operand" "")
815         (plus:DI (match_operand:DI 1 "register_operand" "")
816                  (match_operand:DI 2 "register_operand" "")))
817    (clobber (match_operand 3 "" ""))]
818   "reload_completed"
819   [(parallel [(set (match_dup 3)
820                    (const_int 0))
821               (use (match_dup 4))])
822    (parallel [(set (match_dup 4)
823                    (plus:SI (match_dup 4)
824                             (plus:SI (match_dup 5)
825                                      (match_dup 3))))
826               (set (match_dup 3)
827                    (unspec [(const_int 0)] 3))])
828    (parallel [(set (match_dup 6)
829                    (plus:SI (match_dup 6)
830                             (plus:SI (match_dup 7)
831                                      (match_dup 3))))
832               (set (match_dup 3)
833                    (unspec [(const_int 0)] 3))])]
834   "
835 {
836   operands[4] = operand_subword (operands[0], (WORDS_BIG_ENDIAN != 0), 0, DImode);
837   operands[5] = operand_subword (operands[2], (WORDS_BIG_ENDIAN != 0), 0, DImode);
838   operands[6] = operand_subword (operands[0], (WORDS_BIG_ENDIAN == 0), 0, DImode);
839   operands[7] = operand_subword (operands[2], (WORDS_BIG_ENDIAN == 0), 0, DImode);
840 }")
841
842 (define_insn "*clear_c"
843   [(set (reg:SI 17)
844         (const_int 0))
845    (use (match_operand:SI 0 "register_operand" "r"))]
846   ""
847   "cmp %0,%0"
848   [(set_attr "type" "int2")
849    (set_attr "length" "2")])
850
851 (define_insn "*add_carry"
852   [(set (match_operand:SI 0 "register_operand" "=r")
853         (plus:SI (match_operand:SI 1 "register_operand" "%0")
854                  (plus:SI (match_operand:SI 2 "register_operand" "r")
855                           (reg:SI 17))))
856    (set (reg:SI 17)
857         (unspec [(const_int 0)] 3))]
858   ""
859   "addx %0,%2"
860   [(set_attr "type" "int2")
861    (set_attr "length" "2")])
862
863 (define_insn "subsi3"
864   [(set (match_operand:SI 0 "register_operand" "=r")
865         (minus:SI (match_operand:SI 1 "register_operand" "0")
866                   (match_operand:SI 2 "register_operand" "r")))]
867   ""
868   "sub %0,%2"
869   [(set_attr "type" "int2")
870    (set_attr "length" "2")])
871
872 (define_insn "subdi3"
873   [(set (match_operand:DI 0 "register_operand" "=r")
874         (minus:DI (match_operand:DI 1 "register_operand" "0")
875                   (match_operand:DI 2 "register_operand" "r")))
876    (clobber (reg:SI 17))]
877   ""
878   "#"
879   [(set_attr "type" "multi")
880    (set_attr "length" "6")])
881
882 ;; ??? The cmp clears the condition bit.  Can we speed up somehow?
883 (define_split
884   [(set (match_operand:DI 0 "register_operand" "")
885         (minus:DI (match_operand:DI 1 "register_operand" "")
886                   (match_operand:DI 2 "register_operand" "")))
887    (clobber (match_operand 3 "" ""))]
888   "reload_completed"
889   [(parallel [(set (match_dup 3)
890                    (const_int 0))
891               (use (match_dup 4))])
892    (parallel [(set (match_dup 4)
893                    (minus:SI (match_dup 4)
894                              (minus:SI (match_dup 5)
895                                        (match_dup 3))))
896               (set (match_dup 3)
897                    (unspec [(const_int 0)] 3))])
898    (parallel [(set (match_dup 6)
899                    (minus:SI (match_dup 6)
900                              (minus:SI (match_dup 7)
901                                        (match_dup 3))))
902               (set (match_dup 3)
903                    (unspec [(const_int 0)] 3))])]
904   "
905 {
906   operands[4] = operand_subword (operands[0], (WORDS_BIG_ENDIAN != 0), 0, DImode);
907   operands[5] = operand_subword (operands[2], (WORDS_BIG_ENDIAN != 0), 0, DImode);
908   operands[6] = operand_subword (operands[0], (WORDS_BIG_ENDIAN == 0), 0, DImode);
909   operands[7] = operand_subword (operands[2], (WORDS_BIG_ENDIAN == 0), 0, DImode);
910 }")
911
912 (define_insn "*sub_carry"
913   [(set (match_operand:SI 0 "register_operand" "=r")
914         (minus:SI (match_operand:SI 1 "register_operand" "%0")
915                   (minus:SI (match_operand:SI 2 "register_operand" "r")
916                             (reg:SI 17))))
917    (set (reg:SI 17)
918         (unspec [(const_int 0)] 3))]
919   ""
920   "subx %0,%2"
921   [(set_attr "type" "int2")
922    (set_attr "length" "2")])
923 \f
924 ; Multiply/Divide instructions.
925
926 (define_insn "mulhisi3"
927   [(set (match_operand:SI 0 "register_operand" "=r")
928         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "r"))
929                  (sign_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
930   ""
931   "mullo %1,%2\;mvfacmi %0"
932   [(set_attr "type" "multi")
933    (set_attr "length" "4")])
934
935 (define_insn "mulsi3"
936   [(set (match_operand:SI 0 "register_operand" "=r")
937         (mult:SI (match_operand:SI 1 "register_operand" "%0")
938                  (match_operand:SI 2 "register_operand" "r")))]
939   ""
940   "mul %0,%2"
941   [(set_attr "type" "mul2")
942    (set_attr "length" "2")])
943
944 (define_insn "divsi3"
945   [(set (match_operand:SI 0 "register_operand" "=r")
946         (div:SI (match_operand:SI 1 "register_operand" "0")
947                 (match_operand:SI 2 "register_operand" "r")))]
948   ""
949   "div %0,%2"
950   [(set_attr "type" "div4")
951    (set_attr "length" "4")])
952
953 (define_insn "udivsi3"
954   [(set (match_operand:SI 0 "register_operand" "=r")
955         (udiv:SI (match_operand:SI 1 "register_operand" "0")
956                  (match_operand:SI 2 "register_operand" "r")))]
957   ""
958   "divu %0,%2"
959   [(set_attr "type" "div4")
960    (set_attr "length" "4")])
961
962 (define_insn "modsi3"
963   [(set (match_operand:SI 0 "register_operand" "=r")
964         (mod:SI (match_operand:SI 1 "register_operand" "0")
965                 (match_operand:SI 2 "register_operand" "r")))]
966   ""
967   "rem %0,%2"
968   [(set_attr "type" "div4")
969    (set_attr "length" "4")])
970
971 (define_insn "umodsi3"
972   [(set (match_operand:SI 0 "register_operand" "=r")
973         (umod:SI (match_operand:SI 1 "register_operand" "0")
974                  (match_operand:SI 2 "register_operand" "r")))]
975   ""
976   "remu %0,%2"
977   [(set_attr "type" "div4")
978    (set_attr "length" "4")])
979 \f
980 ;; Boolean instructions.
981 ;;
982 ;; We don't define the DImode versions as expand_binop does a good enough job.
983 ;; And if it doesn't it should be fixed.
984
985 (define_insn "andsi3"
986   [(set (match_operand:SI 0 "register_operand" "=r,r")
987         (and:SI (match_operand:SI 1 "register_operand" "%0,r")
988                 (match_operand:SI 2 "nonmemory_operand" "r,K")))]
989   ""
990   "@
991    and %0,%2
992    and3 %0,%1,%#%2\\t; %X2"
993   [(set_attr "type" "int2,int4")
994    (set_attr "length" "2,4")])
995
996 (define_insn "iorsi3"
997   [(set (match_operand:SI 0 "register_operand" "=r,r")
998         (ior:SI (match_operand:SI 1 "register_operand" "%0,r")
999                 (match_operand:SI 2 "nonmemory_operand" "r,K")))]
1000   ""
1001   "@
1002    or %0,%2
1003    or3 %0,%1,%#%2\\t; %X2"
1004   [(set_attr "type" "int2,int4")
1005    (set_attr "length" "2,4")])
1006
1007 (define_insn "xorsi3"
1008   [(set (match_operand:SI 0 "register_operand" "=r,r")
1009         (xor:SI (match_operand:SI 1 "register_operand" "%0,r")
1010                 (match_operand:SI 2 "nonmemory_operand" "r,K")))]
1011   ""
1012   "@
1013    xor %0,%2
1014    xor3 %0,%1,%#%2\\t; %X2"
1015   [(set_attr "type" "int2,int4")
1016    (set_attr "length" "2,4")])
1017
1018 (define_insn "negsi2"
1019   [(set (match_operand:SI 0 "register_operand" "=r")
1020         (neg:SI (match_operand:SI 1 "register_operand" "r")))]
1021   ""
1022   "neg %0,%1"
1023   [(set_attr "type" "int2")
1024    (set_attr "length" "2")])
1025
1026 (define_insn "one_cmplsi2"
1027   [(set (match_operand:SI 0 "register_operand" "=r")
1028         (not:SI (match_operand:SI 1 "register_operand" "r")))]
1029   ""
1030   "not %0,%1"
1031   [(set_attr "type" "int2")
1032    (set_attr "length" "2")])
1033 \f
1034 ;; Shift instructions.
1035
1036 (define_insn "ashlsi3"
1037   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
1038         (ashift:SI (match_operand:SI 1 "register_operand" "0,0,r")
1039                    (match_operand:SI 2 "reg_or_uint16_operand" "r,O,K")))]
1040   ""
1041   "@
1042    sll %0,%2
1043    slli %0,%#%2
1044    sll3 %0,%1,%#%2"
1045   [(set_attr "type" "shift2,shift2,shift4")
1046    (set_attr "length" "2,2,4")])
1047
1048 (define_insn "ashrsi3"
1049   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
1050         (ashiftrt:SI (match_operand:SI 1 "register_operand" "0,0,r")
1051                      (match_operand:SI 2 "reg_or_uint16_operand" "r,O,K")))]
1052   ""
1053   "@
1054    sra %0,%2
1055    srai %0,%#%2
1056    sra3 %0,%1,%#%2"
1057   [(set_attr "type" "shift2,shift2,shift4")
1058    (set_attr "length" "2,2,4")])
1059
1060 (define_insn "lshrsi3"
1061   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
1062         (lshiftrt:SI (match_operand:SI 1 "register_operand" "0,0,r")
1063                      (match_operand:SI 2 "reg_or_uint16_operand" "r,O,K")))]
1064   ""
1065   "@
1066    srl %0,%2
1067    srli %0,%#%2
1068    srl3 %0,%1,%#%2"
1069   [(set_attr "type" "shift2,shift2,shift4")
1070    (set_attr "length" "2,2,4")])
1071 \f
1072 ;; Compare instructions.
1073 ;; This controls RTL generation and register allocation.
1074
1075 ;; We generate RTL for comparisons and branches by having the cmpxx 
1076 ;; patterns store away the operands.  Then the bcc patterns
1077 ;; emit RTL for both the compare and the branch.
1078 ;;
1079 ;; On the m32r it is more efficient to use the bxxz instructions and
1080 ;; thus merge the compare and branch into one instruction, so they are
1081 ;; preferred.
1082
1083 (define_expand "cmpsi"
1084   [(set (reg:SI 17)
1085         (compare:SI (match_operand:SI 0 "register_operand" "")
1086                     (match_operand:SI 1 "nonmemory_operand" "")))]
1087   ""
1088   "
1089 {
1090   m32r_compare_op0 = operands[0];
1091   m32r_compare_op1 = operands[1];
1092   DONE;
1093 }")
1094
1095
1096 ;; The cmp_xxx_insn patterns set the condition bit to the result of the
1097 ;; comparison.  There isn't a "compare equal" instruction so cmp_eqsi_insn
1098 ;; is quite inefficient.  However, it is rarely used.
1099
1100 (define_insn "cmp_eqsi_insn"
1101   [(set (reg:SI 17)
1102         (eq:SI (match_operand:SI 0 "register_operand" "r,r")
1103                (match_operand:SI 1 "reg_or_cmp_int16_operand" "r,P")))
1104    (clobber (match_scratch:SI 2 "=&r,&r"))]
1105   ""
1106   "*
1107 {
1108   if (which_alternative == 0)
1109     {
1110          return \"mv %2,%0\;sub %2,%1\;cmpui %2,#1\";
1111     }
1112   else
1113     {
1114         if (INTVAL (operands [1]) == 0)
1115           return \"cmpui %0, #1\";
1116         else if (REGNO (operands [2]) == REGNO (operands [0]))
1117           return \"addi %0,%#%N1\;cmpui %2,#1\";
1118         else
1119           return \"add3 %2,%0,%#%N1\;cmpui %2,#1\";
1120     }
1121 }"
1122   [(set_attr "type" "multi,multi")
1123    (set_attr "length" "8,8")])
1124
1125 (define_insn "cmp_ltsi_insn"
1126   [(set (reg:SI 17)
1127         (lt:SI (match_operand:SI 0 "register_operand" "r,r")
1128                (match_operand:SI 1 "reg_or_int16_operand" "r,J")))]
1129   ""
1130   "@
1131    cmp %0,%1
1132    cmpi %0,%#%1"
1133   [(set_attr "type" "int2,int4")
1134    (set_attr "length" "2,4")])
1135
1136 (define_insn "cmp_ltusi_insn"
1137   [(set (reg:SI 17)
1138         (ltu:SI (match_operand:SI 0 "register_operand" "r,r")
1139                 (match_operand:SI 1 "reg_or_uint16_operand" "r,K")))]
1140   ""
1141   "@
1142    cmpu %0,%1
1143    cmpui %0,%#%1"
1144   [(set_attr "type" "int2,int4")
1145    (set_attr "length" "2,4")])
1146
1147 ;; reg == small constant comparisons are best handled by putting the result
1148 ;; of the comparison in a tmp reg and then using beqz/bnez.
1149 ;; ??? The result register doesn't contain 0/STORE_FLAG_VALUE,
1150 ;; it contains 0/non-zero.
1151
1152 (define_insn "cmp_ne_small_const_insn"
1153   [(set (match_operand:SI 0 "register_operand" "=r,r")
1154         (ne:SI (match_operand:SI 1 "register_operand" "0,r")
1155                (match_operand:SI 2 "cmp_int16_operand" "N,P")))]
1156   ""
1157   "@
1158    addi %0,%#%N2
1159    add3 %0,%1,%#%N2"
1160   [(set_attr "type" "int2,int4")
1161    (set_attr "length" "2,4")])
1162 \f
1163 ;; These control RTL generation for conditional jump insns.
1164
1165 (define_expand "beq"
1166   [(set (pc)
1167         (if_then_else (match_dup 1)
1168                       (label_ref (match_operand 0 "" ""))
1169                       (pc)))]
1170   ""
1171   "
1172 {
1173   operands[1] = gen_compare ((int)EQ, m32r_compare_op0, m32r_compare_op1, FALSE);
1174 }")
1175
1176 (define_expand "bne"
1177   [(set (pc)
1178         (if_then_else (match_dup 1)
1179                       (label_ref (match_operand 0 "" ""))
1180                       (pc)))]
1181   ""
1182   "
1183 {
1184   operands[1] = gen_compare ((int)NE, m32r_compare_op0, m32r_compare_op1, FALSE);
1185 }")
1186
1187 (define_expand "bgt"
1188   [(set (pc)
1189         (if_then_else (match_dup 1)
1190                       (label_ref (match_operand 0 "" ""))
1191                       (pc)))]
1192   ""
1193   "
1194 {
1195   operands[1] = gen_compare ((int)GT, m32r_compare_op0, m32r_compare_op1, FALSE);
1196 }")
1197
1198 (define_expand "ble"
1199   [(set (pc)
1200         (if_then_else (match_dup 1)
1201                       (label_ref (match_operand 0 "" ""))
1202                       (pc)))]
1203   ""
1204   "
1205 {
1206   operands[1] = gen_compare ((int)LE, m32r_compare_op0, m32r_compare_op1, FALSE);
1207 }")
1208
1209 (define_expand "bge"
1210   [(set (pc)
1211         (if_then_else (match_dup 1)
1212                       (label_ref (match_operand 0 "" ""))
1213                       (pc)))]
1214   ""
1215   "
1216 {
1217   operands[1] = gen_compare ((int)GE, m32r_compare_op0, m32r_compare_op1, FALSE);
1218 }")
1219
1220 (define_expand "blt"
1221   [(set (pc)
1222         (if_then_else (match_dup 1)
1223                       (label_ref (match_operand 0 "" ""))
1224                       (pc)))]
1225   ""
1226   "
1227 {
1228   operands[1] = gen_compare ((int)LT, m32r_compare_op0, m32r_compare_op1, FALSE);
1229 }")
1230
1231 (define_expand "bgtu"
1232   [(set (pc)
1233         (if_then_else (match_dup 1)
1234                       (label_ref (match_operand 0 "" ""))
1235                       (pc)))]
1236   ""
1237   "
1238 {
1239   operands[1] = gen_compare ((int)GTU, m32r_compare_op0, m32r_compare_op1, FALSE);
1240 }")
1241
1242 (define_expand "bleu"
1243   [(set (pc)
1244         (if_then_else (match_dup 1)
1245                       (label_ref (match_operand 0 "" ""))
1246                       (pc)))]
1247   ""
1248   "
1249 {
1250   operands[1] = gen_compare ((int)LEU, m32r_compare_op0, m32r_compare_op1, FALSE);
1251 }")
1252
1253 (define_expand "bgeu"
1254   [(set (pc)
1255         (if_then_else (match_dup 1)
1256                       (label_ref (match_operand 0 "" ""))
1257                       (pc)))]
1258   ""
1259   "
1260 {
1261   operands[1] = gen_compare ((int)GEU, m32r_compare_op0, m32r_compare_op1, FALSE);
1262 }")
1263
1264 (define_expand "bltu"
1265   [(set (pc)
1266         (if_then_else (match_dup 1)
1267                       (label_ref (match_operand 0 "" ""))
1268                       (pc)))]
1269   ""
1270   "
1271 {
1272   operands[1] = gen_compare ((int)LTU, m32r_compare_op0, m32r_compare_op1, FALSE);
1273 }")
1274
1275 ;; Now match both normal and inverted jump.
1276
1277 (define_insn "*branch_insn"
1278   [(set (pc)
1279         (if_then_else (match_operator 1 "eqne_comparison_operator"
1280                                       [(reg 17) (const_int 0)])
1281                       (label_ref (match_operand 0 "" ""))
1282                       (pc)))]
1283   ""
1284   "*
1285 {
1286   static char instruction[40];
1287   sprintf (instruction, \"%s%s %%l0\",
1288            (GET_CODE (operands[1]) == NE) ? \"bc\" : \"bnc\",
1289            (get_attr_length (insn) == 2) ? \".s\" : \"\");
1290   return instruction;
1291 }"
1292   [(set_attr "type" "branch")
1293    ; We use 400/800 instead of 512,1024 to account for inaccurate insn
1294    ; lengths and insn alignments that are complex to track.
1295    ; It's not important that we be hyper-precise here.  It may be more
1296    ; important blah blah blah when the chip supports parallel execution
1297    ; blah blah blah but until then blah blah blah this is simple and
1298    ; suffices.
1299    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1300                                                  (const_int 400))
1301                                            (const_int 800))
1302                                       (const_int 2)
1303                                       (const_int 4)))])
1304
1305 (define_insn "*rev_branch_insn"
1306   [(set (pc)
1307         (if_then_else (match_operator 1 "eqne_comparison_operator"
1308                                       [(reg 17) (const_int 0)])
1309                       (pc)
1310                       (label_ref (match_operand 0 "" ""))))]
1311   ;"REVERSIBLE_CC_MODE (GET_MODE (XEXP (operands[1], 0)))"
1312   ""
1313   "*
1314 {
1315   static char instruction[40];
1316   sprintf (instruction, \"%s%s %%l0\",
1317            (GET_CODE (operands[1]) == EQ) ? \"bc\" : \"bnc\",
1318            (get_attr_length (insn) == 2) ? \".s\" : \"\");
1319   return instruction;
1320 }"
1321   [(set_attr "type" "branch")
1322    ; We use 400/800 instead of 512,1024 to account for inaccurate insn
1323    ; lengths and insn alignments that are complex to track.
1324    ; It's not important that we be hyper-precise here.  It may be more
1325    ; important blah blah blah when the chip supports parallel execution
1326    ; blah blah blah but until then blah blah blah this is simple and
1327    ; suffices.
1328    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1329                                                  (const_int 400))
1330                                            (const_int 800))
1331                                       (const_int 2)
1332                                       (const_int 4)))])
1333
1334 ; reg/reg compare and branch insns
1335
1336 (define_insn "*reg_branch_insn"
1337   [(set (pc)
1338         (if_then_else (match_operator 1 "eqne_comparison_operator"
1339                                       [(match_operand:SI 2 "register_operand" "r")
1340                                        (match_operand:SI 3 "register_operand" "r")])
1341                       (label_ref (match_operand 0 "" ""))
1342                       (pc)))]
1343   ""
1344   "*
1345 {
1346   /* Is branch target reachable with beq/bne?  */
1347   if (get_attr_length (insn) == 4)
1348     {
1349       if (GET_CODE (operands[1]) == EQ)
1350         return \"beq %2,%3,%l0\";
1351       else
1352         return \"bne %2,%3,%l0\";
1353     }
1354   else
1355     {
1356       if (GET_CODE (operands[1]) == EQ)
1357         return \"bne %2,%3,1f\;bra %l0\;1:\";
1358       else
1359         return \"beq %2,%3,1f\;bra %l0\;1:\";
1360     }
1361 }"
1362   [(set_attr "type" "branch")
1363   ; We use 25000/50000 instead of 32768/65536 to account for slot filling
1364   ; which is complex to track and inaccurate length specs.
1365    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1366                                                  (const_int 25000))
1367                                            (const_int 50000))
1368                                       (const_int 4)
1369                                       (const_int 8)))])
1370
1371 (define_insn "*rev_reg_branch_insn"
1372   [(set (pc)
1373         (if_then_else (match_operator 1 "eqne_comparison_operator"
1374                                       [(match_operand:SI 2 "register_operand" "r")
1375                                        (match_operand:SI 3 "register_operand" "r")])
1376                       (pc)
1377                       (label_ref (match_operand 0 "" ""))))]
1378   ""
1379   "*
1380 {
1381   /* Is branch target reachable with beq/bne?  */
1382   if (get_attr_length (insn) == 4)
1383     {
1384       if (GET_CODE (operands[1]) == NE)
1385         return \"beq %2,%3,%l0\";
1386       else
1387         return \"bne %2,%3,%l0\";
1388     }
1389   else
1390     {
1391       if (GET_CODE (operands[1]) == NE)
1392         return \"bne %2,%3,1f\;bra %l0\;1:\";
1393       else
1394         return \"beq %2,%3,1f\;bra %l0\;1:\";
1395     }
1396 }"
1397   [(set_attr "type" "branch")
1398   ; We use 25000/50000 instead of 32768/65536 to account for slot filling
1399   ; which is complex to track and inaccurate length specs.
1400    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1401                                                  (const_int 25000))
1402                                            (const_int 50000))
1403                                       (const_int 4)
1404                                       (const_int 8)))])
1405
1406 ; reg/zero compare and branch insns
1407
1408 (define_insn "*zero_branch_insn"
1409   [(set (pc)
1410         (if_then_else (match_operator 1 "signed_comparison_operator"
1411                                       [(match_operand:SI 2 "register_operand" "r")
1412                                        (const_int 0)])
1413                       (label_ref (match_operand 0 "" ""))
1414                       (pc)))]
1415   ""
1416   "*
1417 {
1418   char *br,*invbr;
1419   char asmtext[40];
1420
1421   switch (GET_CODE (operands[1]))
1422     {
1423       case EQ : br = \"eq\"; invbr = \"ne\"; break;
1424       case NE : br = \"ne\"; invbr = \"eq\"; break;
1425       case LE : br = \"le\"; invbr = \"gt\"; break;
1426       case GT : br = \"gt\"; invbr = \"le\"; break;
1427       case LT : br = \"lt\"; invbr = \"ge\"; break;
1428       case GE : br = \"ge\"; invbr = \"lt\"; break;
1429     }
1430
1431   /* Is branch target reachable with bxxz?  */
1432   if (get_attr_length (insn) == 4)
1433     {
1434       sprintf (asmtext, \"b%sz %%2,%%l0\", br);
1435       output_asm_insn (asmtext, operands);
1436     }
1437   else
1438     {
1439       sprintf (asmtext, \"b%sz %%2,1f\;bra %%l0\;1:\", invbr);
1440       output_asm_insn (asmtext, operands);
1441     }
1442   return \"\";
1443 }"
1444   [(set_attr "type" "branch")
1445   ; We use 25000/50000 instead of 32768/65536 to account for slot filling
1446   ; which is complex to track and inaccurate length specs.
1447    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1448                                                  (const_int 25000))
1449                                            (const_int 50000))
1450                                       (const_int 4)
1451                                       (const_int 8)))])
1452
1453 (define_insn "*rev_zero_branch_insn"
1454   [(set (pc)
1455         (if_then_else (match_operator 1 "eqne_comparison_operator"
1456                                       [(match_operand:SI 2 "register_operand" "r")
1457                                        (const_int 0)])
1458                       (pc)
1459                       (label_ref (match_operand 0 "" ""))))]
1460   ""
1461   "*
1462 {
1463   char *br,*invbr;
1464   char asmtext[40];
1465
1466   switch (GET_CODE (operands[1]))
1467     {
1468       case EQ : br = \"eq\"; invbr = \"ne\"; break;
1469       case NE : br = \"ne\"; invbr = \"eq\"; break;
1470       case LE : br = \"le\"; invbr = \"gt\"; break;
1471       case GT : br = \"gt\"; invbr = \"le\"; break;
1472       case LT : br = \"lt\"; invbr = \"ge\"; break;
1473       case GE : br = \"ge\"; invbr = \"lt\"; break;
1474     }
1475
1476   /* Is branch target reachable with bxxz?  */
1477   if (get_attr_length (insn) == 4)
1478     {
1479       sprintf (asmtext, \"b%sz %%2,%%l0\", invbr);
1480       output_asm_insn (asmtext, operands);
1481     }
1482   else
1483     {
1484       sprintf (asmtext, \"b%sz %%2,1f\;bra %%l0\;1:\", br);
1485       output_asm_insn (asmtext, operands);
1486     }
1487   return \"\";
1488 }"
1489   [(set_attr "type" "branch")
1490   ; We use 25000/50000 instead of 32768/65536 to account for slot filling
1491   ; which is complex to track and inaccurate length specs.
1492    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1493                                                  (const_int 25000))
1494                                            (const_int 50000))
1495                                       (const_int 4)
1496                                       (const_int 8)))])
1497 \f
1498 ;; Unconditional and other jump instructions.
1499
1500 (define_insn "jump"
1501   [(set (pc) (label_ref (match_operand 0 "" "")))]
1502   ""
1503   "bra %l0"
1504   [(set_attr "type" "uncond_branch")
1505    (set (attr "length") (if_then_else (ltu (plus (minus (match_dup 0) (pc))
1506                                                  (const_int 400))
1507                                            (const_int 800))
1508                                       (const_int 2)
1509                                       (const_int 4)))])
1510
1511 (define_insn "indirect_jump"
1512   [(set (pc) (match_operand:SI 0 "address_operand" "p"))]
1513   ""
1514   "jmp %a0"
1515   [(set_attr "type" "uncond_branch")
1516    (set_attr "length" "2")])
1517  
1518 (define_insn "tablejump"
1519   [(set (pc) (match_operand:SI 0 "address_operand" "p"))
1520    (use (label_ref (match_operand 1 "" "")))]
1521   ""
1522   "jmp %a0"
1523   [(set_attr "type" "uncond_branch")
1524    (set_attr "length" "2")])
1525
1526 (define_expand "call"
1527   ;; operands[1] is stack_size_rtx
1528   ;; operands[2] is next_arg_register
1529   [(parallel [(call (match_operand:SI 0 "call_operand" "")
1530                     (match_operand 1 "" ""))
1531              (clobber (reg:SI 14))])]
1532   ""
1533   "")
1534
1535 (define_insn "*call_via_reg"
1536   [(call (mem:SI (match_operand:SI 0 "register_operand" "r"))
1537          (match_operand 1 "" ""))
1538    (clobber (reg:SI 14))]
1539   ""
1540   "jl %0"
1541   [(set_attr "type" "call")
1542    (set_attr "length" "2")])
1543
1544 (define_insn "*call_via_label"
1545   [(call (mem:SI (match_operand:SI 0 "call_address_operand" ""))
1546          (match_operand 1 "" ""))
1547    (clobber (reg:SI 14))]
1548   ""
1549   "*
1550 {
1551   int call26_p = call26_operand (operands[0], FUNCTION_MODE);
1552
1553   if (! call26_p)
1554     {
1555       /* We may not be able to reach with a `bl' insn so punt and leave it to
1556          the linker.
1557          We do this here, rather than doing a force_reg in the define_expand
1558          so these insns won't be separated, say by scheduling, thus simplifying
1559          the linker.  */
1560       return \"seth r14,%T0\;add3 r14,r14,%B0\;jl r14\";
1561     }
1562   else
1563     return \"bl %0\";
1564 }"
1565   [(set_attr "type" "call")
1566    (set (attr "length")
1567         (if_then_else (eq (symbol_ref "call26_operand (operands[0], FUNCTION_MODE)")
1568                           (const_int 0))
1569                       (const_int 12) ; 10 + 2 for nop filler
1570                       ; The return address must be on a 4 byte boundary so
1571                       ; there's no point in using a value of 2 here.  A 2 byte
1572                       ; insn may go in the left slot but we currently can't
1573                       ; use such knowledge.
1574                       (const_int 4)))])
1575
1576 (define_expand "call_value"
1577   ;; operand 2 is stack_size_rtx
1578   ;; operand 3 is next_arg_register
1579   [(parallel [(set (match_operand 0 "register_operand" "=r")
1580                    (call (match_operand:SI 1 "call_operand" "")
1581                          (match_operand 2 "" "")))
1582              (clobber (reg:SI 14))])]
1583   ""
1584   "")
1585
1586 (define_insn "*call_value_via_reg"
1587   [(set (match_operand 0 "register_operand" "=r")
1588         (call (mem:SI (match_operand:SI 1 "register_operand" "r"))
1589               (match_operand 2 "" "")))
1590    (clobber (reg:SI 14))]
1591   ""
1592   "jl %1"
1593   [(set_attr "type" "call")
1594    (set_attr "length" "2")])
1595
1596 (define_insn "*call_value_via_label"
1597   [(set (match_operand 0 "register_operand" "=r")
1598         (call (mem:SI (match_operand:SI 1 "call_address_operand" ""))
1599               (match_operand 2 "" "")))
1600    (clobber (reg:SI 14))]
1601   ""
1602   "*
1603 {
1604   int call26_p = call26_operand (operands[1], FUNCTION_MODE);
1605
1606   if (! call26_p)
1607     {
1608       /* We may not be able to reach with a `bl' insn so punt and leave it to
1609          the linker.
1610          We do this here, rather than doing a force_reg in the define_expand
1611          so these insns won't be separated, say by scheduling, thus simplifying
1612          the linker.  */
1613       return \"seth r14,%T1\;add3 r14,r14,%B1\;jl r14\";
1614     }
1615   else
1616     return \"bl %1\";
1617 }"
1618   [(set_attr "type" "call")
1619    (set (attr "length")
1620         (if_then_else (eq (symbol_ref "call26_operand (operands[1], FUNCTION_MODE)")
1621                           (const_int 0))
1622                       (const_int 12) ; 10 + 2 for nop filler
1623                       ; The return address must be on a 4 byte boundary so
1624                       ; there's no point in using a value of 2 here.  A 2 byte
1625                       ; insn may go in the left slot but we currently can't
1626                       ; use such knowledge.
1627                       (const_int 4)))])
1628 \f
1629 (define_insn "nop"
1630   [(const_int 0)]
1631   ""
1632   "nop"
1633   [(set_attr "type" "int2")
1634    (set_attr "length" "2")])
1635
1636 ;; UNSPEC_VOLATILE is considered to use and clobber all hard registers and
1637 ;; all of memory.  This blocks insns from being moved across this point.
1638
1639 (define_insn "blockage"
1640   [(unspec_volatile [(const_int 0)] 0)]
1641   ""
1642   "")
1643
1644 ;; Special pattern to flush the icache.
1645
1646 (define_insn "flush_icache"
1647   [(unspec_volatile [(match_operand 0 "memory_operand" "m")] 0)]
1648   ""
1649   "* return \"nop ; flush-icache\";"
1650   [(set_attr "type" "int2")
1651    (set_attr "length" "2")])
1652 \f
1653 ;; Conditional move instructions
1654 ;; Based on those done for the d10v
1655
1656
1657 (define_expand "movsicc"
1658   [
1659    (set (match_operand:SI 0 "register_operand" "r")
1660         (if_then_else:SI (match_operand 1 "" "")
1661                          (match_operand:SI 2 "conditional_move_operand" "O")
1662                          (match_operand:SI 3 "conditional_move_operand" "O")
1663         )
1664    )
1665   ]
1666   ""
1667   "
1668 {
1669   if (! zero_and_one (operands [2], operands [3]))
1670     FAIL;
1671
1672   /* Generate the comparision that will set the carry flag.  */
1673   operands[1] = gen_compare ((int)GET_CODE (operands[1]), m32r_compare_op0,
1674                              m32r_compare_op1, TRUE);
1675
1676   /* See other movsicc pattern below for reason why.  */
1677   emit_insn (gen_blockage());
1678 }")
1679
1680 ;; Generate the conditional instructions based on how the carry flag is examined.
1681 (define_insn "*movsicc_internal"
1682   [(set (match_operand:SI 0 "register_operand" "r")
1683         (if_then_else:SI (match_operand 1 "carry_compare_operand" "")
1684                          (match_operand:SI 2 "conditional_move_operand" "O")
1685                          (match_operand:SI 3 "conditional_move_operand" "O")
1686         )
1687    )]
1688   "zero_and_one (operands [2], operands[3])"
1689   "* return emit_cond_move (operands, insn);"
1690   [(set_attr "type" "multi")
1691    (set_attr "length" "8")
1692   ]
1693 )
1694
1695 (define_insn "movcc_insn"
1696   [(set (match_operand:SI 0 "register_operand" "=r")
1697         (reg:SI 17))]
1698   ""
1699   "mvfc %0, cbr"
1700   [(set_attr "type" "misc")
1701    (set_attr "length" "2")]
1702 )
1703
1704 \f
1705 ;; Split up troublesome insns for better scheduling.
1706 \f
1707 ;; Peepholes go at the end.
1708
1709 ;; ??? Setting the type attribute may not be useful, but for completeness
1710 ;; we do it.
1711
1712 (define_peephole
1713   [(set (mem:SI (plus:SI (match_operand:SI 0 "register_operand" "r")
1714                          (const_int 4)))
1715         (match_operand:SI 1 "register_operand" "r"))]
1716   "0 && dead_or_set_p (insn, operands[0])"
1717   "st %1,@+%0"
1718   [(set_attr "type" "store2")
1719    (set_attr "length" "2")])
1720
1721 ;; This case is triggered by compiling this code:
1722 ;; 
1723 ;; extern void sub(int *);
1724 ;; void main (void)
1725 ;; {
1726 ;;   int i=2,j=3,k;
1727 ;;   while (i < j)  sub(&k);
1728 ;;   i = j / k;
1729 ;;   sub(&i);
1730 ;;   i = j - k;
1731 ;;   sub(&i);
1732 ;; }
1733 ;;
1734 ;; Without the peephole the following assembler is generated for the
1735 ;; divide and subtract expressions:
1736 ;;
1737 ;;         div r5,r4     
1738 ;;         mv r4,r5      
1739 ;;         st r4,@(4,sp) 
1740 ;;         bl sub
1741 ;; 
1742 ;; Simialr code is produced for the subtract expression.  With this
1743 ;; peephole the redundant move is eliminated.
1744 ;;
1745 ;; This optimisation onbly works if PRESERVE_DEATH_INFO_REGNO_P is
1746 ;; defined in m32r.h
1747
1748 (define_peephole
1749   [(set (match_operand:SI 0 "register_operand" "r")
1750         (match_operand:SI 1 "register_operand" "r")
1751    )
1752    (set (mem:SI (plus: SI (match_operand:SI 2 "register_operand" "r")
1753                 (match_operand:SI 3 "immediate_operand" "J")))
1754         (match_dup 0)
1755    )
1756   ]
1757   "0 && dead_or_set_p (insn, operands [0])"
1758   "st %1,@(%3,%2)"
1759   [(set_attr "type" "store4")
1760    (set_attr "length" "4")
1761   ]
1762 )
1763
1764 ;; Block moves, see m32r.c for more details.
1765 ;; Argument 0 is the destination
1766 ;; Argument 1 is the source
1767 ;; Argument 2 is the length
1768 ;; Argument 3 is the alignment
1769
1770 (define_expand "movstrsi"
1771   [(parallel [(set (match_operand:BLK 0 "general_operand" "")
1772                    (match_operand:BLK 1 "general_operand" ""))
1773               (use (match_operand:SI  2 "immediate_operand" ""))
1774               (use (match_operand:SI  3 "immediate_operand" ""))])]
1775   ""
1776   "
1777 {
1778   if (operands[0])              /* avoid unused code messages */
1779     {
1780       m32r_expand_block_move (operands);
1781       DONE;
1782     }
1783 }")
1784
1785 ;; Insn generated by block moves
1786
1787 (define_insn "movstrsi_internal"
1788   [(set (mem:BLK (match_operand:SI 0 "register_operand" "r"))   ;; destination
1789         (mem:BLK (match_operand:SI 1 "register_operand" "r")))  ;; source
1790    (use (match_operand:SI 2 "m32r_block_immediate_operand" "J"));; # bytes to move
1791    (set (match_dup 0) (plus:SI (match_dup 0) (minus:SI (match_dup 2) (const_int 4))))
1792    (set (match_dup 1) (plus:SI (match_dup 1) (match_dup 2)))
1793    (clobber (match_scratch:SI 3 "=&r"))                         ;; temp 1
1794    (clobber (match_scratch:SI 4 "=&r"))]                        ;; temp 2
1795   ""
1796   "* return m32r_output_block_move (insn, operands);"
1797   [(set_attr "type"     "store8")
1798    (set_attr "length"   "72")]) ;; Maximum