* gcc/ChangeLog: Fix ChangeLog entry.
[official-gcc.git] / gcc / dojump.c
blob2aef34d307a572488700c31dcb0884f4404d2a94
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "stor-layout.h"
27 #include "flags.h"
28 #include "function.h"
29 #include "insn-config.h"
30 #include "insn-attr.h"
31 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
32 #include "expr.h"
33 #include "optabs.h"
34 #include "langhooks.h"
35 #include "ggc.h"
36 #include "basic-block.h"
37 #include "tm_p.h"
39 static bool prefer_and_bit_test (enum machine_mode, int);
40 static void do_jump_by_parts_greater (tree, tree, int, rtx, rtx, int);
41 static void do_jump_by_parts_equality (tree, tree, rtx, rtx, int);
42 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code, rtx,
43 rtx, int);
45 /* Invert probability if there is any. -1 stands for unknown. */
47 static inline int
48 inv (int prob)
50 return prob == -1 ? -1 : REG_BR_PROB_BASE - prob;
53 /* At the start of a function, record that we have no previously-pushed
54 arguments waiting to be popped. */
56 void
57 init_pending_stack_adjust (void)
59 pending_stack_adjust = 0;
62 /* Discard any pending stack adjustment. This avoid relying on the
63 RTL optimizers to remove useless adjustments when we know the
64 stack pointer value is dead. */
65 void
66 discard_pending_stack_adjust (void)
68 stack_pointer_delta -= pending_stack_adjust;
69 pending_stack_adjust = 0;
72 /* When exiting from function, if safe, clear out any pending stack adjust
73 so the adjustment won't get done.
75 Note, if the current function calls alloca, then it must have a
76 frame pointer regardless of the value of flag_omit_frame_pointer. */
78 void
79 clear_pending_stack_adjust (void)
81 if (optimize > 0
82 && (! flag_omit_frame_pointer || cfun->calls_alloca)
83 && EXIT_IGNORE_STACK)
84 discard_pending_stack_adjust ();
87 /* Pop any previously-pushed arguments that have not been popped yet. */
89 void
90 do_pending_stack_adjust (void)
92 if (inhibit_defer_pop == 0)
94 if (pending_stack_adjust != 0)
95 adjust_stack (GEN_INT (pending_stack_adjust));
96 pending_stack_adjust = 0;
100 /* Expand conditional expressions. */
102 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.
103 LABEL is an rtx of code CODE_LABEL, in this function and all the
104 functions here. */
106 void
107 jumpifnot (tree exp, rtx label, int prob)
109 do_jump (exp, label, NULL_RTX, inv (prob));
112 void
113 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx label, int prob)
115 do_jump_1 (code, op0, op1, label, NULL_RTX, inv (prob));
118 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
120 void
121 jumpif (tree exp, rtx label, int prob)
123 do_jump (exp, NULL_RTX, label, prob);
126 void
127 jumpif_1 (enum tree_code code, tree op0, tree op1, rtx label, int prob)
129 do_jump_1 (code, op0, op1, NULL_RTX, label, prob);
132 /* Used internally by prefer_and_bit_test. */
134 static GTY(()) rtx and_reg;
135 static GTY(()) rtx and_test;
136 static GTY(()) rtx shift_test;
138 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
139 where X is an arbitrary register of mode MODE. Return true if the former
140 is preferred. */
142 static bool
143 prefer_and_bit_test (enum machine_mode mode, int bitnum)
145 bool speed_p;
147 if (and_test == 0)
149 /* Set up rtxes for the two variations. Use NULL as a placeholder
150 for the BITNUM-based constants. */
151 and_reg = gen_rtx_REG (mode, FIRST_PSEUDO_REGISTER);
152 and_test = gen_rtx_AND (mode, and_reg, NULL);
153 shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
154 const1_rtx);
156 else
158 /* Change the mode of the previously-created rtxes. */
159 PUT_MODE (and_reg, mode);
160 PUT_MODE (and_test, mode);
161 PUT_MODE (shift_test, mode);
162 PUT_MODE (XEXP (shift_test, 0), mode);
165 /* Fill in the integers. */
166 XEXP (and_test, 1)
167 = immed_double_int_const (double_int_zero.set_bit (bitnum), mode);
168 XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
170 speed_p = optimize_insn_for_speed_p ();
171 return (rtx_cost (and_test, IF_THEN_ELSE, 0, speed_p)
172 <= rtx_cost (shift_test, IF_THEN_ELSE, 0, speed_p));
175 /* Subroutine of do_jump, dealing with exploded comparisons of the type
176 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
177 PROB is probability of jump to if_true_label, or -1 if unknown. */
179 void
180 do_jump_1 (enum tree_code code, tree op0, tree op1,
181 rtx if_false_label, rtx if_true_label, int prob)
183 enum machine_mode mode;
184 rtx drop_through_label = 0;
186 switch (code)
188 case EQ_EXPR:
190 tree inner_type = TREE_TYPE (op0);
192 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
193 != MODE_COMPLEX_FLOAT);
194 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
195 != MODE_COMPLEX_INT);
197 if (integer_zerop (op1))
198 do_jump (op0, if_true_label, if_false_label, inv (prob));
199 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
200 && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
201 do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label,
202 prob);
203 else
204 do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
205 prob);
206 break;
209 case NE_EXPR:
211 tree inner_type = TREE_TYPE (op0);
213 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
214 != MODE_COMPLEX_FLOAT);
215 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
216 != MODE_COMPLEX_INT);
218 if (integer_zerop (op1))
219 do_jump (op0, if_false_label, if_true_label, prob);
220 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
221 && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
222 do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label,
223 inv (prob));
224 else
225 do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
226 prob);
227 break;
230 case LT_EXPR:
231 mode = TYPE_MODE (TREE_TYPE (op0));
232 if (GET_MODE_CLASS (mode) == MODE_INT
233 && ! can_compare_p (LT, mode, ccp_jump))
234 do_jump_by_parts_greater (op0, op1, 1, if_false_label, if_true_label,
235 prob);
236 else
237 do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
238 prob);
239 break;
241 case LE_EXPR:
242 mode = TYPE_MODE (TREE_TYPE (op0));
243 if (GET_MODE_CLASS (mode) == MODE_INT
244 && ! can_compare_p (LE, mode, ccp_jump))
245 do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label,
246 inv (prob));
247 else
248 do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
249 prob);
250 break;
252 case GT_EXPR:
253 mode = TYPE_MODE (TREE_TYPE (op0));
254 if (GET_MODE_CLASS (mode) == MODE_INT
255 && ! can_compare_p (GT, mode, ccp_jump))
256 do_jump_by_parts_greater (op0, op1, 0, if_false_label, if_true_label,
257 prob);
258 else
259 do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
260 prob);
261 break;
263 case GE_EXPR:
264 mode = TYPE_MODE (TREE_TYPE (op0));
265 if (GET_MODE_CLASS (mode) == MODE_INT
266 && ! can_compare_p (GE, mode, ccp_jump))
267 do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label,
268 inv (prob));
269 else
270 do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
271 prob);
272 break;
274 case ORDERED_EXPR:
275 do_compare_and_jump (op0, op1, ORDERED, ORDERED,
276 if_false_label, if_true_label, prob);
277 break;
279 case UNORDERED_EXPR:
280 do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
281 if_false_label, if_true_label, prob);
282 break;
284 case UNLT_EXPR:
285 do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
286 prob);
287 break;
289 case UNLE_EXPR:
290 do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
291 prob);
292 break;
294 case UNGT_EXPR:
295 do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
296 prob);
297 break;
299 case UNGE_EXPR:
300 do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
301 prob);
302 break;
304 case UNEQ_EXPR:
305 do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
306 prob);
307 break;
309 case LTGT_EXPR:
310 do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
311 prob);
312 break;
314 case TRUTH_ANDIF_EXPR:
316 /* Spread the probability that the expression is false evenly between
317 the two conditions. So the first condition is false half the total
318 probability of being false. The second condition is false the other
319 half of the total probability of being false, so its jump has a false
320 probability of half the total, relative to the probability we
321 reached it (i.e. the first condition was true). */
322 int op0_prob = -1;
323 int op1_prob = -1;
324 if (prob != -1)
326 int false_prob = inv (prob);
327 int op0_false_prob = false_prob / 2;
328 int op1_false_prob = GCOV_COMPUTE_SCALE ((false_prob / 2),
329 inv (op0_false_prob));
330 /* Get the probability that each jump below is true. */
331 op0_prob = inv (op0_false_prob);
332 op1_prob = inv (op1_false_prob);
334 if (if_false_label == NULL_RTX)
336 drop_through_label = gen_label_rtx ();
337 do_jump (op0, drop_through_label, NULL_RTX, op0_prob);
338 do_jump (op1, NULL_RTX, if_true_label, op1_prob);
340 else
342 do_jump (op0, if_false_label, NULL_RTX, op0_prob);
343 do_jump (op1, if_false_label, if_true_label, op1_prob);
345 break;
348 case TRUTH_ORIF_EXPR:
350 /* Spread the probability evenly between the two conditions. So
351 the first condition has half the total probability of being true.
352 The second condition has the other half of the total probability,
353 so its jump has a probability of half the total, relative to
354 the probability we reached it (i.e. the first condition was false). */
355 int op0_prob = -1;
356 int op1_prob = -1;
357 if (prob != -1)
359 op0_prob = prob / 2;
360 op1_prob = GCOV_COMPUTE_SCALE ((prob / 2), inv (op0_prob));
362 if (if_true_label == NULL_RTX)
364 drop_through_label = gen_label_rtx ();
365 do_jump (op0, NULL_RTX, drop_through_label, op0_prob);
366 do_jump (op1, if_false_label, NULL_RTX, op1_prob);
368 else
370 do_jump (op0, NULL_RTX, if_true_label, op0_prob);
371 do_jump (op1, if_false_label, if_true_label, op1_prob);
373 break;
376 default:
377 gcc_unreachable ();
380 if (drop_through_label)
382 do_pending_stack_adjust ();
383 emit_label (drop_through_label);
387 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
388 the result is zero, or IF_TRUE_LABEL if the result is one.
389 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
390 meaning fall through in that case.
392 do_jump always does any pending stack adjust except when it does not
393 actually perform a jump. An example where there is no jump
394 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
396 PROB is probability of jump to if_true_label, or -1 if unknown. */
398 void
399 do_jump (tree exp, rtx if_false_label, rtx if_true_label, int prob)
401 enum tree_code code = TREE_CODE (exp);
402 rtx temp;
403 int i;
404 tree type;
405 enum machine_mode mode;
406 rtx drop_through_label = 0;
408 switch (code)
410 case ERROR_MARK:
411 break;
413 case INTEGER_CST:
414 temp = integer_zerop (exp) ? if_false_label : if_true_label;
415 if (temp)
416 emit_jump (temp);
417 break;
419 #if 0
420 /* This is not true with #pragma weak */
421 case ADDR_EXPR:
422 /* The address of something can never be zero. */
423 if (if_true_label)
424 emit_jump (if_true_label);
425 break;
426 #endif
428 case NOP_EXPR:
429 if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
430 || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
431 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
432 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
433 goto normal;
434 case CONVERT_EXPR:
435 /* If we are narrowing the operand, we have to do the compare in the
436 narrower mode. */
437 if ((TYPE_PRECISION (TREE_TYPE (exp))
438 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
439 goto normal;
440 case NON_LVALUE_EXPR:
441 case ABS_EXPR:
442 case NEGATE_EXPR:
443 case LROTATE_EXPR:
444 case RROTATE_EXPR:
445 /* These cannot change zero->nonzero or vice versa. */
446 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
447 break;
449 case TRUTH_NOT_EXPR:
450 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
451 inv (prob));
452 break;
454 case COND_EXPR:
456 rtx label1 = gen_label_rtx ();
457 if (!if_true_label || !if_false_label)
459 drop_through_label = gen_label_rtx ();
460 if (!if_true_label)
461 if_true_label = drop_through_label;
462 if (!if_false_label)
463 if_false_label = drop_through_label;
466 do_pending_stack_adjust ();
467 do_jump (TREE_OPERAND (exp, 0), label1, NULL_RTX, -1);
468 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
469 emit_label (label1);
470 do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
471 break;
474 case COMPOUND_EXPR:
475 /* Lowered by gimplify.c. */
476 gcc_unreachable ();
478 case MINUS_EXPR:
479 /* Nonzero iff operands of minus differ. */
480 code = NE_EXPR;
482 /* FALLTHRU */
483 case EQ_EXPR:
484 case NE_EXPR:
485 case LT_EXPR:
486 case LE_EXPR:
487 case GT_EXPR:
488 case GE_EXPR:
489 case ORDERED_EXPR:
490 case UNORDERED_EXPR:
491 case UNLT_EXPR:
492 case UNLE_EXPR:
493 case UNGT_EXPR:
494 case UNGE_EXPR:
495 case UNEQ_EXPR:
496 case LTGT_EXPR:
497 case TRUTH_ANDIF_EXPR:
498 case TRUTH_ORIF_EXPR:
499 other_code:
500 do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
501 if_false_label, if_true_label, prob);
502 break;
504 case BIT_AND_EXPR:
505 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
506 See if the former is preferred for jump tests and restore it
507 if so. */
508 if (integer_onep (TREE_OPERAND (exp, 1)))
510 tree exp0 = TREE_OPERAND (exp, 0);
511 rtx set_label, clr_label;
512 int setclr_prob = prob;
514 /* Strip narrowing integral type conversions. */
515 while (CONVERT_EXPR_P (exp0)
516 && TREE_OPERAND (exp0, 0) != error_mark_node
517 && TYPE_PRECISION (TREE_TYPE (exp0))
518 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
519 exp0 = TREE_OPERAND (exp0, 0);
521 /* "exp0 ^ 1" inverts the sense of the single bit test. */
522 if (TREE_CODE (exp0) == BIT_XOR_EXPR
523 && integer_onep (TREE_OPERAND (exp0, 1)))
525 exp0 = TREE_OPERAND (exp0, 0);
526 clr_label = if_true_label;
527 set_label = if_false_label;
528 setclr_prob = inv (prob);
530 else
532 clr_label = if_false_label;
533 set_label = if_true_label;
536 if (TREE_CODE (exp0) == RSHIFT_EXPR)
538 tree arg = TREE_OPERAND (exp0, 0);
539 tree shift = TREE_OPERAND (exp0, 1);
540 tree argtype = TREE_TYPE (arg);
541 if (TREE_CODE (shift) == INTEGER_CST
542 && compare_tree_int (shift, 0) >= 0
543 && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
544 && prefer_and_bit_test (TYPE_MODE (argtype),
545 TREE_INT_CST_LOW (shift)))
547 unsigned HOST_WIDE_INT mask
548 = (unsigned HOST_WIDE_INT) 1 << TREE_INT_CST_LOW (shift);
549 do_jump (build2 (BIT_AND_EXPR, argtype, arg,
550 build_int_cstu (argtype, mask)),
551 clr_label, set_label, setclr_prob);
552 break;
557 /* If we are AND'ing with a small constant, do this comparison in the
558 smallest type that fits. If the machine doesn't have comparisons
559 that small, it will be converted back to the wider comparison.
560 This helps if we are testing the sign bit of a narrower object.
561 combine can't do this for us because it can't know whether a
562 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
564 if (! SLOW_BYTE_ACCESS
565 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
566 && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
567 && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
568 && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
569 && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
570 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
571 && have_insn_for (COMPARE, TYPE_MODE (type)))
573 do_jump (fold_convert (type, exp), if_false_label, if_true_label,
574 prob);
575 break;
578 if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
579 || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
580 goto normal;
582 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
584 case TRUTH_AND_EXPR:
585 /* High branch cost, expand as the bitwise AND of the conditions.
586 Do the same if the RHS has side effects, because we're effectively
587 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
588 if (BRANCH_COST (optimize_insn_for_speed_p (),
589 false) >= 4
590 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
591 goto normal;
592 code = TRUTH_ANDIF_EXPR;
593 goto other_code;
595 case BIT_IOR_EXPR:
596 case TRUTH_OR_EXPR:
597 /* High branch cost, expand as the bitwise OR of the conditions.
598 Do the same if the RHS has side effects, because we're effectively
599 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
600 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
601 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
602 goto normal;
603 code = TRUTH_ORIF_EXPR;
604 goto other_code;
606 /* Fall through and generate the normal code. */
607 default:
608 normal:
609 temp = expand_normal (exp);
610 do_pending_stack_adjust ();
611 /* The RTL optimizers prefer comparisons against pseudos. */
612 if (GET_CODE (temp) == SUBREG)
614 /* Compare promoted variables in their promoted mode. */
615 if (SUBREG_PROMOTED_VAR_P (temp)
616 && REG_P (XEXP (temp, 0)))
617 temp = XEXP (temp, 0);
618 else
619 temp = copy_to_reg (temp);
621 do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
622 NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
623 GET_MODE (temp), NULL_RTX,
624 if_false_label, if_true_label, prob);
627 if (drop_through_label)
629 do_pending_stack_adjust ();
630 emit_label (drop_through_label);
634 /* Compare OP0 with OP1, word at a time, in mode MODE.
635 UNSIGNEDP says to do unsigned comparison.
636 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
638 static void
639 do_jump_by_parts_greater_rtx (enum machine_mode mode, int unsignedp, rtx op0,
640 rtx op1, rtx if_false_label, rtx if_true_label,
641 int prob)
643 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
644 rtx drop_through_label = 0;
645 bool drop_through_if_true = false, drop_through_if_false = false;
646 enum rtx_code code = GT;
647 int i;
649 if (! if_true_label || ! if_false_label)
650 drop_through_label = gen_label_rtx ();
651 if (! if_true_label)
653 if_true_label = drop_through_label;
654 drop_through_if_true = true;
656 if (! if_false_label)
658 if_false_label = drop_through_label;
659 drop_through_if_false = true;
662 /* Deal with the special case 0 > x: only one comparison is necessary and
663 we reverse it to avoid jumping to the drop-through label. */
664 if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
666 code = LE;
667 if_true_label = if_false_label;
668 if_false_label = drop_through_label;
669 drop_through_if_true = false;
670 drop_through_if_false = true;
673 /* Compare a word at a time, high order first. */
674 for (i = 0; i < nwords; i++)
676 rtx op0_word, op1_word;
678 if (WORDS_BIG_ENDIAN)
680 op0_word = operand_subword_force (op0, i, mode);
681 op1_word = operand_subword_force (op1, i, mode);
683 else
685 op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
686 op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
689 /* All but high-order word must be compared as unsigned. */
690 do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
691 word_mode, NULL_RTX, NULL_RTX, if_true_label,
692 prob);
694 /* Emit only one comparison for 0. Do not emit the last cond jump. */
695 if (op0 == const0_rtx || i == nwords - 1)
696 break;
698 /* Consider lower words only if these are equal. */
699 do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
700 NULL_RTX, NULL_RTX, if_false_label, inv (prob));
703 if (!drop_through_if_false)
704 emit_jump (if_false_label);
705 if (drop_through_label)
706 emit_label (drop_through_label);
709 /* Given a comparison expression EXP for values too wide to be compared
710 with one insn, test the comparison and jump to the appropriate label.
711 The code of EXP is ignored; we always test GT if SWAP is 0,
712 and LT if SWAP is 1. */
714 static void
715 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
716 rtx if_false_label, rtx if_true_label, int prob)
718 rtx op0 = expand_normal (swap ? treeop1 : treeop0);
719 rtx op1 = expand_normal (swap ? treeop0 : treeop1);
720 enum machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
721 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
723 do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
724 if_true_label, prob);
727 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
728 mode, MODE, that is too wide for the available compare insns. Either
729 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
730 to indicate drop through. */
732 static void
733 do_jump_by_parts_zero_rtx (enum machine_mode mode, rtx op0,
734 rtx if_false_label, rtx if_true_label, int prob)
736 int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
737 rtx part;
738 int i;
739 rtx drop_through_label = 0;
741 /* The fastest way of doing this comparison on almost any machine is to
742 "or" all the words and compare the result. If all have to be loaded
743 from memory and this is a very wide item, it's possible this may
744 be slower, but that's highly unlikely. */
746 part = gen_reg_rtx (word_mode);
747 emit_move_insn (part, operand_subword_force (op0, 0, mode));
748 for (i = 1; i < nwords && part != 0; i++)
749 part = expand_binop (word_mode, ior_optab, part,
750 operand_subword_force (op0, i, mode),
751 part, 1, OPTAB_WIDEN);
753 if (part != 0)
755 do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
756 NULL_RTX, if_false_label, if_true_label, prob);
757 return;
760 /* If we couldn't do the "or" simply, do this with a series of compares. */
761 if (! if_false_label)
762 drop_through_label = if_false_label = gen_label_rtx ();
764 for (i = 0; i < nwords; i++)
765 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
766 const0_rtx, EQ, 1, word_mode, NULL_RTX,
767 if_false_label, NULL_RTX, prob);
769 if (if_true_label)
770 emit_jump (if_true_label);
772 if (drop_through_label)
773 emit_label (drop_through_label);
776 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
777 where MODE is an integer mode too wide to be compared with one insn.
778 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
779 to indicate drop through. */
781 static void
782 do_jump_by_parts_equality_rtx (enum machine_mode mode, rtx op0, rtx op1,
783 rtx if_false_label, rtx if_true_label, int prob)
785 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
786 rtx drop_through_label = 0;
787 int i;
789 if (op1 == const0_rtx)
791 do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label,
792 prob);
793 return;
795 else if (op0 == const0_rtx)
797 do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label,
798 prob);
799 return;
802 if (! if_false_label)
803 drop_through_label = if_false_label = gen_label_rtx ();
805 for (i = 0; i < nwords; i++)
806 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
807 operand_subword_force (op1, i, mode),
808 EQ, 0, word_mode, NULL_RTX,
809 if_false_label, NULL_RTX, prob);
811 if (if_true_label)
812 emit_jump (if_true_label);
813 if (drop_through_label)
814 emit_label (drop_through_label);
817 /* Given an EQ_EXPR expression EXP for values too wide to be compared
818 with one insn, test the comparison and jump to the appropriate label. */
820 static void
821 do_jump_by_parts_equality (tree treeop0, tree treeop1, rtx if_false_label,
822 rtx if_true_label, int prob)
824 rtx op0 = expand_normal (treeop0);
825 rtx op1 = expand_normal (treeop1);
826 enum machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
827 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
828 if_true_label, prob);
831 /* Split a comparison into two others, the second of which has the other
832 "orderedness". The first is always ORDERED or UNORDERED if MODE
833 does not honor NaNs (which means that it can be skipped in that case;
834 see do_compare_rtx_and_jump).
836 The two conditions are written in *CODE1 and *CODE2. Return true if
837 the conditions must be ANDed, false if they must be ORed. */
839 bool
840 split_comparison (enum rtx_code code, enum machine_mode mode,
841 enum rtx_code *code1, enum rtx_code *code2)
843 switch (code)
845 case LT:
846 *code1 = ORDERED;
847 *code2 = UNLT;
848 return true;
849 case LE:
850 *code1 = ORDERED;
851 *code2 = UNLE;
852 return true;
853 case GT:
854 *code1 = ORDERED;
855 *code2 = UNGT;
856 return true;
857 case GE:
858 *code1 = ORDERED;
859 *code2 = UNGE;
860 return true;
861 case EQ:
862 *code1 = ORDERED;
863 *code2 = UNEQ;
864 return true;
865 case NE:
866 *code1 = UNORDERED;
867 *code2 = LTGT;
868 return false;
869 case UNLT:
870 *code1 = UNORDERED;
871 *code2 = LT;
872 return false;
873 case UNLE:
874 *code1 = UNORDERED;
875 *code2 = LE;
876 return false;
877 case UNGT:
878 *code1 = UNORDERED;
879 *code2 = GT;
880 return false;
881 case UNGE:
882 *code1 = UNORDERED;
883 *code2 = GE;
884 return false;
885 case UNEQ:
886 *code1 = UNORDERED;
887 *code2 = EQ;
888 return false;
889 case LTGT:
890 /* Do not turn a trapping comparison into a non-trapping one. */
891 if (HONOR_SNANS (mode))
893 *code1 = LT;
894 *code2 = GT;
895 return false;
897 else
899 *code1 = ORDERED;
900 *code2 = NE;
901 return true;
903 default:
904 gcc_unreachable ();
909 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
910 The decision as to signed or unsigned comparison must be made by the caller.
912 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
913 compared. */
915 void
916 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
917 enum machine_mode mode, rtx size, rtx if_false_label,
918 rtx if_true_label, int prob)
920 rtx tem;
921 rtx dummy_label = NULL_RTX;
923 /* Reverse the comparison if that is safe and we want to jump if it is
924 false. Also convert to the reverse comparison if the target can
925 implement it. */
926 if ((! if_true_label
927 || ! can_compare_p (code, mode, ccp_jump))
928 && (! FLOAT_MODE_P (mode)
929 || code == ORDERED || code == UNORDERED
930 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
931 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
933 enum rtx_code rcode;
934 if (FLOAT_MODE_P (mode))
935 rcode = reverse_condition_maybe_unordered (code);
936 else
937 rcode = reverse_condition (code);
939 /* Canonicalize to UNORDERED for the libcall. */
940 if (can_compare_p (rcode, mode, ccp_jump)
941 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
943 tem = if_true_label;
944 if_true_label = if_false_label;
945 if_false_label = tem;
946 code = rcode;
947 prob = inv (prob);
951 /* If one operand is constant, make it the second one. Only do this
952 if the other operand is not constant as well. */
954 if (swap_commutative_operands_p (op0, op1))
956 tem = op0;
957 op0 = op1;
958 op1 = tem;
959 code = swap_condition (code);
962 do_pending_stack_adjust ();
964 code = unsignedp ? unsigned_condition (code) : code;
965 if (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
966 op0, op1)))
968 if (CONSTANT_P (tem))
970 rtx label = (tem == const0_rtx || tem == CONST0_RTX (mode))
971 ? if_false_label : if_true_label;
972 if (label)
973 emit_jump (label);
974 return;
977 code = GET_CODE (tem);
978 mode = GET_MODE (tem);
979 op0 = XEXP (tem, 0);
980 op1 = XEXP (tem, 1);
981 unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
984 if (! if_true_label)
985 dummy_label = if_true_label = gen_label_rtx ();
987 if (GET_MODE_CLASS (mode) == MODE_INT
988 && ! can_compare_p (code, mode, ccp_jump))
990 switch (code)
992 case LTU:
993 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
994 if_false_label, if_true_label, prob);
995 break;
997 case LEU:
998 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
999 if_true_label, if_false_label,
1000 inv (prob));
1001 break;
1003 case GTU:
1004 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1005 if_false_label, if_true_label, prob);
1006 break;
1008 case GEU:
1009 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1010 if_true_label, if_false_label,
1011 inv (prob));
1012 break;
1014 case LT:
1015 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1016 if_false_label, if_true_label, prob);
1017 break;
1019 case LE:
1020 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1021 if_true_label, if_false_label,
1022 inv (prob));
1023 break;
1025 case GT:
1026 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1027 if_false_label, if_true_label, prob);
1028 break;
1030 case GE:
1031 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1032 if_true_label, if_false_label,
1033 inv (prob));
1034 break;
1036 case EQ:
1037 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
1038 if_true_label, prob);
1039 break;
1041 case NE:
1042 do_jump_by_parts_equality_rtx (mode, op0, op1, if_true_label,
1043 if_false_label, inv (prob));
1044 break;
1046 default:
1047 gcc_unreachable ();
1050 else
1052 if (SCALAR_FLOAT_MODE_P (mode)
1053 && ! can_compare_p (code, mode, ccp_jump)
1054 && can_compare_p (swap_condition (code), mode, ccp_jump))
1056 rtx tmp;
1057 code = swap_condition (code);
1058 tmp = op0;
1059 op0 = op1;
1060 op1 = tmp;
1062 else if (SCALAR_FLOAT_MODE_P (mode)
1063 && ! can_compare_p (code, mode, ccp_jump)
1064 /* Never split ORDERED and UNORDERED.
1065 These must be implemented. */
1066 && (code != ORDERED && code != UNORDERED)
1067 /* Split a floating-point comparison if
1068 we can jump on other conditions... */
1069 && (have_insn_for (COMPARE, mode)
1070 /* ... or if there is no libcall for it. */
1071 || code_to_optab (code) == unknown_optab))
1073 enum rtx_code first_code;
1074 bool and_them = split_comparison (code, mode, &first_code, &code);
1076 /* If there are no NaNs, the first comparison should always fall
1077 through. */
1078 if (!HONOR_NANS (mode))
1079 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1081 else
1083 if (and_them)
1085 rtx dest_label;
1086 /* If we only jump if true, just bypass the second jump. */
1087 if (! if_false_label)
1089 if (! dummy_label)
1090 dummy_label = gen_label_rtx ();
1091 dest_label = dummy_label;
1093 else
1094 dest_label = if_false_label;
1095 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1096 size, dest_label, NULL_RTX, prob);
1098 else
1099 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1100 size, NULL_RTX, if_true_label, prob);
1104 emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1105 if_true_label, prob);
1108 if (if_false_label)
1109 emit_jump (if_false_label);
1110 if (dummy_label)
1111 emit_label (dummy_label);
1114 /* Generate code for a comparison expression EXP (including code to compute
1115 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1116 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1117 generated code will drop through.
1118 SIGNED_CODE should be the rtx operation for this comparison for
1119 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1121 We force a stack adjustment unless there are currently
1122 things pushed on the stack that aren't yet used. */
1124 static void
1125 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1126 enum rtx_code unsigned_code, rtx if_false_label,
1127 rtx if_true_label, int prob)
1129 rtx op0, op1;
1130 tree type;
1131 enum machine_mode mode;
1132 int unsignedp;
1133 enum rtx_code code;
1135 /* Don't crash if the comparison was erroneous. */
1136 op0 = expand_normal (treeop0);
1137 if (TREE_CODE (treeop0) == ERROR_MARK)
1138 return;
1140 op1 = expand_normal (treeop1);
1141 if (TREE_CODE (treeop1) == ERROR_MARK)
1142 return;
1144 type = TREE_TYPE (treeop0);
1145 mode = TYPE_MODE (type);
1146 if (TREE_CODE (treeop0) == INTEGER_CST
1147 && (TREE_CODE (treeop1) != INTEGER_CST
1148 || (GET_MODE_BITSIZE (mode)
1149 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1151 /* op0 might have been replaced by promoted constant, in which
1152 case the type of second argument should be used. */
1153 type = TREE_TYPE (treeop1);
1154 mode = TYPE_MODE (type);
1156 unsignedp = TYPE_UNSIGNED (type);
1157 code = unsignedp ? unsigned_code : signed_code;
1159 #ifdef HAVE_canonicalize_funcptr_for_compare
1160 /* If function pointers need to be "canonicalized" before they can
1161 be reliably compared, then canonicalize them.
1162 Only do this if *both* sides of the comparison are function pointers.
1163 If one side isn't, we want a noncanonicalized comparison. See PR
1164 middle-end/17564. */
1165 if (HAVE_canonicalize_funcptr_for_compare
1166 && TREE_CODE (TREE_TYPE (treeop0)) == POINTER_TYPE
1167 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0)))
1168 == FUNCTION_TYPE
1169 && TREE_CODE (TREE_TYPE (treeop1)) == POINTER_TYPE
1170 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1)))
1171 == FUNCTION_TYPE)
1173 rtx new_op0 = gen_reg_rtx (mode);
1174 rtx new_op1 = gen_reg_rtx (mode);
1176 emit_insn (gen_canonicalize_funcptr_for_compare (new_op0, op0));
1177 op0 = new_op0;
1179 emit_insn (gen_canonicalize_funcptr_for_compare (new_op1, op1));
1180 op1 = new_op1;
1182 #endif
1184 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1185 ((mode == BLKmode)
1186 ? expr_size (treeop0) : NULL_RTX),
1187 if_false_label, if_true_label, prob);
1190 #include "gt-dojump.h"