asan.c (asan_emit_stack_protection): Update.
[platform/upstream/gcc.git] / gcc / dojump.c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2    Copyright (C) 1988-2017 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 (machine_mode, int);
41 static void do_jump_by_parts_greater (tree, tree, int,
42                                       rtx_code_label *, rtx_code_label *,
43                                       profile_probability);
44 static void do_jump_by_parts_equality (tree, tree, rtx_code_label *,
45                                        rtx_code_label *, profile_probability);
46 static void do_compare_and_jump (tree, tree, enum rtx_code, enum rtx_code,
47                                  rtx_code_label *, rtx_code_label *,
48                                  profile_probability);
49
50 /* At the start of a function, record that we have no previously-pushed
51    arguments waiting to be popped.  */
52
53 void
54 init_pending_stack_adjust (void)
55 {
56   pending_stack_adjust = 0;
57 }
58
59 /* Discard any pending stack adjustment.  This avoid relying on the
60    RTL optimizers to remove useless adjustments when we know the
61    stack pointer value is dead.  */
62 void
63 discard_pending_stack_adjust (void)
64 {
65   stack_pointer_delta -= pending_stack_adjust;
66   pending_stack_adjust = 0;
67 }
68
69 /* When exiting from function, if safe, clear out any pending stack adjust
70    so the adjustment won't get done.
71
72    Note, if the current function calls alloca, then it must have a
73    frame pointer regardless of the value of flag_omit_frame_pointer.  */
74
75 void
76 clear_pending_stack_adjust (void)
77 {
78   if (optimize > 0
79       && (! flag_omit_frame_pointer || cfun->calls_alloca)
80       && EXIT_IGNORE_STACK)
81     discard_pending_stack_adjust ();
82 }
83
84 /* Pop any previously-pushed arguments that have not been popped yet.  */
85
86 void
87 do_pending_stack_adjust (void)
88 {
89   if (inhibit_defer_pop == 0)
90     {
91       if (pending_stack_adjust != 0)
92         adjust_stack (GEN_INT (pending_stack_adjust));
93       pending_stack_adjust = 0;
94     }
95 }
96
97 /* Remember pending_stack_adjust/stack_pointer_delta.
98    To be used around code that may call do_pending_stack_adjust (),
99    but the generated code could be discarded e.g. using delete_insns_since.  */
100
101 void
102 save_pending_stack_adjust (saved_pending_stack_adjust *save)
103 {
104   save->x_pending_stack_adjust = pending_stack_adjust;
105   save->x_stack_pointer_delta = stack_pointer_delta;
106 }
107
108 /* Restore the saved pending_stack_adjust/stack_pointer_delta.  */
109
110 void
111 restore_pending_stack_adjust (saved_pending_stack_adjust *save)
112 {
113   if (inhibit_defer_pop == 0)
114     {
115       pending_stack_adjust = save->x_pending_stack_adjust;
116       stack_pointer_delta = save->x_stack_pointer_delta;
117     }
118 }
119 \f
120 /* Expand conditional expressions.  */
121
122 /* Generate code to evaluate EXP and jump to LABEL if the value is zero.  */
123
124 void
125 jumpifnot (tree exp, rtx_code_label *label, profile_probability prob)
126 {
127   do_jump (exp, label, NULL, prob.invert ());
128 }
129
130 void
131 jumpifnot_1 (enum tree_code code, tree op0, tree op1, rtx_code_label *label,
132              profile_probability prob)
133 {
134   do_jump_1 (code, op0, op1, label, NULL, prob.invert ());
135 }
136
137 /* Generate code to evaluate EXP and jump to LABEL if the value is nonzero.  */
138
139 void
140 jumpif (tree exp, rtx_code_label *label, profile_probability prob)
141 {
142   do_jump (exp, NULL, label, prob);
143 }
144
145 void
146 jumpif_1 (enum tree_code code, tree op0, tree op1,
147           rtx_code_label *label, profile_probability prob)
148 {
149   do_jump_1 (code, op0, op1, NULL, label, prob);
150 }
151
152 /* Used internally by prefer_and_bit_test.  */
153
154 static GTY(()) rtx and_reg;
155 static GTY(()) rtx and_test;
156 static GTY(()) rtx shift_test;
157
158 /* Compare the relative costs of "(X & (1 << BITNUM))" and "(X >> BITNUM) & 1",
159    where X is an arbitrary register of mode MODE.  Return true if the former
160    is preferred.  */
161
162 static bool
163 prefer_and_bit_test (machine_mode mode, int bitnum)
164 {
165   bool speed_p;
166   wide_int mask = wi::set_bit_in_zero (bitnum, GET_MODE_PRECISION (mode));
167
168   if (and_test == 0)
169     {
170       /* Set up rtxes for the two variations.  Use NULL as a placeholder
171          for the BITNUM-based constants.  */
172       and_reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
173       and_test = gen_rtx_AND (mode, and_reg, NULL);
174       shift_test = gen_rtx_AND (mode, gen_rtx_ASHIFTRT (mode, and_reg, NULL),
175                                 const1_rtx);
176     }
177   else
178     {
179       /* Change the mode of the previously-created rtxes.  */
180       PUT_MODE (and_reg, mode);
181       PUT_MODE (and_test, mode);
182       PUT_MODE (shift_test, mode);
183       PUT_MODE (XEXP (shift_test, 0), mode);
184     }
185
186   /* Fill in the integers.  */
187   XEXP (and_test, 1) = immed_wide_int_const (mask, mode);
188   XEXP (XEXP (shift_test, 0), 1) = GEN_INT (bitnum);
189
190   speed_p = optimize_insn_for_speed_p ();
191   return (rtx_cost (and_test, mode, IF_THEN_ELSE, 0, speed_p)
192           <= rtx_cost (shift_test, mode, IF_THEN_ELSE, 0, speed_p));
193 }
194
195 /* Subroutine of do_jump, dealing with exploded comparisons of the type
196    OP0 CODE OP1 .  IF_FALSE_LABEL and IF_TRUE_LABEL like in do_jump.
197    PROB is probability of jump to if_true_label.  */
198
199 void
200 do_jump_1 (enum tree_code code, tree op0, tree op1,
201            rtx_code_label *if_false_label, rtx_code_label *if_true_label,
202            profile_probability prob)
203 {
204   machine_mode mode;
205   rtx_code_label *drop_through_label = 0;
206
207   switch (code)
208     {
209     case EQ_EXPR:
210       {
211         tree inner_type = TREE_TYPE (op0);
212
213         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
214                     != MODE_COMPLEX_FLOAT);
215         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
216                     != MODE_COMPLEX_INT);
217
218         if (integer_zerop (op1))
219           do_jump (op0, if_true_label, if_false_label,
220                    prob.invert ());
221         else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
222                  && !can_compare_p (EQ, TYPE_MODE (inner_type), ccp_jump))
223           do_jump_by_parts_equality (op0, op1, if_false_label, if_true_label,
224                                      prob);
225         else
226           do_compare_and_jump (op0, op1, EQ, EQ, if_false_label, if_true_label,
227                                prob);
228         break;
229       }
230
231     case NE_EXPR:
232       {
233         tree inner_type = TREE_TYPE (op0);
234
235         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
236                     != MODE_COMPLEX_FLOAT);
237         gcc_assert (GET_MODE_CLASS (TYPE_MODE (inner_type))
238                     != MODE_COMPLEX_INT);
239
240         if (integer_zerop (op1))
241           do_jump (op0, if_false_label, if_true_label, prob);
242         else if (GET_MODE_CLASS (TYPE_MODE (inner_type)) == MODE_INT
243            && !can_compare_p (NE, TYPE_MODE (inner_type), ccp_jump))
244           do_jump_by_parts_equality (op0, op1, if_true_label, if_false_label,
245                                      prob.invert ());
246         else
247           do_compare_and_jump (op0, op1, NE, NE, if_false_label, if_true_label,
248                                prob);
249         break;
250       }
251
252     case LT_EXPR:
253       mode = TYPE_MODE (TREE_TYPE (op0));
254       if (GET_MODE_CLASS (mode) == MODE_INT
255           && ! can_compare_p (LT, mode, ccp_jump))
256         do_jump_by_parts_greater (op0, op1, 1, if_false_label, if_true_label,
257                                   prob);
258       else
259         do_compare_and_jump (op0, op1, LT, LTU, if_false_label, if_true_label,
260                              prob);
261       break;
262
263     case LE_EXPR:
264       mode = TYPE_MODE (TREE_TYPE (op0));
265       if (GET_MODE_CLASS (mode) == MODE_INT
266           && ! can_compare_p (LE, mode, ccp_jump))
267         do_jump_by_parts_greater (op0, op1, 0, if_true_label, if_false_label,
268                                   prob.invert ());
269       else
270         do_compare_and_jump (op0, op1, LE, LEU, if_false_label, if_true_label,
271                              prob);
272       break;
273
274     case GT_EXPR:
275       mode = TYPE_MODE (TREE_TYPE (op0));
276       if (GET_MODE_CLASS (mode) == MODE_INT
277           && ! can_compare_p (GT, mode, ccp_jump))
278         do_jump_by_parts_greater (op0, op1, 0, if_false_label, if_true_label,
279                                   prob);
280       else
281         do_compare_and_jump (op0, op1, GT, GTU, if_false_label, if_true_label,
282                              prob);
283       break;
284
285     case GE_EXPR:
286       mode = TYPE_MODE (TREE_TYPE (op0));
287       if (GET_MODE_CLASS (mode) == MODE_INT
288           && ! can_compare_p (GE, mode, ccp_jump))
289         do_jump_by_parts_greater (op0, op1, 1, if_true_label, if_false_label,
290                                   prob.invert ());
291       else
292         do_compare_and_jump (op0, op1, GE, GEU, if_false_label, if_true_label,
293                              prob);
294       break;
295
296     case ORDERED_EXPR:
297       do_compare_and_jump (op0, op1, ORDERED, ORDERED,
298                            if_false_label, if_true_label, prob);
299       break;
300
301     case UNORDERED_EXPR:
302       do_compare_and_jump (op0, op1, UNORDERED, UNORDERED,
303                            if_false_label, if_true_label, prob);
304       break;
305
306     case UNLT_EXPR:
307       do_compare_and_jump (op0, op1, UNLT, UNLT, if_false_label, if_true_label,
308                            prob);
309       break;
310
311     case UNLE_EXPR:
312       do_compare_and_jump (op0, op1, UNLE, UNLE, if_false_label, if_true_label,
313                            prob);
314       break;
315
316     case UNGT_EXPR:
317       do_compare_and_jump (op0, op1, UNGT, UNGT, if_false_label, if_true_label,
318                            prob);
319       break;
320
321     case UNGE_EXPR:
322       do_compare_and_jump (op0, op1, UNGE, UNGE, if_false_label, if_true_label,
323                            prob);
324       break;
325
326     case UNEQ_EXPR:
327       do_compare_and_jump (op0, op1, UNEQ, UNEQ, if_false_label, if_true_label,
328                            prob);
329       break;
330
331     case LTGT_EXPR:
332       do_compare_and_jump (op0, op1, LTGT, LTGT, if_false_label, if_true_label,
333                            prob);
334       break;
335
336     case TRUTH_ANDIF_EXPR:
337       {
338         /* Spread the probability that the expression is false evenly between
339            the two conditions. So the first condition is false half the total
340            probability of being false. The second condition is false the other
341            half of the total probability of being false, so its jump has a false
342            probability of half the total, relative to the probability we
343            reached it (i.e. the first condition was true).  */
344         profile_probability op0_prob = profile_probability::uninitialized ();
345         profile_probability op1_prob = profile_probability::uninitialized ();
346         if (prob.initialized_p ())
347           {
348             profile_probability false_prob = prob.invert ();
349             profile_probability op0_false_prob = false_prob.apply_scale (1, 2);
350             profile_probability op1_false_prob = false_prob.apply_scale (1, 2)
351                                 / op0_false_prob.invert ();
352             /* Get the probability that each jump below is true.  */
353             op0_prob = op0_false_prob.invert ();
354             op1_prob = op1_false_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             op0_prob = prob.apply_scale (1, 2);
382             op1_prob = prob.apply_scale (1, 2) / op0_prob.invert ();
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   machine_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 NEGATE_EXPR:
471     case LROTATE_EXPR:
472     case RROTATE_EXPR:
473       /* These cannot change zero->nonzero or vice versa.  */
474       do_jump (TREE_OPERAND (exp, 0), if_false_label, if_true_label, prob);
475       break;
476
477     case TRUTH_NOT_EXPR:
478       do_jump (TREE_OPERAND (exp, 0), if_true_label, if_false_label,
479                prob.invert ());
480       break;
481
482     case COND_EXPR:
483       {
484         rtx_code_label *label1 = gen_label_rtx ();
485         if (!if_true_label || !if_false_label)
486           {
487             drop_through_label = gen_label_rtx ();
488             if (!if_true_label)
489               if_true_label = drop_through_label;
490             if (!if_false_label)
491               if_false_label = drop_through_label;
492           }
493
494         do_pending_stack_adjust ();
495         do_jump (TREE_OPERAND (exp, 0), label1, NULL,
496                  profile_probability::uninitialized ());
497         do_jump (TREE_OPERAND (exp, 1), if_false_label, if_true_label, prob);
498         emit_label (label1);
499         do_jump (TREE_OPERAND (exp, 2), if_false_label, if_true_label, prob);
500         break;
501       }
502
503     case COMPOUND_EXPR:
504       /* Lowered by gimplify.c.  */
505       gcc_unreachable ();
506
507     case MINUS_EXPR:
508       /* Nonzero iff operands of minus differ.  */
509       code = NE_EXPR;
510
511       /* FALLTHRU */
512     case EQ_EXPR:
513     case NE_EXPR:
514     case LT_EXPR:
515     case LE_EXPR:
516     case GT_EXPR:
517     case GE_EXPR:
518     case ORDERED_EXPR:
519     case UNORDERED_EXPR:
520     case UNLT_EXPR:
521     case UNLE_EXPR:
522     case UNGT_EXPR:
523     case UNGE_EXPR:
524     case UNEQ_EXPR:
525     case LTGT_EXPR:
526     case TRUTH_ANDIF_EXPR:
527     case TRUTH_ORIF_EXPR:
528     other_code:
529       do_jump_1 (code, TREE_OPERAND (exp, 0), TREE_OPERAND (exp, 1),
530                  if_false_label, if_true_label, prob);
531       break;
532
533     case BIT_AND_EXPR:
534       /* fold_single_bit_test() converts (X & (1 << C)) into (X >> C) & 1.
535          See if the former is preferred for jump tests and restore it
536          if so.  */
537       if (integer_onep (TREE_OPERAND (exp, 1)))
538         {
539           tree exp0 = TREE_OPERAND (exp, 0);
540           rtx_code_label *set_label, *clr_label;
541           profile_probability setclr_prob = prob;
542
543           /* Strip narrowing integral type conversions.  */
544           while (CONVERT_EXPR_P (exp0)
545                  && TREE_OPERAND (exp0, 0) != error_mark_node
546                  && TYPE_PRECISION (TREE_TYPE (exp0))
547                     <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp0, 0))))
548             exp0 = TREE_OPERAND (exp0, 0);
549
550           /* "exp0 ^ 1" inverts the sense of the single bit test.  */
551           if (TREE_CODE (exp0) == BIT_XOR_EXPR
552               && integer_onep (TREE_OPERAND (exp0, 1)))
553             {
554               exp0 = TREE_OPERAND (exp0, 0);
555               clr_label = if_true_label;
556               set_label = if_false_label;
557               setclr_prob = prob.invert ();
558             }
559           else
560             {
561               clr_label = if_false_label;
562               set_label = if_true_label;
563             }
564
565           if (TREE_CODE (exp0) == RSHIFT_EXPR)
566             {
567               tree arg = TREE_OPERAND (exp0, 0);
568               tree shift = TREE_OPERAND (exp0, 1);
569               tree argtype = TREE_TYPE (arg);
570               if (TREE_CODE (shift) == INTEGER_CST
571                   && compare_tree_int (shift, 0) >= 0
572                   && compare_tree_int (shift, HOST_BITS_PER_WIDE_INT) < 0
573                   && prefer_and_bit_test (TYPE_MODE (argtype),
574                                           TREE_INT_CST_LOW (shift)))
575                 {
576                   unsigned HOST_WIDE_INT mask
577                     = HOST_WIDE_INT_1U << TREE_INT_CST_LOW (shift);
578                   do_jump (build2 (BIT_AND_EXPR, argtype, arg,
579                                    build_int_cstu (argtype, mask)),
580                            clr_label, set_label, setclr_prob);
581                   break;
582                 }
583             }
584         }
585
586       /* If we are AND'ing with a small constant, do this comparison in the
587          smallest type that fits.  If the machine doesn't have comparisons
588          that small, it will be converted back to the wider comparison.
589          This helps if we are testing the sign bit of a narrower object.
590          combine can't do this for us because it can't know whether a
591          ZERO_EXTRACT or a compare in a smaller mode exists, but we do.  */
592
593       if (! SLOW_BYTE_ACCESS
594           && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST
595           && TYPE_PRECISION (TREE_TYPE (exp)) <= HOST_BITS_PER_WIDE_INT
596           && (i = tree_floor_log2 (TREE_OPERAND (exp, 1))) >= 0
597           && (mode = mode_for_size (i + 1, MODE_INT, 0)) != BLKmode
598           && (type = lang_hooks.types.type_for_mode (mode, 1)) != 0
599           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (exp))
600           && have_insn_for (COMPARE, TYPE_MODE (type)))
601         {
602           do_jump (fold_convert (type, exp), if_false_label, if_true_label,
603                    prob);
604           break;
605         }
606
607       if (TYPE_PRECISION (TREE_TYPE (exp)) > 1
608           || TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
609         goto normal;
610
611       /* Boolean comparisons can be compiled as TRUTH_AND_EXPR.  */
612       /* FALLTHRU */
613
614     case TRUTH_AND_EXPR:
615       /* High branch cost, expand as the bitwise AND of the conditions.
616          Do the same if the RHS has side effects, because we're effectively
617          turning a TRUTH_AND_EXPR into a TRUTH_ANDIF_EXPR.  */
618       if (BRANCH_COST (optimize_insn_for_speed_p (),
619                        false) >= 4
620           || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
621         goto normal;
622       code = TRUTH_ANDIF_EXPR;
623       goto other_code;
624
625     case BIT_IOR_EXPR:
626     case TRUTH_OR_EXPR:
627       /* High branch cost, expand as the bitwise OR of the conditions.
628          Do the same if the RHS has side effects, because we're effectively
629          turning a TRUTH_OR_EXPR into a TRUTH_ORIF_EXPR.  */
630       if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 4
631           || TREE_SIDE_EFFECTS (TREE_OPERAND (exp, 1)))
632         goto normal;
633       code = TRUTH_ORIF_EXPR;
634       goto other_code;
635
636       /* Fall through and generate the normal code.  */
637     default:
638     normal:
639       temp = expand_normal (exp);
640       do_pending_stack_adjust ();
641       /* The RTL optimizers prefer comparisons against pseudos.  */
642       if (GET_CODE (temp) == SUBREG)
643         {
644           /* Compare promoted variables in their promoted mode.  */
645           if (SUBREG_PROMOTED_VAR_P (temp)
646               && REG_P (XEXP (temp, 0)))
647             temp = XEXP (temp, 0);
648           else
649             temp = copy_to_reg (temp);
650         }
651       do_compare_rtx_and_jump (temp, CONST0_RTX (GET_MODE (temp)),
652                                NE, TYPE_UNSIGNED (TREE_TYPE (exp)),
653                                GET_MODE (temp), NULL_RTX,
654                                if_false_label, if_true_label, prob);
655     }
656
657   if (drop_through_label)
658     {
659       do_pending_stack_adjust ();
660       emit_label (drop_through_label);
661     }
662 }
663 \f
664 /* Compare OP0 with OP1, word at a time, in mode MODE.
665    UNSIGNEDP says to do unsigned comparison.
666    Jump to IF_TRUE_LABEL if OP0 is greater, IF_FALSE_LABEL otherwise.  */
667
668 static void
669 do_jump_by_parts_greater_rtx (machine_mode mode, int unsignedp, rtx op0,
670                               rtx op1, rtx_code_label *if_false_label,
671                               rtx_code_label *if_true_label,
672                               profile_probability prob)
673 {
674   int nwords = (GET_MODE_SIZE (mode) / UNITS_PER_WORD);
675   rtx_code_label *drop_through_label = 0;
676   bool drop_through_if_true = false, drop_through_if_false = false;
677   enum rtx_code code = GT;
678   int i;
679
680   if (! if_true_label || ! if_false_label)
681     drop_through_label = gen_label_rtx ();
682   if (! if_true_label)
683     {
684       if_true_label = drop_through_label;
685       drop_through_if_true = true;
686     }
687   if (! if_false_label)
688     {
689       if_false_label = drop_through_label;
690       drop_through_if_false = true;
691     }
692
693   /* Deal with the special case 0 > x: only one comparison is necessary and
694      we reverse it to avoid jumping to the drop-through label.  */
695   if (op0 == const0_rtx && drop_through_if_true && !drop_through_if_false)
696     {
697       code = LE;
698       if_true_label = if_false_label;
699       if_false_label = drop_through_label;
700       drop_through_if_true = false;
701       drop_through_if_false = true;
702       prob = prob.invert ();
703     }
704
705   /* Compare a word at a time, high order first.  */
706   for (i = 0; i < nwords; i++)
707     {
708       rtx op0_word, op1_word;
709
710       if (WORDS_BIG_ENDIAN)
711         {
712           op0_word = operand_subword_force (op0, i, mode);
713           op1_word = operand_subword_force (op1, i, mode);
714         }
715       else
716         {
717           op0_word = operand_subword_force (op0, nwords - 1 - i, mode);
718           op1_word = operand_subword_force (op1, nwords - 1 - i, mode);
719         }
720
721       /* All but high-order word must be compared as unsigned.  */
722       do_compare_rtx_and_jump (op0_word, op1_word, code, (unsignedp || i > 0),
723                                word_mode, NULL_RTX, NULL, if_true_label,
724                                prob);
725
726       /* Emit only one comparison for 0.  Do not emit the last cond jump.  */
727       if (op0 == const0_rtx || i == nwords - 1)
728         break;
729
730       /* Consider lower words only if these are equal.  */
731       do_compare_rtx_and_jump (op0_word, op1_word, NE, unsignedp, word_mode,
732                                NULL_RTX, NULL, if_false_label,
733                                prob.invert ());
734     }
735
736   if (!drop_through_if_false)
737     emit_jump (if_false_label);
738   if (drop_through_label)
739     emit_label (drop_through_label);
740 }
741
742 /* Given a comparison expression EXP for values too wide to be compared
743    with one insn, test the comparison and jump to the appropriate label.
744    The code of EXP is ignored; we always test GT if SWAP is 0,
745    and LT if SWAP is 1.  */
746
747 static void
748 do_jump_by_parts_greater (tree treeop0, tree treeop1, int swap,
749                           rtx_code_label *if_false_label,
750                           rtx_code_label *if_true_label,
751                           profile_probability prob)
752 {
753   rtx op0 = expand_normal (swap ? treeop1 : treeop0);
754   rtx op1 = expand_normal (swap ? treeop0 : treeop1);
755   machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
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 (machine_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 (machine_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
859 static void
860 do_jump_by_parts_equality (tree treeop0, tree treeop1,
861                            rtx_code_label *if_false_label,
862                            rtx_code_label *if_true_label,
863                            profile_probability prob)
864 {
865   rtx op0 = expand_normal (treeop0);
866   rtx op1 = expand_normal (treeop1);
867   machine_mode mode = TYPE_MODE (TREE_TYPE (treeop0));
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 (0 != (tem = simplify_relational_operation (code, mode, VOIDmode,
1005                                                  op0, op1)))
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   if (GET_MODE_CLASS (mode) == MODE_INT
1028       && ! can_compare_p (code, mode, ccp_jump))
1029     {
1030       switch (code)
1031         {
1032         case LTU:
1033           do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1034                                         if_false_label, if_true_label, prob);
1035           break;
1036
1037         case LEU:
1038           do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1039                                         if_true_label, if_false_label,
1040                                         prob.invert ());
1041           break;
1042
1043         case GTU:
1044           do_jump_by_parts_greater_rtx (mode, 1, op0, op1,
1045                                         if_false_label, if_true_label, prob);
1046           break;
1047
1048         case GEU:
1049           do_jump_by_parts_greater_rtx (mode, 1, op1, op0,
1050                                         if_true_label, if_false_label,
1051                                         prob.invert ());
1052           break;
1053
1054         case LT:
1055           do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1056                                         if_false_label, if_true_label, prob);
1057           break;
1058
1059         case LE:
1060           do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1061                                         if_true_label, if_false_label,
1062                                         prob.invert ());
1063           break;
1064
1065         case GT:
1066           do_jump_by_parts_greater_rtx (mode, 0, op0, op1,
1067                                         if_false_label, if_true_label, prob);
1068           break;
1069
1070         case GE:
1071           do_jump_by_parts_greater_rtx (mode, 0, op1, op0,
1072                                         if_true_label, if_false_label,
1073                                         prob.invert ());
1074           break;
1075
1076         case EQ:
1077           do_jump_by_parts_equality_rtx (mode, op0, op1, if_false_label,
1078                                          if_true_label, prob);
1079           break;
1080
1081         case NE:
1082           do_jump_by_parts_equality_rtx (mode, op0, op1, if_true_label,
1083                                          if_false_label,
1084                                          prob.invert ());
1085           break;
1086
1087         default:
1088           gcc_unreachable ();
1089         }
1090     }
1091   else
1092     {
1093       if (SCALAR_FLOAT_MODE_P (mode)
1094           && ! can_compare_p (code, mode, ccp_jump)
1095           && can_compare_p (swap_condition (code), mode, ccp_jump))
1096         {
1097           code = swap_condition (code);
1098           std::swap (op0, op1);
1099         }
1100       else if (SCALAR_FLOAT_MODE_P (mode)
1101                && ! can_compare_p (code, mode, ccp_jump)
1102                /* Never split ORDERED and UNORDERED.
1103                   These must be implemented.  */
1104                && (code != ORDERED && code != UNORDERED)
1105                /* Split a floating-point comparison if
1106                   we can jump on other conditions...  */
1107                && (have_insn_for (COMPARE, mode)
1108                    /* ... or if there is no libcall for it.  */
1109                    || code_to_optab (code) == unknown_optab))
1110         {
1111           enum rtx_code first_code;
1112           bool and_them = split_comparison (code, mode, &first_code, &code);
1113
1114           /* If there are no NaNs, the first comparison should always fall
1115              through.  */
1116           if (!HONOR_NANS (mode))
1117             gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
1118
1119           else
1120             {
1121               profile_probability first_prob = prob;
1122               if (first_code == UNORDERED)
1123                 first_prob = profile_probability::guessed_always ().apply_scale
1124                                  (1, 100);
1125               else if (first_code == ORDERED)
1126                 first_prob = profile_probability::guessed_always ().apply_scale
1127                                  (99, 100);
1128               if (and_them)
1129                 {
1130                   rtx_code_label *dest_label;
1131                   /* If we only jump if true, just bypass the second jump.  */
1132                   if (! if_false_label)
1133                     {
1134                       if (! dummy_label)
1135                         dummy_label = gen_label_rtx ();
1136                       dest_label = dummy_label;
1137                     }
1138                   else
1139                     dest_label = if_false_label;
1140                   do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1141                                            size, dest_label, NULL, first_prob);
1142                 }
1143               else
1144                 do_compare_rtx_and_jump (op0, op1, first_code, unsignedp, mode,
1145                                          size, NULL, if_true_label, first_prob);
1146             }
1147         }
1148
1149       emit_cmp_and_jump_insns (op0, op1, code, size, mode, unsignedp,
1150                                if_true_label, prob);
1151     }
1152
1153   if (if_false_label)
1154     emit_jump (if_false_label);
1155   if (dummy_label)
1156     emit_label (dummy_label);
1157 }
1158
1159 /* Generate code for a comparison expression EXP (including code to compute
1160    the values to be compared) and a conditional jump to IF_FALSE_LABEL and/or
1161    IF_TRUE_LABEL.  One of the labels can be NULL_RTX, in which case the
1162    generated code will drop through.
1163    SIGNED_CODE should be the rtx operation for this comparison for
1164    signed data; UNSIGNED_CODE, likewise for use if data is unsigned.
1165
1166    We force a stack adjustment unless there are currently
1167    things pushed on the stack that aren't yet used.  */
1168
1169 static void
1170 do_compare_and_jump (tree treeop0, tree treeop1, enum rtx_code signed_code,
1171                      enum rtx_code unsigned_code,
1172                      rtx_code_label *if_false_label,
1173                      rtx_code_label *if_true_label, profile_probability prob)
1174 {
1175   rtx op0, op1;
1176   tree type;
1177   machine_mode mode;
1178   int unsignedp;
1179   enum rtx_code code;
1180
1181   /* Don't crash if the comparison was erroneous.  */
1182   op0 = expand_normal (treeop0);
1183   if (TREE_CODE (treeop0) == ERROR_MARK)
1184     return;
1185
1186   op1 = expand_normal (treeop1);
1187   if (TREE_CODE (treeop1) == ERROR_MARK)
1188     return;
1189
1190   type = TREE_TYPE (treeop0);
1191   mode = TYPE_MODE (type);
1192   if (TREE_CODE (treeop0) == INTEGER_CST
1193       && (TREE_CODE (treeop1) != INTEGER_CST
1194           || (GET_MODE_BITSIZE (mode)
1195               > GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (treeop1))))))
1196     {
1197       /* op0 might have been replaced by promoted constant, in which
1198          case the type of second argument should be used.  */
1199       type = TREE_TYPE (treeop1);
1200       mode = TYPE_MODE (type);
1201     }
1202   unsignedp = TYPE_UNSIGNED (type);
1203   code = unsignedp ? unsigned_code : signed_code;
1204
1205   /* If function pointers need to be "canonicalized" before they can
1206      be reliably compared, then canonicalize them.
1207      Only do this if *both* sides of the comparison are function pointers.
1208      If one side isn't, we want a noncanonicalized comparison.  See PR
1209      middle-end/17564.  */
1210   if (targetm.have_canonicalize_funcptr_for_compare ()
1211       && POINTER_TYPE_P (TREE_TYPE (treeop0))
1212       && POINTER_TYPE_P (TREE_TYPE (treeop1))
1213       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))
1214       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (treeop1))))
1215     {
1216       rtx new_op0 = gen_reg_rtx (mode);
1217       rtx new_op1 = gen_reg_rtx (mode);
1218
1219       emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op0, op0));
1220       op0 = new_op0;
1221
1222       emit_insn (targetm.gen_canonicalize_funcptr_for_compare (new_op1, op1));
1223       op1 = new_op1;
1224     }
1225
1226   do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode,
1227                            ((mode == BLKmode)
1228                             ? expr_size (treeop0) : NULL_RTX),
1229                            if_false_label, if_true_label, prob);
1230 }
1231
1232 #include "gt-dojump.h"