[31/77] Use scalar_int_mode for move2add
[official-gcc.git] / gcc / dojump.c
blobb084253505128aa4fdb29151d8231684045de5cc
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2017 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "predict.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "optabs.h"
31 #include "emit-rtl.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
35 #include "dojump.h"
36 #include "explow.h"
37 #include "expr.h"
38 #include "langhooks.h"
40 static bool prefer_and_bit_test (machine_mode, int);
41 static void do_jump_by_parts_greater (tree, tree, int,
42 rtx_code_label *, rtx_code_label *,
43 profile_probability);
44 static void do_jump_by_parts_equality (tree, tree, rtx_code_label *,
45 rtx_code_label *, profile_probability);
46 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code,
47 rtx_code_label *, rtx_code_label *,
48 profile_probability);
50 /* At the start of a function, record that we have no previously-pushed
51 arguments waiting to be popped. */
53 void
54 init_pending_stack_adjust (void)
56 pending_stack_adjust = 0;
59 /* Discard any pending stack adjustment. This avoid relying on the
60 RTL optimizers to remove useless adjustments when we know the
61 stack pointer value is dead. */
62 void
63 discard_pending_stack_adjust (void)
65 stack_pointer_delta -= pending_stack_adjust;
66 pending_stack_adjust = 0;
69 /* When exiting from function, if safe, clear out any pending stack adjust
70 so the adjustment won't get done.
72 Note, if the current function calls alloca, then it must have a
73 frame pointer regardless of the value of flag_omit_frame_pointer. */
75 void
76 clear_pending_stack_adjust (void)
78 if (optimize > 0
79 && (! flag_omit_frame_pointer || cfun->calls_alloca)
80 && EXIT_IGNORE_STACK)
81 discard_pending_stack_adjust ();
84 /* Pop any previously-pushed arguments that have not been popped yet. */
86 void
87 do_pending_stack_adjust (void)
89 if (inhibit_defer_pop == 0)
91 if (pending_stack_adjust != 0)
92 adjust_stack (GEN_INT (pending_stack_adjust));
93 pending_stack_adjust = 0;
97 /* Remember pending_stack_adjust/stack_pointer_delta.
98 To be used around code that may call do_pending_stack_adjust (),
99 but the generated code could be discarded e.g. using delete_insns_since. */
101 void
102 save_pending_stack_adjust (saved_pending_stack_adjust *save)
104 save->x_pending_stack_adjust = pending_stack_adjust;
105 save->x_stack_pointer_delta = stack_pointer_delta;
108 /* Restore the saved pending_stack_adjust/stack_pointer_delta. */
110 void
111 restore_pending_stack_adjust (saved_pending_stack_adjust *save)
113 if (inhibit_defer_pop == 0)
115 pending_stack_adjust = save->x_pending_stack_adjust;
116 stack_pointer_delta = save->x_stack_pointer_delta;
120 /* Expand conditional expressions. */
122 /* Generate code to evaluate EXP and jump to LABEL if the value is zero. */
124 void
125 jumpifnot (tree exp, rtx_code_label *label, profile_probability prob)
127 do_jump (exp, label, NULL, prob.invert ());
130 void
131 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx_code_label *label,
132 profile_probability prob)
134 do_jump_1 (code, op0, op1, label, NULL, prob.invert ());
137 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
139 void
140 jumpif (tree exp, rtx_code_label *label, profile_probability prob)
142 do_jump (exp, NULL, label, prob);
145 void
146 jumpif_1 (enum tree_code code, tree op0, tree op1,
147 rtx_code_label *label, profile_probability prob)
149 do_jump_1 (code, op0, op1, NULL, label, prob);
152 /* Used internally by prefer_and_bit_test. */
154 static GTY(()) rtx and_reg;
155 static GTY(()) rtx and_test;
156 static GTY(()) rtx shift_test;
158 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
159 where X is an arbitrary register of mode MODE. Return true if the former
160 is preferred. */
162 static bool
163 prefer_and_bit_test (machine_mode mode, int bitnum)
165 bool speed_p;
166 wide_int mask = wi::set_bit_in_zero (bitnum, GET_MODE_PRECISION (mode));
168 if (and_test == 0)
170 /* Set up rtxes for the two variations. Use NULL as a placeholder
171 for the BITNUM-based constants. */
172 and_reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
173 and_test = gen_rtx_AND (mode, and_reg, NULL);
174 shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
175 const1_rtx);
177 else
179 /* Change the mode of the previously-created rtxes. */
180 PUT_MODE (and_reg, mode);
181 PUT_MODE (and_test, mode);
182 PUT_MODE (shift_test, mode);
183 PUT_MODE (XEXP (shift_test, 0), mode);
186 /* Fill in the integers. */
187 XEXP (and_test, 1) = immed_wide_int_const (mask, mode);
188 XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
190 speed_p = optimize_insn_for_speed_p ();
191 return (rtx_cost (and_test, mode, IF_THEN_ELSE, 0, speed_p)
192 <= rtx_cost (shift_test, mode, IF_THEN_ELSE, 0, speed_p));
195 /* Subroutine of do_jump, dealing with exploded comparisons of the type
196 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
197 PROB is probability of jump to if_true_label. */
199 void
200 do_jump_1 (enum tree_code code, tree op0, tree op1,
201 rtx_code_label *if_false_label, rtx_code_label *if_true_label,
202 profile_probability prob)
204 machine_mode mode;
205 rtx_code_label *drop_through_label = 0;
206 scalar_int_mode int_mode;
208 switch (code)
210 case EQ_EXPR:
212 tree inner_type = TREE_TYPE (op0);
214 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
215 != MODE_COMPLEX_FLOAT);
216 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
217 != MODE_COMPLEX_INT);
219 if (integer_zerop (op1))
220 do_jump (op0, if_true_label, if_false_label,
221 prob.invert ());
222 else if (is_int_mode (TYPE_MODE (inner_type), &int_mode)
223 && !can_compare_p (EQ, int_mode, ccp_jump))
224 do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label,
225 prob);
226 else
227 do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
228 prob);
229 break;
232 case NE_EXPR:
234 tree inner_type = TREE_TYPE (op0);
236 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
237 != MODE_COMPLEX_FLOAT);
238 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
239 != MODE_COMPLEX_INT);
241 if (integer_zerop (op1))
242 do_jump (op0, if_false_label, if_true_label, prob);
243 else if (is_int_mode (TYPE_MODE (inner_type), &int_mode)
244 && !can_compare_p (NE, int_mode, ccp_jump))
245 do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label,
246 prob.invert ());
247 else
248 do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
249 prob);
250 break;
253 case LT_EXPR:
254 mode = TYPE_MODE (TREE_TYPE (op0));
255 if (is_int_mode (mode, &int_mode)
256 && ! can_compare_p (LT, int_mode, ccp_jump))
257 do_jump_by_parts_greater (op0, op1, 1, if_false_label,
258 if_true_label, prob);
259 else
260 do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
261 prob);
262 break;
264 case LE_EXPR:
265 mode = TYPE_MODE (TREE_TYPE (op0));
266 if (is_int_mode (mode, &int_mode)
267 && ! can_compare_p (LE, int_mode, ccp_jump))
268 do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label,
269 prob.invert ());
270 else
271 do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
272 prob);
273 break;
275 case GT_EXPR:
276 mode = TYPE_MODE (TREE_TYPE (op0));
277 if (is_int_mode (mode, &int_mode)
278 && ! can_compare_p (GT, int_mode, ccp_jump))
279 do_jump_by_parts_greater (op0, op1, 0, if_false_label,
280 if_true_label, prob);
281 else
282 do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
283 prob);
284 break;
286 case GE_EXPR:
287 mode = TYPE_MODE (TREE_TYPE (op0));
288 if (is_int_mode (mode, &int_mode)
289 && ! can_compare_p (GE, int_mode, ccp_jump))
290 do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label,
291 prob.invert ());
292 else
293 do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
294 prob);
295 break;
297 case ORDERED_EXPR:
298 do_compare_and_jump (op0, op1, ORDERED, ORDERED,
299 if_false_label, if_true_label, prob);
300 break;
302 case UNORDERED_EXPR:
303 do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
304 if_false_label, if_true_label, prob);
305 break;
307 case UNLT_EXPR:
308 do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
309 prob);
310 break;
312 case UNLE_EXPR:
313 do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
314 prob);
315 break;
317 case UNGT_EXPR:
318 do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
319 prob);
320 break;
322 case UNGE_EXPR:
323 do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
324 prob);
325 break;
327 case UNEQ_EXPR:
328 do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
329 prob);
330 break;
332 case LTGT_EXPR:
333 do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
334 prob);
335 break;
337 case TRUTH_ANDIF_EXPR:
339 /* Spread the probability that the expression is false evenly between
340 the two conditions. So the first condition is false half the total
341 probability of being false. The second condition is false the other
342 half of the total probability of being false, so its jump has a false
343 probability of half the total, relative to the probability we
344 reached it (i.e. the first condition was true). */
345 profile_probability op0_prob = profile_probability::uninitialized ();
346 profile_probability op1_prob = profile_probability::uninitialized ();
347 if (prob.initialized_p ())
349 profile_probability false_prob = prob.invert ();
350 profile_probability op0_false_prob = false_prob.apply_scale (1, 2);
351 profile_probability op1_false_prob = false_prob.apply_scale (1, 2)
352 / op0_false_prob.invert ();
353 /* Get the probability that each jump below is true. */
354 op0_prob = op0_false_prob.invert ();
355 op1_prob = op1_false_prob.invert ();
357 if (if_false_label == NULL)
359 drop_through_label = gen_label_rtx ();
360 do_jump (op0, drop_through_label, NULL, op0_prob);
361 do_jump (op1, NULL, if_true_label, op1_prob);
363 else
365 do_jump (op0, if_false_label, NULL, op0_prob);
366 do_jump (op1, if_false_label, if_true_label, op1_prob);
368 break;
371 case TRUTH_ORIF_EXPR:
373 /* Spread the probability evenly between the two conditions. So
374 the first condition has half the total probability of being true.
375 The second condition has the other half of the total probability,
376 so its jump has a probability of half the total, relative to
377 the probability we reached it (i.e. the first condition was false). */
378 profile_probability op0_prob = profile_probability::uninitialized ();
379 profile_probability op1_prob = profile_probability::uninitialized ();
380 if (prob.initialized_p ())
382 op0_prob = prob.apply_scale (1, 2);
383 op1_prob = prob.apply_scale (1, 2) / op0_prob.invert ();
385 if (if_true_label == NULL)
387 drop_through_label = gen_label_rtx ();
388 do_jump (op0, NULL, drop_through_label, op0_prob);
389 do_jump (op1, if_false_label, NULL, op1_prob);
391 else
393 do_jump (op0, NULL, if_true_label, op0_prob);
394 do_jump (op1, if_false_label, if_true_label, op1_prob);
396 break;
399 default:
400 gcc_unreachable ();
403 if (drop_through_label)
405 do_pending_stack_adjust ();
406 emit_label (drop_through_label);
410 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
411 the result is zero, or IF_TRUE_LABEL if the result is one.
412 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
413 meaning fall through in that case.
415 do_jump always does any pending stack adjust except when it does not
416 actually perform a jump. An example where there is no jump
417 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
419 PROB is probability of jump to if_true_label. */
421 void
422 do_jump (tree exp, rtx_code_label *if_false_label,
423 rtx_code_label *if_true_label, profile_probability prob)
425 enum tree_code code = TREE_CODE (exp);
426 rtx temp;
427 int i;
428 tree type;
429 machine_mode mode;
430 rtx_code_label *drop_through_label = NULL;
432 switch (code)
434 case ERROR_MARK:
435 break;
437 case INTEGER_CST:
439 rtx_code_label *lab = integer_zerop (exp) ? if_false_label
440 : if_true_label;
441 if (lab)
442 emit_jump (lab);
443 break;
446 #if 0
447 /* This is not true with #pragma weak */
448 case ADDR_EXPR:
449 /* The address of something can never be zero. */
450 if (if_true_label)
451 emit_jump (if_true_label);
452 break;
453 #endif
455 case NOP_EXPR:
456 if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
457 || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
458 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
459 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
460 goto normal;
461 /* FALLTHRU */
462 case CONVERT_EXPR:
463 /* If we are narrowing the operand, we have to do the compare in the
464 narrower mode. */
465 if ((TYPE_PRECISION (TREE_TYPE (exp))
466 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
467 goto normal;
468 /* FALLTHRU */
469 case NON_LVALUE_EXPR:
470 case ABS_EXPR:
471 case NEGATE_EXPR:
472 case LROTATE_EXPR:
473 case RROTATE_EXPR:
474 /* These cannot change zero->nonzero or vice versa. */
475 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
476 break;
478 case TRUTH_NOT_EXPR:
479 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
480 prob.invert ());
481 break;
483 case COND_EXPR:
485 rtx_code_label *label1 = gen_label_rtx ();
486 if (!if_true_label || !if_false_label)
488 drop_through_label = gen_label_rtx ();
489 if (!if_true_label)
490 if_true_label = drop_through_label;
491 if (!if_false_label)
492 if_false_label = drop_through_label;
495 do_pending_stack_adjust ();
496 do_jump (TREE_OPERAND (exp, 0), label1, NULL,
497 profile_probability::uninitialized ());
498 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
499 emit_label (label1);
500 do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
501 break;
504 case COMPOUND_EXPR:
505 /* Lowered by gimplify.c. */
506 gcc_unreachable ();
508 case MINUS_EXPR:
509 /* Nonzero iff operands of minus differ. */
510 code = NE_EXPR;
512 /* FALLTHRU */
513 case EQ_EXPR:
514 case NE_EXPR:
515 case LT_EXPR:
516 case LE_EXPR:
517 case GT_EXPR:
518 case GE_EXPR:
519 case ORDERED_EXPR:
520 case UNORDERED_EXPR:
521 case UNLT_EXPR:
522 case UNLE_EXPR:
523 case UNGT_EXPR:
524 case UNGE_EXPR:
525 case UNEQ_EXPR:
526 case LTGT_EXPR:
527 case TRUTH_ANDIF_EXPR:
528 case TRUTH_ORIF_EXPR:
529 other_code:
530 do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
531 if_false_label, if_true_label, prob);
532 break;
534 case BIT_AND_EXPR:
535 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
536 See if the former is preferred for jump tests and restore it
537 if so. */
538 if (integer_onep (TREE_OPERAND (exp, 1)))
540 tree exp0 = TREE_OPERAND (exp, 0);
541 rtx_code_label *set_label, *clr_label;
542 profile_probability setclr_prob = prob;
544 /* Strip narrowing integral type conversions. */
545 while (CONVERT_EXPR_P (exp0)
546 && TREE_OPERAND (exp0, 0) != error_mark_node
547 && TYPE_PRECISION (TREE_TYPE (exp0))
548 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
549 exp0 = TREE_OPERAND (exp0, 0);
551 /* "exp0 ^ 1" inverts the sense of the single bit test. */
552 if (TREE_CODE (exp0) == BIT_XOR_EXPR
553 && integer_onep (TREE_OPERAND (exp0, 1)))
555 exp0 = TREE_OPERAND (exp0, 0);
556 clr_label = if_true_label;
557 set_label = if_false_label;
558 setclr_prob = prob.invert ();
560 else
562 clr_label = if_false_label;
563 set_label = if_true_label;
566 if (TREE_CODE (exp0) == RSHIFT_EXPR)
568 tree arg = TREE_OPERAND (exp0, 0);
569 tree shift = TREE_OPERAND (exp0, 1);
570 tree argtype = TREE_TYPE (arg);
571 if (TREE_CODE (shift) == INTEGER_CST
572 && compare_tree_int (shift, 0) >= 0
573 && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
574 && prefer_and_bit_test (TYPE_MODE (argtype),
575 TREE_INT_CST_LOW (shift)))
577 unsigned HOST_WIDE_INT mask
578 = HOST_WIDE_INT_1U << TREE_INT_CST_LOW (shift);
579 do_jump (build2 (BIT_AND_EXPR, argtype, arg,
580 build_int_cstu (argtype, mask)),
581 clr_label, set_label, setclr_prob);
582 break;
587 /* If we are AND'ing with a small constant, do this comparison in the
588 smallest type that fits. If the machine doesn't have comparisons
589 that small, it will be converted back to the wider comparison.
590 This helps if we are testing the sign bit of a narrower object.
591 combine can't do this for us because it can't know whether a
592 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
594 if (! SLOW_BYTE_ACCESS
595 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
596 && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
597 && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
598 && int_mode_for_size (i + 1, 0).exists (&mode)
599 && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
600 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
601 && have_insn_for (COMPARE, TYPE_MODE (type)))
603 do_jump (fold_convert (type, exp), if_false_label, if_true_label,
604 prob);
605 break;
608 if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
609 || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
610 goto normal;
612 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
613 /* FALLTHRU */
615 case TRUTH_AND_EXPR:
616 /* High branch cost, expand as the bitwise AND of the conditions.
617 Do the same if the RHS has side effects, because we're effectively
618 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
619 if (BRANCH_COST (optimize_insn_for_speed_p (),
620 false) >= 4
621 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
622 goto normal;
623 code = TRUTH_ANDIF_EXPR;
624 goto other_code;
626 case BIT_IOR_EXPR:
627 case TRUTH_OR_EXPR:
628 /* High branch cost, expand as the bitwise OR of the conditions.
629 Do the same if the RHS has side effects, because we're effectively
630 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
631 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
632 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
633 goto normal;
634 code = TRUTH_ORIF_EXPR;
635 goto other_code;
637 /* Fall through and generate the normal code. */
638 default:
639 normal:
640 temp = expand_normal (exp);
641 do_pending_stack_adjust ();
642 /* The RTL optimizers prefer comparisons against pseudos. */
643 if (GET_CODE (temp) == SUBREG)
645 /* Compare promoted variables in their promoted mode. */
646 if (SUBREG_PROMOTED_VAR_P (temp)
647 && REG_P (XEXP (temp, 0)))
648 temp = XEXP (temp, 0);
649 else
650 temp = copy_to_reg (temp);
652 do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
653 NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
654 GET_MODE (temp), NULL_RTX,
655 if_false_label, if_true_label, prob);
658 if (drop_through_label)
660 do_pending_stack_adjust ();
661 emit_label (drop_through_label);
665 /* Compare OP0 with OP1, word at a time, in mode MODE.
666 UNSIGNEDP says to do unsigned comparison.
667 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
669 static void
670 do_jump_by_parts_greater_rtx (machine_mode mode, int unsignedp, rtx op0,
671 rtx op1, rtx_code_label *if_false_label,
672 rtx_code_label *if_true_label,
673 profile_probability prob)
675 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
676 rtx_code_label *drop_through_label = 0;
677 bool drop_through_if_true = false, drop_through_if_false = false;
678 enum rtx_code code = GT;
679 int i;
681 if (! if_true_label || ! if_false_label)
682 drop_through_label = gen_label_rtx ();
683 if (! if_true_label)
685 if_true_label = drop_through_label;
686 drop_through_if_true = true;
688 if (! if_false_label)
690 if_false_label = drop_through_label;
691 drop_through_if_false = true;
694 /* Deal with the special case 0 > x: only one comparison is necessary and
695 we reverse it to avoid jumping to the drop-through label. */
696 if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
698 code = LE;
699 if_true_label = if_false_label;
700 if_false_label = drop_through_label;
701 drop_through_if_true = false;
702 drop_through_if_false = true;
703 prob = prob.invert ();
706 /* Compare a word at a time, high order first. */
707 for (i = 0; i < nwords; i++)
709 rtx op0_word, op1_word;
711 if (WORDS_BIG_ENDIAN)
713 op0_word = operand_subword_force (op0, i, mode);
714 op1_word = operand_subword_force (op1, i, mode);
716 else
718 op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
719 op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
722 /* All but high-order word must be compared as unsigned. */
723 do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
724 word_mode, NULL_RTX, NULL, if_true_label,
725 prob);
727 /* Emit only one comparison for 0. Do not emit the last cond jump. */
728 if (op0 == const0_rtx || i == nwords - 1)
729 break;
731 /* Consider lower words only if these are equal. */
732 do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
733 NULL_RTX, NULL, if_false_label,
734 prob.invert ());
737 if (!drop_through_if_false)
738 emit_jump (if_false_label);
739 if (drop_through_label)
740 emit_label (drop_through_label);
743 /* Given a comparison expression EXP for values too wide to be compared
744 with one insn, test the comparison and jump to the appropriate label.
745 The code of EXP is ignored; we always test GT if SWAP is 0,
746 and LT if SWAP is 1. */
748 static void
749 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
750 rtx_code_label *if_false_label,
751 rtx_code_label *if_true_label,
752 profile_probability prob)
754 rtx op0 = expand_normal (swap ? treeop1 : treeop0);
755 rtx op1 = expand_normal (swap ? treeop0 : treeop1);
756 machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
757 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
759 do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
760 if_true_label, prob);
763 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
764 mode, MODE, that is too wide for the available compare insns. Either
765 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL
766 to indicate drop through. */
768 static void
769 do_jump_by_parts_zero_rtx (machine_mode mode, rtx op0,
770 rtx_code_label *if_false_label,
771 rtx_code_label *if_true_label,
772 profile_probability prob)
774 int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
775 rtx part;
776 int i;
777 rtx_code_label *drop_through_label = NULL;
779 /* The fastest way of doing this comparison on almost any machine is to
780 "or" all the words and compare the result. If all have to be loaded
781 from memory and this is a very wide item, it's possible this may
782 be slower, but that's highly unlikely. */
784 part = gen_reg_rtx (word_mode);
785 emit_move_insn (part, operand_subword_force (op0, 0, mode));
786 for (i = 1; i < nwords && part != 0; i++)
787 part = expand_binop (word_mode, ior_optab, part,
788 operand_subword_force (op0, i, mode),
789 part, 1, OPTAB_WIDEN);
791 if (part != 0)
793 do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
794 NULL_RTX, if_false_label, if_true_label, prob);
795 return;
798 /* If we couldn't do the "or" simply, do this with a series of compares. */
799 if (! if_false_label)
800 if_false_label = drop_through_label = gen_label_rtx ();
802 for (i = 0; i < nwords; i++)
803 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
804 const0_rtx, EQ, 1, word_mode, NULL_RTX,
805 if_false_label, NULL, prob);
807 if (if_true_label)
808 emit_jump (if_true_label);
810 if (drop_through_label)
811 emit_label (drop_through_label);
814 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
815 where MODE is an integer mode too wide to be compared with one insn.
816 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
817 to indicate drop through. */
819 static void
820 do_jump_by_parts_equality_rtx (machine_mode mode, rtx op0, rtx op1,
821 rtx_code_label *if_false_label,
822 rtx_code_label *if_true_label,
823 profile_probability prob)
825 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
826 rtx_code_label *drop_through_label = NULL;
827 int i;
829 if (op1 == const0_rtx)
831 do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label,
832 prob);
833 return;
835 else if (op0 == const0_rtx)
837 do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label,
838 prob);
839 return;
842 if (! if_false_label)
843 drop_through_label = if_false_label = gen_label_rtx ();
845 for (i = 0; i < nwords; i++)
846 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
847 operand_subword_force (op1, i, mode),
848 EQ, 0, word_mode, NULL_RTX,
849 if_false_label, NULL, prob);
851 if (if_true_label)
852 emit_jump (if_true_label);
853 if (drop_through_label)
854 emit_label (drop_through_label);
857 /* Given an EQ_EXPR expression EXP for values too wide to be compared
858 with one insn, test the comparison and jump to the appropriate label. */
860 static void
861 do_jump_by_parts_equality (tree treeop0, tree treeop1,
862 rtx_code_label *if_false_label,
863 rtx_code_label *if_true_label,
864 profile_probability prob)
866 rtx op0 = expand_normal (treeop0);
867 rtx op1 = expand_normal (treeop1);
868 machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
869 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
870 if_true_label, prob);
873 /* Split a comparison into two others, the second of which has the other
874 "orderedness". The first is always ORDERED or UNORDERED if MODE
875 does not honor NaNs (which means that it can be skipped in that case;
876 see do_compare_rtx_and_jump).
878 The two conditions are written in *CODE1 and *CODE2. Return true if
879 the conditions must be ANDed, false if they must be ORed. */
881 bool
882 split_comparison (enum rtx_code code, machine_mode mode,
883 enum rtx_code *code1, enum rtx_code *code2)
885 switch (code)
887 case LT:
888 *code1 = ORDERED;
889 *code2 = UNLT;
890 return true;
891 case LE:
892 *code1 = ORDERED;
893 *code2 = UNLE;
894 return true;
895 case GT:
896 *code1 = ORDERED;
897 *code2 = UNGT;
898 return true;
899 case GE:
900 *code1 = ORDERED;
901 *code2 = UNGE;
902 return true;
903 case EQ:
904 *code1 = ORDERED;
905 *code2 = UNEQ;
906 return true;
907 case NE:
908 *code1 = UNORDERED;
909 *code2 = LTGT;
910 return false;
911 case UNLT:
912 *code1 = UNORDERED;
913 *code2 = LT;
914 return false;
915 case UNLE:
916 *code1 = UNORDERED;
917 *code2 = LE;
918 return false;
919 case UNGT:
920 *code1 = UNORDERED;
921 *code2 = GT;
922 return false;
923 case UNGE:
924 *code1 = UNORDERED;
925 *code2 = GE;
926 return false;
927 case UNEQ:
928 *code1 = UNORDERED;
929 *code2 = EQ;
930 return false;
931 case LTGT:
932 /* Do not turn a trapping comparison into a non-trapping one. */
933 if (HONOR_SNANS (mode))
935 *code1 = LT;
936 *code2 = GT;
937 return false;
939 else
941 *code1 = ORDERED;
942 *code2 = NE;
943 return true;
945 default:
946 gcc_unreachable ();
951 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
952 The decision as to signed or unsigned comparison must be made by the caller.
954 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
955 compared. */
957 void
958 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
959 machine_mode mode, rtx size,
960 rtx_code_label *if_false_label,
961 rtx_code_label *if_true_label,
962 profile_probability prob)
964 rtx tem;
965 rtx_code_label *dummy_label = NULL;
967 /* Reverse the comparison if that is safe and we want to jump if it is
968 false. Also convert to the reverse comparison if the target can
969 implement it. */
970 if ((! if_true_label
971 || ! can_compare_p (code, mode, ccp_jump))
972 && (! FLOAT_MODE_P (mode)
973 || code == ORDERED || code == UNORDERED
974 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
975 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
977 enum rtx_code rcode;
978 if (FLOAT_MODE_P (mode))
979 rcode = reverse_condition_maybe_unordered (code);
980 else
981 rcode = reverse_condition (code);
983 /* Canonicalize to UNORDERED for the libcall. */
984 if (can_compare_p (rcode, mode, ccp_jump)
985 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
987 std::swap (if_true_label, if_false_label);
988 code = rcode;
989 prob = prob.invert ();
993 /* If one operand is constant, make it the second one. Only do this
994 if the other operand is not constant as well. */
996 if (swap_commutative_operands_p (op0, op1))
998 std::swap (op0, op1);
999 code = swap_condition (code);
1002 do_pending_stack_adjust ();
1004 code = unsignedp ? unsigned_condition (code) : code;
1005 if (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
1006 op0, op1)))
1008 if (CONSTANT_P (tem))
1010 rtx_code_label *label = (tem == const0_rtx
1011 || tem == CONST0_RTX (mode))
1012 ? if_false_label : if_true_label;
1013 if (label)
1014 emit_jump (label);
1015 return;
1018 code = GET_CODE (tem);
1019 mode = GET_MODE (tem);
1020 op0 = XEXP (tem, 0);
1021 op1 = XEXP (tem, 1);
1022 unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
1025 if (! if_true_label)
1026 dummy_label = if_true_label = gen_label_rtx ();
1028 scalar_int_mode int_mode;
1029 if (is_int_mode (mode, &int_mode)
1030 && ! can_compare_p (code, int_mode, ccp_jump))
1032 switch (code)
1034 case LTU:
1035 do_jump_by_parts_greater_rtx (int_mode, 1, op1, op0,
1036 if_false_label, if_true_label, prob);
1037 break;
1039 case LEU:
1040 do_jump_by_parts_greater_rtx (int_mode, 1, op0, op1,
1041 if_true_label, if_false_label,
1042 prob.invert ());
1043 break;
1045 case GTU:
1046 do_jump_by_parts_greater_rtx (int_mode, 1, op0, op1,
1047 if_false_label, if_true_label, prob);
1048 break;
1050 case GEU:
1051 do_jump_by_parts_greater_rtx (int_mode, 1, op1, op0,
1052 if_true_label, if_false_label,
1053 prob.invert ());
1054 break;
1056 case LT:
1057 do_jump_by_parts_greater_rtx (int_mode, 0, op1, op0,
1058 if_false_label, if_true_label, prob);
1059 break;
1061 case LE:
1062 do_jump_by_parts_greater_rtx (int_mode, 0, op0, op1,
1063 if_true_label, if_false_label,
1064 prob.invert ());
1065 break;
1067 case GT:
1068 do_jump_by_parts_greater_rtx (int_mode, 0, op0, op1,
1069 if_false_label, if_true_label, prob);
1070 break;
1072 case GE:
1073 do_jump_by_parts_greater_rtx (int_mode, 0, op1, op0,
1074 if_true_label, if_false_label,
1075 prob.invert ());
1076 break;
1078 case EQ:
1079 do_jump_by_parts_equality_rtx (int_mode, op0, op1, if_false_label,
1080 if_true_label, prob);
1081 break;
1083 case NE:
1084 do_jump_by_parts_equality_rtx (int_mode, op0, op1, if_true_label,
1085 if_false_label,
1086 prob.invert ());
1087 break;
1089 default:
1090 gcc_unreachable ();
1093 else
1095 if (SCALAR_FLOAT_MODE_P (mode)
1096 && ! can_compare_p (code, mode, ccp_jump)
1097 && can_compare_p (swap_condition (code), mode, ccp_jump))
1099 code = swap_condition (code);
1100 std::swap (op0, op1);
1102 else if (SCALAR_FLOAT_MODE_P (mode)
1103 && ! can_compare_p (code, mode, ccp_jump)
1104 /* Never split ORDERED and UNORDERED.
1105 These must be implemented. */
1106 && (code != ORDERED && code != UNORDERED)
1107 /* Split a floating-point comparison if
1108 we can jump on other conditions... */
1109 && (have_insn_for (COMPARE, mode)
1110 /* ... or if there is no libcall for it. */
1111 || code_to_optab (code) == unknown_optab))
1113 enum rtx_code first_code;
1114 bool and_them = split_comparison (code, mode, &first_code, &code);
1116 /* If there are no NaNs, the first comparison should always fall
1117 through. */
1118 if (!HONOR_NANS (mode))
1119 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1121 else
1123 profile_probability first_prob = prob;
1124 if (first_code == UNORDERED)
1125 first_prob = profile_probability::guessed_always ().apply_scale
1126 (1, 100);
1127 else if (first_code == ORDERED)
1128 first_prob = profile_probability::guessed_always ().apply_scale
1129 (99, 100);
1130 if (and_them)
1132 rtx_code_label *dest_label;
1133 /* If we only jump if true, just bypass the second jump. */
1134 if (! if_false_label)
1136 if (! dummy_label)
1137 dummy_label = gen_label_rtx ();
1138 dest_label = dummy_label;
1140 else
1141 dest_label = if_false_label;
1142 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1143 size, dest_label, NULL, first_prob);
1145 else
1146 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1147 size, NULL, if_true_label, first_prob);
1151 emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1152 if_true_label, prob);
1155 if (if_false_label)
1156 emit_jump (if_false_label);
1157 if (dummy_label)
1158 emit_label (dummy_label);
1161 /* Generate code for a comparison expression EXP (including code to compute
1162 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1163 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1164 generated code will drop through.
1165 SIGNED_CODE should be the rtx operation for this comparison for
1166 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1168 We force a stack adjustment unless there are currently
1169 things pushed on the stack that aren't yet used. */
1171 static void
1172 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1173 enum rtx_code unsigned_code,
1174 rtx_code_label *if_false_label,
1175 rtx_code_label *if_true_label, profile_probability prob)
1177 rtx op0, op1;
1178 tree type;
1179 machine_mode mode;
1180 int unsignedp;
1181 enum rtx_code code;
1183 /* Don't crash if the comparison was erroneous. */
1184 op0 = expand_normal (treeop0);
1185 if (TREE_CODE (treeop0) == ERROR_MARK)
1186 return;
1188 op1 = expand_normal (treeop1);
1189 if (TREE_CODE (treeop1) == ERROR_MARK)
1190 return;
1192 type = TREE_TYPE (treeop0);
1193 mode = TYPE_MODE (type);
1194 if (TREE_CODE (treeop0) == INTEGER_CST
1195 && (TREE_CODE (treeop1) != INTEGER_CST
1196 || (GET_MODE_BITSIZE (mode)
1197 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1199 /* op0 might have been replaced by promoted constant, in which
1200 case the type of second argument should be used. */
1201 type = TREE_TYPE (treeop1);
1202 mode = TYPE_MODE (type);
1204 unsignedp = TYPE_UNSIGNED (type);
1205 code = unsignedp ? unsigned_code : signed_code;
1207 /* If function pointers need to be "canonicalized" before they can
1208 be reliably compared, then canonicalize them.
1209 Only do this if *both* sides of the comparison are function pointers.
1210 If one side isn't, we want a noncanonicalized comparison. See PR
1211 middle-end/17564. */
1212 if (targetm.have_canonicalize_funcptr_for_compare ()
1213 && POINTER_TYPE_P (TREE_TYPE (treeop0))
1214 && POINTER_TYPE_P (TREE_TYPE (treeop1))
1215 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))
1216 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (treeop1))))
1218 rtx new_op0 = gen_reg_rtx (mode);
1219 rtx new_op1 = gen_reg_rtx (mode);
1221 emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op0, op0));
1222 op0 = new_op0;
1224 emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op1, op1));
1225 op1 = new_op1;
1228 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1229 ((mode == BLKmode)
1230 ? expr_size (treeop0) : NULL_RTX),
1231 if_false_label, if_true_label, prob);
1234 #include "gt-dojump.h"