Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / dojump.c
blob561d1b28998e348788f494424ea2c9508acb554e
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "insn-config.h"
29 #include "insn-attr.h"
30 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
31 #include "expr.h"
32 #include "optabs.h"
33 #include "langhooks.h"
34 #include "ggc.h"
35 #include "basic-block.h"
36 #include "tm_p.h"
38 static bool prefer_and_bit_test (enum machine_mode, int);
39 static void do_jump_by_parts_greater (tree, tree, int, rtx, rtx, int);
40 static void do_jump_by_parts_equality (tree, tree, rtx, rtx, int);
41 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code, rtx,
42 rtx, int);
44 /* Invert probability if there is any. -1 stands for unknown. */
46 static inline int
47 inv (int prob)
49 return prob == -1 ? -1 : REG_BR_PROB_BASE - prob;
52 /* At the start of a function, record that we have no previously-pushed
53 arguments waiting to be popped. */
55 void
56 init_pending_stack_adjust (void)
58 pending_stack_adjust = 0;
61 /* Discard any pending stack adjustment. This avoid relying on the
62 RTL optimizers to remove useless adjustments when we know the
63 stack pointer value is dead. */
64 void
65 discard_pending_stack_adjust (void)
67 stack_pointer_delta -= pending_stack_adjust;
68 pending_stack_adjust = 0;
71 /* When exiting from function, if safe, clear out any pending stack adjust
72 so the adjustment won't get done.
74 Note, if the current function calls alloca, then it must have a
75 frame pointer regardless of the value of flag_omit_frame_pointer. */
77 void
78 clear_pending_stack_adjust (void)
80 if (optimize > 0
81 && (! flag_omit_frame_pointer || cfun->calls_alloca)
82 && EXIT_IGNORE_STACK)
83 discard_pending_stack_adjust ();
86 /* Pop any previously-pushed arguments that have not been popped yet. */
88 void
89 do_pending_stack_adjust (void)
91 if (inhibit_defer_pop == 0)
93 if (pending_stack_adjust != 0)
94 adjust_stack (GEN_INT (pending_stack_adjust));
95 pending_stack_adjust = 0;
99 /* Expand conditional expressions. */
101 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.
102 LABEL is an rtx of code CODE_LABEL, in this function and all the
103 functions here. */
105 void
106 jumpifnot (tree exp, rtx label, int prob)
108 do_jump (exp, label, NULL_RTX, inv (prob));
111 void
112 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx label, int prob)
114 do_jump_1 (code, op0, op1, label, NULL_RTX, inv (prob));
117 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
119 void
120 jumpif (tree exp, rtx label, int prob)
122 do_jump (exp, NULL_RTX, label, prob);
125 void
126 jumpif_1 (enum tree_code code, tree op0, tree op1, rtx label, int prob)
128 do_jump_1 (code, op0, op1, NULL_RTX, label, prob);
131 /* Used internally by prefer_and_bit_test. */
133 static GTY(()) rtx and_reg;
134 static GTY(()) rtx and_test;
135 static GTY(()) rtx shift_test;
137 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
138 where X is an arbitrary register of mode MODE. Return true if the former
139 is preferred. */
141 static bool
142 prefer_and_bit_test (enum machine_mode mode, int bitnum)
144 bool speed_p;
146 if (and_test == 0)
148 /* Set up rtxes for the two variations. Use NULL as a placeholder
149 for the BITNUM-based constants. */
150 and_reg = gen_rtx_REG (mode, FIRST_PSEUDO_REGISTER);
151 and_test = gen_rtx_AND (mode, and_reg, NULL);
152 shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
153 const1_rtx);
155 else
157 /* Change the mode of the previously-created rtxes. */
158 PUT_MODE (and_reg, mode);
159 PUT_MODE (and_test, mode);
160 PUT_MODE (shift_test, mode);
161 PUT_MODE (XEXP (shift_test, 0), mode);
164 /* Fill in the integers. */
165 XEXP (and_test, 1)
166 = immed_double_int_const (double_int_zero.set_bit (bitnum), mode);
167 XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
169 speed_p = optimize_insn_for_speed_p ();
170 return (rtx_cost (and_test, IF_THEN_ELSE, 0, speed_p)
171 <= rtx_cost (shift_test, IF_THEN_ELSE, 0, speed_p));
174 /* Subroutine of do_jump, dealing with exploded comparisons of the type
175 OP0 CODE OP1 . IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
176 PROB is probability of jump to if_true_label, or -1 if unknown. */
178 void
179 do_jump_1 (enum tree_code code, tree op0, tree op1,
180 rtx if_false_label, rtx if_true_label, int prob)
182 enum machine_mode mode;
183 rtx drop_through_label = 0;
185 switch (code)
187 case EQ_EXPR:
189 tree inner_type = TREE_TYPE (op0);
191 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
192 != MODE_COMPLEX_FLOAT);
193 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
194 != MODE_COMPLEX_INT);
196 if (integer_zerop (op1))
197 do_jump (op0, if_true_label, if_false_label, inv (prob));
198 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
199 && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
200 do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label,
201 prob);
202 else
203 do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
204 prob);
205 break;
208 case NE_EXPR:
210 tree inner_type = TREE_TYPE (op0);
212 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
213 != MODE_COMPLEX_FLOAT);
214 gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
215 != MODE_COMPLEX_INT);
217 if (integer_zerop (op1))
218 do_jump (op0, if_false_label, if_true_label, prob);
219 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
220 && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
221 do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label,
222 inv (prob));
223 else
224 do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
225 prob);
226 break;
229 case LT_EXPR:
230 mode = TYPE_MODE (TREE_TYPE (op0));
231 if (GET_MODE_CLASS (mode) == MODE_INT
232 && ! can_compare_p (LT, mode, ccp_jump))
233 do_jump_by_parts_greater (op0, op1, 1, if_false_label, if_true_label,
234 prob);
235 else
236 do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
237 prob);
238 break;
240 case LE_EXPR:
241 mode = TYPE_MODE (TREE_TYPE (op0));
242 if (GET_MODE_CLASS (mode) == MODE_INT
243 && ! can_compare_p (LE, mode, ccp_jump))
244 do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label,
245 inv (prob));
246 else
247 do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
248 prob);
249 break;
251 case GT_EXPR:
252 mode = TYPE_MODE (TREE_TYPE (op0));
253 if (GET_MODE_CLASS (mode) == MODE_INT
254 && ! can_compare_p (GT, mode, ccp_jump))
255 do_jump_by_parts_greater (op0, op1, 0, if_false_label, if_true_label,
256 prob);
257 else
258 do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
259 prob);
260 break;
262 case GE_EXPR:
263 mode = TYPE_MODE (TREE_TYPE (op0));
264 if (GET_MODE_CLASS (mode) == MODE_INT
265 && ! can_compare_p (GE, mode, ccp_jump))
266 do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label,
267 inv (prob));
268 else
269 do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
270 prob);
271 break;
273 case ORDERED_EXPR:
274 do_compare_and_jump (op0, op1, ORDERED, ORDERED,
275 if_false_label, if_true_label, prob);
276 break;
278 case UNORDERED_EXPR:
279 do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
280 if_false_label, if_true_label, prob);
281 break;
283 case UNLT_EXPR:
284 do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
285 prob);
286 break;
288 case UNLE_EXPR:
289 do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
290 prob);
291 break;
293 case UNGT_EXPR:
294 do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
295 prob);
296 break;
298 case UNGE_EXPR:
299 do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
300 prob);
301 break;
303 case UNEQ_EXPR:
304 do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
305 prob);
306 break;
308 case LTGT_EXPR:
309 do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
310 prob);
311 break;
313 case TRUTH_ANDIF_EXPR:
314 if (if_false_label == NULL_RTX)
316 drop_through_label = gen_label_rtx ();
317 do_jump (op0, drop_through_label, NULL_RTX, prob);
318 do_jump (op1, NULL_RTX, if_true_label, prob);
320 else
322 do_jump (op0, if_false_label, NULL_RTX, prob);
323 do_jump (op1, if_false_label, if_true_label, prob);
325 break;
327 case TRUTH_ORIF_EXPR:
328 if (if_true_label == NULL_RTX)
330 drop_through_label = gen_label_rtx ();
331 do_jump (op0, NULL_RTX, drop_through_label, prob);
332 do_jump (op1, if_false_label, NULL_RTX, prob);
334 else
336 do_jump (op0, NULL_RTX, if_true_label, prob);
337 do_jump (op1, if_false_label, if_true_label, prob);
339 break;
341 default:
342 gcc_unreachable ();
345 if (drop_through_label)
347 do_pending_stack_adjust ();
348 emit_label (drop_through_label);
352 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
353 the result is zero, or IF_TRUE_LABEL if the result is one.
354 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
355 meaning fall through in that case.
357 do_jump always does any pending stack adjust except when it does not
358 actually perform a jump. An example where there is no jump
359 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
361 PROB is probability of jump to if_true_label, or -1 if unknown. */
363 void
364 do_jump (tree exp, rtx if_false_label, rtx if_true_label, int prob)
366 enum tree_code code = TREE_CODE (exp);
367 rtx temp;
368 int i;
369 tree type;
370 enum machine_mode mode;
371 rtx drop_through_label = 0;
373 switch (code)
375 case ERROR_MARK:
376 break;
378 case INTEGER_CST:
379 temp = integer_zerop (exp) ? if_false_label : if_true_label;
380 if (temp)
381 emit_jump (temp);
382 break;
384 #if 0
385 /* This is not true with #pragma weak */
386 case ADDR_EXPR:
387 /* The address of something can never be zero. */
388 if (if_true_label)
389 emit_jump (if_true_label);
390 break;
391 #endif
393 case NOP_EXPR:
394 if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
395 || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
396 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
397 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
398 goto normal;
399 case CONVERT_EXPR:
400 /* If we are narrowing the operand, we have to do the compare in the
401 narrower mode. */
402 if ((TYPE_PRECISION (TREE_TYPE (exp))
403 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
404 goto normal;
405 case NON_LVALUE_EXPR:
406 /* if a shared pointer conversion that will change representation,
407 then we have to compare in the result type. */
408 if (upc_pts_cvt_op_p (exp))
409 goto normal;
410 case ABS_EXPR:
411 case NEGATE_EXPR:
412 case LROTATE_EXPR:
413 case RROTATE_EXPR:
414 /* These cannot change zero->nonzero or vice versa. */
415 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
416 break;
418 case TRUTH_NOT_EXPR:
419 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
420 inv (prob));
421 break;
423 case COND_EXPR:
425 rtx label1 = gen_label_rtx ();
426 if (!if_true_label || !if_false_label)
428 drop_through_label = gen_label_rtx ();
429 if (!if_true_label)
430 if_true_label = drop_through_label;
431 if (!if_false_label)
432 if_false_label = drop_through_label;
435 do_pending_stack_adjust ();
436 do_jump (TREE_OPERAND (exp, 0), label1, NULL_RTX, -1);
437 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
438 emit_label (label1);
439 do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
440 break;
443 case COMPOUND_EXPR:
444 /* Lowered by gimplify.c. */
445 gcc_unreachable ();
447 case MINUS_EXPR:
448 /* Nonzero iff operands of minus differ. */
449 code = NE_EXPR;
451 /* FALLTHRU */
452 case EQ_EXPR:
453 case NE_EXPR:
454 case LT_EXPR:
455 case LE_EXPR:
456 case GT_EXPR:
457 case GE_EXPR:
458 case ORDERED_EXPR:
459 case UNORDERED_EXPR:
460 case UNLT_EXPR:
461 case UNLE_EXPR:
462 case UNGT_EXPR:
463 case UNGE_EXPR:
464 case UNEQ_EXPR:
465 case LTGT_EXPR:
466 case TRUTH_ANDIF_EXPR:
467 case TRUTH_ORIF_EXPR:
468 other_code:
469 do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
470 if_false_label, if_true_label, prob);
471 break;
473 case BIT_AND_EXPR:
474 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
475 See if the former is preferred for jump tests and restore it
476 if so. */
477 if (integer_onep (TREE_OPERAND (exp, 1)))
479 tree exp0 = TREE_OPERAND (exp, 0);
480 rtx set_label, clr_label;
481 int setclr_prob = prob;
483 /* Strip narrowing integral type conversions. */
484 while (CONVERT_EXPR_P (exp0)
485 && TREE_OPERAND (exp0, 0) != error_mark_node
486 && TYPE_PRECISION (TREE_TYPE (exp0))
487 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
488 exp0 = TREE_OPERAND (exp0, 0);
490 /* "exp0 ^ 1" inverts the sense of the single bit test. */
491 if (TREE_CODE (exp0) == BIT_XOR_EXPR
492 && integer_onep (TREE_OPERAND (exp0, 1)))
494 exp0 = TREE_OPERAND (exp0, 0);
495 clr_label = if_true_label;
496 set_label = if_false_label;
497 setclr_prob = inv (prob);
499 else
501 clr_label = if_false_label;
502 set_label = if_true_label;
505 if (TREE_CODE (exp0) == RSHIFT_EXPR)
507 tree arg = TREE_OPERAND (exp0, 0);
508 tree shift = TREE_OPERAND (exp0, 1);
509 tree argtype = TREE_TYPE (arg);
510 if (TREE_CODE (shift) == INTEGER_CST
511 && compare_tree_int (shift, 0) >= 0
512 && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
513 && prefer_and_bit_test (TYPE_MODE (argtype),
514 TREE_INT_CST_LOW (shift)))
516 unsigned HOST_WIDE_INT mask
517 = (unsigned HOST_WIDE_INT) 1 << TREE_INT_CST_LOW (shift);
518 do_jump (build2 (BIT_AND_EXPR, argtype, arg,
519 build_int_cstu (argtype, mask)),
520 clr_label, set_label, setclr_prob);
521 break;
526 /* If we are AND'ing with a small constant, do this comparison in the
527 smallest type that fits. If the machine doesn't have comparisons
528 that small, it will be converted back to the wider comparison.
529 This helps if we are testing the sign bit of a narrower object.
530 combine can't do this for us because it can't know whether a
531 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
533 if (! SLOW_BYTE_ACCESS
534 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
535 && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
536 && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
537 && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
538 && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
539 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
540 && have_insn_for (COMPARE, TYPE_MODE (type)))
542 do_jump (fold_convert (type, exp), if_false_label, if_true_label,
543 prob);
544 break;
547 if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
548 || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
549 goto normal;
551 /* Boolean comparisons can be compiled as TRUTH_AND_EXPR. */
553 case TRUTH_AND_EXPR:
554 /* High branch cost, expand as the bitwise AND of the conditions.
555 Do the same if the RHS has side effects, because we're effectively
556 turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR. */
557 if (BRANCH_COST (optimize_insn_for_speed_p (),
558 false) >= 4
559 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
560 goto normal;
561 code = TRUTH_ANDIF_EXPR;
562 goto other_code;
564 case BIT_IOR_EXPR:
565 case TRUTH_OR_EXPR:
566 /* High branch cost, expand as the bitwise OR of the conditions.
567 Do the same if the RHS has side effects, because we're effectively
568 turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR. */
569 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
570 || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
571 goto normal;
572 code = TRUTH_ORIF_EXPR;
573 goto other_code;
575 /* Fall through and generate the normal code. */
576 default:
577 normal:
578 temp = expand_normal (exp);
579 do_pending_stack_adjust ();
580 /* The RTL optimizers prefer comparisons against pseudos. */
581 if (GET_CODE (temp) == SUBREG)
583 /* Compare promoted variables in their promoted mode. */
584 if (SUBREG_PROMOTED_VAR_P (temp)
585 && REG_P (XEXP (temp, 0)))
586 temp = XEXP (temp, 0);
587 else
588 temp = copy_to_reg (temp);
590 do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
591 NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
592 GET_MODE (temp), NULL_RTX,
593 if_false_label, if_true_label, prob);
596 if (drop_through_label)
598 do_pending_stack_adjust ();
599 emit_label (drop_through_label);
603 /* Compare OP0 with OP1, word at a time, in mode MODE.
604 UNSIGNEDP says to do unsigned comparison.
605 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
607 static void
608 do_jump_by_parts_greater_rtx (enum machine_mode mode, int unsignedp, rtx op0,
609 rtx op1, rtx if_false_label, rtx if_true_label,
610 int prob)
612 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
613 rtx drop_through_label = 0;
614 bool drop_through_if_true = false, drop_through_if_false = false;
615 enum rtx_code code = GT;
616 int i;
618 if (! if_true_label || ! if_false_label)
619 drop_through_label = gen_label_rtx ();
620 if (! if_true_label)
622 if_true_label = drop_through_label;
623 drop_through_if_true = true;
625 if (! if_false_label)
627 if_false_label = drop_through_label;
628 drop_through_if_false = true;
631 /* Deal with the special case 0 > x: only one comparison is necessary and
632 we reverse it to avoid jumping to the drop-through label. */
633 if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
635 code = LE;
636 if_true_label = if_false_label;
637 if_false_label = drop_through_label;
638 drop_through_if_true = false;
639 drop_through_if_false = true;
642 /* Compare a word at a time, high order first. */
643 for (i = 0; i < nwords; i++)
645 rtx op0_word, op1_word;
647 if (WORDS_BIG_ENDIAN)
649 op0_word = operand_subword_force (op0, i, mode);
650 op1_word = operand_subword_force (op1, i, mode);
652 else
654 op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
655 op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
658 /* All but high-order word must be compared as unsigned. */
659 do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
660 word_mode, NULL_RTX, NULL_RTX, if_true_label,
661 prob);
663 /* Emit only one comparison for 0. Do not emit the last cond jump. */
664 if (op0 == const0_rtx || i == nwords - 1)
665 break;
667 /* Consider lower words only if these are equal. */
668 do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
669 NULL_RTX, NULL_RTX, if_false_label, inv (prob));
672 if (!drop_through_if_false)
673 emit_jump (if_false_label);
674 if (drop_through_label)
675 emit_label (drop_through_label);
678 /* Given a comparison expression EXP for values too wide to be compared
679 with one insn, test the comparison and jump to the appropriate label.
680 The code of EXP is ignored; we always test GT if SWAP is 0,
681 and LT if SWAP is 1. */
683 static void
684 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
685 rtx if_false_label, rtx if_true_label, int prob)
687 rtx op0 = expand_normal (swap ? treeop1 : treeop0);
688 rtx op1 = expand_normal (swap ? treeop0 : treeop1);
689 enum machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
690 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
692 do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
693 if_true_label, prob);
696 /* Jump according to whether OP0 is 0. We assume that OP0 has an integer
697 mode, MODE, that is too wide for the available compare insns. Either
698 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
699 to indicate drop through. */
701 static void
702 do_jump_by_parts_zero_rtx (enum machine_mode mode, rtx op0,
703 rtx if_false_label, rtx if_true_label, int prob)
705 int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
706 rtx part;
707 int i;
708 rtx drop_through_label = 0;
710 /* The fastest way of doing this comparison on almost any machine is to
711 "or" all the words and compare the result. If all have to be loaded
712 from memory and this is a very wide item, it's possible this may
713 be slower, but that's highly unlikely. */
715 part = gen_reg_rtx (word_mode);
716 emit_move_insn (part, operand_subword_force (op0, 0, mode));
717 for (i = 1; i < nwords && part != 0; i++)
718 part = expand_binop (word_mode, ior_optab, part,
719 operand_subword_force (op0, i, mode),
720 part, 1, OPTAB_WIDEN);
722 if (part != 0)
724 do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
725 NULL_RTX, if_false_label, if_true_label, prob);
726 return;
729 /* If we couldn't do the "or" simply, do this with a series of compares. */
730 if (! if_false_label)
731 drop_through_label = if_false_label = gen_label_rtx ();
733 for (i = 0; i < nwords; i++)
734 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
735 const0_rtx, EQ, 1, word_mode, NULL_RTX,
736 if_false_label, NULL_RTX, prob);
738 if (if_true_label)
739 emit_jump (if_true_label);
741 if (drop_through_label)
742 emit_label (drop_through_label);
745 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
746 where MODE is an integer mode too wide to be compared with one insn.
747 Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
748 to indicate drop through. */
750 static void
751 do_jump_by_parts_equality_rtx (enum machine_mode mode, rtx op0, rtx op1,
752 rtx if_false_label, rtx if_true_label, int prob)
754 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
755 rtx drop_through_label = 0;
756 int i;
758 if (op1 == const0_rtx)
760 do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label,
761 prob);
762 return;
764 else if (op0 == const0_rtx)
766 do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label,
767 prob);
768 return;
771 if (! if_false_label)
772 drop_through_label = if_false_label = gen_label_rtx ();
774 for (i = 0; i < nwords; i++)
775 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
776 operand_subword_force (op1, i, mode),
777 EQ, 0, word_mode, NULL_RTX,
778 if_false_label, NULL_RTX, prob);
780 if (if_true_label)
781 emit_jump (if_true_label);
782 if (drop_through_label)
783 emit_label (drop_through_label);
786 /* Given an EQ_EXPR expression EXP for values too wide to be compared
787 with one insn, test the comparison and jump to the appropriate label. */
789 static void
790 do_jump_by_parts_equality (tree treeop0, tree treeop1, rtx if_false_label,
791 rtx if_true_label, int prob)
793 rtx op0 = expand_normal (treeop0);
794 rtx op1 = expand_normal (treeop1);
795 enum machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
796 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
797 if_true_label, prob);
800 /* Split a comparison into two others, the second of which has the other
801 "orderedness". The first is always ORDERED or UNORDERED if MODE
802 does not honor NaNs (which means that it can be skipped in that case;
803 see do_compare_rtx_and_jump).
805 The two conditions are written in *CODE1 and *CODE2. Return true if
806 the conditions must be ANDed, false if they must be ORed. */
808 bool
809 split_comparison (enum rtx_code code, enum machine_mode mode,
810 enum rtx_code *code1, enum rtx_code *code2)
812 switch (code)
814 case LT:
815 *code1 = ORDERED;
816 *code2 = UNLT;
817 return true;
818 case LE:
819 *code1 = ORDERED;
820 *code2 = UNLE;
821 return true;
822 case GT:
823 *code1 = ORDERED;
824 *code2 = UNGT;
825 return true;
826 case GE:
827 *code1 = ORDERED;
828 *code2 = UNGE;
829 return true;
830 case EQ:
831 *code1 = ORDERED;
832 *code2 = UNEQ;
833 return true;
834 case NE:
835 *code1 = UNORDERED;
836 *code2 = LTGT;
837 return false;
838 case UNLT:
839 *code1 = UNORDERED;
840 *code2 = LT;
841 return false;
842 case UNLE:
843 *code1 = UNORDERED;
844 *code2 = LE;
845 return false;
846 case UNGT:
847 *code1 = UNORDERED;
848 *code2 = GT;
849 return false;
850 case UNGE:
851 *code1 = UNORDERED;
852 *code2 = GE;
853 return false;
854 case UNEQ:
855 *code1 = UNORDERED;
856 *code2 = EQ;
857 return false;
858 case LTGT:
859 /* Do not turn a trapping comparison into a non-trapping one. */
860 if (HONOR_SNANS (mode))
862 *code1 = LT;
863 *code2 = GT;
864 return false;
866 else
868 *code1 = ORDERED;
869 *code2 = NE;
870 return true;
872 default:
873 gcc_unreachable ();
878 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
879 The decision as to signed or unsigned comparison must be made by the caller.
881 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
882 compared. */
884 void
885 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
886 enum machine_mode mode, rtx size, rtx if_false_label,
887 rtx if_true_label, int prob)
889 rtx tem;
890 rtx dummy_label = NULL_RTX;
892 /* Reverse the comparison if that is safe and we want to jump if it is
893 false. Also convert to the reverse comparison if the target can
894 implement it. */
895 if ((! if_true_label
896 || ! can_compare_p (code, mode, ccp_jump))
897 && (! FLOAT_MODE_P (mode)
898 || code == ORDERED || code == UNORDERED
899 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
900 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
902 enum rtx_code rcode;
903 if (FLOAT_MODE_P (mode))
904 rcode = reverse_condition_maybe_unordered (code);
905 else
906 rcode = reverse_condition (code);
908 /* Canonicalize to UNORDERED for the libcall. */
909 if (can_compare_p (rcode, mode, ccp_jump)
910 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
912 tem = if_true_label;
913 if_true_label = if_false_label;
914 if_false_label = tem;
915 code = rcode;
916 prob = inv (prob);
920 /* If one operand is constant, make it the second one. Only do this
921 if the other operand is not constant as well. */
923 if (swap_commutative_operands_p (op0, op1))
925 tem = op0;
926 op0 = op1;
927 op1 = tem;
928 code = swap_condition (code);
931 do_pending_stack_adjust ();
933 code = unsignedp ? unsigned_condition (code) : code;
934 if (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
935 op0, op1)))
937 if (CONSTANT_P (tem))
939 rtx label = (tem == const0_rtx || tem == CONST0_RTX (mode))
940 ? if_false_label : if_true_label;
941 if (label)
942 emit_jump (label);
943 return;
946 code = GET_CODE (tem);
947 mode = GET_MODE (tem);
948 op0 = XEXP (tem, 0);
949 op1 = XEXP (tem, 1);
950 unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
953 if (! if_true_label)
954 dummy_label = if_true_label = gen_label_rtx ();
956 if (GET_MODE_CLASS (mode) == MODE_INT
957 && ! can_compare_p (code, mode, ccp_jump))
959 switch (code)
961 case LTU:
962 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
963 if_false_label, if_true_label, prob);
964 break;
966 case LEU:
967 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
968 if_true_label, if_false_label,
969 inv (prob));
970 break;
972 case GTU:
973 do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
974 if_false_label, if_true_label, prob);
975 break;
977 case GEU:
978 do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
979 if_true_label, if_false_label,
980 inv (prob));
981 break;
983 case LT:
984 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
985 if_false_label, if_true_label, prob);
986 break;
988 case LE:
989 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
990 if_true_label, if_false_label,
991 inv (prob));
992 break;
994 case GT:
995 do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
996 if_false_label, if_true_label, prob);
997 break;
999 case GE:
1000 do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1001 if_true_label, if_false_label,
1002 inv (prob));
1003 break;
1005 case EQ:
1006 do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
1007 if_true_label, prob);
1008 break;
1010 case NE:
1011 do_jump_by_parts_equality_rtx (mode, op0, op1, if_true_label,
1012 if_false_label, inv (prob));
1013 break;
1015 default:
1016 gcc_unreachable ();
1019 else
1021 if (SCALAR_FLOAT_MODE_P (mode)
1022 && ! can_compare_p (code, mode, ccp_jump)
1023 && can_compare_p (swap_condition (code), mode, ccp_jump))
1025 rtx tmp;
1026 code = swap_condition (code);
1027 tmp = op0;
1028 op0 = op1;
1029 op1 = tmp;
1031 else if (SCALAR_FLOAT_MODE_P (mode)
1032 && ! can_compare_p (code, mode, ccp_jump)
1033 /* Never split ORDERED and UNORDERED.
1034 These must be implemented. */
1035 && (code != ORDERED && code != UNORDERED)
1036 /* Split a floating-point comparison if
1037 we can jump on other conditions... */
1038 && (have_insn_for (COMPARE, mode)
1039 /* ... or if there is no libcall for it. */
1040 || code_to_optab (code) == unknown_optab))
1042 enum rtx_code first_code;
1043 bool and_them = split_comparison (code, mode, &first_code, &code);
1045 /* If there are no NaNs, the first comparison should always fall
1046 through. */
1047 if (!HONOR_NANS (mode))
1048 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1050 else
1052 if (and_them)
1054 rtx dest_label;
1055 /* If we only jump if true, just bypass the second jump. */
1056 if (! if_false_label)
1058 if (! dummy_label)
1059 dummy_label = gen_label_rtx ();
1060 dest_label = dummy_label;
1062 else
1063 dest_label = if_false_label;
1064 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1065 size, dest_label, NULL_RTX, prob);
1067 else
1068 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1069 size, NULL_RTX, if_true_label, prob);
1073 emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1074 if_true_label, prob);
1077 if (if_false_label)
1078 emit_jump (if_false_label);
1079 if (dummy_label)
1080 emit_label (dummy_label);
1083 /* Generate code for a comparison expression EXP (including code to compute
1084 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1085 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
1086 generated code will drop through.
1087 SIGNED_CODE should be the rtx operation for this comparison for
1088 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1090 We force a stack adjustment unless there are currently
1091 things pushed on the stack that aren't yet used. */
1093 static void
1094 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1095 enum rtx_code unsigned_code, rtx if_false_label,
1096 rtx if_true_label, int prob)
1098 rtx op0, op1;
1099 tree type;
1100 enum machine_mode mode;
1101 int unsignedp;
1102 enum rtx_code code;
1104 /* Don't crash if the comparison was erroneous. */
1105 op0 = expand_normal (treeop0);
1106 if (TREE_CODE (treeop0) == ERROR_MARK)
1107 return;
1109 op1 = expand_normal (treeop1);
1110 if (TREE_CODE (treeop1) == ERROR_MARK)
1111 return;
1113 type = TREE_TYPE (treeop0);
1114 mode = TYPE_MODE (type);
1115 if (TREE_CODE (treeop0) == INTEGER_CST
1116 && (TREE_CODE (treeop1) != INTEGER_CST
1117 || (GET_MODE_BITSIZE (mode)
1118 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1120 /* op0 might have been replaced by promoted constant, in which
1121 case the type of second argument should be used. */
1122 type = TREE_TYPE (treeop1);
1123 mode = TYPE_MODE (type);
1125 unsignedp = TYPE_UNSIGNED (type);
1126 code = unsignedp ? unsigned_code : signed_code;
1128 #ifdef HAVE_canonicalize_funcptr_for_compare
1129 /* If function pointers need to be "canonicalized" before they can
1130 be reliably compared, then canonicalize them.
1131 Only do this if *both* sides of the comparison are function pointers.
1132 If one side isn't, we want a noncanonicalized comparison. See PR
1133 middle-end/17564. */
1134 if (HAVE_canonicalize_funcptr_for_compare
1135 && TREE_CODE (TREE_TYPE (treeop0)) == POINTER_TYPE
1136 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop0)))
1137 == FUNCTION_TYPE
1138 && TREE_CODE (TREE_TYPE (treeop1)) == POINTER_TYPE
1139 && TREE_CODE (TREE_TYPE (TREE_TYPE (treeop1)))
1140 == FUNCTION_TYPE)
1142 rtx new_op0 = gen_reg_rtx (mode);
1143 rtx new_op1 = gen_reg_rtx (mode);
1145 emit_insn (gen_canonicalize_funcptr_for_compare (new_op0, op0));
1146 op0 = new_op0;
1148 emit_insn (gen_canonicalize_funcptr_for_compare (new_op1, op1));
1149 op1 = new_op1;
1151 #endif
1153 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1154 ((mode == BLKmode)
1155 ? expr_size (treeop0) : NULL_RTX),
1156 if_false_label, if_true_label, prob);
1159 #include "gt-dojump.h"