2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dojump.c
blob6adc1a653cc38bdc82d7a7aa0416d5569d3d427e
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 "backend.h"
24 #include "predict.h"
25 #include "rtl.h"
26 #include "alias.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "stor-layout.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "insn-attr.h"
33 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "calls.h"
38 #include "emit-rtl.h"
39 #include "varasm.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "insn-codes.h"
43 #include "optabs.h"
44 #include "langhooks.h"
45 #include "tm_p.h"
46 #include "target.h"
48 static bool prefer_and_bit_test (machine_mode, int);
49 static void do_jump_by_parts_greater (tree, tree, int,
50 rtx_code_label *, rtx_code_label *, int);
51 static void do_jump_by_parts_equality (tree, tree, rtx_code_label *,
52 rtx_code_label *, int);
53 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code,
54 rtx_code_label *, rtx_code_label *, int);
56 /* Invert probability if there is any. -1 stands for unknown. */
58 static inline int
59 inv (int prob)
61 return prob == -1 ? -1 : REG_BR_PROB_BASE - prob;
64 /* At the start of a function, record that we have no previously-pushed
65 arguments waiting to be popped. */
67 void
68 init_pending_stack_adjust (void)
70 pending_stack_adjust = 0;
73 /* Discard any pending stack adjustment. This avoid relying on the
74 RTL optimizers to remove useless adjustments when we know the
75 stack pointer value is dead. */
76 void
77 discard_pending_stack_adjust (void)
79 stack_pointer_delta -= pending_stack_adjust;
80 pending_stack_adjust = 0;
83 /* When exiting from function, if safe, clear out any pending stack adjust
84 so the adjustment won't get done.
86 Note, if the current function calls alloca, then it must have a
87 frame pointer regardless of the value of flag_omit_frame_pointer. */
89 void
90 clear_pending_stack_adjust (void)
92 if (optimize > 0
93 && (! flag_omit_frame_pointer || cfun->calls_alloca)
94 && EXIT_IGNORE_STACK)
95 discard_pending_stack_adjust ();
98 /* Pop any previously-pushed arguments that have not been popped yet. */
100 void
101 do_pending_stack_adjust (void)
103 if (inhibit_defer_pop == 0)
105 if (pending_stack_adjust != 0)
106 adjust_stack (GEN_INT (pending_stack_adjust));
107 pending_stack_adjust = 0;
111 /* Remember pending_stack_adjust/stack_pointer_delta.
112 To be used around code that may call do_pending_stack_adjust (),
113 but the generated code could be discarded e.g. using delete_insns_since. */
115 void
116 save_pending_stack_adjust (saved_pending_stack_adjust *save)
118 save->x_pending_stack_adjust = pending_stack_adjust;
119 save->x_stack_pointer_delta = stack_pointer_delta;
122 /* Restore the saved pending_stack_adjust/stack_pointer_delta. */
124 void
125 restore_pending_stack_adjust (saved_pending_stack_adjust *save)
127 if (inhibit_defer_pop == 0)
129 pending_stack_adjust = save->x_pending_stack_adjust;
130 stack_pointer_delta = save->x_stack_pointer_delta;
134 /* Expand conditional expressions. */
136 /* Generate code to evaluate EXP and jump to LABEL if the value is zero. */
138 void
139 jumpifnot (tree exp, rtx_code_label *label, int prob)
141 do_jump (exp, label, NULL, inv (prob));
144 void
145 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx_code_label *label,
146 int prob)
148 do_jump_1 (code, op0, op1, label, NULL, inv (prob));
151 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
153 void
154 jumpif (tree exp, rtx_code_label *label, int prob)
156 do_jump (exp, NULL, label, prob);
159 void
160 jumpif_1 (enum tree_code code, tree op0, tree op1,
161 rtx_code_label *label, int prob)
163 do_jump_1 (code, op0, op1, NULL, label, prob);
166 /* Used internally by prefer_and_bit_test. */
168 static GTY(()) rtx and_reg;
169 static GTY(()) rtx and_test;
170 static GTY(()) rtx shift_test;
172 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
173 where X is an arbitrary register of mode MODE. Return true if the former
174 is preferred. */
176 static bool
177 prefer_and_bit_test (machine_mode mode, int bitnum)
179 bool speed_p;
180 wide_int mask = wi::set_bit_in_zero (bitnum, GET_MODE_PRECISION (mode));
182 if (and_test == 0)
184 /* Set up rtxes for the two variations. Use NULL as a placeholder
185 for the BITNUM-based constants. */
186 and_reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
187 and_test = gen_rtx_AND (mode, and_reg, NULL);
188 shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
189 const1_rtx);
191 else
193 /* Change the mode of the previously-created rtxes. */
194 PUT_MODE (and_reg, mode);
195 PUT_MODE (and_test, mode);
196 PUT_MODE (shift_test, mode);
197 PUT_MODE (XEXP (shift_test, 0), mode);
200 /* Fill in the integers. */
201 XEXP (and_test, 1) = immed_wide_int_const (mask, mode);
202 XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
204 speed_p = optimize_insn_for_speed_p ();
205 return (rtx_cost (and_test, mode, IF_THEN_ELSE, 0, speed_p)
206 <= rtx_cost (shift_test, mode, IF_THEN_ELSE, 0, speed_p));
209 /* Subroutine of do_jump, dealing with exploded comparisons of the type
210 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
211 PROB is probability of jump to if_true_label, or -1 if unknown. */
213 void
214 do_jump_1 (enum tree_code code, tree op0, tree op1,
215 rtx_code_label *if_false_label, rtx_code_label *if_true_label,
216 int prob)
218 machine_mode mode;
219 rtx_code_label *drop_through_label = 0;
221 switch (code)
223 case EQ_EXPR:
225 tree inner_type = TREE_TYPE (op0);
227 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
228 != MODE_COMPLEX_FLOAT);
229 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
230 != MODE_COMPLEX_INT);
232 if (integer_zerop (op1))
233 do_jump (op0, if_true_label, if_false_label, inv (prob));
234 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
235 && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
236 do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label,
237 prob);
238 else
239 do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
240 prob);
241 break;
244 case NE_EXPR:
246 tree inner_type = TREE_TYPE (op0);
248 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
249 != MODE_COMPLEX_FLOAT);
250 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
251 != MODE_COMPLEX_INT);
253 if (integer_zerop (op1))
254 do_jump (op0, if_false_label, if_true_label, prob);
255 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
256 && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
257 do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label,
258 inv (prob));
259 else
260 do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
261 prob);
262 break;
265 case LT_EXPR:
266 mode = TYPE_MODE (TREE_TYPE (op0));
267 if (GET_MODE_CLASS (mode) == MODE_INT
268 && ! can_compare_p (LT, mode, ccp_jump))
269 do_jump_by_parts_greater (op0, op1, 1, if_false_label, if_true_label,
270 prob);
271 else
272 do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
273 prob);
274 break;
276 case LE_EXPR:
277 mode = TYPE_MODE (TREE_TYPE (op0));
278 if (GET_MODE_CLASS (mode) == MODE_INT
279 && ! can_compare_p (LE, mode, ccp_jump))
280 do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label,
281 inv (prob));
282 else
283 do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
284 prob);
285 break;
287 case GT_EXPR:
288 mode = TYPE_MODE (TREE_TYPE (op0));
289 if (GET_MODE_CLASS (mode) == MODE_INT
290 && ! can_compare_p (GT, mode, ccp_jump))
291 do_jump_by_parts_greater (op0, op1, 0, if_false_label, if_true_label,
292 prob);
293 else
294 do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
295 prob);
296 break;
298 case GE_EXPR:
299 mode = TYPE_MODE (TREE_TYPE (op0));
300 if (GET_MODE_CLASS (mode) == MODE_INT
301 && ! can_compare_p (GE, mode, ccp_jump))
302 do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label,
303 inv (prob));
304 else
305 do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
306 prob);
307 break;
309 case ORDERED_EXPR:
310 do_compare_and_jump (op0, op1, ORDERED, ORDERED,
311 if_false_label, if_true_label, prob);
312 break;
314 case UNORDERED_EXPR:
315 do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
316 if_false_label, if_true_label, prob);
317 break;
319 case UNLT_EXPR:
320 do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
321 prob);
322 break;
324 case UNLE_EXPR:
325 do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
326 prob);
327 break;
329 case UNGT_EXPR:
330 do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
331 prob);
332 break;
334 case UNGE_EXPR:
335 do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
336 prob);
337 break;
339 case UNEQ_EXPR:
340 do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
341 prob);
342 break;
344 case LTGT_EXPR:
345 do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
346 prob);
347 break;
349 case TRUTH_ANDIF_EXPR:
351 /* Spread the probability that the expression is false evenly between
352 the two conditions. So the first condition is false half the total
353 probability of being false. The second condition is false the other
354 half of the total probability of being false, so its jump has a false
355 probability of half the total, relative to the probability we
356 reached it (i.e. the first condition was true). */
357 int op0_prob = -1;
358 int op1_prob = -1;
359 if (prob != -1)
361 int false_prob = inv (prob);
362 int op0_false_prob = false_prob / 2;
363 int op1_false_prob = GCOV_COMPUTE_SCALE ((false_prob / 2),
364 inv (op0_false_prob));
365 /* Get the probability that each jump below is true. */
366 op0_prob = inv (op0_false_prob);
367 op1_prob = inv (op1_false_prob);
369 if (if_false_label == NULL)
371 drop_through_label = gen_label_rtx ();
372 do_jump (op0, drop_through_label, NULL, op0_prob);
373 do_jump (op1, NULL, if_true_label, op1_prob);
375 else
377 do_jump (op0, if_false_label, NULL, op0_prob);
378 do_jump (op1, if_false_label, if_true_label, op1_prob);
380 break;
383 case TRUTH_ORIF_EXPR:
385 /* Spread the probability evenly between the two conditions. So
386 the first condition has half the total probability of being true.
387 The second condition has the other half of the total probability,
388 so its jump has a probability of half the total, relative to
389 the probability we reached it (i.e. the first condition was false). */
390 int op0_prob = -1;
391 int op1_prob = -1;
392 if (prob != -1)
394 op0_prob = prob / 2;
395 op1_prob = GCOV_COMPUTE_SCALE ((prob / 2), inv (op0_prob));
397 if (if_true_label == NULL)
399 drop_through_label = gen_label_rtx ();
400 do_jump (op0, NULL, drop_through_label, op0_prob);
401 do_jump (op1, if_false_label, NULL, op1_prob);
403 else
405 do_jump (op0, NULL, if_true_label, op0_prob);
406 do_jump (op1, if_false_label, if_true_label, op1_prob);
408 break;
411 default:
412 gcc_unreachable ();
415 if (drop_through_label)
417 do_pending_stack_adjust ();
418 emit_label (drop_through_label);
422 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
423 the result is zero, or IF_TRUE_LABEL if the result is one.
424 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
425 meaning fall through in that case.
427 do_jump always does any pending stack adjust except when it does not
428 actually perform a jump. An example where there is no jump
429 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
431 PROB is probability of jump to if_true_label, or -1 if unknown. */
433 void
434 do_jump (tree exp, rtx_code_label *if_false_label,
435 rtx_code_label *if_true_label, int prob)
437 enum tree_code code = TREE_CODE (exp);
438 rtx temp;
439 int i;
440 tree type;
441 machine_mode mode;
442 rtx_code_label *drop_through_label = NULL;
444 switch (code)
446 case ERROR_MARK:
447 break;
449 case INTEGER_CST:
451 rtx_code_label *lab = integer_zerop (exp) ? if_false_label
452 : if_true_label;
453 if (lab)
454 emit_jump (lab);
455 break;
458 #if 0
459 /* This is not true with #pragma weak */
460 case ADDR_EXPR:
461 /* The address of something can never be zero. */
462 if (if_true_label)
463 emit_jump (if_true_label);
464 break;
465 #endif
467 case NOP_EXPR:
468 if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
469 || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
470 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
471 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
472 goto normal;
473 case CONVERT_EXPR:
474 /* If we are narrowing the operand, we have to do the compare in the
475 narrower mode. */
476 if ((TYPE_PRECISION (TREE_TYPE (exp))
477 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
478 goto normal;
479 case NON_LVALUE_EXPR:
480 case ABS_EXPR:
481 case NEGATE_EXPR:
482 case LROTATE_EXPR:
483 case RROTATE_EXPR:
484 /* These cannot change zero->nonzero or vice versa. */
485 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
486 break;
488 case TRUTH_NOT_EXPR:
489 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
490 inv (prob));
491 break;
493 case COND_EXPR:
495 rtx_code_label *label1 = gen_label_rtx ();
496 if (!if_true_label || !if_false_label)
498 drop_through_label = gen_label_rtx ();
499 if (!if_true_label)
500 if_true_label = drop_through_label;
501 if (!if_false_label)
502 if_false_label = drop_through_label;
505 do_pending_stack_adjust ();
506 do_jump (TREE_OPERAND (exp, 0), label1, NULL, -1);
507 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
508 emit_label (label1);
509 do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
510 break;
513 case COMPOUND_EXPR:
514 /* Lowered by gimplify.c. */
515 gcc_unreachable ();
517 case MINUS_EXPR:
518 /* Nonzero iff operands of minus differ. */
519 code = NE_EXPR;
521 /* FALLTHRU */
522 case EQ_EXPR:
523 case NE_EXPR:
524 case LT_EXPR:
525 case LE_EXPR:
526 case GT_EXPR:
527 case GE_EXPR:
528 case ORDERED_EXPR:
529 case UNORDERED_EXPR:
530 case UNLT_EXPR:
531 case UNLE_EXPR:
532 case UNGT_EXPR:
533 case UNGE_EXPR:
534 case UNEQ_EXPR:
535 case LTGT_EXPR:
536 case TRUTH_ANDIF_EXPR:
537 case TRUTH_ORIF_EXPR:
538 other_code:
539 do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
540 if_false_label, if_true_label, prob);
541 break;
543 case BIT_AND_EXPR:
544 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
545 See if the former is preferred for jump tests and restore it
546 if so. */
547 if (integer_onep (TREE_OPERAND (exp, 1)))
549 tree exp0 = TREE_OPERAND (exp, 0);
550 rtx_code_label *set_label, *clr_label;
551 int setclr_prob = prob;
553 /* Strip narrowing integral type conversions. */
554 while (CONVERT_EXPR_P (exp0)
555 && TREE_OPERAND (exp0, 0) != error_mark_node
556 && TYPE_PRECISION (TREE_TYPE (exp0))
557 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
558 exp0 = TREE_OPERAND (exp0, 0);
560 /* "exp0 ^ 1" inverts the sense of the single bit test. */
561 if (TREE_CODE (exp0) == BIT_XOR_EXPR
562 && integer_onep (TREE_OPERAND (exp0, 1)))
564 exp0 = TREE_OPERAND (exp0, 0);
565 clr_label = if_true_label;
566 set_label = if_false_label;
567 setclr_prob = inv (prob);
569 else
571 clr_label = if_false_label;
572 set_label = if_true_label;
575 if (TREE_CODE (exp0) == RSHIFT_EXPR)
577 tree arg = TREE_OPERAND (exp0, 0);
578 tree shift = TREE_OPERAND (exp0, 1);
579 tree argtype = TREE_TYPE (arg);
580 if (TREE_CODE (shift) == INTEGER_CST
581 && compare_tree_int (shift, 0) >= 0
582 && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
583 && prefer_and_bit_test (TYPE_MODE (argtype),
584 TREE_INT_CST_LOW (shift)))
586 unsigned HOST_WIDE_INT mask
587 = (unsigned HOST_WIDE_INT) 1 << TREE_INT_CST_LOW (shift);
588 do_jump (build2 (BIT_AND_EXPR, argtype, arg,
589 build_int_cstu (argtype, mask)),
590 clr_label, set_label, setclr_prob);
591 break;
596 /* If we are AND'ing with a small constant, do this comparison in the
597 smallest type that fits. If the machine doesn't have comparisons
598 that small, it will be converted back to the wider comparison.
599 This helps if we are testing the sign bit of a narrower object.
600 combine can't do this for us because it can't know whether a
601 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
603 if (! SLOW_BYTE_ACCESS
604 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
605 && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
606 && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
607 && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
608 && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
609 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
610 && have_insn_for (COMPARE, TYPE_MODE (type)))
612 do_jump (fold_convert (type, exp), if_false_label, if_true_label,
613 prob);
614 break;
617 if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
618 || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
619 goto normal;
621 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
623 case TRUTH_AND_EXPR:
624 /* High branch cost, expand as the bitwise AND of the conditions.
625 Do the same if the RHS has side effects, because we're effectively
626 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
627 if (BRANCH_COST (optimize_insn_for_speed_p (),
628 false) >= 4
629 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
630 goto normal;
631 code = TRUTH_ANDIF_EXPR;
632 goto other_code;
634 case BIT_IOR_EXPR:
635 case TRUTH_OR_EXPR:
636 /* High branch cost, expand as the bitwise OR of the conditions.
637 Do the same if the RHS has side effects, because we're effectively
638 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
639 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
640 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
641 goto normal;
642 code = TRUTH_ORIF_EXPR;
643 goto other_code;
645 /* Fall through and generate the normal code. */
646 default:
647 normal:
648 temp = expand_normal (exp);
649 do_pending_stack_adjust ();
650 /* The RTL optimizers prefer comparisons against pseudos. */
651 if (GET_CODE (temp) == SUBREG)
653 /* Compare promoted variables in their promoted mode. */
654 if (SUBREG_PROMOTED_VAR_P (temp)
655 && REG_P (XEXP (temp, 0)))
656 temp = XEXP (temp, 0);
657 else
658 temp = copy_to_reg (temp);
660 do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
661 NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
662 GET_MODE (temp), NULL_RTX,
663 if_false_label, if_true_label, prob);
666 if (drop_through_label)
668 do_pending_stack_adjust ();
669 emit_label (drop_through_label);
673 /* Compare OP0 with OP1, word at a time, in mode MODE.
674 UNSIGNEDP says to do unsigned comparison.
675 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
677 static void
678 do_jump_by_parts_greater_rtx (machine_mode mode, int unsignedp, rtx op0,
679 rtx op1, rtx_code_label *if_false_label,
680 rtx_code_label *if_true_label,
681 int prob)
683 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
684 rtx_code_label *drop_through_label = 0;
685 bool drop_through_if_true = false, drop_through_if_false = false;
686 enum rtx_code code = GT;
687 int i;
689 if (! if_true_label || ! if_false_label)
690 drop_through_label = gen_label_rtx ();
691 if (! if_true_label)
693 if_true_label = drop_through_label;
694 drop_through_if_true = true;
696 if (! if_false_label)
698 if_false_label = drop_through_label;
699 drop_through_if_false = true;
702 /* Deal with the special case 0 > x: only one comparison is necessary and
703 we reverse it to avoid jumping to the drop-through label. */
704 if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
706 code = LE;
707 if_true_label = if_false_label;
708 if_false_label = drop_through_label;
709 drop_through_if_true = false;
710 drop_through_if_false = true;
713 /* Compare a word at a time, high order first. */
714 for (i = 0; i < nwords; i++)
716 rtx op0_word, op1_word;
718 if (WORDS_BIG_ENDIAN)
720 op0_word = operand_subword_force (op0, i, mode);
721 op1_word = operand_subword_force (op1, i, mode);
723 else
725 op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
726 op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
729 /* All but high-order word must be compared as unsigned. */
730 do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
731 word_mode, NULL_RTX, NULL, if_true_label,
732 prob);
734 /* Emit only one comparison for 0. Do not emit the last cond jump. */
735 if (op0 == const0_rtx || i == nwords - 1)
736 break;
738 /* Consider lower words only if these are equal. */
739 do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
740 NULL_RTX, NULL, if_false_label, inv (prob));
743 if (!drop_through_if_false)
744 emit_jump (if_false_label);
745 if (drop_through_label)
746 emit_label (drop_through_label);
749 /* Given a comparison expression EXP for values too wide to be compared
750 with one insn, test the comparison and jump to the appropriate label.
751 The code of EXP is ignored; we always test GT if SWAP is 0,
752 and LT if SWAP is 1. */
754 static void
755 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
756 rtx_code_label *if_false_label,
757 rtx_code_label *if_true_label, int prob)
759 rtx op0 = expand_normal (swap ? treeop1 : treeop0);
760 rtx op1 = expand_normal (swap ? treeop0 : treeop1);
761 machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
762 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
764 do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
765 if_true_label, prob);
768 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
769 mode, MODE, that is too wide for the available compare insns. Either
770 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL
771 to indicate drop through. */
773 static void
774 do_jump_by_parts_zero_rtx (machine_mode mode, rtx op0,
775 rtx_code_label *if_false_label,
776 rtx_code_label *if_true_label, int prob)
778 int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
779 rtx part;
780 int i;
781 rtx_code_label *drop_through_label = NULL;
783 /* The fastest way of doing this comparison on almost any machine is to
784 "or" all the words and compare the result. If all have to be loaded
785 from memory and this is a very wide item, it's possible this may
786 be slower, but that's highly unlikely. */
788 part = gen_reg_rtx (word_mode);
789 emit_move_insn (part, operand_subword_force (op0, 0, mode));
790 for (i = 1; i < nwords && part != 0; i++)
791 part = expand_binop (word_mode, ior_optab, part,
792 operand_subword_force (op0, i, mode),
793 part, 1, OPTAB_WIDEN);
795 if (part != 0)
797 do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
798 NULL_RTX, if_false_label, if_true_label, prob);
799 return;
802 /* If we couldn't do the "or" simply, do this with a series of compares. */
803 if (! if_false_label)
804 if_false_label = drop_through_label = gen_label_rtx ();
806 for (i = 0; i < nwords; i++)
807 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
808 const0_rtx, EQ, 1, word_mode, NULL_RTX,
809 if_false_label, NULL, prob);
811 if (if_true_label)
812 emit_jump (if_true_label);
814 if (drop_through_label)
815 emit_label (drop_through_label);
818 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
819 where MODE is an integer mode too wide to be compared with one insn.
820 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
821 to indicate drop through. */
823 static void
824 do_jump_by_parts_equality_rtx (machine_mode mode, rtx op0, rtx op1,
825 rtx_code_label *if_false_label,
826 rtx_code_label *if_true_label, int prob)
828 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
829 rtx_code_label *drop_through_label = NULL;
830 int i;
832 if (op1 == const0_rtx)
834 do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label,
835 prob);
836 return;
838 else if (op0 == const0_rtx)
840 do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label,
841 prob);
842 return;
845 if (! if_false_label)
846 drop_through_label = if_false_label = gen_label_rtx ();
848 for (i = 0; i < nwords; i++)
849 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
850 operand_subword_force (op1, i, mode),
851 EQ, 0, word_mode, NULL_RTX,
852 if_false_label, NULL, prob);
854 if (if_true_label)
855 emit_jump (if_true_label);
856 if (drop_through_label)
857 emit_label (drop_through_label);
860 /* Given an EQ_EXPR expression EXP for values too wide to be compared
861 with one insn, test the comparison and jump to the appropriate label. */
863 static void
864 do_jump_by_parts_equality (tree treeop0, tree treeop1,
865 rtx_code_label *if_false_label,
866 rtx_code_label *if_true_label, int prob)
868 rtx op0 = expand_normal (treeop0);
869 rtx op1 = expand_normal (treeop1);
870 machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
871 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
872 if_true_label, prob);
875 /* Split a comparison into two others, the second of which has the other
876 "orderedness". The first is always ORDERED or UNORDERED if MODE
877 does not honor NaNs (which means that it can be skipped in that case;
878 see do_compare_rtx_and_jump).
880 The two conditions are written in *CODE1 and *CODE2. Return true if
881 the conditions must be ANDed, false if they must be ORed. */
883 bool
884 split_comparison (enum rtx_code code, machine_mode mode,
885 enum rtx_code *code1, enum rtx_code *code2)
887 switch (code)
889 case LT:
890 *code1 = ORDERED;
891 *code2 = UNLT;
892 return true;
893 case LE:
894 *code1 = ORDERED;
895 *code2 = UNLE;
896 return true;
897 case GT:
898 *code1 = ORDERED;
899 *code2 = UNGT;
900 return true;
901 case GE:
902 *code1 = ORDERED;
903 *code2 = UNGE;
904 return true;
905 case EQ:
906 *code1 = ORDERED;
907 *code2 = UNEQ;
908 return true;
909 case NE:
910 *code1 = UNORDERED;
911 *code2 = LTGT;
912 return false;
913 case UNLT:
914 *code1 = UNORDERED;
915 *code2 = LT;
916 return false;
917 case UNLE:
918 *code1 = UNORDERED;
919 *code2 = LE;
920 return false;
921 case UNGT:
922 *code1 = UNORDERED;
923 *code2 = GT;
924 return false;
925 case UNGE:
926 *code1 = UNORDERED;
927 *code2 = GE;
928 return false;
929 case UNEQ:
930 *code1 = UNORDERED;
931 *code2 = EQ;
932 return false;
933 case LTGT:
934 /* Do not turn a trapping comparison into a non-trapping one. */
935 if (HONOR_SNANS (mode))
937 *code1 = LT;
938 *code2 = GT;
939 return false;
941 else
943 *code1 = ORDERED;
944 *code2 = NE;
945 return true;
947 default:
948 gcc_unreachable ();
953 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
954 The decision as to signed or unsigned comparison must be made by the caller.
956 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
957 compared. */
959 void
960 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
961 machine_mode mode, rtx size,
962 rtx_code_label *if_false_label,
963 rtx_code_label *if_true_label, int prob)
965 rtx tem;
966 rtx_code_label *dummy_label = NULL;
968 /* Reverse the comparison if that is safe and we want to jump if it is
969 false. Also convert to the reverse comparison if the target can
970 implement it. */
971 if ((! if_true_label
972 || ! can_compare_p (code, mode, ccp_jump))
973 && (! FLOAT_MODE_P (mode)
974 || code == ORDERED || code == UNORDERED
975 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
976 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
978 enum rtx_code rcode;
979 if (FLOAT_MODE_P (mode))
980 rcode = reverse_condition_maybe_unordered (code);
981 else
982 rcode = reverse_condition (code);
984 /* Canonicalize to UNORDERED for the libcall. */
985 if (can_compare_p (rcode, mode, ccp_jump)
986 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
988 std::swap (if_true_label, if_false_label);
989 code = rcode;
990 prob = inv (prob);
994 /* If one operand is constant, make it the second one. Only do this
995 if the other operand is not constant as well. */
997 if (swap_commutative_operands_p (op0, op1))
999 std::swap (op0, op1);
1000 code = swap_condition (code);
1003 do_pending_stack_adjust ();
1005 code = unsignedp ? unsigned_condition (code) : code;
1006 if (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
1007 op0, op1)))
1009 if (CONSTANT_P (tem))
1011 rtx_code_label *label = (tem == const0_rtx
1012 || tem == CONST0_RTX (mode))
1013 ? if_false_label : if_true_label;
1014 if (label)
1015 emit_jump (label);
1016 return;
1019 code = GET_CODE (tem);
1020 mode = GET_MODE (tem);
1021 op0 = XEXP (tem, 0);
1022 op1 = XEXP (tem, 1);
1023 unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
1026 if (! if_true_label)
1027 dummy_label = if_true_label = gen_label_rtx ();
1029 if (GET_MODE_CLASS (mode) == MODE_INT
1030 && ! can_compare_p (code, mode, ccp_jump))
1032 switch (code)
1034 case LTU:
1035 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1036 if_false_label, if_true_label, prob);
1037 break;
1039 case LEU:
1040 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1041 if_true_label, if_false_label,
1042 inv (prob));
1043 break;
1045 case GTU:
1046 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1047 if_false_label, if_true_label, prob);
1048 break;
1050 case GEU:
1051 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1052 if_true_label, if_false_label,
1053 inv (prob));
1054 break;
1056 case LT:
1057 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1058 if_false_label, if_true_label, prob);
1059 break;
1061 case LE:
1062 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1063 if_true_label, if_false_label,
1064 inv (prob));
1065 break;
1067 case GT:
1068 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1069 if_false_label, if_true_label, prob);
1070 break;
1072 case GE:
1073 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1074 if_true_label, if_false_label,
1075 inv (prob));
1076 break;
1078 case EQ:
1079 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
1080 if_true_label, prob);
1081 break;
1083 case NE:
1084 do_jump_by_parts_equality_rtx (mode, op0, op1, if_true_label,
1085 if_false_label, inv (prob));
1086 break;
1088 default:
1089 gcc_unreachable ();
1092 else
1094 if (SCALAR_FLOAT_MODE_P (mode)
1095 && ! can_compare_p (code, mode, ccp_jump)
1096 && can_compare_p (swap_condition (code), mode, ccp_jump))
1098 code = swap_condition (code);
1099 std::swap (op0, op1);
1101 else if (SCALAR_FLOAT_MODE_P (mode)
1102 && ! can_compare_p (code, mode, ccp_jump)
1103 /* Never split ORDERED and UNORDERED.
1104 These must be implemented. */
1105 && (code != ORDERED && code != UNORDERED)
1106 /* Split a floating-point comparison if
1107 we can jump on other conditions... */
1108 && (have_insn_for (COMPARE, mode)
1109 /* ... or if there is no libcall for it. */
1110 || code_to_optab (code) == unknown_optab))
1112 enum rtx_code first_code;
1113 bool and_them = split_comparison (code, mode, &first_code, &code);
1115 /* If there are no NaNs, the first comparison should always fall
1116 through. */
1117 if (!HONOR_NANS (mode))
1118 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1120 else
1122 int first_prob = prob;
1123 if (first_code == UNORDERED)
1124 first_prob = REG_BR_PROB_BASE / 100;
1125 else if (first_code == ORDERED)
1126 first_prob = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100;
1127 if (and_them)
1129 rtx_code_label *dest_label;
1130 /* If we only jump if true, just bypass the second jump. */
1131 if (! if_false_label)
1133 if (! dummy_label)
1134 dummy_label = gen_label_rtx ();
1135 dest_label = dummy_label;
1137 else
1138 dest_label = if_false_label;
1139 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1140 size, dest_label, NULL, first_prob);
1142 else
1143 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1144 size, NULL, if_true_label, first_prob);
1148 emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1149 if_true_label, prob);
1152 if (if_false_label)
1153 emit_jump (if_false_label);
1154 if (dummy_label)
1155 emit_label (dummy_label);
1158 /* Generate code for a comparison expression EXP (including code to compute
1159 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1160 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1161 generated code will drop through.
1162 SIGNED_CODE should be the rtx operation for this comparison for
1163 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1165 We force a stack adjustment unless there are currently
1166 things pushed on the stack that aren't yet used. */
1168 static void
1169 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1170 enum rtx_code unsigned_code,
1171 rtx_code_label *if_false_label,
1172 rtx_code_label *if_true_label, int prob)
1174 rtx op0, op1;
1175 tree type;
1176 machine_mode mode;
1177 int unsignedp;
1178 enum rtx_code code;
1180 /* Don't crash if the comparison was erroneous. */
1181 op0 = expand_normal (treeop0);
1182 if (TREE_CODE (treeop0) == ERROR_MARK)
1183 return;
1185 op1 = expand_normal (treeop1);
1186 if (TREE_CODE (treeop1) == ERROR_MARK)
1187 return;
1189 type = TREE_TYPE (treeop0);
1190 mode = TYPE_MODE (type);
1191 if (TREE_CODE (treeop0) == INTEGER_CST
1192 && (TREE_CODE (treeop1) != INTEGER_CST
1193 || (GET_MODE_BITSIZE (mode)
1194 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1196 /* op0 might have been replaced by promoted constant, in which
1197 case the type of second argument should be used. */
1198 type = TREE_TYPE (treeop1);
1199 mode = TYPE_MODE (type);
1201 unsignedp = TYPE_UNSIGNED (type);
1202 code = unsignedp ? unsigned_code : signed_code;
1204 /* If function pointers need to be "canonicalized" before they can
1205 be reliably compared, then canonicalize them.
1206 Only do this if *both* sides of the comparison are function pointers.
1207 If one side isn't, we want a noncanonicalized comparison. See PR
1208 middle-end/17564. */
1209 if (targetm.have_canonicalize_funcptr_for_compare ()
1210 && TREE_CODE (TREE_TYPE (treeop0)) == POINTER_TYPE
1211 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0)))
1212 == FUNCTION_TYPE
1213 && TREE_CODE (TREE_TYPE (treeop1)) == POINTER_TYPE
1214 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1)))
1215 == FUNCTION_TYPE)
1217 rtx new_op0 = gen_reg_rtx (mode);
1218 rtx new_op1 = gen_reg_rtx (mode);
1220 emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op0, op0));
1221 op0 = new_op0;
1223 emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op1, op1));
1224 op1 = new_op1;
1227 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1228 ((mode == BLKmode)
1229 ? expr_size (treeop0) : NULL_RTX),
1230 if_false_label, if_true_label, prob);
1233 #include "gt-dojump.h"