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"
51 static bool prefer_and_bit_test (machine_mode
, int);
52 static void do_jump_by_parts_greater (tree
, tree
, int,
53 rtx_code_label
*, rtx_code_label
*, int);
54 static void do_jump_by_parts_equality (tree
, tree
, rtx_code_label
*,
55 rtx_code_label
*, int);
56 static void do_compare_and_jump (tree
, tree
, enum rtx_code
, enum rtx_code
,
57 rtx_code_label
*, rtx_code_label
*, int);
59 /* Invert probability if there is any. -1 stands for unknown. */
64 return prob
== -1 ? -1 : REG_BR_PROB_BASE
- prob
;
67 /* At the start of a function, record that we have no previously-pushed
68 arguments waiting to be popped. */
71 init_pending_stack_adjust (void)
73 pending_stack_adjust
= 0;
76 /* Discard any pending stack adjustment. This avoid relying on the
77 RTL optimizers to remove useless adjustments when we know the
78 stack pointer value is dead. */
80 discard_pending_stack_adjust (void)
82 stack_pointer_delta
-= pending_stack_adjust
;
83 pending_stack_adjust
= 0;
86 /* When exiting from function, if safe, clear out any pending stack adjust
87 so the adjustment won't get done.
89 Note, if the current function calls alloca, then it must have a
90 frame pointer regardless of the value of flag_omit_frame_pointer. */
93 clear_pending_stack_adjust (void)
96 && (! flag_omit_frame_pointer
|| cfun
->calls_alloca
)
98 discard_pending_stack_adjust ();
101 /* Pop any previously-pushed arguments that have not been popped yet. */
104 do_pending_stack_adjust (void)
106 if (inhibit_defer_pop
== 0)
108 if (pending_stack_adjust
!= 0)
109 adjust_stack (GEN_INT (pending_stack_adjust
));
110 pending_stack_adjust
= 0;
114 /* Remember pending_stack_adjust/stack_pointer_delta.
115 To be used around code that may call do_pending_stack_adjust (),
116 but the generated code could be discarded e.g. using delete_insns_since. */
119 save_pending_stack_adjust (saved_pending_stack_adjust
*save
)
121 save
->x_pending_stack_adjust
= pending_stack_adjust
;
122 save
->x_stack_pointer_delta
= stack_pointer_delta
;
125 /* Restore the saved pending_stack_adjust/stack_pointer_delta. */
128 restore_pending_stack_adjust (saved_pending_stack_adjust
*save
)
130 if (inhibit_defer_pop
== 0)
132 pending_stack_adjust
= save
->x_pending_stack_adjust
;
133 stack_pointer_delta
= save
->x_stack_pointer_delta
;
137 /* Expand conditional expressions. */
139 /* Generate code to evaluate EXP and jump to LABEL if the value is zero. */
142 jumpifnot (tree exp
, rtx_code_label
*label
, int prob
)
144 do_jump (exp
, label
, NULL
, inv (prob
));
148 jumpifnot_1 (enum tree_code code
, tree op0
, tree op1
, rtx_code_label
*label
,
151 do_jump_1 (code
, op0
, op1
, label
, NULL
, inv (prob
));
154 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
157 jumpif (tree exp
, rtx_code_label
*label
, int prob
)
159 do_jump (exp
, NULL
, label
, prob
);
163 jumpif_1 (enum tree_code code
, tree op0
, tree op1
,
164 rtx_code_label
*label
, int prob
)
166 do_jump_1 (code
, op0
, op1
, NULL
, label
, prob
);
169 /* Used internally by prefer_and_bit_test. */
171 static GTY(()) rtx and_reg
;
172 static GTY(()) rtx and_test
;
173 static GTY(()) rtx shift_test
;
175 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
176 where X is an arbitrary register of mode MODE. Return true if the former
180 prefer_and_bit_test (machine_mode mode
, int bitnum
)
183 wide_int mask
= wi::set_bit_in_zero (bitnum
, GET_MODE_PRECISION (mode
));
187 /* Set up rtxes for the two variations. Use NULL as a placeholder
188 for the BITNUM-based constants. */
189 and_reg
= gen_rtx_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
190 and_test
= gen_rtx_AND (mode
, and_reg
, NULL
);
191 shift_test
= gen_rtx_AND (mode
, gen_rtx_ASHIFTRT (mode
, and_reg
, NULL
),
196 /* Change the mode of the previously-created rtxes. */
197 PUT_MODE (and_reg
, mode
);
198 PUT_MODE (and_test
, mode
);
199 PUT_MODE (shift_test
, mode
);
200 PUT_MODE (XEXP (shift_test
, 0), mode
);
203 /* Fill in the integers. */
204 XEXP (and_test
, 1) = immed_wide_int_const (mask
, mode
);
205 XEXP (XEXP (shift_test
, 0), 1) = GEN_INT (bitnum
);
207 speed_p
= optimize_insn_for_speed_p ();
208 return (rtx_cost (and_test
, IF_THEN_ELSE
, 0, speed_p
)
209 <= rtx_cost (shift_test
, IF_THEN_ELSE
, 0, speed_p
));
212 /* Subroutine of do_jump, dealing with exploded comparisons of the type
213 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
214 PROB is probability of jump to if_true_label, or -1 if unknown. */
217 do_jump_1 (enum tree_code code
, tree op0
, tree op1
,
218 rtx_code_label
*if_false_label
, rtx_code_label
*if_true_label
,
222 rtx_code_label
*drop_through_label
= 0;
228 tree inner_type
= TREE_TYPE (op0
);
230 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
231 != MODE_COMPLEX_FLOAT
);
232 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
233 != MODE_COMPLEX_INT
);
235 if (integer_zerop (op1
))
236 do_jump (op0
, if_true_label
, if_false_label
, inv (prob
));
237 else if (GET_MODE_CLASS (TYPE_MODE (inner_type
)) == MODE_INT
238 && !can_compare_p (EQ
, TYPE_MODE (inner_type
), ccp_jump
))
239 do_jump_by_parts_equality (op0
, op1
, if_false_label
, if_true_label
,
242 do_compare_and_jump (op0
, op1
, EQ
, EQ
, if_false_label
, if_true_label
,
249 tree inner_type
= TREE_TYPE (op0
);
251 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
252 != MODE_COMPLEX_FLOAT
);
253 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type
))
254 != MODE_COMPLEX_INT
);
256 if (integer_zerop (op1
))
257 do_jump (op0
, if_false_label
, if_true_label
, prob
);
258 else if (GET_MODE_CLASS (TYPE_MODE (inner_type
)) == MODE_INT
259 && !can_compare_p (NE
, TYPE_MODE (inner_type
), ccp_jump
))
260 do_jump_by_parts_equality (op0
, op1
, if_true_label
, if_false_label
,
263 do_compare_and_jump (op0
, op1
, NE
, NE
, if_false_label
, if_true_label
,
269 mode
= TYPE_MODE (TREE_TYPE (op0
));
270 if (GET_MODE_CLASS (mode
) == MODE_INT
271 && ! can_compare_p (LT
, mode
, ccp_jump
))
272 do_jump_by_parts_greater (op0
, op1
, 1, if_false_label
, if_true_label
,
275 do_compare_and_jump (op0
, op1
, LT
, LTU
, if_false_label
, if_true_label
,
280 mode
= TYPE_MODE (TREE_TYPE (op0
));
281 if (GET_MODE_CLASS (mode
) == MODE_INT
282 && ! can_compare_p (LE
, mode
, ccp_jump
))
283 do_jump_by_parts_greater (op0
, op1
, 0, if_true_label
, if_false_label
,
286 do_compare_and_jump (op0
, op1
, LE
, LEU
, if_false_label
, if_true_label
,
291 mode
= TYPE_MODE (TREE_TYPE (op0
));
292 if (GET_MODE_CLASS (mode
) == MODE_INT
293 && ! can_compare_p (GT
, mode
, ccp_jump
))
294 do_jump_by_parts_greater (op0
, op1
, 0, if_false_label
, if_true_label
,
297 do_compare_and_jump (op0
, op1
, GT
, GTU
, if_false_label
, if_true_label
,
302 mode
= TYPE_MODE (TREE_TYPE (op0
));
303 if (GET_MODE_CLASS (mode
) == MODE_INT
304 && ! can_compare_p (GE
, mode
, ccp_jump
))
305 do_jump_by_parts_greater (op0
, op1
, 1, if_true_label
, if_false_label
,
308 do_compare_and_jump (op0
, op1
, GE
, GEU
, if_false_label
, if_true_label
,
313 do_compare_and_jump (op0
, op1
, ORDERED
, ORDERED
,
314 if_false_label
, if_true_label
, prob
);
318 do_compare_and_jump (op0
, op1
, UNORDERED
, UNORDERED
,
319 if_false_label
, if_true_label
, prob
);
323 do_compare_and_jump (op0
, op1
, UNLT
, UNLT
, if_false_label
, if_true_label
,
328 do_compare_and_jump (op0
, op1
, UNLE
, UNLE
, if_false_label
, if_true_label
,
333 do_compare_and_jump (op0
, op1
, UNGT
, UNGT
, if_false_label
, if_true_label
,
338 do_compare_and_jump (op0
, op1
, UNGE
, UNGE
, if_false_label
, if_true_label
,
343 do_compare_and_jump (op0
, op1
, UNEQ
, UNEQ
, if_false_label
, if_true_label
,
348 do_compare_and_jump (op0
, op1
, LTGT
, LTGT
, if_false_label
, if_true_label
,
352 case TRUTH_ANDIF_EXPR
:
354 /* Spread the probability that the expression is false evenly between
355 the two conditions. So the first condition is false half the total
356 probability of being false. The second condition is false the other
357 half of the total probability of being false, so its jump has a false
358 probability of half the total, relative to the probability we
359 reached it (i.e. the first condition was true). */
364 int false_prob
= inv (prob
);
365 int op0_false_prob
= false_prob
/ 2;
366 int op1_false_prob
= GCOV_COMPUTE_SCALE ((false_prob
/ 2),
367 inv (op0_false_prob
));
368 /* Get the probability that each jump below is true. */
369 op0_prob
= inv (op0_false_prob
);
370 op1_prob
= inv (op1_false_prob
);
372 if (if_false_label
== NULL
)
374 drop_through_label
= gen_label_rtx ();
375 do_jump (op0
, drop_through_label
, NULL
, op0_prob
);
376 do_jump (op1
, NULL
, if_true_label
, op1_prob
);
380 do_jump (op0
, if_false_label
, NULL
, op0_prob
);
381 do_jump (op1
, if_false_label
, if_true_label
, op1_prob
);
386 case TRUTH_ORIF_EXPR
:
388 /* Spread the probability evenly between the two conditions. So
389 the first condition has half the total probability of being true.
390 The second condition has the other half of the total probability,
391 so its jump has a probability of half the total, relative to
392 the probability we reached it (i.e. the first condition was false). */
398 op1_prob
= GCOV_COMPUTE_SCALE ((prob
/ 2), inv (op0_prob
));
400 if (if_true_label
== NULL
)
402 drop_through_label
= gen_label_rtx ();
403 do_jump (op0
, NULL
, drop_through_label
, op0_prob
);
404 do_jump (op1
, if_false_label
, NULL
, op1_prob
);
408 do_jump (op0
, NULL
, if_true_label
, op0_prob
);
409 do_jump (op1
, if_false_label
, if_true_label
, op1_prob
);
418 if (drop_through_label
)
420 do_pending_stack_adjust ();
421 emit_label (drop_through_label
);
425 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
426 the result is zero, or IF_TRUE_LABEL if the result is one.
427 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
428 meaning fall through in that case.
430 do_jump always does any pending stack adjust except when it does not
431 actually perform a jump. An example where there is no jump
432 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
434 PROB is probability of jump to if_true_label, or -1 if unknown. */
437 do_jump (tree exp
, rtx_code_label
*if_false_label
,
438 rtx_code_label
*if_true_label
, int prob
)
440 enum tree_code code
= TREE_CODE (exp
);
445 rtx_code_label
*drop_through_label
= NULL
;
454 rtx_code_label
*lab
= integer_zerop (exp
) ? if_false_label
462 /* This is not true with #pragma weak */
464 /* The address of something can never be zero. */
466 emit_jump (if_true_label
);
471 if (TREE_CODE (TREE_OPERAND (exp
, 0)) == COMPONENT_REF
472 || TREE_CODE (TREE_OPERAND (exp
, 0)) == BIT_FIELD_REF
473 || TREE_CODE (TREE_OPERAND (exp
, 0)) == ARRAY_REF
474 || TREE_CODE (TREE_OPERAND (exp
, 0)) == ARRAY_RANGE_REF
)
477 /* If we are narrowing the operand, we have to do the compare in the
479 if ((TYPE_PRECISION (TREE_TYPE (exp
))
480 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0)))))
482 case NON_LVALUE_EXPR
:
487 /* These cannot change zero->nonzero or vice versa. */
488 do_jump (TREE_OPERAND (exp
, 0), if_false_label
, if_true_label
, prob
);
492 do_jump (TREE_OPERAND (exp
, 0), if_true_label
, if_false_label
,
498 rtx_code_label
*label1
= gen_label_rtx ();
499 if (!if_true_label
|| !if_false_label
)
501 drop_through_label
= gen_label_rtx ();
503 if_true_label
= drop_through_label
;
505 if_false_label
= drop_through_label
;
508 do_pending_stack_adjust ();
509 do_jump (TREE_OPERAND (exp
, 0), label1
, NULL
, -1);
510 do_jump (TREE_OPERAND (exp
, 1), if_false_label
, if_true_label
, prob
);
512 do_jump (TREE_OPERAND (exp
, 2), if_false_label
, if_true_label
, prob
);
517 /* Lowered by gimplify.c. */
521 /* Nonzero iff operands of minus differ. */
539 case TRUTH_ANDIF_EXPR
:
540 case TRUTH_ORIF_EXPR
:
542 do_jump_1 (code
, TREE_OPERAND (exp
, 0), TREE_OPERAND (exp
, 1),
543 if_false_label
, if_true_label
, prob
);
547 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
548 See if the former is preferred for jump tests and restore it
550 if (integer_onep (TREE_OPERAND (exp
, 1)))
552 tree exp0
= TREE_OPERAND (exp
, 0);
553 rtx_code_label
*set_label
, *clr_label
;
554 int setclr_prob
= prob
;
556 /* Strip narrowing integral type conversions. */
557 while (CONVERT_EXPR_P (exp0
)
558 && TREE_OPERAND (exp0
, 0) != error_mark_node
559 && TYPE_PRECISION (TREE_TYPE (exp0
))
560 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0
, 0))))
561 exp0
= TREE_OPERAND (exp0
, 0);
563 /* "exp0 ^ 1" inverts the sense of the single bit test. */
564 if (TREE_CODE (exp0
) == BIT_XOR_EXPR
565 && integer_onep (TREE_OPERAND (exp0
, 1)))
567 exp0
= TREE_OPERAND (exp0
, 0);
568 clr_label
= if_true_label
;
569 set_label
= if_false_label
;
570 setclr_prob
= inv (prob
);
574 clr_label
= if_false_label
;
575 set_label
= if_true_label
;
578 if (TREE_CODE (exp0
) == RSHIFT_EXPR
)
580 tree arg
= TREE_OPERAND (exp0
, 0);
581 tree shift
= TREE_OPERAND (exp0
, 1);
582 tree argtype
= TREE_TYPE (arg
);
583 if (TREE_CODE (shift
) == INTEGER_CST
584 && compare_tree_int (shift
, 0) >= 0
585 && compare_tree_int (shift
, HOST_BITS_PER_WIDE_INT
) < 0
586 && prefer_and_bit_test (TYPE_MODE (argtype
),
587 TREE_INT_CST_LOW (shift
)))
589 unsigned HOST_WIDE_INT mask
590 = (unsigned HOST_WIDE_INT
) 1 << TREE_INT_CST_LOW (shift
);
591 do_jump (build2 (BIT_AND_EXPR
, argtype
, arg
,
592 build_int_cstu (argtype
, mask
)),
593 clr_label
, set_label
, setclr_prob
);
599 /* If we are AND'ing with a small constant, do this comparison in the
600 smallest type that fits. If the machine doesn't have comparisons
601 that small, it will be converted back to the wider comparison.
602 This helps if we are testing the sign bit of a narrower object.
603 combine can't do this for us because it can't know whether a
604 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
606 if (! SLOW_BYTE_ACCESS
607 && TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
608 && TYPE_PRECISION (TREE_TYPE (exp
)) <= HOST_BITS_PER_WIDE_INT
609 && (i
= tree_floor_log2 (TREE_OPERAND (exp
, 1))) >= 0
610 && (mode
= mode_for_size (i
+ 1, MODE_INT
, 0)) != BLKmode
611 && (type
= lang_hooks
.types
.type_for_mode (mode
, 1)) != 0
612 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (exp
))
613 && have_insn_for (COMPARE
, TYPE_MODE (type
)))
615 do_jump (fold_convert (type
, exp
), if_false_label
, if_true_label
,
620 if (TYPE_PRECISION (TREE_TYPE (exp
)) > 1
621 || TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
624 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
627 /* High branch cost, expand as the bitwise AND of the conditions.
628 Do the same if the RHS has side effects, because we're effectively
629 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
630 if (BRANCH_COST (optimize_insn_for_speed_p (),
632 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp
, 1)))
634 code
= TRUTH_ANDIF_EXPR
;
639 /* High branch cost, expand as the bitwise OR of the conditions.
640 Do the same if the RHS has side effects, because we're effectively
641 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
642 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
643 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp
, 1)))
645 code
= TRUTH_ORIF_EXPR
;
648 /* Fall through and generate the normal code. */
651 temp
= expand_normal (exp
);
652 do_pending_stack_adjust ();
653 /* The RTL optimizers prefer comparisons against pseudos. */
654 if (GET_CODE (temp
) == SUBREG
)
656 /* Compare promoted variables in their promoted mode. */
657 if (SUBREG_PROMOTED_VAR_P (temp
)
658 && REG_P (XEXP (temp
, 0)))
659 temp
= XEXP (temp
, 0);
661 temp
= copy_to_reg (temp
);
663 do_compare_rtx_and_jump (temp
, CONST0_RTX (GET_MODE (temp
)),
664 NE
, TYPE_UNSIGNED (TREE_TYPE (exp
)),
665 GET_MODE (temp
), NULL_RTX
,
666 if_false_label
, if_true_label
, prob
);
669 if (drop_through_label
)
671 do_pending_stack_adjust ();
672 emit_label (drop_through_label
);
676 /* Compare OP0 with OP1, word at a time, in mode MODE.
677 UNSIGNEDP says to do unsigned comparison.
678 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
681 do_jump_by_parts_greater_rtx (machine_mode mode
, int unsignedp
, rtx op0
,
682 rtx op1
, rtx_code_label
*if_false_label
,
683 rtx_code_label
*if_true_label
,
686 int nwords
= (GET_MODE_SIZE (mode
) / UNITS_PER_WORD
);
687 rtx_code_label
*drop_through_label
= 0;
688 bool drop_through_if_true
= false, drop_through_if_false
= false;
689 enum rtx_code code
= GT
;
692 if (! if_true_label
|| ! if_false_label
)
693 drop_through_label
= gen_label_rtx ();
696 if_true_label
= drop_through_label
;
697 drop_through_if_true
= true;
699 if (! if_false_label
)
701 if_false_label
= drop_through_label
;
702 drop_through_if_false
= true;
705 /* Deal with the special case 0 > x: only one comparison is necessary and
706 we reverse it to avoid jumping to the drop-through label. */
707 if (op0
== const0_rtx
&& drop_through_if_true
&& !drop_through_if_false
)
710 if_true_label
= if_false_label
;
711 if_false_label
= drop_through_label
;
712 drop_through_if_true
= false;
713 drop_through_if_false
= true;
716 /* Compare a word at a time, high order first. */
717 for (i
= 0; i
< nwords
; i
++)
719 rtx op0_word
, op1_word
;
721 if (WORDS_BIG_ENDIAN
)
723 op0_word
= operand_subword_force (op0
, i
, mode
);
724 op1_word
= operand_subword_force (op1
, i
, mode
);
728 op0_word
= operand_subword_force (op0
, nwords
- 1 - i
, mode
);
729 op1_word
= operand_subword_force (op1
, nwords
- 1 - i
, mode
);
732 /* All but high-order word must be compared as unsigned. */
733 do_compare_rtx_and_jump (op0_word
, op1_word
, code
, (unsignedp
|| i
> 0),
734 word_mode
, NULL_RTX
, NULL
, if_true_label
,
737 /* Emit only one comparison for 0. Do not emit the last cond jump. */
738 if (op0
== const0_rtx
|| i
== nwords
- 1)
741 /* Consider lower words only if these are equal. */
742 do_compare_rtx_and_jump (op0_word
, op1_word
, NE
, unsignedp
, word_mode
,
743 NULL_RTX
, NULL
, if_false_label
, inv (prob
));
746 if (!drop_through_if_false
)
747 emit_jump (if_false_label
);
748 if (drop_through_label
)
749 emit_label (drop_through_label
);
752 /* Given a comparison expression EXP for values too wide to be compared
753 with one insn, test the comparison and jump to the appropriate label.
754 The code of EXP is ignored; we always test GT if SWAP is 0,
755 and LT if SWAP is 1. */
758 do_jump_by_parts_greater (tree treeop0
, tree treeop1
, int swap
,
759 rtx_code_label
*if_false_label
,
760 rtx_code_label
*if_true_label
, int prob
)
762 rtx op0
= expand_normal (swap
? treeop1
: treeop0
);
763 rtx op1
= expand_normal (swap
? treeop0
: treeop1
);
764 machine_mode mode
= TYPE_MODE (TREE_TYPE (treeop0
));
765 int unsignedp
= TYPE_UNSIGNED (TREE_TYPE (treeop0
));
767 do_jump_by_parts_greater_rtx (mode
, unsignedp
, op0
, op1
, if_false_label
,
768 if_true_label
, prob
);
771 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
772 mode, MODE, that is too wide for the available compare insns. Either
773 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL
774 to indicate drop through. */
777 do_jump_by_parts_zero_rtx (machine_mode mode
, rtx op0
,
778 rtx_code_label
*if_false_label
,
779 rtx_code_label
*if_true_label
, int prob
)
781 int nwords
= GET_MODE_SIZE (mode
) / UNITS_PER_WORD
;
784 rtx_code_label
*drop_through_label
= NULL
;
786 /* The fastest way of doing this comparison on almost any machine is to
787 "or" all the words and compare the result. If all have to be loaded
788 from memory and this is a very wide item, it's possible this may
789 be slower, but that's highly unlikely. */
791 part
= gen_reg_rtx (word_mode
);
792 emit_move_insn (part
, operand_subword_force (op0
, 0, mode
));
793 for (i
= 1; i
< nwords
&& part
!= 0; i
++)
794 part
= expand_binop (word_mode
, ior_optab
, part
,
795 operand_subword_force (op0
, i
, mode
),
796 part
, 1, OPTAB_WIDEN
);
800 do_compare_rtx_and_jump (part
, const0_rtx
, EQ
, 1, word_mode
,
801 NULL_RTX
, if_false_label
, if_true_label
, prob
);
805 /* If we couldn't do the "or" simply, do this with a series of compares. */
806 if (! if_false_label
)
807 if_false_label
= drop_through_label
= gen_label_rtx ();
809 for (i
= 0; i
< nwords
; i
++)
810 do_compare_rtx_and_jump (operand_subword_force (op0
, i
, mode
),
811 const0_rtx
, EQ
, 1, word_mode
, NULL_RTX
,
812 if_false_label
, NULL
, prob
);
815 emit_jump (if_true_label
);
817 if (drop_through_label
)
818 emit_label (drop_through_label
);
821 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
822 where MODE is an integer mode too wide to be compared with one insn.
823 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
824 to indicate drop through. */
827 do_jump_by_parts_equality_rtx (machine_mode mode
, rtx op0
, rtx op1
,
828 rtx_code_label
*if_false_label
,
829 rtx_code_label
*if_true_label
, int prob
)
831 int nwords
= (GET_MODE_SIZE (mode
) / UNITS_PER_WORD
);
832 rtx_code_label
*drop_through_label
= NULL
;
835 if (op1
== const0_rtx
)
837 do_jump_by_parts_zero_rtx (mode
, op0
, if_false_label
, if_true_label
,
841 else if (op0
== const0_rtx
)
843 do_jump_by_parts_zero_rtx (mode
, op1
, if_false_label
, if_true_label
,
848 if (! if_false_label
)
849 drop_through_label
= if_false_label
= gen_label_rtx ();
851 for (i
= 0; i
< nwords
; i
++)
852 do_compare_rtx_and_jump (operand_subword_force (op0
, i
, mode
),
853 operand_subword_force (op1
, i
, mode
),
854 EQ
, 0, word_mode
, NULL_RTX
,
855 if_false_label
, NULL
, prob
);
858 emit_jump (if_true_label
);
859 if (drop_through_label
)
860 emit_label (drop_through_label
);
863 /* Given an EQ_EXPR expression EXP for values too wide to be compared
864 with one insn, test the comparison and jump to the appropriate label. */
867 do_jump_by_parts_equality (tree treeop0
, tree treeop1
,
868 rtx_code_label
*if_false_label
,
869 rtx_code_label
*if_true_label
, int prob
)
871 rtx op0
= expand_normal (treeop0
);
872 rtx op1
= expand_normal (treeop1
);
873 machine_mode mode
= TYPE_MODE (TREE_TYPE (treeop0
));
874 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_false_label
,
875 if_true_label
, prob
);
878 /* Split a comparison into two others, the second of which has the other
879 "orderedness". The first is always ORDERED or UNORDERED if MODE
880 does not honor NaNs (which means that it can be skipped in that case;
881 see do_compare_rtx_and_jump).
883 The two conditions are written in *CODE1 and *CODE2. Return true if
884 the conditions must be ANDed, false if they must be ORed. */
887 split_comparison (enum rtx_code code
, machine_mode mode
,
888 enum rtx_code
*code1
, enum rtx_code
*code2
)
937 /* Do not turn a trapping comparison into a non-trapping one. */
938 if (HONOR_SNANS (mode
))
956 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
957 The decision as to signed or unsigned comparison must be made by the caller.
959 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
963 do_compare_rtx_and_jump (rtx op0
, rtx op1
, enum rtx_code code
, int unsignedp
,
964 machine_mode mode
, rtx size
,
965 rtx_code_label
*if_false_label
,
966 rtx_code_label
*if_true_label
, int prob
)
969 rtx_code_label
*dummy_label
= NULL
;
971 /* Reverse the comparison if that is safe and we want to jump if it is
972 false. Also convert to the reverse comparison if the target can
975 || ! can_compare_p (code
, mode
, ccp_jump
))
976 && (! FLOAT_MODE_P (mode
)
977 || code
== ORDERED
|| code
== UNORDERED
978 || (! HONOR_NANS (mode
) && (code
== LTGT
|| code
== UNEQ
))
979 || (! HONOR_SNANS (mode
) && (code
== EQ
|| code
== NE
))))
982 if (FLOAT_MODE_P (mode
))
983 rcode
= reverse_condition_maybe_unordered (code
);
985 rcode
= reverse_condition (code
);
987 /* Canonicalize to UNORDERED for the libcall. */
988 if (can_compare_p (rcode
, mode
, ccp_jump
)
989 || (code
== ORDERED
&& ! can_compare_p (ORDERED
, mode
, ccp_jump
)))
991 std::swap (if_true_label
, if_false_label
);
997 /* If one operand is constant, make it the second one. Only do this
998 if the other operand is not constant as well. */
1000 if (swap_commutative_operands_p (op0
, op1
))
1002 std::swap (op0
, op1
);
1003 code
= swap_condition (code
);
1006 do_pending_stack_adjust ();
1008 code
= unsignedp
? unsigned_condition (code
) : code
;
1009 if (0 != (tem
= simplify_relational_operation (code
, mode
, VOIDmode
,
1012 if (CONSTANT_P (tem
))
1014 rtx_code_label
*label
= (tem
== const0_rtx
1015 || tem
== CONST0_RTX (mode
))
1016 ? if_false_label
: if_true_label
;
1022 code
= GET_CODE (tem
);
1023 mode
= GET_MODE (tem
);
1024 op0
= XEXP (tem
, 0);
1025 op1
= XEXP (tem
, 1);
1026 unsignedp
= (code
== GTU
|| code
== LTU
|| code
== GEU
|| code
== LEU
);
1029 if (! if_true_label
)
1030 dummy_label
= if_true_label
= gen_label_rtx ();
1032 if (GET_MODE_CLASS (mode
) == MODE_INT
1033 && ! can_compare_p (code
, mode
, ccp_jump
))
1038 do_jump_by_parts_greater_rtx (mode
, 1, op1
, op0
,
1039 if_false_label
, if_true_label
, prob
);
1043 do_jump_by_parts_greater_rtx (mode
, 1, op0
, op1
,
1044 if_true_label
, if_false_label
,
1049 do_jump_by_parts_greater_rtx (mode
, 1, op0
, op1
,
1050 if_false_label
, if_true_label
, prob
);
1054 do_jump_by_parts_greater_rtx (mode
, 1, op1
, op0
,
1055 if_true_label
, if_false_label
,
1060 do_jump_by_parts_greater_rtx (mode
, 0, op1
, op0
,
1061 if_false_label
, if_true_label
, prob
);
1065 do_jump_by_parts_greater_rtx (mode
, 0, op0
, op1
,
1066 if_true_label
, if_false_label
,
1071 do_jump_by_parts_greater_rtx (mode
, 0, op0
, op1
,
1072 if_false_label
, if_true_label
, prob
);
1076 do_jump_by_parts_greater_rtx (mode
, 0, op1
, op0
,
1077 if_true_label
, if_false_label
,
1082 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_false_label
,
1083 if_true_label
, prob
);
1087 do_jump_by_parts_equality_rtx (mode
, op0
, op1
, if_true_label
,
1088 if_false_label
, inv (prob
));
1097 if (SCALAR_FLOAT_MODE_P (mode
)
1098 && ! can_compare_p (code
, mode
, ccp_jump
)
1099 && can_compare_p (swap_condition (code
), mode
, ccp_jump
))
1101 code
= swap_condition (code
);
1102 std::swap (op0
, op1
);
1104 else if (SCALAR_FLOAT_MODE_P (mode
)
1105 && ! can_compare_p (code
, mode
, ccp_jump
)
1106 /* Never split ORDERED and UNORDERED.
1107 These must be implemented. */
1108 && (code
!= ORDERED
&& code
!= UNORDERED
)
1109 /* Split a floating-point comparison if
1110 we can jump on other conditions... */
1111 && (have_insn_for (COMPARE
, mode
)
1112 /* ... or if there is no libcall for it. */
1113 || code_to_optab (code
) == unknown_optab
))
1115 enum rtx_code first_code
;
1116 bool and_them
= split_comparison (code
, mode
, &first_code
, &code
);
1118 /* If there are no NaNs, the first comparison should always fall
1120 if (!HONOR_NANS (mode
))
1121 gcc_assert (first_code
== (and_them
? ORDERED
: UNORDERED
));
1125 int first_prob
= prob
;
1126 if (first_code
== UNORDERED
)
1127 first_prob
= REG_BR_PROB_BASE
/ 100;
1128 else if (first_code
== ORDERED
)
1129 first_prob
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 100;
1132 rtx_code_label
*dest_label
;
1133 /* If we only jump if true, just bypass the second jump. */
1134 if (! if_false_label
)
1137 dummy_label
= gen_label_rtx ();
1138 dest_label
= dummy_label
;
1141 dest_label
= if_false_label
;
1142 do_compare_rtx_and_jump (op0
, op1
, first_code
, unsignedp
, mode
,
1143 size
, dest_label
, NULL
, first_prob
);
1146 do_compare_rtx_and_jump (op0
, op1
, first_code
, unsignedp
, mode
,
1147 size
, NULL
, if_true_label
, first_prob
);
1151 emit_cmp_and_jump_insns (op0
, op1
, code
, size
, mode
, unsignedp
,
1152 if_true_label
, prob
);
1156 emit_jump (if_false_label
);
1158 emit_label (dummy_label
);
1161 /* Generate code for a comparison expression EXP (including code to compute
1162 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1163 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1164 generated code will drop through.
1165 SIGNED_CODE should be the rtx operation for this comparison for
1166 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1168 We force a stack adjustment unless there are currently
1169 things pushed on the stack that aren't yet used. */
1172 do_compare_and_jump (tree treeop0
, tree treeop1
, enum rtx_code signed_code
,
1173 enum rtx_code unsigned_code
,
1174 rtx_code_label
*if_false_label
,
1175 rtx_code_label
*if_true_label
, int prob
)
1183 /* Don't crash if the comparison was erroneous. */
1184 op0
= expand_normal (treeop0
);
1185 if (TREE_CODE (treeop0
) == ERROR_MARK
)
1188 op1
= expand_normal (treeop1
);
1189 if (TREE_CODE (treeop1
) == ERROR_MARK
)
1192 type
= TREE_TYPE (treeop0
);
1193 mode
= TYPE_MODE (type
);
1194 if (TREE_CODE (treeop0
) == INTEGER_CST
1195 && (TREE_CODE (treeop1
) != INTEGER_CST
1196 || (GET_MODE_BITSIZE (mode
)
1197 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1
))))))
1199 /* op0 might have been replaced by promoted constant, in which
1200 case the type of second argument should be used. */
1201 type
= TREE_TYPE (treeop1
);
1202 mode
= TYPE_MODE (type
);
1204 unsignedp
= TYPE_UNSIGNED (type
);
1205 code
= unsignedp
? unsigned_code
: signed_code
;
1207 #ifdef HAVE_canonicalize_funcptr_for_compare
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 (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 (gen_canonicalize_funcptr_for_compare (new_op0
, op0
));
1227 emit_insn (gen_canonicalize_funcptr_for_compare (new_op1
, op1
));
1232 do_compare_rtx_and_jump (op0
, op1
, code
, unsignedp
, mode
,
1234 ? expr_size (treeop0
) : NULL_RTX
),
1235 if_false_label
, if_true_label
, prob
);
1238 #include "gt-dojump.h"