OSDN Git Service

2009-10-01 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / dojump.c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "function.h"
30 #include "insn-config.h"
31 #include "insn-attr.h"
32 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
33 #include "expr.h"
34 #include "optabs.h"
35 #include "langhooks.h"
36 #include "ggc.h"
37 #include "basic-block.h"
38
39 static bool prefer_and_bit_test (enum machine_mode, int);
40 static void do_jump_by_parts_greater (tree, tree, int, rtx, rtx);
41 static void do_jump_by_parts_equality (tree, tree, rtx, rtx);
42 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code, rtx,
43                                  rtx);
44
45 /* At the start of a function, record that we have no previously-pushed
46    arguments waiting to be popped.  */
47
48 void
49 init_pending_stack_adjust (void)
50 {
51   pending_stack_adjust = 0;
52 }
53
54 /* Discard any pending stack adjustment.  This avoid relying on the
55    RTL optimizers to remove useless adjustments when we know the
56    stack pointer value is dead.  */
57 void
58 discard_pending_stack_adjust (void)
59 {
60   stack_pointer_delta -= pending_stack_adjust;
61   pending_stack_adjust = 0;
62 }
63
64 /* When exiting from function, if safe, clear out any pending stack adjust
65    so the adjustment won't get done.
66
67    Note, if the current function calls alloca, then it must have a
68    frame pointer regardless of the value of flag_omit_frame_pointer.  */
69
70 void
71 clear_pending_stack_adjust (void)
72 {
73   if (optimize > 0
74       && (! flag_omit_frame_pointer || cfun->calls_alloca)
75       && EXIT_IGNORE_STACK)
76     discard_pending_stack_adjust ();
77 }
78
79 /* Pop any previously-pushed arguments that have not been popped yet.  */
80
81 void
82 do_pending_stack_adjust (void)
83 {
84   if (inhibit_defer_pop == 0)
85     {
86       if (pending_stack_adjust != 0)
87         adjust_stack (GEN_INT (pending_stack_adjust));
88       pending_stack_adjust = 0;
89     }
90 }
91 \f
92 /* Expand conditional expressions.  */
93
94 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.
95    LABEL is an rtx of code CODE_LABEL, in this function and all the
96    functions here.  */
97
98 void
99 jumpifnot (tree exp, rtx label)
100 {
101   do_jump (exp, label, NULL_RTX);
102 }
103
104 void
105 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx label)
106 {
107   do_jump_1 (code, op0, op1, label, NULL_RTX);
108 }
109
110 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero.  */
111
112 void
113 jumpif (tree exp, rtx label)
114 {
115   do_jump (exp, NULL_RTX, label);
116 }
117
118 void
119 jumpif_1 (enum tree_code code, tree op0, tree op1, rtx label)
120 {
121   do_jump_1 (code, op0, op1, NULL_RTX, label);
122 }
123
124 /* Used internally by prefer_and_bit_test.  */
125
126 static GTY(()) rtx and_reg;
127 static GTY(()) rtx and_test;
128 static GTY(()) rtx shift_test;
129
130 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
131    where X is an arbitrary register of mode MODE.  Return true if the former
132    is preferred.  */
133
134 static bool
135 prefer_and_bit_test (enum machine_mode mode, int bitnum)
136 {
137   if (and_test == 0)
138     {
139       /* Set up rtxes for the two variations.  Use NULL as a placeholder
140          for the BITNUM-based constants.  */
141       and_reg = gen_rtx_REG (mode, FIRST_PSEUDO_REGISTER);
142       and_test = gen_rtx_AND (mode, and_reg, NULL);
143       shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
144                                 const1_rtx);
145     }
146   else
147     {
148       /* Change the mode of the previously-created rtxes.  */
149       PUT_MODE (and_reg, mode);
150       PUT_MODE (and_test, mode);
151       PUT_MODE (shift_test, mode);
152       PUT_MODE (XEXP (shift_test, 0), mode);
153     }
154
155   /* Fill in the integers.  */
156   XEXP (and_test, 1)
157     = immed_double_const ((unsigned HOST_WIDE_INT) 1 << bitnum, 0, mode);
158   XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
159
160   return (rtx_cost (and_test, IF_THEN_ELSE, optimize_insn_for_speed_p ())
161           <= rtx_cost (shift_test, IF_THEN_ELSE, optimize_insn_for_speed_p ()));
162 }
163
164 /* Subroutine of do_jump, dealing with exploded comparisons of the type
165    OP0 CODE OP1 .  IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.  */
166
167 void
168 do_jump_1 (enum tree_code code, tree op0, tree op1,
169            rtx if_false_label, rtx if_true_label)
170 {
171   enum machine_mode mode;
172   rtx drop_through_label = 0;
173
174   switch (code)
175     {
176     case EQ_EXPR:
177       {
178         tree inner_type = TREE_TYPE (op0);
179
180         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
181                     != MODE_COMPLEX_FLOAT);
182         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
183                     != MODE_COMPLEX_INT);
184
185         if (integer_zerop (op1))
186           do_jump (op0, if_true_label, if_false_label);
187         else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
188                  && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
189           do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label);
190         else
191           do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label);
192         break;
193       }
194
195     case NE_EXPR:
196       {
197         tree inner_type = TREE_TYPE (op0);
198
199         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
200                     != MODE_COMPLEX_FLOAT);
201         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
202                     != MODE_COMPLEX_INT);
203
204         if (integer_zerop (op1))
205           do_jump (op0, if_false_label, if_true_label);
206         else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
207            && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
208           do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label);
209         else
210           do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label);
211         break;
212       }
213
214     case LT_EXPR:
215       mode = TYPE_MODE (TREE_TYPE (op0));
216       if (GET_MODE_CLASS (mode) == MODE_INT
217           && ! can_compare_p (LT, mode, ccp_jump))
218         do_jump_by_parts_greater (op0, op1, 1, if_false_label, if_true_label);
219       else
220         do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label);
221       break;
222
223     case LE_EXPR:
224       mode = TYPE_MODE (TREE_TYPE (op0));
225       if (GET_MODE_CLASS (mode) == MODE_INT
226           && ! can_compare_p (LE, mode, ccp_jump))
227         do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label);
228       else
229         do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label);
230       break;
231
232     case GT_EXPR:
233       mode = TYPE_MODE (TREE_TYPE (op0));
234       if (GET_MODE_CLASS (mode) == MODE_INT
235           && ! can_compare_p (GT, mode, ccp_jump))
236         do_jump_by_parts_greater (op0, op1, 0, if_false_label, if_true_label);
237       else
238         do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label);
239       break;
240
241     case GE_EXPR:
242       mode = TYPE_MODE (TREE_TYPE (op0));
243       if (GET_MODE_CLASS (mode) == MODE_INT
244           && ! can_compare_p (GE, mode, ccp_jump))
245         do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label);
246       else
247         do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label);
248       break;
249
250     case ORDERED_EXPR:
251       do_compare_and_jump (op0, op1, ORDERED, ORDERED,
252                            if_false_label, if_true_label);
253       break;
254
255     case UNORDERED_EXPR:
256       do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
257                            if_false_label, if_true_label);
258       break;
259
260     case UNLT_EXPR:
261       do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label);
262       break;
263
264     case UNLE_EXPR:
265       do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label);
266       break;
267
268     case UNGT_EXPR:
269       do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label);
270       break;
271
272     case UNGE_EXPR:
273       do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label);
274       break;
275
276     case UNEQ_EXPR:
277       do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label);
278       break;
279
280     case LTGT_EXPR:
281       do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label);
282       break;
283
284     case TRUTH_ANDIF_EXPR:
285       if (if_false_label == NULL_RTX)
286         {
287           drop_through_label = gen_label_rtx ();
288           do_jump (op0, drop_through_label, NULL_RTX);
289           do_jump (op1, NULL_RTX, if_true_label);
290         }
291       else
292         {
293           do_jump (op0, if_false_label, NULL_RTX);
294           do_jump (op1, if_false_label, if_true_label);
295         }
296       break;
297
298     case TRUTH_ORIF_EXPR:
299       if (if_true_label == NULL_RTX)
300         {
301           drop_through_label = gen_label_rtx ();
302           do_jump (op0, NULL_RTX, drop_through_label);
303           do_jump (op1, if_false_label, NULL_RTX);
304         }
305       else
306         {
307           do_jump (op0, NULL_RTX, if_true_label);
308           do_jump (op1, if_false_label, if_true_label);
309         }
310       break;
311
312     default:
313       gcc_unreachable ();
314     }
315
316   if (drop_through_label)
317     {
318       do_pending_stack_adjust ();
319       emit_label (drop_through_label);
320     }
321 }
322
323 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
324    the result is zero, or IF_TRUE_LABEL if the result is one.
325    Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
326    meaning fall through in that case.
327
328    do_jump always does any pending stack adjust except when it does not
329    actually perform a jump.  An example where there is no jump
330    is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.  */
331
332 void
333 do_jump (tree exp, rtx if_false_label, rtx if_true_label)
334 {
335   enum tree_code code = TREE_CODE (exp);
336   rtx temp;
337   int i;
338   tree type;
339   enum machine_mode mode;
340   rtx drop_through_label = 0;
341
342   switch (code)
343     {
344     case ERROR_MARK:
345       break;
346
347     case INTEGER_CST:
348       temp = integer_zerop (exp) ? if_false_label : if_true_label;
349       if (temp)
350         emit_jump (temp);
351       break;
352
353 #if 0
354       /* This is not true with #pragma weak  */
355     case ADDR_EXPR:
356       /* The address of something can never be zero.  */
357       if (if_true_label)
358         emit_jump (if_true_label);
359       break;
360 #endif
361
362     case NOP_EXPR:
363       if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
364           || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
365           || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
366           || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
367         goto normal;
368     case CONVERT_EXPR:
369       /* If we are narrowing the operand, we have to do the compare in the
370          narrower mode.  */
371       if ((TYPE_PRECISION (TREE_TYPE (exp))
372            < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
373         goto normal;
374     case NON_LVALUE_EXPR:
375     case ABS_EXPR:
376     case NEGATE_EXPR:
377     case LROTATE_EXPR:
378     case RROTATE_EXPR:
379       /* These cannot change zero->nonzero or vice versa.  */
380       do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label);
381       break;
382
383     case TRUTH_NOT_EXPR:
384       do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label);
385       break;
386
387     case COND_EXPR:
388       {
389         rtx label1 = gen_label_rtx ();
390         if (!if_true_label || !if_false_label)
391           {
392             drop_through_label = gen_label_rtx ();
393             if (!if_true_label)
394               if_true_label = drop_through_label;
395             if (!if_false_label)
396               if_false_label = drop_through_label;
397           }
398
399         do_pending_stack_adjust ();
400         do_jump (TREE_OPERAND (exp, 0), label1, NULL_RTX);
401         do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label);
402         emit_label (label1);
403         do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label);
404         break;
405       }
406
407     case COMPOUND_EXPR:
408       /* Lowered by gimplify.c.  */
409       gcc_unreachable ();
410
411     case COMPONENT_REF:
412     case BIT_FIELD_REF:
413     case ARRAY_REF:
414     case ARRAY_RANGE_REF:
415       {
416         HOST_WIDE_INT bitsize, bitpos;
417         int unsignedp;
418         enum machine_mode mode;
419         tree type;
420         tree offset;
421         int volatilep = 0;
422
423         /* Get description of this reference.  We don't actually care
424            about the underlying object here.  */
425         get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
426                              &unsignedp, &volatilep, false);
427
428         type = lang_hooks.types.type_for_size (bitsize, unsignedp);
429         if (! SLOW_BYTE_ACCESS
430             && type != 0 && bitsize >= 0
431             && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
432             && have_insn_for (COMPARE, TYPE_MODE (type)))
433           {
434             do_jump (fold_convert (type, exp), if_false_label, if_true_label);
435             break;
436           }
437         goto normal;
438       }
439
440     case MINUS_EXPR:
441       /* Nonzero iff operands of minus differ.  */
442       code = NE_EXPR;
443
444       /* FALLTHRU */
445     case EQ_EXPR:
446     case NE_EXPR:
447     case LT_EXPR:
448     case LE_EXPR:
449     case GT_EXPR:
450     case GE_EXPR:
451     case ORDERED_EXPR:
452     case UNORDERED_EXPR:
453     case UNLT_EXPR:
454     case UNLE_EXPR:
455     case UNGT_EXPR:
456     case UNGE_EXPR:
457     case UNEQ_EXPR:
458     case LTGT_EXPR:
459     case TRUTH_ANDIF_EXPR:
460     case TRUTH_ORIF_EXPR:
461       do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
462                  if_false_label, if_true_label);
463       break;
464
465     case BIT_AND_EXPR:
466       /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
467          See if the former is preferred for jump tests and restore it
468          if so.  */
469       if (integer_onep (TREE_OPERAND (exp, 1)))
470         {
471           tree exp0 = TREE_OPERAND (exp, 0);
472           rtx set_label, clr_label;
473
474           /* Strip narrowing integral type conversions.  */
475           while (CONVERT_EXPR_P (exp0)
476                  && TREE_OPERAND (exp0, 0) != error_mark_node
477                  && TYPE_PRECISION (TREE_TYPE (exp0))
478                     <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
479             exp0 = TREE_OPERAND (exp0, 0);
480
481           /* "exp0 ^ 1" inverts the sense of the single bit test.  */
482           if (TREE_CODE (exp0) == BIT_XOR_EXPR
483               && integer_onep (TREE_OPERAND (exp0, 1)))
484             {
485               exp0 = TREE_OPERAND (exp0, 0);
486               clr_label = if_true_label;
487               set_label = if_false_label;
488             }
489           else
490             {
491               clr_label = if_false_label;
492               set_label = if_true_label;
493             }
494
495           if (TREE_CODE (exp0) == RSHIFT_EXPR)
496             {
497               tree arg = TREE_OPERAND (exp0, 0);
498               tree shift = TREE_OPERAND (exp0, 1);
499               tree argtype = TREE_TYPE (arg);
500               if (TREE_CODE (shift) == INTEGER_CST
501                   && compare_tree_int (shift, 0) >= 0
502                   && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
503                   && prefer_and_bit_test (TYPE_MODE (argtype),
504                                           TREE_INT_CST_LOW (shift)))
505                 {
506                   unsigned HOST_WIDE_INT mask
507                     = (unsigned HOST_WIDE_INT) 1 << TREE_INT_CST_LOW (shift);
508                   do_jump (build2 (BIT_AND_EXPR, argtype, arg,
509                                    build_int_cst_wide_type (argtype, mask, 0)),
510                            clr_label, set_label);
511                   break;
512                 }
513             }
514         }
515
516       /* If we are AND'ing with a small constant, do this comparison in the
517          smallest type that fits.  If the machine doesn't have comparisons
518          that small, it will be converted back to the wider comparison.
519          This helps if we are testing the sign bit of a narrower object.
520          combine can't do this for us because it can't know whether a
521          ZERO_EXTRACT or a compare in a smaller mode exists, but we do.  */
522
523       if (! SLOW_BYTE_ACCESS
524           && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
525           && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
526           && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
527           && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
528           && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
529           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
530           && have_insn_for (COMPARE, TYPE_MODE (type)))
531         {
532           do_jump (fold_convert (type, exp), if_false_label, if_true_label);
533           break;
534         }
535
536       if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
537           || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
538         goto normal;
539
540       /* Boolean comparisons can be compiled as TRUTH_AND_EXPR.  */
541
542     case TRUTH_AND_EXPR:
543       /* High branch cost, expand as the bitwise AND of the conditions.
544          Do the same if the RHS has side effects, because we're effectively
545          turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR.  */
546       if (BRANCH_COST (optimize_insn_for_speed_p (),
547                        false) >= 4
548           || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
549         goto normal;
550
551     case BIT_IOR_EXPR:
552     case TRUTH_OR_EXPR:
553       /* High branch cost, expand as the bitwise OR of the conditions.
554          Do the same if the RHS has side effects, because we're effectively
555          turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR.  */
556       if (BRANCH_COST (optimize_insn_for_speed_p (), false)>= 4
557           || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
558         goto normal;
559
560       /* Fall through and generate the normal code.  */
561     default:
562     normal:
563       temp = expand_normal (exp);
564       do_pending_stack_adjust ();
565       /* The RTL optimizers prefer comparisons against pseudos.  */
566       if (GET_CODE (temp) == SUBREG)
567         {
568           /* Compare promoted variables in their promoted mode.  */
569           if (SUBREG_PROMOTED_VAR_P (temp)
570               && REG_P (XEXP (temp, 0)))
571             temp = XEXP (temp, 0);
572           else
573             temp = copy_to_reg (temp);
574         }
575       do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
576                                NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
577                                GET_MODE (temp), NULL_RTX,
578                                if_false_label, if_true_label);
579     }
580
581   if (drop_through_label)
582     {
583       do_pending_stack_adjust ();
584       emit_label (drop_through_label);
585     }
586 }
587 \f
588 /* Compare OP0 with OP1, word at a time, in mode MODE.
589    UNSIGNEDP says to do unsigned comparison.
590    Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise.  */
591
592 static void
593 do_jump_by_parts_greater_rtx (enum machine_mode mode, int unsignedp, rtx op0,
594                               rtx op1, rtx if_false_label, rtx if_true_label)
595 {
596   int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
597   rtx drop_through_label = 0;
598   int i;
599
600   if (! if_true_label || ! if_false_label)
601     drop_through_label = gen_label_rtx ();
602   if (! if_true_label)
603     if_true_label = drop_through_label;
604   if (! if_false_label)
605     if_false_label = drop_through_label;
606
607   /* Compare a word at a time, high order first.  */
608   for (i = 0; i < nwords; i++)
609     {
610       rtx op0_word, op1_word;
611
612       if (WORDS_BIG_ENDIAN)
613         {
614           op0_word = operand_subword_force (op0, i, mode);
615           op1_word = operand_subword_force (op1, i, mode);
616         }
617       else
618         {
619           op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
620           op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
621         }
622
623       /* All but high-order word must be compared as unsigned.  */
624       do_compare_rtx_and_jump (op0_word, op1_word, GT,
625                                (unsignedp || i > 0), word_mode, NULL_RTX,
626                                NULL_RTX, if_true_label);
627
628       /* Consider lower words only if these are equal.  */
629       do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
630                                NULL_RTX, NULL_RTX, if_false_label);
631     }
632
633   if (if_false_label)
634     emit_jump (if_false_label);
635   if (drop_through_label)
636     emit_label (drop_through_label);
637 }
638
639 /* Given a comparison expression EXP for values too wide to be compared
640    with one insn, test the comparison and jump to the appropriate label.
641    The code of EXP is ignored; we always test GT if SWAP is 0,
642    and LT if SWAP is 1.  */
643
644 static void
645 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
646                           rtx if_false_label, rtx if_true_label)
647 {
648   rtx op0 = expand_normal (swap ? treeop1 : treeop0);
649   rtx op1 = expand_normal (swap ? treeop0 : treeop1);
650   enum machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
651   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
652
653   do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
654                                 if_true_label);
655 }
656 \f
657 /* Jump according to whether OP0 is 0.  We assume that OP0 has an integer
658    mode, MODE, that is too wide for the available compare insns.  Either
659    Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
660    to indicate drop through.  */
661
662 static void
663 do_jump_by_parts_zero_rtx (enum machine_mode mode, rtx op0,
664                            rtx if_false_label, rtx if_true_label)
665 {
666   int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
667   rtx part;
668   int i;
669   rtx drop_through_label = 0;
670
671   /* The fastest way of doing this comparison on almost any machine is to
672      "or" all the words and compare the result.  If all have to be loaded
673      from memory and this is a very wide item, it's possible this may
674      be slower, but that's highly unlikely.  */
675
676   part = gen_reg_rtx (word_mode);
677   emit_move_insn (part, operand_subword_force (op0, 0, mode));
678   for (i = 1; i < nwords && part != 0; i++)
679     part = expand_binop (word_mode, ior_optab, part,
680                          operand_subword_force (op0, i, mode),
681                          part, 1, OPTAB_WIDEN);
682
683   if (part != 0)
684     {
685       do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
686                                NULL_RTX, if_false_label, if_true_label);
687
688       return;
689     }
690
691   /* If we couldn't do the "or" simply, do this with a series of compares.  */
692   if (! if_false_label)
693     drop_through_label = if_false_label = gen_label_rtx ();
694
695   for (i = 0; i < nwords; i++)
696     do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
697                              const0_rtx, EQ, 1, word_mode, NULL_RTX,
698                              if_false_label, NULL_RTX);
699
700   if (if_true_label)
701     emit_jump (if_true_label);
702
703   if (drop_through_label)
704     emit_label (drop_through_label);
705 }
706
707 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
708    where MODE is an integer mode too wide to be compared with one insn.
709    Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
710    to indicate drop through.  */
711
712 static void
713 do_jump_by_parts_equality_rtx (enum machine_mode mode, rtx op0, rtx op1,
714                                rtx if_false_label, rtx if_true_label)
715 {
716   int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
717   rtx drop_through_label = 0;
718   int i;
719
720   if (op1 == const0_rtx)
721     {
722       do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label);
723       return;
724     }
725   else if (op0 == const0_rtx)
726     {
727       do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label);
728       return;
729     }
730
731   if (! if_false_label)
732     drop_through_label = if_false_label = gen_label_rtx ();
733
734   for (i = 0; i < nwords; i++)
735     do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
736                              operand_subword_force (op1, i, mode),
737                              EQ, 0, word_mode, NULL_RTX,
738                              if_false_label, NULL_RTX);
739
740   if (if_true_label)
741     emit_jump (if_true_label);
742   if (drop_through_label)
743     emit_label (drop_through_label);
744 }
745
746 /* Given an EQ_EXPR expression EXP for values too wide to be compared
747    with one insn, test the comparison and jump to the appropriate label.  */
748
749 static void
750 do_jump_by_parts_equality (tree treeop0, tree treeop1, rtx if_false_label,
751                            rtx if_true_label)
752 {
753   rtx op0 = expand_normal (treeop0);
754   rtx op1 = expand_normal (treeop1);
755   enum machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
756   do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
757                                  if_true_label);
758 }
759 \f
760 /* Split a comparison into two others, the second of which has the other
761    "orderedness".  The first is always ORDERED or UNORDERED if MODE
762    does not honor NaNs (which means that it can be skipped in that case;
763    see do_compare_rtx_and_jump).
764
765    The two conditions are written in *CODE1 and *CODE2.  Return true if
766    the conditions must be ANDed, false if they must be ORed.  */
767
768 bool
769 split_comparison (enum rtx_code code, enum machine_mode mode,
770                   enum rtx_code *code1, enum rtx_code *code2)
771 {
772   switch (code)
773     {
774     case LT:
775       *code1 = ORDERED;
776       *code2 = UNLT;
777       return true;
778     case LE:
779       *code1 = ORDERED;
780       *code2 = UNLE;
781       return true;
782     case GT:
783       *code1 = ORDERED;
784       *code2 = UNGT;
785       return true;
786     case GE:
787       *code1 = ORDERED;
788       *code2 = UNGE;
789       return true;
790     case EQ:
791       *code1 = ORDERED;
792       *code2 = UNEQ;
793       return true;
794     case NE:
795       *code1 = UNORDERED;
796       *code2 = LTGT;
797       return false;
798     case UNLT:
799       *code1 = UNORDERED;
800       *code2 = LT;
801       return false;
802     case UNLE:
803       *code1 = UNORDERED;
804       *code2 = LE;
805       return false;
806     case UNGT:
807       *code1 = UNORDERED;
808       *code2 = GT;
809       return false;
810     case UNGE:
811       *code1 = UNORDERED;
812       *code2 = GE;
813       return false;
814     case UNEQ:
815       *code1 = UNORDERED;
816       *code2 = EQ;
817       return false;
818     case LTGT:
819       /* Do not turn a trapping comparison into a non-trapping one.  */
820       if (HONOR_SNANS (mode))
821         {
822           *code1 = LT;
823           *code2 = GT;
824           return false;
825         }
826       else
827         {
828           *code1 = ORDERED;
829           *code2 = NE;
830           return true;
831         }
832     default:
833       gcc_unreachable ();
834     }
835 }
836
837
838 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
839    The decision as to signed or unsigned comparison must be made by the caller.
840
841    If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
842    compared.  */
843
844 void
845 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
846                          enum machine_mode mode, rtx size, rtx if_false_label,
847                          rtx if_true_label)
848 {
849   rtx tem;
850   rtx dummy_label = NULL_RTX;
851
852   /* Reverse the comparison if that is safe and we want to jump if it is
853      false.  Also convert to the reverse comparison if the target can
854      implement it.  */
855   if ((! if_true_label
856        || ! can_compare_p (code, mode, ccp_jump))
857       && (! FLOAT_MODE_P (mode)
858           || code == ORDERED || code == UNORDERED
859           || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
860           || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
861     {
862       enum rtx_code rcode;
863       if (FLOAT_MODE_P (mode))
864         rcode = reverse_condition_maybe_unordered (code);
865       else
866         rcode = reverse_condition (code);
867
868       /* Canonicalize to UNORDERED for the libcall.  */
869       if (can_compare_p (rcode, mode, ccp_jump)
870           || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
871         {
872           tem = if_true_label;
873           if_true_label = if_false_label;
874           if_false_label = tem;
875           code = rcode;
876         }
877     }
878
879   /* If one operand is constant, make it the second one.  Only do this
880      if the other operand is not constant as well.  */
881
882   if (swap_commutative_operands_p (op0, op1))
883     {
884       tem = op0;
885       op0 = op1;
886       op1 = tem;
887       code = swap_condition (code);
888     }
889
890   do_pending_stack_adjust ();
891
892   code = unsignedp ? unsigned_condition (code) : code;
893   if (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
894                                                  op0, op1)))
895     {
896       if (CONSTANT_P (tem))
897         {
898           rtx label = (tem == const0_rtx || tem == CONST0_RTX (mode))
899                       ? if_false_label : if_true_label;
900           if (label)
901             emit_jump (label);
902           return;
903         }
904
905       code = GET_CODE (tem);
906       mode = GET_MODE (tem);
907       op0 = XEXP (tem, 0);
908       op1 = XEXP (tem, 1);
909       unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
910     }
911
912   if (! if_true_label)
913     dummy_label = if_true_label = gen_label_rtx ();
914
915   if (GET_MODE_CLASS (mode) == MODE_INT
916       && ! can_compare_p (code, mode, ccp_jump))
917     {
918       switch (code)
919         {
920         case LTU:
921           do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
922                                         if_false_label, if_true_label);
923           break;
924
925         case LEU:
926           do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
927                                         if_true_label, if_false_label);
928           break;
929
930         case GTU:
931           do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
932                                         if_false_label, if_true_label);
933           break;
934
935         case GEU:
936           do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
937                                         if_true_label, if_false_label);
938           break;
939
940         case LT:
941           do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
942                                         if_false_label, if_true_label);
943           break;
944
945         case LE:
946           do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
947                                         if_true_label, if_false_label);
948           break;
949
950         case GT:
951           do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
952                                         if_false_label, if_true_label);
953           break;
954
955         case GE:
956           do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
957                                         if_true_label, if_false_label);
958           break;
959
960         case EQ:
961           do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
962                                          if_true_label);
963           break;
964
965         case NE:
966           do_jump_by_parts_equality_rtx (mode, op0, op1, if_true_label,
967                                          if_false_label);
968           break;
969
970         default:
971           gcc_unreachable ();
972         }
973     }
974   else
975     {
976       if (GET_MODE_CLASS (mode) == MODE_FLOAT
977           && ! can_compare_p (code, mode, ccp_jump)
978           && can_compare_p (swap_condition (code), mode, ccp_jump))
979         {
980           rtx tmp;
981           code = swap_condition (code);
982           tmp = op0;
983           op0 = op1;
984           op1 = tmp;
985         }
986
987       else if (GET_MODE_CLASS (mode) == MODE_FLOAT
988                && ! can_compare_p (code, mode, ccp_jump)
989
990                /* Never split ORDERED and UNORDERED.  These must be implemented.  */
991                && (code != ORDERED && code != UNORDERED)
992
993                /* Split a floating-point comparison if we can jump on other
994                   conditions...  */
995                && (have_insn_for (COMPARE, mode)
996
997                    /* ... or if there is no libcall for it.  */
998                    || code_to_optab[code] == NULL))
999         {
1000           enum rtx_code first_code;
1001           bool and_them = split_comparison (code, mode, &first_code, &code);
1002
1003           /* If there are no NaNs, the first comparison should always fall
1004              through.  */
1005           if (!HONOR_NANS (mode))
1006             gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1007
1008           else
1009             {
1010               if (and_them)
1011                 {
1012                   rtx dest_label;
1013                   /* If we only jump if true, just bypass the second jump.  */
1014                   if (! if_false_label)
1015                     {
1016                       if (! dummy_label)
1017                         dummy_label = gen_label_rtx ();
1018                       dest_label = dummy_label;
1019                     }
1020                   else
1021                     dest_label = if_false_label;
1022                   do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1023                                            size, dest_label, NULL_RTX);
1024                 }
1025               else
1026                 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1027                                          size, NULL_RTX, if_true_label);
1028             }
1029         }
1030
1031       emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1032                                if_true_label);
1033     }
1034
1035   if (if_false_label)
1036     emit_jump (if_false_label);
1037   if (dummy_label)
1038     emit_label (dummy_label);
1039 }
1040
1041 /* Generate code for a comparison expression EXP (including code to compute
1042    the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1043    IF_TRUE_LABEL.  One of the labels can be NULL_RTX, in which case the
1044    generated code will drop through.
1045    SIGNED_CODE should be the rtx operation for this comparison for
1046    signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1047
1048    We force a stack adjustment unless there are currently
1049    things pushed on the stack that aren't yet used.  */
1050
1051 static void
1052 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1053                      enum rtx_code unsigned_code, rtx if_false_label,
1054                      rtx if_true_label)
1055 {
1056   rtx op0, op1;
1057   tree type;
1058   enum machine_mode mode;
1059   int unsignedp;
1060   enum rtx_code code;
1061
1062   /* Don't crash if the comparison was erroneous.  */
1063   op0 = expand_normal (treeop0);
1064   if (TREE_CODE (treeop0) == ERROR_MARK)
1065     return;
1066
1067   op1 = expand_normal (treeop1);
1068   if (TREE_CODE (treeop1) == ERROR_MARK)
1069     return;
1070
1071   type = TREE_TYPE (treeop0);
1072   mode = TYPE_MODE (type);
1073   if (TREE_CODE (treeop0) == INTEGER_CST
1074       && (TREE_CODE (treeop1) != INTEGER_CST
1075           || (GET_MODE_BITSIZE (mode)
1076               > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1077     {
1078       /* op0 might have been replaced by promoted constant, in which
1079          case the type of second argument should be used.  */
1080       type = TREE_TYPE (treeop1);
1081       mode = TYPE_MODE (type);
1082     }
1083   unsignedp = TYPE_UNSIGNED (type);
1084   code = unsignedp ? unsigned_code : signed_code;
1085
1086 #ifdef HAVE_canonicalize_funcptr_for_compare
1087   /* If function pointers need to be "canonicalized" before they can
1088      be reliably compared, then canonicalize them.
1089      Only do this if *both* sides of the comparison are function pointers.
1090      If one side isn't, we want a noncanonicalized comparison.  See PR
1091      middle-end/17564.  */
1092   if (HAVE_canonicalize_funcptr_for_compare
1093       && TREE_CODE (TREE_TYPE (treeop0)) == POINTER_TYPE
1094       && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0)))
1095           == FUNCTION_TYPE
1096       && TREE_CODE (TREE_TYPE (treeop1)) == POINTER_TYPE
1097       && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1)))
1098           == FUNCTION_TYPE)
1099     {
1100       rtx new_op0 = gen_reg_rtx (mode);
1101       rtx new_op1 = gen_reg_rtx (mode);
1102
1103       emit_insn (gen_canonicalize_funcptr_for_compare (new_op0, op0));
1104       op0 = new_op0;
1105
1106       emit_insn (gen_canonicalize_funcptr_for_compare (new_op1, op1));
1107       op1 = new_op1;
1108     }
1109 #endif
1110
1111   do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1112                            ((mode == BLKmode)
1113                             ? expr_size (treeop0) : NULL_RTX),
1114                            if_false_label, if_true_label);
1115 }
1116
1117 #include "gt-dojump.h"