1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
30 #include "insn-config.h"
31 #include "insn-attr.h"
32 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
35 #include "langhooks.h"
38 static bool prefer_and_bit_test (enum machine_mode, int);
39 static void do_jump_by_parts_greater (tree, int, rtx, rtx);
40 static void do_jump_by_parts_equality (tree, rtx, rtx);
41 static void do_compare_and_jump (tree, enum rtx_code, enum rtx_code, rtx,
44 /* At the start of a function, record that we have no previously-pushed
45 arguments waiting to be popped. */
48 init_pending_stack_adjust (void)
50 pending_stack_adjust = 0;
53 /* When exiting from function, if safe, clear out any pending stack adjust
54 so the adjustment won't get done.
56 Note, if the current function calls alloca, then it must have a
57 frame pointer regardless of the value of flag_omit_frame_pointer. */
60 clear_pending_stack_adjust (void)
63 && (! flag_omit_frame_pointer || current_function_calls_alloca)
65 && ! (DECL_INLINE (current_function_decl) && ! flag_no_inline)
66 && ! flag_inline_functions)
68 stack_pointer_delta -= pending_stack_adjust,
69 pending_stack_adjust = 0;
73 /* Pop any previously-pushed arguments that have not been popped yet. */
76 do_pending_stack_adjust (void)
78 if (inhibit_defer_pop == 0)
80 if (pending_stack_adjust != 0)
81 adjust_stack (GEN_INT (pending_stack_adjust));
82 pending_stack_adjust = 0;
86 /* Expand conditional expressions. */
88 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.
89 LABEL is an rtx of code CODE_LABEL, in this function and all the
93 jumpifnot (tree exp, rtx label)
95 do_jump (exp, label, NULL_RTX);
98 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero. */
101 jumpif (tree exp, rtx label)
103 do_jump (exp, NULL_RTX, label);
106 /* Used internally by prefer_and_bit_test. */
108 static GTY(()) rtx and_reg;
109 static GTY(()) rtx and_test;
110 static GTY(()) rtx shift_test;
112 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
113 where X is an arbitrary register of mode MODE. Return true if the former
117 prefer_and_bit_test (enum machine_mode mode, int bitnum)
121 /* Set up rtxes for the two variations. Use NULL as a placeholder
122 for the BITNUM-based constants. */
123 and_reg = gen_rtx_REG (mode, FIRST_PSEUDO_REGISTER);
124 and_test = gen_rtx_AND (mode, and_reg, NULL);
125 shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
130 /* Change the mode of the previously-created rtxes. */
131 PUT_MODE (and_reg, mode);
132 PUT_MODE (and_test, mode);
133 PUT_MODE (shift_test, mode);
134 PUT_MODE (XEXP (shift_test, 0), mode);
137 /* Fill in the integers. */
138 XEXP (and_test, 1) = GEN_INT ((unsigned HOST_WIDE_INT) 1 << bitnum);
139 XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
141 return (rtx_cost (and_test, IF_THEN_ELSE)
142 <= rtx_cost (shift_test, IF_THEN_ELSE));
145 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
146 the result is zero, or IF_TRUE_LABEL if the result is one.
147 Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
148 meaning fall through in that case.
150 do_jump always does any pending stack adjust except when it does not
151 actually perform a jump. An example where there is no jump
152 is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
154 This function is responsible for optimizing cases such as
155 &&, || and comparison operators in EXP. */
158 do_jump (tree exp, rtx if_false_label, rtx if_true_label)
160 enum tree_code code = TREE_CODE (exp);
161 /* Some cases need to create a label to jump to
162 in order to properly fall through.
163 These cases set DROP_THROUGH_LABEL nonzero. */
164 rtx drop_through_label = 0;
168 enum machine_mode mode;
178 temp = integer_zerop (exp) ? if_false_label : if_true_label;
184 /* This is not true with #pragma weak */
186 /* The address of something can never be zero. */
188 emit_jump (if_true_label);
193 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label);
194 TREE_OPERAND (exp, 0)
195 = lang_hooks.unsave_expr_now (TREE_OPERAND (exp, 0));
199 if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
200 || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
201 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
202 || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
205 /* If we are narrowing the operand, we have to do the compare in the
207 if ((TYPE_PRECISION (TREE_TYPE (exp))
208 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
210 case NON_LVALUE_EXPR:
216 /* These cannot change zero->nonzero or vice versa. */
217 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label);
221 /* This is never less insns than evaluating the PLUS_EXPR followed by
222 a test and can be longer if the test is eliminated. */
224 /* Reduce to minus. */
225 exp = build (MINUS_EXPR, TREE_TYPE (exp),
226 TREE_OPERAND (exp, 0),
227 fold (build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (exp, 1)),
228 TREE_OPERAND (exp, 1))));
229 /* Process as MINUS. */
233 /* Nonzero iff operands of minus differ. */
234 do_compare_and_jump (build (NE_EXPR, TREE_TYPE (exp),
235 TREE_OPERAND (exp, 0),
236 TREE_OPERAND (exp, 1)),
237 NE, NE, if_false_label, if_true_label);
241 /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
242 See if the former is preferred for jump tests and restore it
244 if (TREE_CODE (TREE_OPERAND (exp, 0)) == RSHIFT_EXPR
245 && integer_onep (TREE_OPERAND (exp, 1)))
247 tree arg = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
248 tree shift = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
249 tree one = TREE_OPERAND (exp, 1);
250 tree argtype = TREE_TYPE (arg);
251 if (TREE_CODE (shift) == INTEGER_CST
252 && compare_tree_int (shift, 0) > 0
253 && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
254 && prefer_and_bit_test (TYPE_MODE (argtype),
255 TREE_INT_CST_LOW (shift)))
257 do_jump (build (BIT_AND_EXPR, argtype, arg,
258 fold (build (LSHIFT_EXPR, argtype, one, shift))),
259 if_false_label, if_true_label);
264 /* If we are AND'ing with a small constant, do this comparison in the
265 smallest type that fits. If the machine doesn't have comparisons
266 that small, it will be converted back to the wider comparison.
267 This helps if we are testing the sign bit of a narrower object.
268 combine can't do this for us because it can't know whether a
269 ZERO_EXTRACT or a compare in a smaller mode exists, but we do. */
271 if (! SLOW_BYTE_ACCESS
272 && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
273 && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
274 && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
275 && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
276 && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
277 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
278 && (cmp_optab->handlers[(int) TYPE_MODE (type)].insn_code
279 != CODE_FOR_nothing))
281 do_jump (convert (type, exp), if_false_label, if_true_label);
287 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label);
290 case TRUTH_ANDIF_EXPR:
291 if (if_false_label == 0)
292 if_false_label = drop_through_label = gen_label_rtx ();
293 do_jump (TREE_OPERAND (exp, 0), if_false_label, NULL_RTX);
294 start_cleanup_deferral ();
295 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label);
296 end_cleanup_deferral ();
299 case TRUTH_ORIF_EXPR:
300 if (if_true_label == 0)
301 if_true_label = drop_through_label = gen_label_rtx ();
302 do_jump (TREE_OPERAND (exp, 0), NULL_RTX, if_true_label);
303 start_cleanup_deferral ();
304 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label);
305 end_cleanup_deferral ();
310 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
311 preserve_temp_slots (NULL_RTX);
315 do_pending_stack_adjust ();
316 do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label);
322 case ARRAY_RANGE_REF:
324 HOST_WIDE_INT bitsize, bitpos;
326 enum machine_mode mode;
331 /* Get description of this reference. We don't actually care
332 about the underlying object here. */
333 get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
334 &unsignedp, &volatilep);
336 type = lang_hooks.types.type_for_size (bitsize, unsignedp);
337 if (! SLOW_BYTE_ACCESS
338 && type != 0 && bitsize >= 0
339 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
340 && (cmp_optab->handlers[(int) TYPE_MODE (type)].insn_code
341 != CODE_FOR_nothing))
343 do_jump (convert (type, exp), if_false_label, if_true_label);
350 /* Do (a ? 1 : 0) and (a ? 0 : 1) as special cases. */
351 if (integer_onep (TREE_OPERAND (exp, 1))
352 && integer_zerop (TREE_OPERAND (exp, 2)))
353 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label);
355 else if (integer_zerop (TREE_OPERAND (exp, 1))
356 && integer_onep (TREE_OPERAND (exp, 2)))
357 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label);
361 rtx label1 = gen_label_rtx ();
362 drop_through_label = gen_label_rtx ();
364 do_jump (TREE_OPERAND (exp, 0), label1, NULL_RTX);
366 start_cleanup_deferral ();
367 /* Now the THEN-expression. */
368 do_jump (TREE_OPERAND (exp, 1),
369 if_false_label ? if_false_label : drop_through_label,
370 if_true_label ? if_true_label : drop_through_label);
371 /* In case the do_jump just above never jumps. */
372 do_pending_stack_adjust ();
375 /* Now the ELSE-expression. */
376 do_jump (TREE_OPERAND (exp, 2),
377 if_false_label ? if_false_label : drop_through_label,
378 if_true_label ? if_true_label : drop_through_label);
379 end_cleanup_deferral ();
385 tree inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
387 if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_COMPLEX_FLOAT
388 || GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_COMPLEX_INT)
390 tree exp0 = save_expr (TREE_OPERAND (exp, 0));
391 tree exp1 = save_expr (TREE_OPERAND (exp, 1));
394 (build (TRUTH_ANDIF_EXPR, TREE_TYPE (exp),
395 fold (build (EQ_EXPR, TREE_TYPE (exp),
396 fold (build1 (REALPART_EXPR,
397 TREE_TYPE (inner_type),
399 fold (build1 (REALPART_EXPR,
400 TREE_TYPE (inner_type),
402 fold (build (EQ_EXPR, TREE_TYPE (exp),
403 fold (build1 (IMAGPART_EXPR,
404 TREE_TYPE (inner_type),
406 fold (build1 (IMAGPART_EXPR,
407 TREE_TYPE (inner_type),
409 if_false_label, if_true_label);
412 else if (integer_zerop (TREE_OPERAND (exp, 1)))
413 do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label);
415 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
416 && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
417 do_jump_by_parts_equality (exp, if_false_label, if_true_label);
419 do_compare_and_jump (exp, EQ, EQ, if_false_label, if_true_label);
425 tree inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
427 if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_COMPLEX_FLOAT
428 || GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_COMPLEX_INT)
430 tree exp0 = save_expr (TREE_OPERAND (exp, 0));
431 tree exp1 = save_expr (TREE_OPERAND (exp, 1));
434 (build (TRUTH_ORIF_EXPR, TREE_TYPE (exp),
435 fold (build (NE_EXPR, TREE_TYPE (exp),
436 fold (build1 (REALPART_EXPR,
437 TREE_TYPE (inner_type),
439 fold (build1 (REALPART_EXPR,
440 TREE_TYPE (inner_type),
442 fold (build (NE_EXPR, TREE_TYPE (exp),
443 fold (build1 (IMAGPART_EXPR,
444 TREE_TYPE (inner_type),
446 fold (build1 (IMAGPART_EXPR,
447 TREE_TYPE (inner_type),
449 if_false_label, if_true_label);
452 else if (integer_zerop (TREE_OPERAND (exp, 1)))
453 do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label);
455 else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
456 && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
457 do_jump_by_parts_equality (exp, if_true_label, if_false_label);
459 do_compare_and_jump (exp, NE, NE, if_false_label, if_true_label);
464 mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
465 if (GET_MODE_CLASS (mode) == MODE_INT
466 && ! can_compare_p (LT, mode, ccp_jump))
467 do_jump_by_parts_greater (exp, 1, if_false_label, if_true_label);
469 do_compare_and_jump (exp, LT, LTU, if_false_label, if_true_label);
473 mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
474 if (GET_MODE_CLASS (mode) == MODE_INT
475 && ! can_compare_p (LE, mode, ccp_jump))
476 do_jump_by_parts_greater (exp, 0, if_true_label, if_false_label);
478 do_compare_and_jump (exp, LE, LEU, if_false_label, if_true_label);
482 mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
483 if (GET_MODE_CLASS (mode) == MODE_INT
484 && ! can_compare_p (GT, mode, ccp_jump))
485 do_jump_by_parts_greater (exp, 0, if_false_label, if_true_label);
487 do_compare_and_jump (exp, GT, GTU, if_false_label, if_true_label);
491 mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
492 if (GET_MODE_CLASS (mode) == MODE_INT
493 && ! can_compare_p (GE, mode, ccp_jump))
494 do_jump_by_parts_greater (exp, 1, if_true_label, if_false_label);
496 do_compare_and_jump (exp, GE, GEU, if_false_label, if_true_label);
502 enum rtx_code cmp, rcmp;
505 if (code == UNORDERED_EXPR)
506 cmp = UNORDERED, rcmp = ORDERED;
508 cmp = ORDERED, rcmp = UNORDERED;
509 mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
512 if (! can_compare_p (cmp, mode, ccp_jump)
513 && (can_compare_p (rcmp, mode, ccp_jump)
514 /* If the target doesn't provide either UNORDERED or ORDERED
515 comparisons, canonicalize on UNORDERED for the library. */
516 || rcmp == UNORDERED))
520 do_compare_and_jump (exp, cmp, cmp, if_false_label, if_true_label);
522 do_compare_and_jump (exp, rcmp, rcmp, if_true_label, if_false_label);
527 enum rtx_code rcode1;
528 enum tree_code tcode2;
552 mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
553 if (can_compare_p (rcode1, mode, ccp_jump))
554 do_compare_and_jump (exp, rcode1, rcode1, if_false_label,
558 tree op0 = save_expr (TREE_OPERAND (exp, 0));
559 tree op1 = save_expr (TREE_OPERAND (exp, 1));
562 /* If the target doesn't support combined unordered
563 compares, decompose into UNORDERED + comparison. */
564 cmp0 = fold (build (UNORDERED_EXPR, TREE_TYPE (exp), op0, op1));
565 cmp1 = fold (build (tcode2, TREE_TYPE (exp), op0, op1));
566 exp = build (TRUTH_ORIF_EXPR, TREE_TYPE (exp), cmp0, cmp1);
567 do_jump (exp, if_false_label, if_true_label);
573 __builtin_expect (<test>, 0) and
574 __builtin_expect (<test>, 1)
576 We need to do this here, so that <test> is not converted to a SCC
577 operation on machines that use condition code registers and COMPARE
578 like the PowerPC, and then the jump is done based on whether the SCC
579 operation produced a 1 or 0. */
581 /* Check for a built-in function. */
583 tree fndecl = get_callee_fndecl (exp);
584 tree arglist = TREE_OPERAND (exp, 1);
587 && DECL_BUILT_IN (fndecl)
588 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
589 && arglist != NULL_TREE
590 && TREE_CHAIN (arglist) != NULL_TREE)
592 rtx seq = expand_builtin_expect_jump (exp, if_false_label,
602 /* Fall through and generate the normal code. */
606 temp = expand_expr (exp, NULL_RTX, VOIDmode, 0);
608 /* This is not needed any more and causes poor code since it causes
609 comparisons and tests from non-SI objects to have different code
611 /* Copy to register to avoid generating bad insns by cse
612 from (set (mem ...) (arithop)) (set (cc0) (mem ...)). */
613 if (!cse_not_expected && GET_CODE (temp) == MEM)
614 temp = copy_to_reg (temp);
616 do_pending_stack_adjust ();
617 /* Do any postincrements in the expression that was tested. */
620 if (GET_CODE (temp) == CONST_INT
621 || (GET_CODE (temp) == CONST_DOUBLE && GET_MODE (temp) == VOIDmode)
622 || GET_CODE (temp) == LABEL_REF)
624 rtx target = temp == const0_rtx ? if_false_label : if_true_label;
628 else if (GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
629 && ! can_compare_p (NE, GET_MODE (temp), ccp_jump))
630 /* Note swapping the labels gives us not-equal. */
631 do_jump_by_parts_equality_rtx (temp, if_true_label, if_false_label);
632 else if (GET_MODE (temp) != VOIDmode)
634 /* The RTL optimizers prefer comparisons against pseudos. */
635 if (GET_CODE (temp) == SUBREG)
637 /* Compare promoted variables in their promoted mode. */
638 if (SUBREG_PROMOTED_VAR_P (temp)
639 && GET_CODE (XEXP (temp, 0)) == REG)
640 temp = XEXP (temp, 0);
642 temp = copy_to_reg (temp);
644 do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
645 NE, TREE_UNSIGNED (TREE_TYPE (exp)),
646 GET_MODE (temp), NULL_RTX,
647 if_false_label, if_true_label);
653 if (drop_through_label)
655 /* If do_jump produces code that might be jumped around,
656 do any stack adjusts from that code, before the place
657 where control merges in. */
658 do_pending_stack_adjust ();
659 emit_label (drop_through_label);
663 /* Given a comparison expression EXP for values too wide to be compared
664 with one insn, test the comparison and jump to the appropriate label.
665 The code of EXP is ignored; we always test GT if SWAP is 0,
666 and LT if SWAP is 1. */
669 do_jump_by_parts_greater (tree exp, int swap, rtx if_false_label,
672 rtx op0 = expand_expr (TREE_OPERAND (exp, swap), NULL_RTX, VOIDmode, 0);
673 rtx op1 = expand_expr (TREE_OPERAND (exp, !swap), NULL_RTX, VOIDmode, 0);
674 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
675 int unsignedp = TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
677 do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label, if_true_label);
680 /* Compare OP0 with OP1, word at a time, in mode MODE.
681 UNSIGNEDP says to do unsigned comparison.
682 Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise. */
685 do_jump_by_parts_greater_rtx (enum machine_mode mode, int unsignedp, rtx op0,
686 rtx op1, rtx if_false_label, rtx if_true_label)
688 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
689 rtx drop_through_label = 0;
692 if (! if_true_label || ! if_false_label)
693 drop_through_label = gen_label_rtx ();
695 if_true_label = drop_through_label;
696 if (! if_false_label)
697 if_false_label = drop_through_label;
699 /* Compare a word at a time, high order first. */
700 for (i = 0; i < nwords; i++)
702 rtx op0_word, op1_word;
704 if (WORDS_BIG_ENDIAN)
706 op0_word = operand_subword_force (op0, i, mode);
707 op1_word = operand_subword_force (op1, i, mode);
711 op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
712 op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
715 /* All but high-order word must be compared as unsigned. */
716 do_compare_rtx_and_jump (op0_word, op1_word, GT,
717 (unsignedp || i > 0), word_mode, NULL_RTX,
718 NULL_RTX, if_true_label);
720 /* Consider lower words only if these are equal. */
721 do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
722 NULL_RTX, NULL_RTX, if_false_label);
726 emit_jump (if_false_label);
727 if (drop_through_label)
728 emit_label (drop_through_label);
731 /* Given an EQ_EXPR expression EXP for values too wide to be compared
732 with one insn, test the comparison and jump to the appropriate label. */
735 do_jump_by_parts_equality (tree exp, rtx if_false_label, rtx if_true_label)
737 rtx op0 = expand_expr (TREE_OPERAND (exp, 0), NULL_RTX, VOIDmode, 0);
738 rtx op1 = expand_expr (TREE_OPERAND (exp, 1), NULL_RTX, VOIDmode, 0);
739 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
740 int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
742 rtx drop_through_label = 0;
744 if (! if_false_label)
745 drop_through_label = if_false_label = gen_label_rtx ();
747 for (i = 0; i < nwords; i++)
748 do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
749 operand_subword_force (op1, i, mode),
750 EQ, TREE_UNSIGNED (TREE_TYPE (exp)),
751 word_mode, NULL_RTX, if_false_label, NULL_RTX);
754 emit_jump (if_true_label);
755 if (drop_through_label)
756 emit_label (drop_through_label);
759 /* Jump according to whether OP0 is 0.
760 We assume that OP0 has an integer mode that is too wide
761 for the available compare insns. */
764 do_jump_by_parts_equality_rtx (rtx op0, rtx if_false_label, rtx if_true_label)
766 int nwords = GET_MODE_SIZE (GET_MODE (op0)) / UNITS_PER_WORD;
769 rtx drop_through_label = 0;
771 /* The fastest way of doing this comparison on almost any machine is to
772 "or" all the words and compare the result. If all have to be loaded
773 from memory and this is a very wide item, it's possible this may
774 be slower, but that's highly unlikely. */
776 part = gen_reg_rtx (word_mode);
777 emit_move_insn (part, operand_subword_force (op0, 0, GET_MODE (op0)));
778 for (i = 1; i < nwords && part != 0; i++)
779 part = expand_binop (word_mode, ior_optab, part,
780 operand_subword_force (op0, i, GET_MODE (op0)),
781 part, 1, OPTAB_WIDEN);
785 do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
786 NULL_RTX, if_false_label, if_true_label);
791 /* If we couldn't do the "or" simply, do this with a series of compares. */
792 if (! if_false_label)
793 drop_through_label = if_false_label = gen_label_rtx ();
795 for (i = 0; i < nwords; i++)
796 do_compare_rtx_and_jump (operand_subword_force (op0, i, GET_MODE (op0)),
797 const0_rtx, EQ, 1, word_mode, NULL_RTX,
798 if_false_label, NULL_RTX);
801 emit_jump (if_true_label);
803 if (drop_through_label)
804 emit_label (drop_through_label);
807 /* Generate code for a comparison of OP0 and OP1 with rtx code CODE.
808 (including code to compute the values to be compared)
809 and set (CC0) according to the result.
810 The decision as to signed or unsigned comparison must be made by the caller.
812 We force a stack adjustment unless there are currently
813 things pushed on the stack that aren't yet used.
815 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
819 compare_from_rtx (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
820 enum machine_mode mode, rtx size)
825 /* If one operand is constant, make it the second one. Only do this
826 if the other operand is not constant as well. */
828 if (swap_commutative_operands_p (op0, op1))
833 code = swap_condition (code);
838 op0 = force_not_mem (op0);
839 op1 = force_not_mem (op1);
842 do_pending_stack_adjust ();
844 ucode = unsignedp ? unsigned_condition (code) : code;
845 tem = simplify_const_relational_operation (ucode, mode, op0, op1);
850 /* There's no need to do this now that combine.c can eliminate lots of
851 sign extensions. This can be less efficient in certain cases on other
854 /* If this is a signed equality comparison, we can do it as an
855 unsigned comparison since zero-extension is cheaper than sign
856 extension and comparisons with zero are done as unsigned. This is
857 the case even on machines that can do fast sign extension, since
858 zero-extension is easier to combine with other operations than
859 sign-extension is. If we are comparing against a constant, we must
860 convert it to what it would look like unsigned. */
861 if ((code == EQ || code == NE) && ! unsignedp
862 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
864 if (GET_CODE (op1) == CONST_INT
865 && (INTVAL (op1) & GET_MODE_MASK (GET_MODE (op0))) != INTVAL (op1))
866 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (GET_MODE (op0)));
871 emit_cmp_insn (op0, op1, code, size, mode, unsignedp);
874 return gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
876 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
880 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
881 The decision as to signed or unsigned comparison must be made by the caller.
883 If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
887 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
888 enum machine_mode mode, rtx size, rtx if_false_label,
893 int dummy_true_label = 0;
895 /* Reverse the comparison if that is safe and we want to jump if it is
897 if (! if_true_label && ! FLOAT_MODE_P (mode))
899 if_true_label = if_false_label;
901 code = reverse_condition (code);
904 /* If one operand is constant, make it the second one. Only do this
905 if the other operand is not constant as well. */
907 if (swap_commutative_operands_p (op0, op1))
912 code = swap_condition (code);
917 op0 = force_not_mem (op0);
918 op1 = force_not_mem (op1);
921 do_pending_stack_adjust ();
923 ucode = unsignedp ? unsigned_condition (code) : code;
924 tem = simplify_const_relational_operation (ucode, mode, op0, op1);
927 if (tem == const_true_rtx)
930 emit_jump (if_true_label);
935 emit_jump (if_false_label);
941 /* There's no need to do this now that combine.c can eliminate lots of
942 sign extensions. This can be less efficient in certain cases on other
945 /* If this is a signed equality comparison, we can do it as an
946 unsigned comparison since zero-extension is cheaper than sign
947 extension and comparisons with zero are done as unsigned. This is
948 the case even on machines that can do fast sign extension, since
949 zero-extension is easier to combine with other operations than
950 sign-extension is. If we are comparing against a constant, we must
951 convert it to what it would look like unsigned. */
952 if ((code == EQ || code == NE) && ! unsignedp
953 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
955 if (GET_CODE (op1) == CONST_INT
956 && (INTVAL (op1) & GET_MODE_MASK (GET_MODE (op0))) != INTVAL (op1))
957 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (GET_MODE (op0)));
964 dummy_true_label = 1;
965 if_true_label = gen_label_rtx ();
968 emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
972 emit_jump (if_false_label);
973 if (dummy_true_label)
974 emit_label (if_true_label);
977 /* Generate code for a comparison expression EXP (including code to compute
978 the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
979 IF_TRUE_LABEL. One of the labels can be NULL_RTX, in which case the
980 generated code will drop through.
981 SIGNED_CODE should be the rtx operation for this comparison for
982 signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
984 We force a stack adjustment unless there are currently
985 things pushed on the stack that aren't yet used. */
988 do_compare_and_jump (tree exp, enum rtx_code signed_code,
989 enum rtx_code unsigned_code, rtx if_false_label,
994 enum machine_mode mode;
998 /* Don't crash if the comparison was erroneous. */
999 op0 = expand_expr (TREE_OPERAND (exp, 0), NULL_RTX, VOIDmode, 0);
1000 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ERROR_MARK)
1003 op1 = expand_expr (TREE_OPERAND (exp, 1), NULL_RTX, VOIDmode, 0);
1004 if (TREE_CODE (TREE_OPERAND (exp, 1)) == ERROR_MARK)
1007 type = TREE_TYPE (TREE_OPERAND (exp, 0));
1008 mode = TYPE_MODE (type);
1009 if (TREE_CODE (TREE_OPERAND (exp, 0)) == INTEGER_CST
1010 && (TREE_CODE (TREE_OPERAND (exp, 1)) != INTEGER_CST
1011 || (GET_MODE_BITSIZE (mode)
1012 > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp,
1015 /* op0 might have been replaced by promoted constant, in which
1016 case the type of second argument should be used. */
1017 type = TREE_TYPE (TREE_OPERAND (exp, 1));
1018 mode = TYPE_MODE (type);
1020 unsignedp = TREE_UNSIGNED (type);
1021 code = unsignedp ? unsigned_code : signed_code;
1023 #ifdef HAVE_canonicalize_funcptr_for_compare
1024 /* If function pointers need to be "canonicalized" before they can
1025 be reliably compared, then canonicalize them. */
1026 if (HAVE_canonicalize_funcptr_for_compare
1027 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
1028 && (TREE_CODE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))))
1031 rtx new_op0 = gen_reg_rtx (mode);
1033 emit_insn (gen_canonicalize_funcptr_for_compare (new_op0, op0));
1037 if (HAVE_canonicalize_funcptr_for_compare
1038 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 1))) == POINTER_TYPE
1039 && (TREE_CODE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 1))))
1042 rtx new_op1 = gen_reg_rtx (mode);
1044 emit_insn (gen_canonicalize_funcptr_for_compare (new_op1, op1));
1049 /* Do any postincrements in the expression that was tested. */
1052 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1054 ? expr_size (TREE_OPERAND (exp, 0)) : NULL_RTX),
1055 if_false_label, if_true_label);
1058 #include "gt-dojump.h"