re PR middle-end/87188 (Function pointer canonicalization optimized away)
[platform/upstream/gcc.git] / gcc / dojump.c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2    Copyright (C) 1988-2018 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "predict.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "optabs.h"
31 #include "emit-rtl.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
35 #include "dojump.h"
36 #include "explow.h"
37 #include "expr.h"
38 #include "langhooks.h"
39
40 static bool prefer_and_bit_test (scalar_int_mode, int);
41 static void do_jump_by_parts_greater (scalar_int_mode, tree, tree, int,
42                                       rtx_code_label *, rtx_code_label *,
43                                       profile_probability);
44 static void do_jump_by_parts_equality (scalar_int_mode, tree, tree,
45                                        rtx_code_label *, rtx_code_label *,
46                                        profile_probability);
47 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code,
48                                  rtx_code_label *, rtx_code_label *,
49                                  profile_probability);
50
51 /* At the start of a function, record that we have no previously-pushed
52    arguments waiting to be popped.  */
53
54 void
55 init_pending_stack_adjust (void)
56 {
57   pending_stack_adjust = 0;
58 }
59
60 /* Discard any pending stack adjustment.  This avoid relying on the
61    RTL optimizers to remove useless adjustments when we know the
62    stack pointer value is dead.  */
63 void
64 discard_pending_stack_adjust (void)
65 {
66   stack_pointer_delta -= pending_stack_adjust;
67   pending_stack_adjust = 0;
68 }
69
70 /* When exiting from function, if safe, clear out any pending stack adjust
71    so the adjustment won't get done.
72
73    Note, if the current function calls alloca, then it must have a
74    frame pointer regardless of the value of flag_omit_frame_pointer.  */
75
76 void
77 clear_pending_stack_adjust (void)
78 {
79   if (optimize > 0
80       && (! flag_omit_frame_pointer || cfun->calls_alloca)
81       && EXIT_IGNORE_STACK)
82     discard_pending_stack_adjust ();
83 }
84
85 /* Pop any previously-pushed arguments that have not been popped yet.  */
86
87 void
88 do_pending_stack_adjust (void)
89 {
90   if (inhibit_defer_pop == 0)
91     {
92       if (maybe_ne (pending_stack_adjust, 0))
93         adjust_stack (gen_int_mode (pending_stack_adjust, Pmode));
94       pending_stack_adjust = 0;
95     }
96 }
97
98 /* Remember pending_stack_adjust/stack_pointer_delta.
99    To be used around code that may call do_pending_stack_adjust (),
100    but the generated code could be discarded e.g. using delete_insns_since.  */
101
102 void
103 save_pending_stack_adjust (saved_pending_stack_adjust *save)
104 {
105   save->x_pending_stack_adjust = pending_stack_adjust;
106   save->x_stack_pointer_delta = stack_pointer_delta;
107 }
108
109 /* Restore the saved pending_stack_adjust/stack_pointer_delta.  */
110
111 void
112 restore_pending_stack_adjust (saved_pending_stack_adjust *save)
113 {
114   if (inhibit_defer_pop == 0)
115     {
116       pending_stack_adjust = save->x_pending_stack_adjust;
117       stack_pointer_delta = save->x_stack_pointer_delta;
118     }
119 }
120 \f
121 /* Expand conditional expressions.  */
122
123 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.  */
124
125 void
126 jumpifnot (tree exp, rtx_code_label *label, profile_probability prob)
127 {
128   do_jump (exp, label, NULL, prob.invert ());
129 }
130
131 void
132 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx_code_label *label,
133              profile_probability prob)
134 {
135   do_jump_1 (code, op0, op1, label, NULL, prob.invert ());
136 }
137
138 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero.  */
139
140 void
141 jumpif (tree exp, rtx_code_label *label, profile_probability prob)
142 {
143   do_jump (exp, NULL, label, prob);
144 }
145
146 void
147 jumpif_1 (enum tree_code code, tree op0, tree op1,
148           rtx_code_label *label, profile_probability prob)
149 {
150   do_jump_1 (code, op0, op1, NULL, label, prob);
151 }
152
153 /* Used internally by prefer_and_bit_test.  */
154
155 static GTY(()) rtx and_reg;
156 static GTY(()) rtx and_test;
157 static GTY(()) rtx shift_test;
158
159 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
160    where X is an arbitrary register of mode MODE.  Return true if the former
161    is preferred.  */
162
163 static bool
164 prefer_and_bit_test (scalar_int_mode mode, int bitnum)
165 {
166   bool speed_p;
167   wide_int mask = wi::set_bit_in_zero (bitnum, GET_MODE_PRECISION (mode));
168
169   if (and_test == 0)
170     {
171       /* Set up rtxes for the two variations.  Use NULL as a placeholder
172          for the BITNUM-based constants.  */
173       and_reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
174       and_test = gen_rtx_AND (mode, and_reg, NULL);
175       shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
176                                 const1_rtx);
177     }
178   else
179     {
180       /* Change the mode of the previously-created rtxes.  */
181       PUT_MODE (and_reg, mode);
182       PUT_MODE (and_test, mode);
183       PUT_MODE (shift_test, mode);
184       PUT_MODE (XEXP (shift_test, 0), mode);
185     }
186
187   /* Fill in the integers.  */
188   XEXP (and_test, 1) = immed_wide_int_const (mask, mode);
189   XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
190
191   speed_p = optimize_insn_for_speed_p ();
192   return (rtx_cost (and_test, mode, IF_THEN_ELSE, 0, speed_p)
193           <= rtx_cost (shift_test, mode, IF_THEN_ELSE, 0, speed_p));
194 }
195
196 /* Subroutine of do_jump, dealing with exploded comparisons of the type
197    OP0 CODE OP1 .  IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
198    PROB is probability of jump to if_true_label.  */
199
200 void
201 do_jump_1 (enum tree_code code, tree op0, tree op1,
202            rtx_code_label *if_false_label, rtx_code_label *if_true_label,
203            profile_probability prob)
204 {
205   machine_mode mode;
206   rtx_code_label *drop_through_label = 0;
207   scalar_int_mode int_mode;
208
209   switch (code)
210     {
211     case EQ_EXPR:
212       {
213         tree inner_type = TREE_TYPE (op0);
214
215         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
216                     != MODE_COMPLEX_FLOAT);
217         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
218                     != MODE_COMPLEX_INT);
219
220         if (integer_zerop (op1))
221           do_jump (op0, if_true_label, if_false_label,
222                    prob.invert ());
223         else if (is_int_mode (TYPE_MODE (inner_type), &int_mode)
224                  && !can_compare_p (EQ, int_mode, ccp_jump))
225           do_jump_by_parts_equality (int_mode, op0, op1, if_false_label,
226                                      if_true_label, prob);
227         else
228           do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
229                                prob);
230         break;
231       }
232
233     case NE_EXPR:
234       {
235         tree inner_type = TREE_TYPE (op0);
236
237         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
238                     != MODE_COMPLEX_FLOAT);
239         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
240                     != MODE_COMPLEX_INT);
241
242         if (integer_zerop (op1))
243           do_jump (op0, if_false_label, if_true_label, prob);
244         else if (is_int_mode (TYPE_MODE (inner_type), &int_mode)
245                  && !can_compare_p (NE, int_mode, ccp_jump))
246           do_jump_by_parts_equality (int_mode, op0, op1, if_true_label,
247                                      if_false_label, prob.invert ());
248         else
249           do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
250                                prob);
251         break;
252       }
253
254     case LT_EXPR:
255       mode = TYPE_MODE (TREE_TYPE (op0));
256       if (is_int_mode (mode, &int_mode)
257           && ! can_compare_p (LT, int_mode, ccp_jump))
258         do_jump_by_parts_greater (int_mode, op0, op1, 1, if_false_label,
259                                   if_true_label, prob);
260       else
261         do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
262                              prob);
263       break;
264
265     case LE_EXPR:
266       mode = TYPE_MODE (TREE_TYPE (op0));
267       if (is_int_mode (mode, &int_mode)
268           && ! can_compare_p (LE, int_mode, ccp_jump))
269         do_jump_by_parts_greater (int_mode, op0, op1, 0, if_true_label,
270                                   if_false_label, prob.invert ());
271       else
272         do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
273                              prob);
274       break;
275
276     case GT_EXPR:
277       mode = TYPE_MODE (TREE_TYPE (op0));
278       if (is_int_mode (mode, &int_mode)
279           && ! can_compare_p (GT, int_mode, ccp_jump))
280         do_jump_by_parts_greater (int_mode, op0, op1, 0, if_false_label,
281                                   if_true_label, prob);
282       else
283         do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
284                              prob);
285       break;
286
287     case GE_EXPR:
288       mode = TYPE_MODE (TREE_TYPE (op0));
289       if (is_int_mode (mode, &int_mode)
290           && ! can_compare_p (GE, int_mode, ccp_jump))
291         do_jump_by_parts_greater (int_mode, op0, op1, 1, if_true_label,
292                                   if_false_label, prob.invert ());
293       else
294         do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
295                              prob);
296       break;
297
298     case ORDERED_EXPR:
299       do_compare_and_jump (op0, op1, ORDERED, ORDERED,
300                            if_false_label, if_true_label, prob);
301       break;
302
303     case UNORDERED_EXPR:
304       do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
305                            if_false_label, if_true_label, prob);
306       break;
307
308     case UNLT_EXPR:
309       do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
310                            prob);
311       break;
312
313     case UNLE_EXPR:
314       do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
315                            prob);
316       break;
317
318     case UNGT_EXPR:
319       do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
320                            prob);
321       break;
322
323     case UNGE_EXPR:
324       do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
325                            prob);
326       break;
327
328     case UNEQ_EXPR:
329       do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
330                            prob);
331       break;
332
333     case LTGT_EXPR:
334       do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
335                            prob);
336       break;
337
338     case TRUTH_ANDIF_EXPR:
339       {
340         /* Spread the probability that the expression is false evenly between
341            the two conditions. So the first condition is false half the total
342            probability of being false. The second condition is false the other
343            half of the total probability of being false, so its jump has a false
344            probability of half the total, relative to the probability we
345            reached it (i.e. the first condition was true).  */
346         profile_probability op0_prob = profile_probability::uninitialized ();
347         profile_probability op1_prob = profile_probability::uninitialized ();
348         if (prob.initialized_p ())
349           {
350             op1_prob = prob.invert ();
351             op0_prob = op1_prob.split (profile_probability::even ());
352             /* Get the probability that each jump below is true.  */
353             op0_prob = op0_prob.invert ();
354             op1_prob = op1_prob.invert ();
355           }
356         if (if_false_label == NULL)
357           {
358             drop_through_label = gen_label_rtx ();
359             do_jump (op0, drop_through_label, NULL, op0_prob);
360             do_jump (op1, NULL, if_true_label, op1_prob);
361           }
362         else
363           {
364             do_jump (op0, if_false_label, NULL, op0_prob);
365             do_jump (op1, if_false_label, if_true_label, op1_prob);
366           }
367         break;
368       }
369
370     case TRUTH_ORIF_EXPR:
371       {
372         /* Spread the probability evenly between the two conditions. So
373            the first condition has half the total probability of being true.
374            The second condition has the other half of the total probability,
375            so its jump has a probability of half the total, relative to
376            the probability we reached it (i.e. the first condition was false).  */
377         profile_probability op0_prob = profile_probability::uninitialized ();
378         profile_probability op1_prob = profile_probability::uninitialized ();
379         if (prob.initialized_p ())
380           {
381             op1_prob = prob;
382             op0_prob = op1_prob.split (profile_probability::even ());
383           }
384         if (if_true_label == NULL)
385           {
386             drop_through_label = gen_label_rtx ();
387             do_jump (op0, NULL, drop_through_label, op0_prob);
388             do_jump (op1, if_false_label, NULL, op1_prob);
389           }
390         else
391           {
392             do_jump (op0, NULL, if_true_label, op0_prob);
393             do_jump (op1, if_false_label, if_true_label, op1_prob);
394           }
395         break;
396       }
397
398     default:
399       gcc_unreachable ();
400     }
401
402   if (drop_through_label)
403     {
404       do_pending_stack_adjust ();
405       emit_label (drop_through_label);
406     }
407 }
408
409 /* Generate code to evaluate EXP and jump to IF_FALSE_LABEL if
410    the result is zero, or IF_TRUE_LABEL if the result is one.
411    Either of IF_FALSE_LABEL and IF_TRUE_LABEL may be zero,
412    meaning fall through in that case.
413
414    do_jump always does any pending stack adjust except when it does not
415    actually perform a jump.  An example where there is no jump
416    is when EXP is `(foo (), 0)' and IF_FALSE_LABEL is null.
417
418    PROB is probability of jump to if_true_label.  */
419
420 void
421 do_jump (tree exp, rtx_code_label *if_false_label,
422          rtx_code_label *if_true_label, profile_probability prob)
423 {
424   enum tree_code code = TREE_CODE (exp);
425   rtx temp;
426   int i;
427   tree type;
428   scalar_int_mode mode;
429   rtx_code_label *drop_through_label = NULL;
430
431   switch (code)
432     {
433     case ERROR_MARK:
434       break;
435
436     case INTEGER_CST:
437       {
438         rtx_code_label *lab = integer_zerop (exp) ? if_false_label
439                                                   : if_true_label;
440         if (lab)
441           emit_jump (lab);
442         break;
443       }
444
445 #if 0
446       /* This is not true with #pragma weak  */
447     case ADDR_EXPR:
448       /* The address of something can never be zero.  */
449       if (if_true_label)
450         emit_jump (if_true_label);
451       break;
452 #endif
453
454     case NOP_EXPR:
455       if (TREE_CODE (TREE_OPERAND (exp, 0)) == COMPONENT_REF
456           || TREE_CODE (TREE_OPERAND (exp, 0)) == BIT_FIELD_REF
457           || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_REF
458           || TREE_CODE (TREE_OPERAND (exp, 0)) == ARRAY_RANGE_REF)
459         goto normal;
460       /* FALLTHRU */
461     case CONVERT_EXPR:
462       /* If we are narrowing the operand, we have to do the compare in the
463          narrower mode.  */
464       if ((TYPE_PRECISION (TREE_TYPE (exp))
465            < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0)))))
466         goto normal;
467       /* FALLTHRU */
468     case NON_LVALUE_EXPR:
469     case ABS_EXPR:
470     case ABSU_EXPR:
471     case NEGATE_EXPR:
472     case LROTATE_EXPR:
473     case RROTATE_EXPR:
474       /* These cannot change zero->nonzero or vice versa.  */
475       do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
476       break;
477
478     case TRUTH_NOT_EXPR:
479       do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
480                prob.invert ());
481       break;
482
483     case COND_EXPR:
484       {
485         rtx_code_label *label1 = gen_label_rtx ();
486         if (!if_true_label || !if_false_label)
487           {
488             drop_through_label = gen_label_rtx ();
489             if (!if_true_label)
490               if_true_label = drop_through_label;
491             if (!if_false_label)
492               if_false_label = drop_through_label;
493           }
494
495         do_pending_stack_adjust ();
496         do_jump (TREE_OPERAND (exp, 0), label1, NULL,
497                  profile_probability::uninitialized ());
498         do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
499         emit_label (label1);
500         do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
501         break;
502       }
503
504     case COMPOUND_EXPR:
505       /* Lowered by gimplify.c.  */
506       gcc_unreachable ();
507
508     case MINUS_EXPR:
509       /* Nonzero iff operands of minus differ.  */
510       code = NE_EXPR;
511
512       /* FALLTHRU */
513     case EQ_EXPR:
514     case NE_EXPR:
515     case LT_EXPR:
516     case LE_EXPR:
517     case GT_EXPR:
518     case GE_EXPR:
519     case ORDERED_EXPR:
520     case UNORDERED_EXPR:
521     case UNLT_EXPR:
522     case UNLE_EXPR:
523     case UNGT_EXPR:
524     case UNGE_EXPR:
525     case UNEQ_EXPR:
526     case LTGT_EXPR:
527     case TRUTH_ANDIF_EXPR:
528     case TRUTH_ORIF_EXPR:
529     other_code:
530       do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
531                  if_false_label, if_true_label, prob);
532       break;
533
534     case BIT_AND_EXPR:
535       /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
536          See if the former is preferred for jump tests and restore it
537          if so.  */
538       if (integer_onep (TREE_OPERAND (exp, 1)))
539         {
540           tree exp0 = TREE_OPERAND (exp, 0);
541           rtx_code_label *set_label, *clr_label;
542           profile_probability setclr_prob = prob;
543
544           /* Strip narrowing integral type conversions.  */
545           while (CONVERT_EXPR_P (exp0)
546                  && TREE_OPERAND (exp0, 0) != error_mark_node
547                  && TYPE_PRECISION (TREE_TYPE (exp0))
548                     <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
549             exp0 = TREE_OPERAND (exp0, 0);
550
551           /* "exp0 ^ 1" inverts the sense of the single bit test.  */
552           if (TREE_CODE (exp0) == BIT_XOR_EXPR
553               && integer_onep (TREE_OPERAND (exp0, 1)))
554             {
555               exp0 = TREE_OPERAND (exp0, 0);
556               clr_label = if_true_label;
557               set_label = if_false_label;
558               setclr_prob = prob.invert ();
559             }
560           else
561             {
562               clr_label = if_false_label;
563               set_label = if_true_label;
564             }
565
566           if (TREE_CODE (exp0) == RSHIFT_EXPR)
567             {
568               tree arg = TREE_OPERAND (exp0, 0);
569               tree shift = TREE_OPERAND (exp0, 1);
570               tree argtype = TREE_TYPE (arg);
571               if (TREE_CODE (shift) == INTEGER_CST
572                   && compare_tree_int (shift, 0) >= 0
573                   && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
574                   && prefer_and_bit_test (SCALAR_INT_TYPE_MODE (argtype),
575                                           TREE_INT_CST_LOW (shift)))
576                 {
577                   unsigned HOST_WIDE_INT mask
578                     = HOST_WIDE_INT_1U << TREE_INT_CST_LOW (shift);
579                   do_jump (build2 (BIT_AND_EXPR, argtype, arg,
580                                    build_int_cstu (argtype, mask)),
581                            clr_label, set_label, setclr_prob);
582                   break;
583                 }
584             }
585         }
586
587       /* If we are AND'ing with a small constant, do this comparison in the
588          smallest type that fits.  If the machine doesn't have comparisons
589          that small, it will be converted back to the wider comparison.
590          This helps if we are testing the sign bit of a narrower object.
591          combine can't do this for us because it can't know whether a
592          ZERO_EXTRACT or a compare in a smaller mode exists, but we do.  */
593
594       if (! SLOW_BYTE_ACCESS
595           && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
596           && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
597           && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
598           && int_mode_for_size (i + 1, 0).exists (&mode)
599           && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
600           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
601           && have_insn_for (COMPARE, TYPE_MODE (type)))
602         {
603           do_jump (fold_convert (type, exp), if_false_label, if_true_label,
604                    prob);
605           break;
606         }
607
608       if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
609           || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
610         goto normal;
611
612       /* Boolean comparisons can be compiled as TRUTH_AND_EXPR.  */
613       /* FALLTHRU */
614
615     case TRUTH_AND_EXPR:
616       /* High branch cost, expand as the bitwise AND of the conditions.
617          Do the same if the RHS has side effects, because we're effectively
618          turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR.  */
619       if (BRANCH_COST (optimize_insn_for_speed_p (),
620                        false) >= 4
621           || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
622         goto normal;
623       code = TRUTH_ANDIF_EXPR;
624       goto other_code;
625
626     case BIT_IOR_EXPR:
627     case TRUTH_OR_EXPR:
628       /* High branch cost, expand as the bitwise OR of the conditions.
629          Do the same if the RHS has side effects, because we're effectively
630          turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR.  */
631       if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
632           || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
633         goto normal;
634       code = TRUTH_ORIF_EXPR;
635       goto other_code;
636
637       /* Fall through and generate the normal code.  */
638     default:
639     normal:
640       temp = expand_normal (exp);
641       do_pending_stack_adjust ();
642       /* The RTL optimizers prefer comparisons against pseudos.  */
643       if (GET_CODE (temp) == SUBREG)
644         {
645           /* Compare promoted variables in their promoted mode.  */
646           if (SUBREG_PROMOTED_VAR_P (temp)
647               && REG_P (XEXP (temp, 0)))
648             temp = XEXP (temp, 0);
649           else
650             temp = copy_to_reg (temp);
651         }
652       do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
653                                NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
654                                GET_MODE (temp), NULL_RTX,
655                                if_false_label, if_true_label, prob);
656     }
657
658   if (drop_through_label)
659     {
660       do_pending_stack_adjust ();
661       emit_label (drop_through_label);
662     }
663 }
664 \f
665 /* Compare OP0 with OP1, word at a time, in mode MODE.
666    UNSIGNEDP says to do unsigned comparison.
667    Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise.  */
668
669 static void
670 do_jump_by_parts_greater_rtx (scalar_int_mode mode, int unsignedp, rtx op0,
671                               rtx op1, rtx_code_label *if_false_label,
672                               rtx_code_label *if_true_label,
673                               profile_probability prob)
674 {
675   int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
676   rtx_code_label *drop_through_label = 0;
677   bool drop_through_if_true = false, drop_through_if_false = false;
678   enum rtx_code code = GT;
679   int i;
680
681   if (! if_true_label || ! if_false_label)
682     drop_through_label = gen_label_rtx ();
683   if (! if_true_label)
684     {
685       if_true_label = drop_through_label;
686       drop_through_if_true = true;
687     }
688   if (! if_false_label)
689     {
690       if_false_label = drop_through_label;
691       drop_through_if_false = true;
692     }
693
694   /* Deal with the special case 0 > x: only one comparison is necessary and
695      we reverse it to avoid jumping to the drop-through label.  */
696   if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
697     {
698       code = LE;
699       if_true_label = if_false_label;
700       if_false_label = drop_through_label;
701       drop_through_if_true = false;
702       drop_through_if_false = true;
703       prob = prob.invert ();
704     }
705
706   /* Compare a word at a time, high order first.  */
707   for (i = 0; i < nwords; i++)
708     {
709       rtx op0_word, op1_word;
710
711       if (WORDS_BIG_ENDIAN)
712         {
713           op0_word = operand_subword_force (op0, i, mode);
714           op1_word = operand_subword_force (op1, i, mode);
715         }
716       else
717         {
718           op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
719           op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
720         }
721
722       /* All but high-order word must be compared as unsigned.  */
723       do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
724                                word_mode, NULL_RTX, NULL, if_true_label,
725                                prob);
726
727       /* Emit only one comparison for 0.  Do not emit the last cond jump.  */
728       if (op0 == const0_rtx || i == nwords - 1)
729         break;
730
731       /* Consider lower words only if these are equal.  */
732       do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
733                                NULL_RTX, NULL, if_false_label,
734                                prob.invert ());
735     }
736
737   if (!drop_through_if_false)
738     emit_jump (if_false_label);
739   if (drop_through_label)
740     emit_label (drop_through_label);
741 }
742
743 /* Given a comparison expression EXP for values too wide to be compared
744    with one insn, test the comparison and jump to the appropriate label.
745    The code of EXP is ignored; we always test GT if SWAP is 0,
746    and LT if SWAP is 1.  MODE is the mode of the two operands.  */
747
748 static void
749 do_jump_by_parts_greater (scalar_int_mode mode, tree treeop0, tree treeop1,
750                           int swap, rtx_code_label *if_false_label,
751                           rtx_code_label *if_true_label,
752                           profile_probability prob)
753 {
754   rtx op0 = expand_normal (swap ? treeop1 : treeop0);
755   rtx op1 = expand_normal (swap ? treeop0 : treeop1);
756   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
757
758   do_jump_by_parts_greater_rtx (mode, unsignedp, op0, op1, if_false_label,
759                                 if_true_label, prob);
760 }
761 \f
762 /* Jump according to whether OP0 is 0.  We assume that OP0 has an integer
763    mode, MODE, that is too wide for the available compare insns.  Either
764    Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL
765    to indicate drop through.  */
766
767 static void
768 do_jump_by_parts_zero_rtx (scalar_int_mode mode, rtx op0,
769                            rtx_code_label *if_false_label,
770                            rtx_code_label *if_true_label,
771                            profile_probability prob)
772 {
773   int nwords = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
774   rtx part;
775   int i;
776   rtx_code_label *drop_through_label = NULL;
777
778   /* The fastest way of doing this comparison on almost any machine is to
779      "or" all the words and compare the result.  If all have to be loaded
780      from memory and this is a very wide item, it's possible this may
781      be slower, but that's highly unlikely.  */
782
783   part = gen_reg_rtx (word_mode);
784   emit_move_insn (part, operand_subword_force (op0, 0, mode));
785   for (i = 1; i < nwords && part != 0; i++)
786     part = expand_binop (word_mode, ior_optab, part,
787                          operand_subword_force (op0, i, mode),
788                          part, 1, OPTAB_WIDEN);
789
790   if (part != 0)
791     {
792       do_compare_rtx_and_jump (part, const0_rtx, EQ, 1, word_mode,
793                                NULL_RTX, if_false_label, if_true_label, prob);
794       return;
795     }
796
797   /* If we couldn't do the "or" simply, do this with a series of compares.  */
798   if (! if_false_label)
799     if_false_label = drop_through_label = gen_label_rtx ();
800
801   for (i = 0; i < nwords; i++)
802     do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
803                              const0_rtx, EQ, 1, word_mode, NULL_RTX,
804                              if_false_label, NULL, prob);
805
806   if (if_true_label)
807     emit_jump (if_true_label);
808
809   if (drop_through_label)
810     emit_label (drop_through_label);
811 }
812
813 /* Test for the equality of two RTX expressions OP0 and OP1 in mode MODE,
814    where MODE is an integer mode too wide to be compared with one insn.
815    Either (but not both) of IF_TRUE_LABEL and IF_FALSE_LABEL may be NULL_RTX
816    to indicate drop through.  */
817
818 static void
819 do_jump_by_parts_equality_rtx (scalar_int_mode mode, rtx op0, rtx op1,
820                                rtx_code_label *if_false_label,
821                                rtx_code_label *if_true_label,
822                                profile_probability prob)
823 {
824   int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
825   rtx_code_label *drop_through_label = NULL;
826   int i;
827
828   if (op1 == const0_rtx)
829     {
830       do_jump_by_parts_zero_rtx (mode, op0, if_false_label, if_true_label,
831                                  prob);
832       return;
833     }
834   else if (op0 == const0_rtx)
835     {
836       do_jump_by_parts_zero_rtx (mode, op1, if_false_label, if_true_label,
837                                  prob);
838       return;
839     }
840
841   if (! if_false_label)
842     drop_through_label = if_false_label = gen_label_rtx ();
843
844   for (i = 0; i < nwords; i++)
845     do_compare_rtx_and_jump (operand_subword_force (op0, i, mode),
846                              operand_subword_force (op1, i, mode),
847                              EQ, 0, word_mode, NULL_RTX,
848                              if_false_label, NULL, prob);
849
850   if (if_true_label)
851     emit_jump (if_true_label);
852   if (drop_through_label)
853     emit_label (drop_through_label);
854 }
855
856 /* Given an EQ_EXPR expression EXP for values too wide to be compared
857    with one insn, test the comparison and jump to the appropriate label.
858    MODE is the mode of the two operands.  */
859
860 static void
861 do_jump_by_parts_equality (scalar_int_mode mode, tree treeop0, tree treeop1,
862                            rtx_code_label *if_false_label,
863                            rtx_code_label *if_true_label,
864                            profile_probability prob)
865 {
866   rtx op0 = expand_normal (treeop0);
867   rtx op1 = expand_normal (treeop1);
868   do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
869                                  if_true_label, prob);
870 }
871 \f
872 /* Split a comparison into two others, the second of which has the other
873    "orderedness".  The first is always ORDERED or UNORDERED if MODE
874    does not honor NaNs (which means that it can be skipped in that case;
875    see do_compare_rtx_and_jump).
876
877    The two conditions are written in *CODE1 and *CODE2.  Return true if
878    the conditions must be ANDed, false if they must be ORed.  */
879
880 bool
881 split_comparison (enum rtx_code code, machine_mode mode,
882                   enum rtx_code *code1, enum rtx_code *code2)
883 {
884   switch (code)
885     {
886     case LT:
887       *code1 = ORDERED;
888       *code2 = UNLT;
889       return true;
890     case LE:
891       *code1 = ORDERED;
892       *code2 = UNLE;
893       return true;
894     case GT:
895       *code1 = ORDERED;
896       *code2 = UNGT;
897       return true;
898     case GE:
899       *code1 = ORDERED;
900       *code2 = UNGE;
901       return true;
902     case EQ:
903       *code1 = ORDERED;
904       *code2 = UNEQ;
905       return true;
906     case NE:
907       *code1 = UNORDERED;
908       *code2 = LTGT;
909       return false;
910     case UNLT:
911       *code1 = UNORDERED;
912       *code2 = LT;
913       return false;
914     case UNLE:
915       *code1 = UNORDERED;
916       *code2 = LE;
917       return false;
918     case UNGT:
919       *code1 = UNORDERED;
920       *code2 = GT;
921       return false;
922     case UNGE:
923       *code1 = UNORDERED;
924       *code2 = GE;
925       return false;
926     case UNEQ:
927       *code1 = UNORDERED;
928       *code2 = EQ;
929       return false;
930     case LTGT:
931       /* Do not turn a trapping comparison into a non-trapping one.  */
932       if (HONOR_SNANS (mode))
933         {
934           *code1 = LT;
935           *code2 = GT;
936           return false;
937         }
938       else
939         {
940           *code1 = ORDERED;
941           *code2 = NE;
942           return true;
943         }
944     default:
945       gcc_unreachable ();
946     }
947 }
948
949
950 /* Like do_compare_and_jump but expects the values to compare as two rtx's.
951    The decision as to signed or unsigned comparison must be made by the caller.
952
953    If MODE is BLKmode, SIZE is an RTX giving the size of the objects being
954    compared.  */
955
956 void
957 do_compare_rtx_and_jump (rtx op0, rtx op1, enum rtx_code code, int unsignedp,
958                          machine_mode mode, rtx size,
959                          rtx_code_label *if_false_label,
960                          rtx_code_label *if_true_label,
961                          profile_probability prob)
962 {
963   rtx tem;
964   rtx_code_label *dummy_label = NULL;
965
966   /* Reverse the comparison if that is safe and we want to jump if it is
967      false.  Also convert to the reverse comparison if the target can
968      implement it.  */
969   if ((! if_true_label
970        || ! can_compare_p (code, mode, ccp_jump))
971       && (! FLOAT_MODE_P (mode)
972           || code == ORDERED || code == UNORDERED
973           || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
974           || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
975     {
976       enum rtx_code rcode;
977       if (FLOAT_MODE_P (mode))
978         rcode = reverse_condition_maybe_unordered (code);
979       else
980         rcode = reverse_condition (code);
981
982       /* Canonicalize to UNORDERED for the libcall.  */
983       if (can_compare_p (rcode, mode, ccp_jump)
984           || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
985         {
986           std::swap (if_true_label, if_false_label);
987           code = rcode;
988           prob = prob.invert ();
989         }
990     }
991
992   /* If one operand is constant, make it the second one.  Only do this
993      if the other operand is not constant as well.  */
994
995   if (swap_commutative_operands_p (op0, op1))
996     {
997       std::swap (op0, op1);
998       code = swap_condition (code);
999     }
1000
1001   do_pending_stack_adjust ();
1002
1003   code = unsignedp ? unsigned_condition (code) : code;
1004   if ((tem = simplify_relational_operation (code, mode, VOIDmode,
1005                                             op0, op1)) != 0)
1006     {
1007       if (CONSTANT_P (tem))
1008         {
1009           rtx_code_label *label = (tem == const0_rtx
1010                                    || tem == CONST0_RTX (mode))
1011                                         ? if_false_label : if_true_label;
1012           if (label)
1013             emit_jump (label);
1014           return;
1015         }
1016
1017       code = GET_CODE (tem);
1018       mode = GET_MODE (tem);
1019       op0 = XEXP (tem, 0);
1020       op1 = XEXP (tem, 1);
1021       unsignedp = (code == GTU || code == LTU || code == GEU || code == LEU);
1022     }
1023
1024   if (! if_true_label)
1025     dummy_label = if_true_label = gen_label_rtx ();
1026
1027   scalar_int_mode int_mode;
1028   if (is_int_mode (mode, &int_mode)
1029       && ! can_compare_p (code, int_mode, ccp_jump))
1030     {
1031       switch (code)
1032         {
1033         case LTU:
1034           do_jump_by_parts_greater_rtx (int_mode, 1, op1, op0,
1035                                         if_false_label, if_true_label, prob);
1036           break;
1037
1038         case LEU:
1039           do_jump_by_parts_greater_rtx (int_mode, 1, op0, op1,
1040                                         if_true_label, if_false_label,
1041                                         prob.invert ());
1042           break;
1043
1044         case GTU:
1045           do_jump_by_parts_greater_rtx (int_mode, 1, op0, op1,
1046                                         if_false_label, if_true_label, prob);
1047           break;
1048
1049         case GEU:
1050           do_jump_by_parts_greater_rtx (int_mode, 1, op1, op0,
1051                                         if_true_label, if_false_label,
1052                                         prob.invert ());
1053           break;
1054
1055         case LT:
1056           do_jump_by_parts_greater_rtx (int_mode, 0, op1, op0,
1057                                         if_false_label, if_true_label, prob);
1058           break;
1059
1060         case LE:
1061           do_jump_by_parts_greater_rtx (int_mode, 0, op0, op1,
1062                                         if_true_label, if_false_label,
1063                                         prob.invert ());
1064           break;
1065
1066         case GT:
1067           do_jump_by_parts_greater_rtx (int_mode, 0, op0, op1,
1068                                         if_false_label, if_true_label, prob);
1069           break;
1070
1071         case GE:
1072           do_jump_by_parts_greater_rtx (int_mode, 0, op1, op0,
1073                                         if_true_label, if_false_label,
1074                                         prob.invert ());
1075           break;
1076
1077         case EQ:
1078           do_jump_by_parts_equality_rtx (int_mode, op0, op1, if_false_label,
1079                                          if_true_label, prob);
1080           break;
1081
1082         case NE:
1083           do_jump_by_parts_equality_rtx (int_mode, op0, op1, if_true_label,
1084                                          if_false_label,
1085                                          prob.invert ());
1086           break;
1087
1088         default:
1089           gcc_unreachable ();
1090         }
1091     }
1092   else
1093     {
1094       if (SCALAR_FLOAT_MODE_P (mode)
1095           && ! can_compare_p (code, mode, ccp_jump)
1096           && can_compare_p (swap_condition (code), mode, ccp_jump))
1097         {
1098           code = swap_condition (code);
1099           std::swap (op0, op1);
1100         }
1101       else if (SCALAR_FLOAT_MODE_P (mode)
1102                && ! can_compare_p (code, mode, ccp_jump)
1103                /* Never split ORDERED and UNORDERED.
1104                   These must be implemented.  */
1105                && (code != ORDERED && code != UNORDERED)
1106                /* Split a floating-point comparison if
1107                   we can jump on other conditions...  */
1108                && (have_insn_for (COMPARE, mode)
1109                    /* ... or if there is no libcall for it.  */
1110                    || code_to_optab (code) == unknown_optab))
1111         {
1112           enum rtx_code first_code;
1113           bool and_them = split_comparison (code, mode, &first_code, &code);
1114
1115           /* If there are no NaNs, the first comparison should always fall
1116              through.  */
1117           if (!HONOR_NANS (mode))
1118             gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1119
1120           else
1121             {
1122               profile_probability cprob
1123                 = profile_probability::guessed_always ();
1124               if (first_code == UNORDERED)
1125                 cprob = cprob.apply_scale (1, 100);
1126               else if (first_code == ORDERED)
1127                 cprob = cprob.apply_scale (99, 100);
1128               else
1129                 cprob = profile_probability::even ();
1130               /* We want to split:
1131                  if (x) goto t; // prob;
1132                  into
1133                  if (a) goto t; // first_prob;
1134                  if (b) goto t; // prob;
1135                  such that the overall probability of jumping to t
1136                  remains the same and first_prob is prob * cprob.  */
1137               if (and_them)
1138                 {
1139                   rtx_code_label *dest_label;
1140                   prob = prob.invert ();
1141                   profile_probability first_prob = prob.split (cprob).invert ();
1142                   prob = prob.invert ();
1143                   /* If we only jump if true, just bypass the second jump.  */
1144                   if (! if_false_label)
1145                     {
1146                       if (! dummy_label)
1147                         dummy_label = gen_label_rtx ();
1148                       dest_label = dummy_label;
1149                     }
1150                   else
1151                     dest_label = if_false_label;
1152                   do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1153                                            size, dest_label, NULL, first_prob);
1154                 }
1155               else
1156                 {
1157                   profile_probability first_prob = prob.split (cprob);
1158                   do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1159                                            size, NULL, if_true_label, first_prob);
1160                 }
1161             }
1162         }
1163
1164       emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1165                                if_true_label, prob);
1166     }
1167
1168   if (if_false_label)
1169     emit_jump (if_false_label);
1170   if (dummy_label)
1171     emit_label (dummy_label);
1172 }
1173
1174 /* Generate code for a comparison expression EXP (including code to compute
1175    the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1176    IF_TRUE_LABEL.  One of the labels can be NULL_RTX, in which case the
1177    generated code will drop through.
1178    SIGNED_CODE should be the rtx operation for this comparison for
1179    signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1180
1181    We force a stack adjustment unless there are currently
1182    things pushed on the stack that aren't yet used.  */
1183
1184 static void
1185 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1186                      enum rtx_code unsigned_code,
1187                      rtx_code_label *if_false_label,
1188                      rtx_code_label *if_true_label, profile_probability prob)
1189 {
1190   rtx op0, op1;
1191   tree type;
1192   machine_mode mode;
1193   int unsignedp;
1194   enum rtx_code code;
1195
1196   /* Don't crash if the comparison was erroneous.  */
1197   op0 = expand_normal (treeop0);
1198   if (TREE_CODE (treeop0) == ERROR_MARK)
1199     return;
1200
1201   op1 = expand_normal (treeop1);
1202   if (TREE_CODE (treeop1) == ERROR_MARK)
1203     return;
1204
1205   type = TREE_TYPE (treeop0);
1206   if (TREE_CODE (treeop0) == INTEGER_CST
1207       && (TREE_CODE (treeop1) != INTEGER_CST
1208           || (GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type))
1209               > GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (treeop1))))))
1210     /* op0 might have been replaced by promoted constant, in which
1211        case the type of second argument should be used.  */
1212     type = TREE_TYPE (treeop1);
1213   mode = TYPE_MODE (type);
1214   unsignedp = TYPE_UNSIGNED (type);
1215   code = unsignedp ? unsigned_code : signed_code;
1216
1217   /* If function pointers need to be "canonicalized" before they can
1218      be reliably compared, then canonicalize them.  Canonicalize the
1219      expression when one of the operands is a function pointer.  This
1220      handles the case where the other operand is a void pointer.  See
1221      PR middle-end/17564.  */
1222   if (targetm.have_canonicalize_funcptr_for_compare ()
1223       && ((POINTER_TYPE_P (TREE_TYPE (treeop0))
1224            && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0))))
1225           || (POINTER_TYPE_P (TREE_TYPE (treeop1))
1226               && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (treeop1))))))
1227     {
1228       rtx new_op0 = gen_reg_rtx (mode);
1229       rtx new_op1 = gen_reg_rtx (mode);
1230
1231       emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op0, op0));
1232       op0 = new_op0;
1233
1234       emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op1, op1));
1235       op1 = new_op1;
1236     }
1237
1238   do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1239                            ((mode == BLKmode)
1240                             ? expr_size (treeop0) : NULL_RTX),
1241                            if_false_label, if_true_label, prob);
1242 }
1243
1244 #include "gt-dojump.h"