1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2014 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"
26 #include "stor-layout.h"
32 #include "hard-reg-set.h"
35 #include "insn-config.h"
36 #include "insn-attr.h"
37 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
40 #include "langhooks.h"
42 #include "basic-block.h"
45 static bool prefer_and_bit_test (enum machine_mode
, int);
46 static void do_jump_by_parts_greater (tree
, tree
, int, rtx
, rtx
, int);
47 static void do_jump_by_parts_equality (tree
, tree
, rtx
, rtx
, int);
48 static void do_compare_and_jump (tree
, tree
, enum rtx_code
, enum rtx_code
, rtx
,
51 /* Invert probability if there is any. -1 stands for unknown. */
56 return prob
== -1 ? -1 : REG_BR_PROB_BASE
- prob
;
59 /* At the start of a function, record that we have no previously-pushed
60 arguments waiting to be popped. */
63 init_pending_stack_adjust (void)
65 pending_stack_adjust
= 0;
68 /* Discard any pending stack adjustment. This avoid relying on the
69 RTL optimizers to remove useless adjustments when we know the
70 stack pointer value is dead. */
72 discard_pending_stack_adjust (void)
74 stack_pointer_delta
-= pending_stack_adjust
;
75 pending_stack_adjust
= 0;
78 /* When exiting from function, if safe, clear out any pending stack adjust
79 so the adjustment won't get done.
81 Note, if the current function calls alloca, then it must have a
82 frame pointer regardless of the value of flag_omit_frame_pointer. */
85 clear_pending_stack_adjust (void)
88 && (! flag_omit_frame_pointer
|| cfun
->calls_alloca
)
90 discard_pending_stack_adjust ();
93 /* Pop any previously-pushed arguments that have not been popped yet. */
96 do_pending_stack_adjust (void)
98 if (inhibit_defer_pop
== 0)
100 if (pending_stack_adjust
!= 0)
101 adjust_stack (GEN_INT (pending_stack_adjust
));
102 pending_stack_adjust
= 0;
106 /* Remember pending_stack_adjust/stack_pointer_delta.
107 To be used around code that may call do_pending_stack_adjust (),
108 but the generated code could be discarded e.g. using delete_insns_since. */
111 save_pending_stack_adjust (saved_pending_stack_adjust
*save
)
113 save
->x_pending_stack_adjust
= pending_stack_adjust
;
114 save
->x_stack_pointer_delta
= stack_pointer_delta
;
117 /* Restore the saved pending_stack_adjust/stack_pointer_delta. */
120 restore_pending_stack_adjust (saved_pending_stack_adjust
*save
)
122 if (inhibit_defer_pop
== 0)
124 pending_stack_adjust
= save
->x_pending_stack_adjust
;
125 stack_pointer_delta
= save
->x_stack_pointer_delta
;
129 /* Expand conditional expressions. */
131 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.
132 LABEL is an rtx of code CODE_LABEL, in this function and all the
136 jumpifnot (tree exp
, rtx label
, int prob
)
138 do_jump (exp
, label
, NULL_RTX
, inv (prob
));
142 jumpifnot_1 (enum tree_code code
, tree op0
, tree op1
, rtx label
, int prob
)
144 do_jump_1 (code
, op0
, op1
, label
, NULL_RTX
, inv (prob
));
147 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
150 jumpif (tree exp
, rtx label
, int prob
)
152 do_jump (exp
, NULL_RTX
, label
, prob
);
156 jumpif_1 (enum tree_code code
, tree op0
, tree op1
, rtx label
, int prob
)
158 do_jump_1 (code
, op0
, op1
, NULL_RTX
, label
, prob
);
161 /* Used internally by prefer_and_bit_test. */
163 static GTY(()) rtx and_reg
;
164 static GTY(()) rtx and_test
;
165 static GTY(()) rtx shift_test
;
167 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
168 where X is an arbitrary register of mode MODE. Return true if the former
172 prefer_and_bit_test (enum machine_mode mode
, int bitnum
)
175 wide_int mask
= wi::set_bit_in_zero (bitnum
, GET_MODE_PRECISION (mode
));
179 /* Set up rtxes for the two variations. Use NULL as a placeholder
180 for the BITNUM-based constants. */
181 and_reg
= gen_rtx_REG (mode
, FIRST_PSEUDO_REGISTER
);
182 and_test
= gen_rtx_AND (mode
, and_reg
, NULL
);
183 shift_test
= gen_rtx_AND (mode
, gen_rtx_ASHIFTRT (mode
, and_reg
, NULL
),
188 /* Change the mode of the previously-created rtxes. */
189 PUT_MODE (and_reg
, mode
);
190 PUT_MODE (and_test
, mode
);
191 PUT_MODE (shift_test
, mode
);
192 PUT_MODE (XEXP (shift_test
, 0), mode
);
195 /* Fill in the integers. */
196 XEXP (and_test
, 1) = immed_wide_int_const (mask
, mode
);
197 XEXP (XEXP (shift_test
, 0), 1) = GEN_INT (bitnum
);
199 speed_p
= optimize_insn_for_speed_p ();
200 return (rtx_cost (and_test
, IF_THEN_ELSE
, 0, speed_p
)
201 <= rtx_cost (shift_test
, IF_THEN_ELSE
, 0, speed_p
));
204 /* Subroutine of do_jump, dealing with exploded comparisons of the type
205 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
206 PROB is probability of jump to if_true_label, or -1 if unknown. */
209 do_jump_1 (enum tree_code code
, tree op0
, tree op1
,
210 rtx if_false_label
, rtx if_true_label
, int prob
)
212 enum machine_mode mode
;
213 rtx_code_label
*drop_through_label
= 0;
219 tree inner_type
= TREE_TYPE (op0
);
221 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
222 != MODE_COMPLEX_FLOAT
);
223 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
224 != MODE_COMPLEX_INT
);
226 if (integer_zerop (op1
))
227 do_jump (op0
, if_true_label
, if_false_label
, inv (prob
));
228 else if (GET_MODE_CLASS (TYPE_MODE (inner_type
)) == MODE_INT
229 && !can_compare_p (EQ
, TYPE_MODE (inner_type
), ccp_jump
))
230 do_jump_by_parts_equality (op0
, op1
, if_false_label
, if_true_label
,
233 do_compare_and_jump (op0
, op1
, EQ
, EQ
, if_false_label
, if_true_label
,
240 tree inner_type
= TREE_TYPE (op0
);
242 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
243 != MODE_COMPLEX_FLOAT
);
244 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
245 != MODE_COMPLEX_INT
);
247 if (integer_zerop (op1
))
248 do_jump (op0
, if_false_label
, if_true_label
, prob
);
249 else if (GET_MODE_CLASS (TYPE_MODE (inner_type
)) == MODE_INT
250 && !can_compare_p (NE
, TYPE_MODE (inner_type
), ccp_jump
))
251 do_jump_by_parts_equality (op0
, op1
, if_true_label
, if_false_label
,
254 do_compare_and_jump (op0
, op1
, NE
, NE
, if_false_label
, if_true_label
,
260 mode
= TYPE_MODE (TREE_TYPE (op0
));
261 if (GET_MODE_CLASS (mode
) == MODE_INT
262 && ! can_compare_p (LT
, mode
, ccp_jump
))
263 do_jump_by_parts_greater (op0
, op1
, 1, if_false_label
, if_true_label
,
266 do_compare_and_jump (op0
, op1
, LT
, LTU
, if_false_label
, if_true_label
,
271 mode
= TYPE_MODE (TREE_TYPE (op0
));
272 if (GET_MODE_CLASS (mode
) == MODE_INT
273 && ! can_compare_p (LE
, mode
, ccp_jump
))
274 do_jump_by_parts_greater (op0
, op1
, 0, if_true_label
, if_false_label
,
277 do_compare_and_jump (op0
, op1
, LE
, LEU
, if_false_label
, if_true_label
,
282 mode
= TYPE_MODE (TREE_TYPE (op0
));
283 if (GET_MODE_CLASS (mode
) == MODE_INT
284 && ! can_compare_p (GT
, mode
, ccp_jump
))
285 do_jump_by_parts_greater (op0
, op1
, 0, if_false_label
, if_true_label
,
288 do_compare_and_jump (op0
, op1
, GT
, GTU
, if_false_label
, if_true_label
,
293 mode
= TYPE_MODE (TREE_TYPE (op0
));
294 if (GET_MODE_CLASS (mode
) == MODE_INT
295 && ! can_compare_p (GE
, mode
, ccp_jump
))
296 do_jump_by_parts_greater (op0
, op1
, 1, if_true_label
, if_false_label
,
299 do_compare_and_jump (op0
, op1
, GE
, GEU
, if_false_label
, if_true_label
,
304 do_compare_and_jump (op0
, op1
, ORDERED
, ORDERED
,
305 if_false_label
, if_true_label
, prob
);
309 do_compare_and_jump (op0
, op1
, UNORDERED
, UNORDERED
,
310 if_false_label
, if_true_label
, prob
);
314 do_compare_and_jump (op0
, op1
, UNLT
, UNLT
, if_false_label
, if_true_label
,
319 do_compare_and_jump (op0
, op1
, UNLE
, UNLE
, if_false_label
, if_true_label
,
324 do_compare_and_jump (op0
, op1
, UNGT
, UNGT
, if_false_label
, if_true_label
,
329 do_compare_and_jump (op0
, op1
, UNGE
, UNGE
, if_false_label
, if_true_label
,
334 do_compare_and_jump (op0
, op1
, UNEQ
, UNEQ
, if_false_label
, if_true_label
,
339 do_compare_and_jump (op0
, op1
, LTGT
, LTGT
, if_false_label
, if_true_label
,
343 case TRUTH_ANDIF_EXPR
:
345 /* Spread the probability that the expression is false evenly between
346 the two conditions. So the first condition is false half the total
347 probability of being false. The second condition is false the other
348 half of the total probability of being false, so its jump has a false
349 probability of half the total, relative to the probability we
350 reached it (i.e. the first condition was true). */
355 int false_prob
= inv (prob
);
356 int op0_false_prob
= false_prob
/ 2;
357 int op1_false_prob
= GCOV_COMPUTE_SCALE ((false_prob
/ 2),
358 inv (op0_false_prob
));
359 /* Get the probability that each jump below is true. */
360 op0_prob
= inv (op0_false_prob
);
361 op1_prob
= inv (op1_false_prob
);
363 if (if_false_label
== NULL_RTX
)
365 drop_through_label
= gen_label_rtx ();
366 do_jump (op0
, drop_through_label
, NULL_RTX
, op0_prob
);
367 do_jump (op1
, NULL_RTX
, if_true_label
, op1_prob
);
371 do_jump (op0
, if_false_label
, NULL_RTX
, op0_prob
);
372 do_jump (op1
, if_false_label
, if_true_label
, op1_prob
);
377 case TRUTH_ORIF_EXPR
:
379 /* Spread the probability evenly between the two conditions. So
380 the first condition has half the total probability of being true.
381 The second condition has the other half of the total probability,
382 so its jump has a probability of half the total, relative to
383 the probability we reached it (i.e. the first condition was false). */
389 op1_prob
= GCOV_COMPUTE_SCALE ((prob
/ 2), inv (op0_prob
));
391 if (if_true_label
== NULL_RTX
)
393 drop_through_label
= gen_label_rtx ();
394 do_jump (op0
, NULL_RTX
, drop_through_label
, op0_prob
);
395 do_jump (op1
, if_false_label
, NULL_RTX
, op1_prob
);
399 do_jump (op0
, NULL_RTX
, if_true_label
, op0_prob
);
400 do_jump (op1
, if_false_label
, if_true_label
, op1_prob
);
409 if (drop_through_label
)
411 do_pending_stack_adjust ();
412 emit_label (drop_through_label
);
416 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
417 the result is zero, or IF_TRUE_LABEL if the result is one.
418 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
419 meaning fall through in that case.
421 do_jump always does any pending stack adjust except when it does not
422 actually perform a jump. An example where there is no jump
423 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
425 PROB is probability of jump to if_true_label, or -1 if unknown. */
428 do_jump (tree exp
, rtx if_false_label
, rtx if_true_label
, int prob
)
430 enum tree_code code
= TREE_CODE (exp
);
434 enum machine_mode mode
;
435 rtx_code_label
*drop_through_label
= 0;
443 temp
= integer_zerop (exp
) ? if_false_label
: if_true_label
;
449 /* This is not true with #pragma weak */
451 /* The address of something can never be zero. */
453 emit_jump (if_true_label
);
458 if (TREE_CODE (TREE_OPERAND (exp
, 0)) == COMPONENT_REF
459 || TREE_CODE (TREE_OPERAND (exp
, 0)) == BIT_FIELD_REF
460 || TREE_CODE (TREE_OPERAND (exp
, 0)) == ARRAY_REF
461 || TREE_CODE (TREE_OPERAND (exp
, 0)) == ARRAY_RANGE_REF
)
464 /* If we are narrowing the operand, we have to do the compare in the
466 if ((TYPE_PRECISION (TREE_TYPE (exp
))
467 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0)))))
469 case NON_LVALUE_EXPR
:
474 /* These cannot change zero->nonzero or vice versa. */
475 do_jump (TREE_OPERAND (exp
, 0), if_false_label
, if_true_label
, prob
);
479 do_jump (TREE_OPERAND (exp
, 0), if_true_label
, if_false_label
,
485 rtx_code_label
*label1
= gen_label_rtx ();
486 if (!if_true_label
|| !if_false_label
)
488 drop_through_label
= gen_label_rtx ();
490 if_true_label
= drop_through_label
;
492 if_false_label
= drop_through_label
;
495 do_pending_stack_adjust ();
496 do_jump (TREE_OPERAND (exp
, 0), label1
, NULL_RTX
, -1);
497 do_jump (TREE_OPERAND (exp
, 1), if_false_label
, if_true_label
, prob
);
499 do_jump (TREE_OPERAND (exp
, 2), if_false_label
, if_true_label
, prob
);
504 /* Lowered by gimplify.c. */
508 /* Nonzero iff operands of minus differ. */
526 case TRUTH_ANDIF_EXPR
:
527 case TRUTH_ORIF_EXPR
:
529 do_jump_1 (code
, TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
530 if_false_label
, if_true_label
, prob
);
534 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
535 See if the former is preferred for jump tests and restore it
537 if (integer_onep (TREE_OPERAND (exp
, 1)))
539 tree exp0
= TREE_OPERAND (exp
, 0);
540 rtx set_label
, clr_label
;
541 int setclr_prob
= prob
;
543 /* Strip narrowing integral type conversions. */
544 while (CONVERT_EXPR_P (exp0
)
545 && TREE_OPERAND (exp0
, 0) != error_mark_node
546 && TYPE_PRECISION (TREE_TYPE (exp0
))
547 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0
, 0))))
548 exp0
= TREE_OPERAND (exp0
, 0);
550 /* "exp0 ^ 1" inverts the sense of the single bit test. */
551 if (TREE_CODE (exp0
) == BIT_XOR_EXPR
552 && integer_onep (TREE_OPERAND (exp0
, 1)))
554 exp0
= TREE_OPERAND (exp0
, 0);
555 clr_label
= if_true_label
;
556 set_label
= if_false_label
;
557 setclr_prob
= inv (prob
);
561 clr_label
= if_false_label
;
562 set_label
= if_true_label
;
565 if (TREE_CODE (exp0
) == RSHIFT_EXPR
)
567 tree arg
= TREE_OPERAND (exp0
, 0);
568 tree shift
= TREE_OPERAND (exp0
, 1);
569 tree argtype
= TREE_TYPE (arg
);
570 if (TREE_CODE (shift
) == INTEGER_CST
571 && compare_tree_int (shift
, 0) >= 0
572 && compare_tree_int (shift
, HOST_BITS_PER_WIDE_INT
) < 0
573 && prefer_and_bit_test (TYPE_MODE (argtype
),
574 TREE_INT_CST_LOW (shift
)))
576 unsigned HOST_WIDE_INT mask
577 = (unsigned HOST_WIDE_INT
) 1 << TREE_INT_CST_LOW (shift
);
578 do_jump (build2 (BIT_AND_EXPR
, argtype
, arg
,
579 build_int_cstu (argtype
, mask
)),
580 clr_label
, set_label
, setclr_prob
);
586 /* If we are AND'ing with a small constant, do this comparison in the
587 smallest type that fits. If the machine doesn't have comparisons
588 that small, it will be converted back to the wider comparison.
589 This helps if we are testing the sign bit of a narrower object.
590 combine can't do this for us because it can't know whether a
591 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
593 if (! SLOW_BYTE_ACCESS
594 && TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
595 && TYPE_PRECISION (TREE_TYPE (exp
)) <= HOST_BITS_PER_WIDE_INT
596 && (i
= tree_floor_log2 (TREE_OPERAND (exp
, 1))) >= 0
597 && (mode
= mode_for_size (i
+ 1, MODE_INT
, 0)) != BLKmode
598 && (type
= lang_hooks
.types
.type_for_mode (mode
, 1)) != 0
599 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (exp
))
600 && have_insn_for (COMPARE
, TYPE_MODE (type
)))
602 do_jump (fold_convert (type
, exp
), if_false_label
, if_true_label
,
607 if (TYPE_PRECISION (TREE_TYPE (exp
)) > 1
608 || TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
611 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
614 /* High branch cost, expand as the bitwise AND of the conditions.
615 Do the same if the RHS has side effects, because we're effectively
616 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
617 if (BRANCH_COST (optimize_insn_for_speed_p (),
619 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp
, 1)))
621 code
= TRUTH_ANDIF_EXPR
;
626 /* High branch cost, expand as the bitwise OR of the conditions.
627 Do the same if the RHS has side effects, because we're effectively
628 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
629 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
630 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp
, 1)))
632 code
= TRUTH_ORIF_EXPR
;
635 /* Fall through and generate the normal code. */
638 temp
= expand_normal (exp
);
639 do_pending_stack_adjust ();
640 /* The RTL optimizers prefer comparisons against pseudos. */
641 if (GET_CODE (temp
) == SUBREG
)
643 /* Compare promoted variables in their promoted mode. */
644 if (SUBREG_PROMOTED_VAR_P (temp
)
645 && REG_P (XEXP (temp
, 0)))
646 temp
= XEXP (temp
, 0);
648 temp
= copy_to_reg (temp
);
650 do_compare_rtx_and_jump (temp
, CONST0_RTX (GET_MODE (temp
)),
651 NE
, TYPE_UNSIGNED (TREE_TYPE (exp
)),
652 GET_MODE (temp
), NULL_RTX
,
653 if_false_label
, if_true_label
, prob
);
656 if (drop_through_label
)
658 do_pending_stack_adjust ();
659 emit_label (drop_through_label
);
663 /* Compare OP0 with OP1, word at a time, in mode MODE.
664 UNSIGNEDP says to do unsigned comparison.
665 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
668 do_jump_by_parts_greater_rtx (enum machine_mode mode
, int unsignedp
, rtx op0
,
669 rtx op1
, rtx if_false_label
, rtx if_true_label
,
672 int nwords
= (GET_MODE_SIZE (mode
) / UNITS_PER_WORD
);
673 rtx drop_through_label
= 0;
674 bool drop_through_if_true
= false, drop_through_if_false
= false;
675 enum rtx_code code
= GT
;
678 if (! if_true_label
|| ! if_false_label
)
679 drop_through_label
= gen_label_rtx ();
682 if_true_label
= drop_through_label
;
683 drop_through_if_true
= true;
685 if (! if_false_label
)
687 if_false_label
= drop_through_label
;
688 drop_through_if_false
= true;
691 /* Deal with the special case 0 > x: only one comparison is necessary and
692 we reverse it to avoid jumping to the drop-through label. */
693 if (op0
== const0_rtx
&& drop_through_if_true
&& !drop_through_if_false
)
696 if_true_label
= if_false_label
;
697 if_false_label
= drop_through_label
;
698 drop_through_if_true
= false;
699 drop_through_if_false
= true;
702 /* Compare a word at a time, high order first. */
703 for (i
= 0; i
< nwords
; i
++)
705 rtx op0_word
, op1_word
;
707 if (WORDS_BIG_ENDIAN
)
709 op0_word
= operand_subword_force (op0
, i
, mode
);
710 op1_word
= operand_subword_force (op1
, i
, mode
);
714 op0_word
= operand_subword_force (op0
, nwords
- 1 - i
, mode
);
715 op1_word
= operand_subword_force (op1
, nwords
- 1 - i
, mode
);
718 /* All but high-order word must be compared as unsigned. */
719 do_compare_rtx_and_jump (op0_word
, op1_word
, code
, (unsignedp
|| i
> 0),
720 word_mode
, NULL_RTX
, NULL_RTX
, if_true_label
,
723 /* Emit only one comparison for 0. Do not emit the last cond jump. */
724 if (op0
== const0_rtx
|| i
== nwords
- 1)
727 /* Consider lower words only if these are equal. */
728 do_compare_rtx_and_jump (op0_word
, op1_word
, NE
, unsignedp
, word_mode
,
729 NULL_RTX
, NULL_RTX
, if_false_label
, inv (prob
));
732 if (!drop_through_if_false
)
733 emit_jump (if_false_label
);
734 if (drop_through_label
)
735 emit_label (drop_through_label
);
738 /* Given a comparison expression EXP for values too wide to be compared
739 with one insn, test the comparison and jump to the appropriate label.
740 The code of EXP is ignored; we always test GT if SWAP is 0,
741 and LT if SWAP is 1. */
744 do_jump_by_parts_greater (tree treeop0
, tree treeop1
, int swap
,
745 rtx if_false_label
, rtx if_true_label
, int prob
)
747 rtx op0
= expand_normal (swap
? treeop1
: treeop0
);
748 rtx op1
= expand_normal (swap
? treeop0
: treeop1
);
749 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (treeop0
));
750 int unsignedp
= TYPE_UNSIGNED (TREE_TYPE (treeop0
));
752 do_jump_by_parts_greater_rtx (mode
, unsignedp
, op0
, op1
, if_false_label
,
753 if_true_label
, prob
);
756 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
757 mode, MODE, that is too wide for the available compare insns. Either
758 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
759 to indicate drop through. */
762 do_jump_by_parts_zero_rtx (enum machine_mode mode
, rtx op0
,
763 rtx if_false_label
, rtx if_true_label
, int prob
)
765 int nwords
= GET_MODE_SIZE (mode
) / UNITS_PER_WORD
;
768 rtx drop_through_label
= 0;
770 /* The fastest way of doing this comparison on almost any machine is to
771 "or" all the words and compare the result. If all have to be loaded
772 from memory and this is a very wide item, it's possible this may
773 be slower, but that's highly unlikely. */
775 part
= gen_reg_rtx (word_mode
);
776 emit_move_insn (part
, operand_subword_force (op0
, 0, mode
));
777 for (i
= 1; i
< nwords
&& part
!= 0; i
++)
778 part
= expand_binop (word_mode
, ior_optab
, part
,
779 operand_subword_force (op0
, i
, mode
),
780 part
, 1, OPTAB_WIDEN
);
784 do_compare_rtx_and_jump (part
, const0_rtx
, EQ
, 1, word_mode
,
785 NULL_RTX
, if_false_label
, if_true_label
, prob
);
789 /* If we couldn't do the "or" simply, do this with a series of compares. */
790 if (! if_false_label
)
791 drop_through_label
= if_false_label
= gen_label_rtx ();
793 for (i
= 0; i
< nwords
; i
++)
794 do_compare_rtx_and_jump (operand_subword_force (op0
, i
, mode
),
795 const0_rtx
, EQ
, 1, word_mode
, NULL_RTX
,
796 if_false_label
, NULL_RTX
, prob
);
799 emit_jump (if_true_label
);
801 if (drop_through_label
)
802 emit_label (drop_through_label
);
805 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
806 where MODE is an integer mode too wide to be compared with one insn.
807 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
808 to indicate drop through. */
811 do_jump_by_parts_equality_rtx (enum machine_mode mode
, rtx op0
, rtx op1
,
812 rtx if_false_label
, rtx if_true_label
, int prob
)
814 int nwords
= (GET_MODE_SIZE (mode
) / UNITS_PER_WORD
);
815 rtx drop_through_label
= 0;
818 if (op1
== const0_rtx
)
820 do_jump_by_parts_zero_rtx (mode
, op0
, if_false_label
, if_true_label
,
824 else if (op0
== const0_rtx
)
826 do_jump_by_parts_zero_rtx (mode
, op1
, if_false_label
, if_true_label
,
831 if (! if_false_label
)
832 drop_through_label
= if_false_label
= gen_label_rtx ();
834 for (i
= 0; i
< nwords
; i
++)
835 do_compare_rtx_and_jump (operand_subword_force (op0
, i
, mode
),
836 operand_subword_force (op1
, i
, mode
),
837 EQ
, 0, word_mode
, NULL_RTX
,
838 if_false_label
, NULL_RTX
, prob
);
841 emit_jump (if_true_label
);
842 if (drop_through_label
)
843 emit_label (drop_through_label
);
846 /* Given an EQ_EXPR expression EXP for values too wide to be compared
847 with one insn, test the comparison and jump to the appropriate label. */
850 do_jump_by_parts_equality (tree treeop0
, tree treeop1
, rtx if_false_label
,
851 rtx if_true_label
, int prob
)
853 rtx op0
= expand_normal (treeop0
);
854 rtx op1
= expand_normal (treeop1
);
855 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (treeop0
));
856 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_false_label
,
857 if_true_label
, prob
);
860 /* Split a comparison into two others, the second of which has the other
861 "orderedness". The first is always ORDERED or UNORDERED if MODE
862 does not honor NaNs (which means that it can be skipped in that case;
863 see do_compare_rtx_and_jump).
865 The two conditions are written in *CODE1 and *CODE2. Return true if
866 the conditions must be ANDed, false if they must be ORed. */
869 split_comparison (enum rtx_code code
, enum machine_mode mode
,
870 enum rtx_code
*code1
, enum rtx_code
*code2
)
919 /* Do not turn a trapping comparison into a non-trapping one. */
920 if (HONOR_SNANS (mode
))
938 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
939 The decision as to signed or unsigned comparison must be made by the caller.
941 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
945 do_compare_rtx_and_jump (rtx op0
, rtx op1
, enum rtx_code code
, int unsignedp
,
946 enum machine_mode mode
, rtx size
, rtx if_false_label
,
947 rtx if_true_label
, int prob
)
950 rtx dummy_label
= NULL
;
952 /* Reverse the comparison if that is safe and we want to jump if it is
953 false. Also convert to the reverse comparison if the target can
956 || ! can_compare_p (code
, mode
, ccp_jump
))
957 && (! FLOAT_MODE_P (mode
)
958 || code
== ORDERED
|| code
== UNORDERED
959 || (! HONOR_NANS (mode
) && (code
== LTGT
|| code
== UNEQ
))
960 || (! HONOR_SNANS (mode
) && (code
== EQ
|| code
== NE
))))
963 if (FLOAT_MODE_P (mode
))
964 rcode
= reverse_condition_maybe_unordered (code
);
966 rcode
= reverse_condition (code
);
968 /* Canonicalize to UNORDERED for the libcall. */
969 if (can_compare_p (rcode
, mode
, ccp_jump
)
970 || (code
== ORDERED
&& ! can_compare_p (ORDERED
, mode
, ccp_jump
)))
973 if_true_label
= if_false_label
;
974 if_false_label
= tem
;
980 /* If one operand is constant, make it the second one. Only do this
981 if the other operand is not constant as well. */
983 if (swap_commutative_operands_p (op0
, op1
))
988 code
= swap_condition (code
);
991 do_pending_stack_adjust ();
993 code
= unsignedp
? unsigned_condition (code
) : code
;
994 if (0 != (tem
= simplify_relational_operation (code
, mode
, VOIDmode
,
997 if (CONSTANT_P (tem
))
999 rtx label
= (tem
== const0_rtx
|| tem
== CONST0_RTX (mode
))
1000 ? if_false_label
: if_true_label
;
1006 code
= GET_CODE (tem
);
1007 mode
= GET_MODE (tem
);
1008 op0
= XEXP (tem
, 0);
1009 op1
= XEXP (tem
, 1);
1010 unsignedp
= (code
== GTU
|| code
== LTU
|| code
== GEU
|| code
== LEU
);
1013 if (! if_true_label
)
1014 dummy_label
= if_true_label
= gen_label_rtx ();
1016 if (GET_MODE_CLASS (mode
) == MODE_INT
1017 && ! can_compare_p (code
, mode
, ccp_jump
))
1022 do_jump_by_parts_greater_rtx (mode
, 1, op1
, op0
,
1023 if_false_label
, if_true_label
, prob
);
1027 do_jump_by_parts_greater_rtx (mode
, 1, op0
, op1
,
1028 if_true_label
, if_false_label
,
1033 do_jump_by_parts_greater_rtx (mode
, 1, op0
, op1
,
1034 if_false_label
, if_true_label
, prob
);
1038 do_jump_by_parts_greater_rtx (mode
, 1, op1
, op0
,
1039 if_true_label
, if_false_label
,
1044 do_jump_by_parts_greater_rtx (mode
, 0, op1
, op0
,
1045 if_false_label
, if_true_label
, prob
);
1049 do_jump_by_parts_greater_rtx (mode
, 0, op0
, op1
,
1050 if_true_label
, if_false_label
,
1055 do_jump_by_parts_greater_rtx (mode
, 0, op0
, op1
,
1056 if_false_label
, if_true_label
, prob
);
1060 do_jump_by_parts_greater_rtx (mode
, 0, op1
, op0
,
1061 if_true_label
, if_false_label
,
1066 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_false_label
,
1067 if_true_label
, prob
);
1071 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_true_label
,
1072 if_false_label
, inv (prob
));
1081 if (SCALAR_FLOAT_MODE_P (mode
)
1082 && ! can_compare_p (code
, mode
, ccp_jump
)
1083 && can_compare_p (swap_condition (code
), mode
, ccp_jump
))
1086 code
= swap_condition (code
);
1091 else if (SCALAR_FLOAT_MODE_P (mode
)
1092 && ! can_compare_p (code
, mode
, ccp_jump
)
1093 /* Never split ORDERED and UNORDERED.
1094 These must be implemented. */
1095 && (code
!= ORDERED
&& code
!= UNORDERED
)
1096 /* Split a floating-point comparison if
1097 we can jump on other conditions... */
1098 && (have_insn_for (COMPARE
, mode
)
1099 /* ... or if there is no libcall for it. */
1100 || code_to_optab (code
) == unknown_optab
))
1102 enum rtx_code first_code
;
1103 bool and_them
= split_comparison (code
, mode
, &first_code
, &code
);
1105 /* If there are no NaNs, the first comparison should always fall
1107 if (!HONOR_NANS (mode
))
1108 gcc_assert (first_code
== (and_them
? ORDERED
: UNORDERED
));
1112 int first_prob
= prob
;
1113 if (first_code
== UNORDERED
)
1114 first_prob
= REG_BR_PROB_BASE
/ 100;
1115 else if (first_code
== ORDERED
)
1116 first_prob
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 100;
1120 /* If we only jump if true, just bypass the second jump. */
1121 if (! if_false_label
)
1124 dummy_label
= gen_label_rtx ();
1125 dest_label
= dummy_label
;
1128 dest_label
= if_false_label
;
1129 do_compare_rtx_and_jump (op0
, op1
, first_code
, unsignedp
, mode
,
1130 size
, dest_label
, NULL_RTX
,
1134 do_compare_rtx_and_jump (op0
, op1
, first_code
, unsignedp
, mode
,
1135 size
, NULL_RTX
, if_true_label
,
1140 emit_cmp_and_jump_insns (op0
, op1
, code
, size
, mode
, unsignedp
,
1141 if_true_label
, prob
);
1145 emit_jump (if_false_label
);
1147 emit_label (dummy_label
);
1150 /* Generate code for a comparison expression EXP (including code to compute
1151 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1152 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1153 generated code will drop through.
1154 SIGNED_CODE should be the rtx operation for this comparison for
1155 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1157 We force a stack adjustment unless there are currently
1158 things pushed on the stack that aren't yet used. */
1161 do_compare_and_jump (tree treeop0
, tree treeop1
, enum rtx_code signed_code
,
1162 enum rtx_code unsigned_code
, rtx if_false_label
,
1163 rtx if_true_label
, int prob
)
1167 enum machine_mode mode
;
1171 /* Don't crash if the comparison was erroneous. */
1172 op0
= expand_normal (treeop0
);
1173 if (TREE_CODE (treeop0
) == ERROR_MARK
)
1176 op1
= expand_normal (treeop1
);
1177 if (TREE_CODE (treeop1
) == ERROR_MARK
)
1180 type
= TREE_TYPE (treeop0
);
1181 mode
= TYPE_MODE (type
);
1182 if (TREE_CODE (treeop0
) == INTEGER_CST
1183 && (TREE_CODE (treeop1
) != INTEGER_CST
1184 || (GET_MODE_BITSIZE (mode
)
1185 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1
))))))
1187 /* op0 might have been replaced by promoted constant, in which
1188 case the type of second argument should be used. */
1189 type
= TREE_TYPE (treeop1
);
1190 mode
= TYPE_MODE (type
);
1192 unsignedp
= TYPE_UNSIGNED (type
);
1193 code
= unsignedp
? unsigned_code
: signed_code
;
1195 #ifdef HAVE_canonicalize_funcptr_for_compare
1196 /* If function pointers need to be "canonicalized" before they can
1197 be reliably compared, then canonicalize them.
1198 Only do this if *both* sides of the comparison are function pointers.
1199 If one side isn't, we want a noncanonicalized comparison. See PR
1200 middle-end/17564. */
1201 if (HAVE_canonicalize_funcptr_for_compare
1202 && TREE_CODE (TREE_TYPE (treeop0
)) == POINTER_TYPE
1203 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0
)))
1205 && TREE_CODE (TREE_TYPE (treeop1
)) == POINTER_TYPE
1206 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1
)))
1209 rtx new_op0
= gen_reg_rtx (mode
);
1210 rtx new_op1
= gen_reg_rtx (mode
);
1212 emit_insn (gen_canonicalize_funcptr_for_compare (new_op0
, op0
));
1215 emit_insn (gen_canonicalize_funcptr_for_compare (new_op1
, op1
));
1220 do_compare_rtx_and_jump (op0
, op1
, code
, unsignedp
, mode
,
1222 ? expr_size (treeop0
) : NULL_RTX
),
1223 if_false_label
, if_true_label
, prob
);
1226 #include "gt-dojump.h"