2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dojump.c
blob196d106630b9808a34d04d69edbe2c19f18a1cd5
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2015 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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "flags.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "insn-attr.h"
36 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
37 #include "expmed.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "calls.h"
41 #include "emit-rtl.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "insn-codes.h"
46 #include "optabs.h"
47 #include "langhooks.h"
48 #include "predict.h"
49 #include "basic-block.h"
50 #include "tm_p.h"
52 static bool prefer_and_bit_test (machine_mode, int);
53 static void do_jump_by_parts_greater (tree, tree, int,
54 rtx_code_label *, rtx_code_label *, int);
55 static void do_jump_by_parts_equality (tree, tree, rtx_code_label *,
56 rtx_code_label *, int);
57 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code,
58 rtx_code_label *, rtx_code_label *, int);
60 /* Invert probability if there is any. -1 stands for unknown. */
62 static inline int
63 inv (int prob)
65 return prob == -1 ? -1 : REG_BR_PROB_BASE - prob;
68 /* At the start of a function, record that we have no previously-pushed
69 arguments waiting to be popped. */
71 void
72 init_pending_stack_adjust (void)
74 pending_stack_adjust = 0;
77 /* Discard any pending stack adjustment. This avoid relying on the
78 RTL optimizers to remove useless adjustments when we know the
79 stack pointer value is dead. */
80 void
81 discard_pending_stack_adjust (void)
83 stack_pointer_delta -= pending_stack_adjust;
84 pending_stack_adjust = 0;
87 /* When exiting from function, if safe, clear out any pending stack adjust
88 so the adjustment won't get done.
90 Note, if the current function calls alloca, then it must have a
91 frame pointer regardless of the value of flag_omit_frame_pointer. */
93 void
94 clear_pending_stack_adjust (void)
96 if (optimize > 0
97 && (! flag_omit_frame_pointer || cfun->calls_alloca)
98 && EXIT_IGNORE_STACK)
99 discard_pending_stack_adjust ();
102 /* Pop any previously-pushed arguments that have not been popped yet. */
104 void
105 do_pending_stack_adjust (void)
107 if (inhibit_defer_pop == 0)
109 if (pending_stack_adjust != 0)
110 adjust_stack (GEN_INT (pending_stack_adjust));
111 pending_stack_adjust = 0;
115 /* Remember pending_stack_adjust/stack_pointer_delta.
116 To be used around code that may call do_pending_stack_adjust (),
117 but the generated code could be discarded e.g. using delete_insns_since. */
119 void
120 save_pending_stack_adjust (saved_pending_stack_adjust *save)
122 save->x_pending_stack_adjust = pending_stack_adjust;
123 save->x_stack_pointer_delta = stack_pointer_delta;
126 /* Restore the saved pending_stack_adjust/stack_pointer_delta. */
128 void
129 restore_pending_stack_adjust (saved_pending_stack_adjust *save)
131 if (inhibit_defer_pop == 0)
133 pending_stack_adjust = save->x_pending_stack_adjust;
134 stack_pointer_delta = save->x_stack_pointer_delta;
138 /* Expand conditional expressions. */
140 /* Generate code to evaluate EXP and jump to LABEL if the value is zero. */
142 void
143 jumpifnot (tree exp, rtx_code_label *label, int prob)
145 do_jump (exp, label, NULL, inv (prob));
148 void
149 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx_code_label *label,
150 int prob)
152 do_jump_1 (code, op0, op1, label, NULL, inv (prob));
155 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
157 void
158 jumpif (tree exp, rtx_code_label *label, int prob)
160 do_jump (exp, NULL, label, prob);
163 void
164 jumpif_1 (enum tree_code code, tree op0, tree op1,
165 rtx_code_label *label, int prob)
167 do_jump_1 (code, op0, op1, NULL, label, prob);
170 /* Used internally by prefer_and_bit_test. */
172 static GTY(()) rtx and_reg;
173 static GTY(()) rtx and_test;
174 static GTY(()) rtx shift_test;
176 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
177 where X is an arbitrary register of mode MODE. Return true if the former
178 is preferred. */
180 static bool
181 prefer_and_bit_test (machine_mode mode, int bitnum)
183 bool speed_p;
184 wide_int mask = wi::set_bit_in_zero (bitnum, GET_MODE_PRECISION (mode));
186 if (and_test == 0)
188 /* Set up rtxes for the two variations. Use NULL as a placeholder
189 for the BITNUM-based constants. */
190 and_reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
191 and_test = gen_rtx_AND (mode, and_reg, NULL);
192 shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
193 const1_rtx);
195 else
197 /* Change the mode of the previously-created rtxes. */
198 PUT_MODE (and_reg, mode);
199 PUT_MODE (and_test, mode);
200 PUT_MODE (shift_test, mode);
201 PUT_MODE (XEXP (shift_test, 0), mode);
204 /* Fill in the integers. */
205 XEXP (and_test, 1) = immed_wide_int_const (mask, mode);
206 XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
208 speed_p = optimize_insn_for_speed_p ();
209 return (rtx_cost (and_test, IF_THEN_ELSE, 0, speed_p)
210 <= rtx_cost (shift_test, IF_THEN_ELSE, 0, speed_p));
213 /* Subroutine of do_jump, dealing with exploded comparisons of the type
214 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
215 PROB is probability of jump to if_true_label, or -1 if unknown. */
217 void
218 do_jump_1 (enum tree_code code, tree op0, tree op1,
219 rtx_code_label *if_false_label, rtx_code_label *if_true_label,
220 int prob)
222 machine_mode mode;
223 rtx_code_label *drop_through_label = 0;
225 switch (code)
227 case EQ_EXPR:
229 tree inner_type = TREE_TYPE (op0);
231 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
232 != MODE_COMPLEX_FLOAT);
233 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
234 != MODE_COMPLEX_INT);
236 if (integer_zerop (op1))
237 do_jump (op0, if_true_label, if_false_label, inv (prob));
238 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
239 && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
240 do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label,
241 prob);
242 else
243 do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
244 prob);
245 break;
248 case NE_EXPR:
250 tree inner_type = TREE_TYPE (op0);
252 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
253 != MODE_COMPLEX_FLOAT);
254 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
255 != MODE_COMPLEX_INT);
257 if (integer_zerop (op1))
258 do_jump (op0, if_false_label, if_true_label, prob);
259 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
260 && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
261 do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label,
262 inv (prob));
263 else
264 do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
265 prob);
266 break;
269 case LT_EXPR:
270 mode = TYPE_MODE (TREE_TYPE (op0));
271 if (GET_MODE_CLASS (mode) == MODE_INT
272 && ! can_compare_p (LT, mode, ccp_jump))
273 do_jump_by_parts_greater (op0, op1, 1, if_false_label, if_true_label,
274 prob);
275 else
276 do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
277 prob);
278 break;
280 case LE_EXPR:
281 mode = TYPE_MODE (TREE_TYPE (op0));
282 if (GET_MODE_CLASS (mode) == MODE_INT
283 && ! can_compare_p (LE, mode, ccp_jump))
284 do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label,
285 inv (prob));
286 else
287 do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
288 prob);
289 break;
291 case GT_EXPR:
292 mode = TYPE_MODE (TREE_TYPE (op0));
293 if (GET_MODE_CLASS (mode) == MODE_INT
294 && ! can_compare_p (GT, mode, ccp_jump))
295 do_jump_by_parts_greater (op0, op1, 0, if_false_label, if_true_label,
296 prob);
297 else
298 do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
299 prob);
300 break;
302 case GE_EXPR:
303 mode = TYPE_MODE (TREE_TYPE (op0));
304 if (GET_MODE_CLASS (mode) == MODE_INT
305 && ! can_compare_p (GE, mode, ccp_jump))
306 do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label,
307 inv (prob));
308 else
309 do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
310 prob);
311 break;
313 case ORDERED_EXPR:
314 do_compare_and_jump (op0, op1, ORDERED, ORDERED,
315 if_false_label, if_true_label, prob);
316 break;
318 case UNORDERED_EXPR:
319 do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
320 if_false_label, if_true_label, prob);
321 break;
323 case UNLT_EXPR:
324 do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
325 prob);
326 break;
328 case UNLE_EXPR:
329 do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
330 prob);
331 break;
333 case UNGT_EXPR:
334 do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
335 prob);
336 break;
338 case UNGE_EXPR:
339 do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
340 prob);
341 break;
343 case UNEQ_EXPR:
344 do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
345 prob);
346 break;
348 case LTGT_EXPR:
349 do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
350 prob);
351 break;
353 case TRUTH_ANDIF_EXPR:
355 /* Spread the probability that the expression is false evenly between
356 the two conditions. So the first condition is false half the total
357 probability of being false. The second condition is false the other
358 half of the total probability of being false, so its jump has a false
359 probability of half the total, relative to the probability we
360 reached it (i.e. the first condition was true). */
361 int op0_prob = -1;
362 int op1_prob = -1;
363 if (prob != -1)
365 int false_prob = inv (prob);
366 int op0_false_prob = false_prob / 2;
367 int op1_false_prob = GCOV_COMPUTE_SCALE ((false_prob / 2),
368 inv (op0_false_prob));
369 /* Get the probability that each jump below is true. */
370 op0_prob = inv (op0_false_prob);
371 op1_prob = inv (op1_false_prob);
373 if (if_false_label == NULL)
375 drop_through_label = gen_label_rtx ();
376 do_jump (op0, drop_through_label, NULL, op0_prob);
377 do_jump (op1, NULL, if_true_label, op1_prob);
379 else
381 do_jump (op0, if_false_label, NULL, op0_prob);
382 do_jump (op1, if_false_label, if_true_label, op1_prob);
384 break;
387 case TRUTH_ORIF_EXPR:
389 /* Spread the probability evenly between the two conditions. So
390 the first condition has half the total probability of being true.
391 The second condition has the other half of the total probability,
392 so its jump has a probability of half the total, relative to
393 the probability we reached it (i.e. the first condition was false). */
394 int op0_prob = -1;
395 int op1_prob = -1;
396 if (prob != -1)
398 op0_prob = prob / 2;
399 op1_prob = GCOV_COMPUTE_SCALE ((prob / 2), inv (op0_prob));
401 if (if_true_label == NULL)
403 drop_through_label = gen_label_rtx ();
404 do_jump (op0, NULL, drop_through_label, op0_prob);
405 do_jump (op1, if_false_label, NULL, op1_prob);
407 else
409 do_jump (op0, NULL, if_true_label, op0_prob);
410 do_jump (op1, if_false_label, if_true_label, op1_prob);
412 break;
415 default:
416 gcc_unreachable ();
419 if (drop_through_label)
421 do_pending_stack_adjust ();
422 emit_label (drop_through_label);
426 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
427 the result is zero, or IF_TRUE_LABEL if the result is one.
428 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
429 meaning fall through in that case.
431 do_jump always does any pending stack adjust except when it does not
432 actually perform a jump. An example where there is no jump
433 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
435 PROB is probability of jump to if_true_label, or -1 if unknown. */
437 void
438 do_jump (tree exp, rtx_code_label *if_false_label,
439 rtx_code_label *if_true_label, int prob)
441 enum tree_code code = TREE_CODE (exp);
442 rtx temp;
443 int i;
444 tree type;
445 machine_mode mode;
446 rtx_code_label *drop_through_label = NULL;
448 switch (code)
450 case ERROR_MARK:
451 break;
453 case INTEGER_CST:
455 rtx_code_label *lab = integer_zerop (exp) ? if_false_label
456 : if_true_label;
457 if (lab)
458 emit_jump (lab);
459 break;
462 #if 0
463 /* This is not true with #pragma weak */
464 case ADDR_EXPR:
465 /* The address of something can never be zero. */
466 if (if_true_label)
467 emit_jump (if_true_label);
468 break;
469 #endif
471 case NOP_EXPR:
472 if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
473 || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
474 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
475 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
476 goto normal;
477 case CONVERT_EXPR:
478 /* If we are narrowing the operand, we have to do the compare in the
479 narrower mode. */
480 if ((TYPE_PRECISION (TREE_TYPE (exp))
481 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
482 goto normal;
483 case NON_LVALUE_EXPR:
484 case ABS_EXPR:
485 case NEGATE_EXPR:
486 case LROTATE_EXPR:
487 case RROTATE_EXPR:
488 /* These cannot change zero->nonzero or vice versa. */
489 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
490 break;
492 case TRUTH_NOT_EXPR:
493 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
494 inv (prob));
495 break;
497 case COND_EXPR:
499 rtx_code_label *label1 = gen_label_rtx ();
500 if (!if_true_label || !if_false_label)
502 drop_through_label = gen_label_rtx ();
503 if (!if_true_label)
504 if_true_label = drop_through_label;
505 if (!if_false_label)
506 if_false_label = drop_through_label;
509 do_pending_stack_adjust ();
510 do_jump (TREE_OPERAND (exp, 0), label1, NULL, -1);
511 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
512 emit_label (label1);
513 do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
514 break;
517 case COMPOUND_EXPR:
518 /* Lowered by gimplify.c. */
519 gcc_unreachable ();
521 case MINUS_EXPR:
522 /* Nonzero iff operands of minus differ. */
523 code = NE_EXPR;
525 /* FALLTHRU */
526 case EQ_EXPR:
527 case NE_EXPR:
528 case LT_EXPR:
529 case LE_EXPR:
530 case GT_EXPR:
531 case GE_EXPR:
532 case ORDERED_EXPR:
533 case UNORDERED_EXPR:
534 case UNLT_EXPR:
535 case UNLE_EXPR:
536 case UNGT_EXPR:
537 case UNGE_EXPR:
538 case UNEQ_EXPR:
539 case LTGT_EXPR:
540 case TRUTH_ANDIF_EXPR:
541 case TRUTH_ORIF_EXPR:
542 other_code:
543 do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
544 if_false_label, if_true_label, prob);
545 break;
547 case BIT_AND_EXPR:
548 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
549 See if the former is preferred for jump tests and restore it
550 if so. */
551 if (integer_onep (TREE_OPERAND (exp, 1)))
553 tree exp0 = TREE_OPERAND (exp, 0);
554 rtx_code_label *set_label, *clr_label;
555 int setclr_prob = prob;
557 /* Strip narrowing integral type conversions. */
558 while (CONVERT_EXPR_P (exp0)
559 && TREE_OPERAND (exp0, 0) != error_mark_node
560 && TYPE_PRECISION (TREE_TYPE (exp0))
561 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
562 exp0 = TREE_OPERAND (exp0, 0);
564 /* "exp0 ^ 1" inverts the sense of the single bit test. */
565 if (TREE_CODE (exp0) == BIT_XOR_EXPR
566 && integer_onep (TREE_OPERAND (exp0, 1)))
568 exp0 = TREE_OPERAND (exp0, 0);
569 clr_label = if_true_label;
570 set_label = if_false_label;
571 setclr_prob = inv (prob);
573 else
575 clr_label = if_false_label;
576 set_label = if_true_label;
579 if (TREE_CODE (exp0) == RSHIFT_EXPR)
581 tree arg = TREE_OPERAND (exp0, 0);
582 tree shift = TREE_OPERAND (exp0, 1);
583 tree argtype = TREE_TYPE (arg);
584 if (TREE_CODE (shift) == INTEGER_CST
585 && compare_tree_int (shift, 0) >= 0
586 && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
587 && prefer_and_bit_test (TYPE_MODE (argtype),
588 TREE_INT_CST_LOW (shift)))
590 unsigned HOST_WIDE_INT mask
591 = (unsigned HOST_WIDE_INT) 1 << TREE_INT_CST_LOW (shift);
592 do_jump (build2 (BIT_AND_EXPR, argtype, arg,
593 build_int_cstu (argtype, mask)),
594 clr_label, set_label, setclr_prob);
595 break;
600 /* If we are AND'ing with a small constant, do this comparison in the
601 smallest type that fits. If the machine doesn't have comparisons
602 that small, it will be converted back to the wider comparison.
603 This helps if we are testing the sign bit of a narrower object.
604 combine can't do this for us because it can't know whether a
605 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
607 if (! SLOW_BYTE_ACCESS
608 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
609 && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
610 && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
611 && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
612 && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
613 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
614 && have_insn_for (COMPARE, TYPE_MODE (type)))
616 do_jump (fold_convert (type, exp), if_false_label, if_true_label,
617 prob);
618 break;
621 if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
622 || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
623 goto normal;
625 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
627 case TRUTH_AND_EXPR:
628 /* High branch cost, expand as the bitwise AND of the conditions.
629 Do the same if the RHS has side effects, because we're effectively
630 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
631 if (BRANCH_COST (optimize_insn_for_speed_p (),
632 false) >= 4
633 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
634 goto normal;
635 code = TRUTH_ANDIF_EXPR;
636 goto other_code;
638 case BIT_IOR_EXPR:
639 case TRUTH_OR_EXPR:
640 /* High branch cost, expand as the bitwise OR of the conditions.
641 Do the same if the RHS has side effects, because we're effectively
642 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
643 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
644 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
645 goto normal;
646 code = TRUTH_ORIF_EXPR;
647 goto other_code;
649 /* Fall through and generate the normal code. */
650 default:
651 normal:
652 temp = expand_normal (exp);
653 do_pending_stack_adjust ();
654 /* The RTL optimizers prefer comparisons against pseudos. */
655 if (GET_CODE (temp) == SUBREG)
657 /* Compare promoted variables in their promoted mode. */
658 if (SUBREG_PROMOTED_VAR_P (temp)
659 && REG_P (XEXP (temp, 0)))
660 temp = XEXP (temp, 0);
661 else
662 temp = copy_to_reg (temp);
664 do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
665 NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
666 GET_MODE (temp), NULL_RTX,
667 if_false_label, if_true_label, prob);
670 if (drop_through_label)
672 do_pending_stack_adjust ();
673 emit_label (drop_through_label);
677 /* Compare OP0 with OP1, word at a time, in mode MODE.
678 UNSIGNEDP says to do unsigned comparison.
679 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
681 static void
682 do_jump_by_parts_greater_rtx (machine_mode mode, int unsignedp, rtx op0,
683 rtx op1, rtx_code_label *if_false_label,
684 rtx_code_label *if_true_label,
685 int prob)
687 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
688 rtx_code_label *drop_through_label = 0;
689 bool drop_through_if_true = false, drop_through_if_false = false;
690 enum rtx_code code = GT;
691 int i;
693 if (! if_true_label || ! if_false_label)
694 drop_through_label = gen_label_rtx ();
695 if (! if_true_label)
697 if_true_label = drop_through_label;
698 drop_through_if_true = true;
700 if (! if_false_label)
702 if_false_label = drop_through_label;
703 drop_through_if_false = true;
706 /* Deal with the special case 0 > x: only one comparison is necessary and
707 we reverse it to avoid jumping to the drop-through label. */
708 if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
710 code = LE;
711 if_true_label = if_false_label;
712 if_false_label = drop_through_label;
713 drop_through_if_true = false;
714 drop_through_if_false = true;
717 /* Compare a word at a time, high order first. */
718 for (i = 0; i < nwords; i++)
720 rtx op0_word, op1_word;
722 if (WORDS_BIG_ENDIAN)
724 op0_word = operand_subword_force (op0, i, mode);
725 op1_word = operand_subword_force (op1, i, mode);
727 else
729 op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
730 op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
733 /* All but high-order word must be compared as unsigned. */
734 do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
735 word_mode, NULL_RTX, NULL, if_true_label,
736 prob);
738 /* Emit only one comparison for 0. Do not emit the last cond jump. */
739 if (op0 == const0_rtx || i == nwords - 1)
740 break;
742 /* Consider lower words only if these are equal. */
743 do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
744 NULL_RTX, NULL, if_false_label, inv (prob));
747 if (!drop_through_if_false)
748 emit_jump (if_false_label);
749 if (drop_through_label)
750 emit_label (drop_through_label);
753 /* Given a comparison expression EXP for values too wide to be compared
754 with one insn, test the comparison and jump to the appropriate label.
755 The code of EXP is ignored; we always test GT if SWAP is 0,
756 and LT if SWAP is 1. */
758 static void
759 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
760 rtx_code_label *if_false_label,
761 rtx_code_label *if_true_label, int prob)
763 rtx op0 = expand_normal (swap ? treeop1 : treeop0);
764 rtx op1 = expand_normal (swap ? treeop0 : treeop1);
765 machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
766 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
768 do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
769 if_true_label, prob);
772 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
773 mode, MODE, that is too wide for the available compare insns. Either
774 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL
775 to indicate drop through. */
777 static void
778 do_jump_by_parts_zero_rtx (machine_mode mode, rtx op0,
779 rtx_code_label *if_false_label,
780 rtx_code_label *if_true_label, int prob)
782 int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
783 rtx part;
784 int i;
785 rtx_code_label *drop_through_label = NULL;
787 /* The fastest way of doing this comparison on almost any machine is to
788 "or" all the words and compare the result. If all have to be loaded
789 from memory and this is a very wide item, it's possible this may
790 be slower, but that's highly unlikely. */
792 part = gen_reg_rtx (word_mode);
793 emit_move_insn (part, operand_subword_force (op0, 0, mode));
794 for (i = 1; i < nwords && part != 0; i++)
795 part = expand_binop (word_mode, ior_optab, part,
796 operand_subword_force (op0, i, mode),
797 part, 1, OPTAB_WIDEN);
799 if (part != 0)
801 do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
802 NULL_RTX, if_false_label, if_true_label, prob);
803 return;
806 /* If we couldn't do the "or" simply, do this with a series of compares. */
807 if (! if_false_label)
808 if_false_label = drop_through_label = gen_label_rtx ();
810 for (i = 0; i < nwords; i++)
811 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
812 const0_rtx, EQ, 1, word_mode, NULL_RTX,
813 if_false_label, NULL, prob);
815 if (if_true_label)
816 emit_jump (if_true_label);
818 if (drop_through_label)
819 emit_label (drop_through_label);
822 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
823 where MODE is an integer mode too wide to be compared with one insn.
824 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
825 to indicate drop through. */
827 static void
828 do_jump_by_parts_equality_rtx (machine_mode mode, rtx op0, rtx op1,
829 rtx_code_label *if_false_label,
830 rtx_code_label *if_true_label, int prob)
832 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
833 rtx_code_label *drop_through_label = NULL;
834 int i;
836 if (op1 == const0_rtx)
838 do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label,
839 prob);
840 return;
842 else if (op0 == const0_rtx)
844 do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label,
845 prob);
846 return;
849 if (! if_false_label)
850 drop_through_label = if_false_label = gen_label_rtx ();
852 for (i = 0; i < nwords; i++)
853 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
854 operand_subword_force (op1, i, mode),
855 EQ, 0, word_mode, NULL_RTX,
856 if_false_label, NULL, prob);
858 if (if_true_label)
859 emit_jump (if_true_label);
860 if (drop_through_label)
861 emit_label (drop_through_label);
864 /* Given an EQ_EXPR expression EXP for values too wide to be compared
865 with one insn, test the comparison and jump to the appropriate label. */
867 static void
868 do_jump_by_parts_equality (tree treeop0, tree treeop1,
869 rtx_code_label *if_false_label,
870 rtx_code_label *if_true_label, int prob)
872 rtx op0 = expand_normal (treeop0);
873 rtx op1 = expand_normal (treeop1);
874 machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
875 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
876 if_true_label, prob);
879 /* Split a comparison into two others, the second of which has the other
880 "orderedness". The first is always ORDERED or UNORDERED if MODE
881 does not honor NaNs (which means that it can be skipped in that case;
882 see do_compare_rtx_and_jump).
884 The two conditions are written in *CODE1 and *CODE2. Return true if
885 the conditions must be ANDed, false if they must be ORed. */
887 bool
888 split_comparison (enum rtx_code code, machine_mode mode,
889 enum rtx_code *code1, enum rtx_code *code2)
891 switch (code)
893 case LT:
894 *code1 = ORDERED;
895 *code2 = UNLT;
896 return true;
897 case LE:
898 *code1 = ORDERED;
899 *code2 = UNLE;
900 return true;
901 case GT:
902 *code1 = ORDERED;
903 *code2 = UNGT;
904 return true;
905 case GE:
906 *code1 = ORDERED;
907 *code2 = UNGE;
908 return true;
909 case EQ:
910 *code1 = ORDERED;
911 *code2 = UNEQ;
912 return true;
913 case NE:
914 *code1 = UNORDERED;
915 *code2 = LTGT;
916 return false;
917 case UNLT:
918 *code1 = UNORDERED;
919 *code2 = LT;
920 return false;
921 case UNLE:
922 *code1 = UNORDERED;
923 *code2 = LE;
924 return false;
925 case UNGT:
926 *code1 = UNORDERED;
927 *code2 = GT;
928 return false;
929 case UNGE:
930 *code1 = UNORDERED;
931 *code2 = GE;
932 return false;
933 case UNEQ:
934 *code1 = UNORDERED;
935 *code2 = EQ;
936 return false;
937 case LTGT:
938 /* Do not turn a trapping comparison into a non-trapping one. */
939 if (HONOR_SNANS (mode))
941 *code1 = LT;
942 *code2 = GT;
943 return false;
945 else
947 *code1 = ORDERED;
948 *code2 = NE;
949 return true;
951 default:
952 gcc_unreachable ();
957 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
958 The decision as to signed or unsigned comparison must be made by the caller.
960 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
961 compared. */
963 void
964 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
965 machine_mode mode, rtx size,
966 rtx_code_label *if_false_label,
967 rtx_code_label *if_true_label, int prob)
969 rtx tem;
970 rtx_code_label *dummy_label = NULL;
972 /* Reverse the comparison if that is safe and we want to jump if it is
973 false. Also convert to the reverse comparison if the target can
974 implement it. */
975 if ((! if_true_label
976 || ! can_compare_p (code, mode, ccp_jump))
977 && (! FLOAT_MODE_P (mode)
978 || code == ORDERED || code == UNORDERED
979 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
980 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
982 enum rtx_code rcode;
983 if (FLOAT_MODE_P (mode))
984 rcode = reverse_condition_maybe_unordered (code);
985 else
986 rcode = reverse_condition (code);
988 /* Canonicalize to UNORDERED for the libcall. */
989 if (can_compare_p (rcode, mode, ccp_jump)
990 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
992 std::swap (if_true_label, if_false_label);
993 code = rcode;
994 prob = inv (prob);
998 /* If one operand is constant, make it the second one. Only do this
999 if the other operand is not constant as well. */
1001 if (swap_commutative_operands_p (op0, op1))
1003 std::swap (op0, op1);
1004 code = swap_condition (code);
1007 do_pending_stack_adjust ();
1009 code = unsignedp ? unsigned_condition (code) : code;
1010 if (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
1011 op0, op1)))
1013 if (CONSTANT_P (tem))
1015 rtx_code_label *label = (tem == const0_rtx
1016 || tem == CONST0_RTX (mode))
1017 ? if_false_label : if_true_label;
1018 if (label)
1019 emit_jump (label);
1020 return;
1023 code = GET_CODE (tem);
1024 mode = GET_MODE (tem);
1025 op0 = XEXP (tem, 0);
1026 op1 = XEXP (tem, 1);
1027 unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
1030 if (! if_true_label)
1031 dummy_label = if_true_label = gen_label_rtx ();
1033 if (GET_MODE_CLASS (mode) == MODE_INT
1034 && ! can_compare_p (code, mode, ccp_jump))
1036 switch (code)
1038 case LTU:
1039 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1040 if_false_label, if_true_label, prob);
1041 break;
1043 case LEU:
1044 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1045 if_true_label, if_false_label,
1046 inv (prob));
1047 break;
1049 case GTU:
1050 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1051 if_false_label, if_true_label, prob);
1052 break;
1054 case GEU:
1055 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1056 if_true_label, if_false_label,
1057 inv (prob));
1058 break;
1060 case LT:
1061 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1062 if_false_label, if_true_label, prob);
1063 break;
1065 case LE:
1066 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1067 if_true_label, if_false_label,
1068 inv (prob));
1069 break;
1071 case GT:
1072 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1073 if_false_label, if_true_label, prob);
1074 break;
1076 case GE:
1077 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1078 if_true_label, if_false_label,
1079 inv (prob));
1080 break;
1082 case EQ:
1083 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
1084 if_true_label, prob);
1085 break;
1087 case NE:
1088 do_jump_by_parts_equality_rtx (mode, op0, op1, if_true_label,
1089 if_false_label, inv (prob));
1090 break;
1092 default:
1093 gcc_unreachable ();
1096 else
1098 if (SCALAR_FLOAT_MODE_P (mode)
1099 && ! can_compare_p (code, mode, ccp_jump)
1100 && can_compare_p (swap_condition (code), mode, ccp_jump))
1102 code = swap_condition (code);
1103 std::swap (op0, op1);
1105 else if (SCALAR_FLOAT_MODE_P (mode)
1106 && ! can_compare_p (code, mode, ccp_jump)
1107 /* Never split ORDERED and UNORDERED.
1108 These must be implemented. */
1109 && (code != ORDERED && code != UNORDERED)
1110 /* Split a floating-point comparison if
1111 we can jump on other conditions... */
1112 && (have_insn_for (COMPARE, mode)
1113 /* ... or if there is no libcall for it. */
1114 || code_to_optab (code) == unknown_optab))
1116 enum rtx_code first_code;
1117 bool and_them = split_comparison (code, mode, &first_code, &code);
1119 /* If there are no NaNs, the first comparison should always fall
1120 through. */
1121 if (!HONOR_NANS (mode))
1122 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1124 else
1126 int first_prob = prob;
1127 if (first_code == UNORDERED)
1128 first_prob = REG_BR_PROB_BASE / 100;
1129 else if (first_code == ORDERED)
1130 first_prob = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100;
1131 if (and_them)
1133 rtx_code_label *dest_label;
1134 /* If we only jump if true, just bypass the second jump. */
1135 if (! if_false_label)
1137 if (! dummy_label)
1138 dummy_label = gen_label_rtx ();
1139 dest_label = dummy_label;
1141 else
1142 dest_label = if_false_label;
1143 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1144 size, dest_label, NULL, first_prob);
1146 else
1147 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1148 size, NULL, if_true_label, first_prob);
1152 emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1153 if_true_label, prob);
1156 if (if_false_label)
1157 emit_jump (if_false_label);
1158 if (dummy_label)
1159 emit_label (dummy_label);
1162 /* Generate code for a comparison expression EXP (including code to compute
1163 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1164 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1165 generated code will drop through.
1166 SIGNED_CODE should be the rtx operation for this comparison for
1167 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1169 We force a stack adjustment unless there are currently
1170 things pushed on the stack that aren't yet used. */
1172 static void
1173 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1174 enum rtx_code unsigned_code,
1175 rtx_code_label *if_false_label,
1176 rtx_code_label *if_true_label, int prob)
1178 rtx op0, op1;
1179 tree type;
1180 machine_mode mode;
1181 int unsignedp;
1182 enum rtx_code code;
1184 /* Don't crash if the comparison was erroneous. */
1185 op0 = expand_normal (treeop0);
1186 if (TREE_CODE (treeop0) == ERROR_MARK)
1187 return;
1189 op1 = expand_normal (treeop1);
1190 if (TREE_CODE (treeop1) == ERROR_MARK)
1191 return;
1193 type = TREE_TYPE (treeop0);
1194 mode = TYPE_MODE (type);
1195 if (TREE_CODE (treeop0) == INTEGER_CST
1196 && (TREE_CODE (treeop1) != INTEGER_CST
1197 || (GET_MODE_BITSIZE (mode)
1198 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1200 /* op0 might have been replaced by promoted constant, in which
1201 case the type of second argument should be used. */
1202 type = TREE_TYPE (treeop1);
1203 mode = TYPE_MODE (type);
1205 unsignedp = TYPE_UNSIGNED (type);
1206 code = unsignedp ? unsigned_code : signed_code;
1208 #ifdef HAVE_canonicalize_funcptr_for_compare
1209 /* If function pointers need to be "canonicalized" before they can
1210 be reliably compared, then canonicalize them.
1211 Only do this if *both* sides of the comparison are function pointers.
1212 If one side isn't, we want a noncanonicalized comparison. See PR
1213 middle-end/17564. */
1214 if (HAVE_canonicalize_funcptr_for_compare
1215 && TREE_CODE (TREE_TYPE (treeop0)) == POINTER_TYPE
1216 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0)))
1217 == FUNCTION_TYPE
1218 && TREE_CODE (TREE_TYPE (treeop1)) == POINTER_TYPE
1219 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1)))
1220 == FUNCTION_TYPE)
1222 rtx new_op0 = gen_reg_rtx (mode);
1223 rtx new_op1 = gen_reg_rtx (mode);
1225 emit_insn (gen_canonicalize_funcptr_for_compare (new_op0, op0));
1226 op0 = new_op0;
1228 emit_insn (gen_canonicalize_funcptr_for_compare (new_op1, op1));
1229 op1 = new_op1;
1231 #endif
1233 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1234 ((mode == BLKmode)
1235 ? expr_size (treeop0) : NULL_RTX),
1236 if_false_label, if_true_label, prob);
1239 #include "gt-dojump.h"