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
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
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/>. */
22 #include "coretypes.h"
28 #include "fold-const.h"
29 #include "stor-layout.h"
31 #include "hard-reg-set.h"
33 #include "insn-config.h"
34 #include "insn-attr.h"
35 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
44 #include "insn-codes.h"
46 #include "langhooks.h"
48 #include "basic-block.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. */
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. */
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. */
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. */
94 clear_pending_stack_adjust (void)
97 && (! flag_omit_frame_pointer
|| cfun
->calls_alloca
)
99 discard_pending_stack_adjust ();
102 /* Pop any previously-pushed arguments that have not been popped yet. */
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. */
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. */
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. */
143 jumpifnot (tree exp
, rtx_code_label
*label
, int prob
)
145 do_jump (exp
, label
, NULL
, inv (prob
));
149 jumpifnot_1 (enum tree_code code
, tree op0
, tree op1
, rtx_code_label
*label
,
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. */
158 jumpif (tree exp
, rtx_code_label
*label
, int prob
)
160 do_jump (exp
, NULL
, label
, prob
);
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
181 prefer_and_bit_test (machine_mode mode
, int bitnum
)
184 wide_int mask
= wi::set_bit_in_zero (bitnum
, GET_MODE_PRECISION (mode
));
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
),
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. */
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
,
223 rtx_code_label
*drop_through_label
= 0;
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
,
243 do_compare_and_jump (op0
, op1
, EQ
, EQ
, if_false_label
, if_true_label
,
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
,
264 do_compare_and_jump (op0
, op1
, NE
, NE
, if_false_label
, if_true_label
,
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
,
276 do_compare_and_jump (op0
, op1
, LT
, LTU
, if_false_label
, if_true_label
,
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
,
287 do_compare_and_jump (op0
, op1
, LE
, LEU
, if_false_label
, if_true_label
,
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
,
298 do_compare_and_jump (op0
, op1
, GT
, GTU
, if_false_label
, if_true_label
,
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
,
309 do_compare_and_jump (op0
, op1
, GE
, GEU
, if_false_label
, if_true_label
,
314 do_compare_and_jump (op0
, op1
, ORDERED
, ORDERED
,
315 if_false_label
, if_true_label
, prob
);
319 do_compare_and_jump (op0
, op1
, UNORDERED
, UNORDERED
,
320 if_false_label
, if_true_label
, prob
);
324 do_compare_and_jump (op0
, op1
, UNLT
, UNLT
, if_false_label
, if_true_label
,
329 do_compare_and_jump (op0
, op1
, UNLE
, UNLE
, if_false_label
, if_true_label
,
334 do_compare_and_jump (op0
, op1
, UNGT
, UNGT
, if_false_label
, if_true_label
,
339 do_compare_and_jump (op0
, op1
, UNGE
, UNGE
, if_false_label
, if_true_label
,
344 do_compare_and_jump (op0
, op1
, UNEQ
, UNEQ
, if_false_label
, if_true_label
,
349 do_compare_and_jump (op0
, op1
, LTGT
, LTGT
, if_false_label
, if_true_label
,
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). */
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
);
381 do_jump (op0
, if_false_label
, NULL
, op0_prob
);
382 do_jump (op1
, if_false_label
, if_true_label
, op1_prob
);
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). */
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
);
409 do_jump (op0
, NULL
, if_true_label
, op0_prob
);
410 do_jump (op1
, if_false_label
, if_true_label
, op1_prob
);
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. */
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
);
446 rtx_code_label
*drop_through_label
= NULL
;
455 rtx_code_label
*lab
= integer_zerop (exp
) ? if_false_label
463 /* This is not true with #pragma weak */
465 /* The address of something can never be zero. */
467 emit_jump (if_true_label
);
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
)
478 /* If we are narrowing the operand, we have to do the compare in the
480 if ((TYPE_PRECISION (TREE_TYPE (exp
))
481 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0)))))
483 case NON_LVALUE_EXPR
:
488 /* These cannot change zero->nonzero or vice versa. */
489 do_jump (TREE_OPERAND (exp
, 0), if_false_label
, if_true_label
, prob
);
493 do_jump (TREE_OPERAND (exp
, 0), if_true_label
, if_false_label
,
499 rtx_code_label
*label1
= gen_label_rtx ();
500 if (!if_true_label
|| !if_false_label
)
502 drop_through_label
= gen_label_rtx ();
504 if_true_label
= drop_through_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
);
513 do_jump (TREE_OPERAND (exp
, 2), if_false_label
, if_true_label
, prob
);
518 /* Lowered by gimplify.c. */
522 /* Nonzero iff operands of minus differ. */
540 case TRUTH_ANDIF_EXPR
:
541 case TRUTH_ORIF_EXPR
:
543 do_jump_1 (code
, TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
544 if_false_label
, if_true_label
, prob
);
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
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
);
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
);
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
,
621 if (TYPE_PRECISION (TREE_TYPE (exp
)) > 1
622 || TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
625 /* Boolean comparisons can be compiled as 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 (),
633 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp
, 1)))
635 code
= TRUTH_ANDIF_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)))
646 code
= TRUTH_ORIF_EXPR
;
649 /* Fall through and generate the normal code. */
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);
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. */
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
,
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
;
693 if (! if_true_label
|| ! if_false_label
)
694 drop_through_label
= gen_label_rtx ();
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
)
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
);
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
,
738 /* Emit only one comparison for 0. Do not emit the last cond jump. */
739 if (op0
== const0_rtx
|| i
== nwords
- 1)
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. */
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. */
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
;
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
);
801 do_compare_rtx_and_jump (part
, const0_rtx
, EQ
, 1, word_mode
,
802 NULL_RTX
, if_false_label
, if_true_label
, prob
);
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
);
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. */
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
;
836 if (op1
== const0_rtx
)
838 do_jump_by_parts_zero_rtx (mode
, op0
, if_false_label
, if_true_label
,
842 else if (op0
== const0_rtx
)
844 do_jump_by_parts_zero_rtx (mode
, op1
, if_false_label
, if_true_label
,
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
);
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. */
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. */
888 split_comparison (enum rtx_code code
, machine_mode mode
,
889 enum rtx_code
*code1
, enum rtx_code
*code2
)
938 /* Do not turn a trapping comparison into a non-trapping one. */
939 if (HONOR_SNANS (mode
))
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
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
)
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
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
))))
983 if (FLOAT_MODE_P (mode
))
984 rcode
= reverse_condition_maybe_unordered (code
);
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
);
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
,
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
;
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
))
1039 do_jump_by_parts_greater_rtx (mode
, 1, op1
, op0
,
1040 if_false_label
, if_true_label
, prob
);
1044 do_jump_by_parts_greater_rtx (mode
, 1, op0
, op1
,
1045 if_true_label
, if_false_label
,
1050 do_jump_by_parts_greater_rtx (mode
, 1, op0
, op1
,
1051 if_false_label
, if_true_label
, prob
);
1055 do_jump_by_parts_greater_rtx (mode
, 1, op1
, op0
,
1056 if_true_label
, if_false_label
,
1061 do_jump_by_parts_greater_rtx (mode
, 0, op1
, op0
,
1062 if_false_label
, if_true_label
, prob
);
1066 do_jump_by_parts_greater_rtx (mode
, 0, op0
, op1
,
1067 if_true_label
, if_false_label
,
1072 do_jump_by_parts_greater_rtx (mode
, 0, op0
, op1
,
1073 if_false_label
, if_true_label
, prob
);
1077 do_jump_by_parts_greater_rtx (mode
, 0, op1
, op0
,
1078 if_true_label
, if_false_label
,
1083 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_false_label
,
1084 if_true_label
, prob
);
1088 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_true_label
,
1089 if_false_label
, inv (prob
));
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
1121 if (!HONOR_NANS (mode
))
1122 gcc_assert (first_code
== (and_them
? ORDERED
: UNORDERED
));
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;
1133 rtx_code_label
*dest_label
;
1134 /* If we only jump if true, just bypass the second jump. */
1135 if (! if_false_label
)
1138 dummy_label
= gen_label_rtx ();
1139 dest_label
= dummy_label
;
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
);
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
);
1157 emit_jump (if_false_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. */
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
)
1184 /* Don't crash if the comparison was erroneous. */
1185 op0
= expand_normal (treeop0
);
1186 if (TREE_CODE (treeop0
) == ERROR_MARK
)
1189 op1
= expand_normal (treeop1
);
1190 if (TREE_CODE (treeop1
) == ERROR_MARK
)
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 /* If function pointers need to be "canonicalized" before they can
1209 be reliably compared, then canonicalize them.
1210 Only do this if *both* sides of the comparison are function pointers.
1211 If one side isn't, we want a noncanonicalized comparison. See PR
1212 middle-end/17564. */
1213 if (targetm
.have_canonicalize_funcptr_for_compare ()
1214 && TREE_CODE (TREE_TYPE (treeop0
)) == POINTER_TYPE
1215 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0
)))
1217 && TREE_CODE (TREE_TYPE (treeop1
)) == POINTER_TYPE
1218 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1
)))
1221 rtx new_op0
= gen_reg_rtx (mode
);
1222 rtx new_op1
= gen_reg_rtx (mode
);
1224 emit_insn (targetm
.gen_canonicalize_funcptr_for_compare (new_op0
, op0
));
1227 emit_insn (targetm
.gen_canonicalize_funcptr_for_compare (new_op1
, op1
));
1231 do_compare_rtx_and_jump (op0
, op1
, code
, unsignedp
, mode
,
1233 ? expr_size (treeop0
) : NULL_RTX
),
1234 if_false_label
, if_true_label
, prob
);
1237 #include "gt-dojump.h"