LCOV - code coverage report
Current view: top level - gcc - omp-expand.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.2 % 6253 5893
Test Date: 2026-12-05 16:10:02 Functions: 94.8 % 77 73
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Expansion pass for OMP directives.  Outlines regions of certain OMP
       2              :    directives to separate functions, converts others into explicit calls to the
       3              :    runtime library (libgomp) and so forth
       4              : 
       5              : Copyright (C) 2005-2026 Free Software Foundation, Inc.
       6              : 
       7              : This file is part of GCC.
       8              : 
       9              : GCC is free software; you can redistribute it and/or modify it under
      10              : the terms of the GNU General Public License as published by the Free
      11              : Software Foundation; either version 3, or (at your option) any later
      12              : version.
      13              : 
      14              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      15              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      16              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      17              : for more details.
      18              : 
      19              : You should have received a copy of the GNU General Public License
      20              : along with GCC; see the file COPYING3.  If not see
      21              : <http://www.gnu.org/licenses/>.  */
      22              : 
      23              : #include "config.h"
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "memmodel.h"
      27              : #include "backend.h"
      28              : #include "target.h"
      29              : #include "rtl.h"
      30              : #include "tree.h"
      31              : #include "gimple.h"
      32              : #include "cfghooks.h"
      33              : #include "tree-pass.h"
      34              : #include "ssa.h"
      35              : #include "optabs.h"
      36              : #include "cgraph.h"
      37              : #include "pretty-print.h"
      38              : #include "diagnostic-core.h"
      39              : #include "fold-const.h"
      40              : #include "stor-layout.h"
      41              : #include "cfganal.h"
      42              : #include "internal-fn.h"
      43              : #include "gimplify.h"
      44              : #include "gimple-iterator.h"
      45              : #include "gimplify-me.h"
      46              : #include "gimple-walk.h"
      47              : #include "tree-cfg.h"
      48              : #include "tree-into-ssa.h"
      49              : #include "tree-ssa.h"
      50              : #include "splay-tree.h"
      51              : #include "cfgloop.h"
      52              : #include "omp-general.h"
      53              : #include "omp-offload.h"
      54              : #include "tree-cfgcleanup.h"
      55              : #include "alloc-pool.h"
      56              : #include "symbol-summary.h"
      57              : #include "gomp-constants.h"
      58              : #include "gimple-pretty-print.h"
      59              : #include "stringpool.h"
      60              : #include "attribs.h"
      61              : #include "tree-eh.h"
      62              : #include "opts.h"
      63              : 
      64              : /* OMP region information.  Every parallel and workshare
      65              :    directive is enclosed between two markers, the OMP_* directive
      66              :    and a corresponding GIMPLE_OMP_RETURN statement.  */
      67              : 
      68              : struct omp_region
      69              : {
      70              :   /* The enclosing region.  */
      71              :   struct omp_region *outer;
      72              : 
      73              :   /* First child region.  */
      74              :   struct omp_region *inner;
      75              : 
      76              :   /* Next peer region.  */
      77              :   struct omp_region *next;
      78              : 
      79              :   /* Block containing the omp directive as its last stmt.  */
      80              :   basic_block entry;
      81              : 
      82              :   /* Block containing the GIMPLE_OMP_RETURN as its last stmt.  */
      83              :   basic_block exit;
      84              : 
      85              :   /* Block containing the GIMPLE_OMP_CONTINUE as its last stmt.  */
      86              :   basic_block cont;
      87              : 
      88              :   /* If this is a combined parallel+workshare region, this is a list
      89              :      of additional arguments needed by the combined parallel+workshare
      90              :      library call.  */
      91              :   vec<tree, va_gc> *ws_args;
      92              : 
      93              :   /* The code for the omp directive of this region.  */
      94              :   enum gimple_code type;
      95              : 
      96              :   /* Schedule kind, only used for GIMPLE_OMP_FOR type regions.  */
      97              :   enum omp_clause_schedule_kind sched_kind;
      98              : 
      99              :   /* Schedule modifiers.  */
     100              :   unsigned char sched_modifiers;
     101              : 
     102              :   /* True if this is a combined parallel+workshare region.  */
     103              :   bool is_combined_parallel;
     104              : 
     105              :   /* Copy of fd.lastprivate_conditional != 0.  */
     106              :   bool has_lastprivate_conditional;
     107              : 
     108              :   /* The ordered stmt if type is GIMPLE_OMP_ORDERED and it has
     109              :      a depend clause.  */
     110              :   gomp_ordered *ord_stmt;
     111              : };
     112              : 
     113              : static struct omp_region *root_omp_region;
     114              : static bool omp_any_child_fn_dumped;
     115              : 
     116              : static void expand_omp_build_assign (gimple_stmt_iterator *, tree, tree,
     117              :                                      bool = false);
     118              : static gphi *find_phi_with_arg_on_edge (tree, edge);
     119              : static void expand_omp (struct omp_region *region);
     120              : 
     121              : /* Return true if REGION is a combined parallel+workshare region.  */
     122              : 
     123              : static inline bool
     124        45212 : is_combined_parallel (struct omp_region *region)
     125              : {
     126        45212 :   return region->is_combined_parallel;
     127              : }
     128              : 
     129              : /* Return true is REGION is or is contained within an offload region.  */
     130              : 
     131              : static bool
     132        11217 : is_in_offload_region (struct omp_region *region)
     133              : {
     134        30156 :   gimple *entry_stmt = last_nondebug_stmt (region->entry);
     135        30156 :   if (is_gimple_omp (entry_stmt)
     136        29200 :       && is_gimple_omp_offloaded (entry_stmt))
     137              :     return true;
     138        25822 :   else if (region->outer)
     139              :     return is_in_offload_region (region->outer);
     140              :   else
     141         6883 :     return (lookup_attribute ("omp declare target",
     142         6883 :                               DECL_ATTRIBUTES (current_function_decl))
     143         6883 :             != NULL);
     144              : }
     145              : 
     146              : /* Given two blocks PAR_ENTRY_BB and WS_ENTRY_BB such that WS_ENTRY_BB
     147              :    is the immediate dominator of PAR_ENTRY_BB, return true if there
     148              :    are no data dependencies that would prevent expanding the parallel
     149              :    directive at PAR_ENTRY_BB as a combined parallel+workshare region.
     150              : 
     151              :    When expanding a combined parallel+workshare region, the call to
     152              :    the child function may need additional arguments in the case of
     153              :    GIMPLE_OMP_FOR regions.  In some cases, these arguments are
     154              :    computed out of variables passed in from the parent to the child
     155              :    via 'struct .omp_data_s'.  For instance:
     156              : 
     157              :         #pragma omp parallel for schedule (guided, i * 4)
     158              :         for (j ...)
     159              : 
     160              :    Is lowered into:
     161              : 
     162              :         # BLOCK 2 (PAR_ENTRY_BB)
     163              :         .omp_data_o.i = i;
     164              :         #pragma omp parallel [child fn: bar.omp_fn.0 ( ..., D.1598)
     165              : 
     166              :         # BLOCK 3 (WS_ENTRY_BB)
     167              :         .omp_data_i = &.omp_data_o;
     168              :         D.1667 = .omp_data_i->i;
     169              :         D.1598 = D.1667 * 4;
     170              :         #pragma omp for schedule (guided, D.1598)
     171              : 
     172              :    When we outline the parallel region, the call to the child function
     173              :    'bar.omp_fn.0' will need the value D.1598 in its argument list, but
     174              :    that value is computed *after* the call site.  So, in principle we
     175              :    cannot do the transformation.
     176              : 
     177              :    To see whether the code in WS_ENTRY_BB blocks the combined
     178              :    parallel+workshare call, we collect all the variables used in the
     179              :    GIMPLE_OMP_FOR header check whether they appear on the LHS of any
     180              :    statement in WS_ENTRY_BB.  If so, then we cannot emit the combined
     181              :    call.
     182              : 
     183              :    FIXME.  If we had the SSA form built at this point, we could merely
     184              :    hoist the code in block 3 into block 2 and be done with it.  But at
     185              :    this point we don't have dataflow information and though we could
     186              :    hack something up here, it is really not worth the aggravation.  */
     187              : 
     188              : static bool
     189         9573 : workshare_safe_to_combine_p (basic_block ws_entry_bb)
     190              : {
     191         9573 :   struct omp_for_data fd;
     192         9573 :   gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
     193              : 
     194         9573 :   if (gimple_code (ws_stmt) == GIMPLE_OMP_SECTIONS)
     195              :     return true;
     196              : 
     197         9450 :   gcc_assert (gimple_code (ws_stmt) == GIMPLE_OMP_FOR);
     198         9450 :   if (gimple_omp_for_kind (ws_stmt) != GF_OMP_FOR_KIND_FOR)
     199              :     return false;
     200              : 
     201         9433 :   omp_extract_for_data (as_a <gomp_for *> (ws_stmt), &fd, NULL);
     202              : 
     203         9433 :   if (fd.collapse > 1 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
     204              :     return false;
     205         7457 :   if (fd.iter_type != long_integer_type_node)
     206              :     return false;
     207              : 
     208              :   /* FIXME.  We give up too easily here.  If any of these arguments
     209              :      are not constants, they will likely involve variables that have
     210              :      been mapped into fields of .omp_data_s for sharing with the child
     211              :      function.  With appropriate data flow, it would be possible to
     212              :      see through this.  */
     213         1510 :   if (!is_gimple_min_invariant (fd.loop.n1)
     214         1280 :       || !is_gimple_min_invariant (fd.loop.n2)
     215         1149 :       || !is_gimple_min_invariant (fd.loop.step)
     216         2644 :       || (fd.chunk_size && !is_gimple_min_invariant (fd.chunk_size)))
     217          378 :     return false;
     218              : 
     219              :   return true;
     220              : }
     221              : 
     222              : /* Adjust CHUNK_SIZE from SCHEDULE clause, depending on simd modifier
     223              :    presence (SIMD_SCHEDULE).  */
     224              : 
     225              : static tree
     226         8390 : omp_adjust_chunk_size (tree chunk_size, bool simd_schedule, bool offload)
     227              : {
     228         8390 :   if (!simd_schedule || integer_zerop (chunk_size))
     229              :     return chunk_size;
     230              : 
     231           34 :   tree vf;
     232           34 :   tree type = TREE_TYPE (chunk_size);
     233              : 
     234           34 :   if (offload)
     235              :     {
     236            2 :       cfun->curr_properties &= ~PROP_gimple_lomp_dev;
     237            2 :       vf = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_MAX_VF,
     238              :                                          unsigned_type_node, 0);
     239            2 :       vf = fold_convert (type, vf);
     240              :     }
     241              :   else
     242              :     {
     243           32 :       poly_uint64 vf_num = omp_max_vf (false);
     244           32 :       if (known_eq (vf_num, 1U))
     245         8390 :         return chunk_size;
     246           31 :       vf = build_int_cst (type, vf_num);
     247              :     }
     248              : 
     249           33 :   tree vf_minus_one = fold_build2 (MINUS_EXPR, type, vf,
     250              :                                    build_int_cst (type, 1));
     251           33 :   tree negative_vf = fold_build1 (NEGATE_EXPR, type, vf);
     252           33 :   chunk_size = fold_build2 (PLUS_EXPR, type, chunk_size, vf_minus_one);
     253           33 :   return fold_build2 (BIT_AND_EXPR, type, chunk_size, negative_vf);
     254              : }
     255              : 
     256              : /* Collect additional arguments needed to emit a combined
     257              :    parallel+workshare call.  WS_STMT is the workshare directive being
     258              :    expanded.  */
     259              : 
     260              : static vec<tree, va_gc> *
     261         1200 : get_ws_args_for (gimple *par_stmt, gimple *ws_stmt, bool offload)
     262              : {
     263         1200 :   tree t;
     264         1200 :   location_t loc = gimple_location (ws_stmt);
     265         1200 :   vec<tree, va_gc> *ws_args;
     266              : 
     267         1200 :   if (gomp_for *for_stmt = dyn_cast <gomp_for *> (ws_stmt))
     268              :     {
     269         1084 :       struct omp_for_data fd;
     270         1084 :       tree n1, n2;
     271              : 
     272         1084 :       omp_extract_for_data (for_stmt, &fd, NULL);
     273         1084 :       n1 = fd.loop.n1;
     274         1084 :       n2 = fd.loop.n2;
     275              : 
     276         1084 :       if (gimple_omp_for_combined_into_p (for_stmt))
     277              :         {
     278          769 :           tree innerc
     279          769 :             = omp_find_clause (gimple_omp_parallel_clauses (par_stmt),
     280              :                                OMP_CLAUSE__LOOPTEMP_);
     281          769 :           gcc_assert (innerc);
     282          769 :           n1 = OMP_CLAUSE_DECL (innerc);
     283          769 :           innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
     284              :                                     OMP_CLAUSE__LOOPTEMP_);
     285          769 :           gcc_assert (innerc);
     286          769 :           n2 = OMP_CLAUSE_DECL (innerc);
     287              :         }
     288              : 
     289         1084 :       vec_alloc (ws_args, 3 + (fd.chunk_size != 0));
     290              : 
     291         1084 :       t = fold_convert_loc (loc, long_integer_type_node, n1);
     292         1084 :       ws_args->quick_push (t);
     293              : 
     294         1084 :       t = fold_convert_loc (loc, long_integer_type_node, n2);
     295         1084 :       ws_args->quick_push (t);
     296              : 
     297         1084 :       t = fold_convert_loc (loc, long_integer_type_node, fd.loop.step);
     298         1084 :       ws_args->quick_push (t);
     299              : 
     300         1084 :       if (fd.chunk_size)
     301              :         {
     302          538 :           t = fold_convert_loc (loc, long_integer_type_node, fd.chunk_size);
     303          538 :           t = omp_adjust_chunk_size (t, fd.simd_schedule, offload);
     304          538 :           ws_args->quick_push (t);
     305              :         }
     306              : 
     307         1084 :       return ws_args;
     308              :     }
     309          116 :   else if (gimple_code (ws_stmt) == GIMPLE_OMP_SECTIONS)
     310              :     {
     311              :       /* Number of sections is equal to the number of edges from the
     312              :          GIMPLE_OMP_SECTIONS_SWITCH statement, except for the one to
     313              :          the exit of the sections region.  */
     314          116 :       basic_block bb = single_succ (gimple_bb (ws_stmt));
     315          232 :       t = build_int_cst (unsigned_type_node, EDGE_COUNT (bb->succs) - 1);
     316          116 :       vec_alloc (ws_args, 1);
     317          116 :       ws_args->quick_push (t);
     318          116 :       return ws_args;
     319              :     }
     320              : 
     321            0 :   gcc_unreachable ();
     322              : }
     323              : 
     324              : /* Discover whether REGION is a combined parallel+workshare region.  */
     325              : 
     326              : static void
     327        17174 : determine_parallel_type (struct omp_region *region)
     328              : {
     329        17174 :   basic_block par_entry_bb, par_exit_bb;
     330        17174 :   basic_block ws_entry_bb, ws_exit_bb;
     331              : 
     332        17174 :   if (region == NULL || region->inner == NULL
     333        15619 :       || region->exit == NULL || region->inner->exit == NULL
     334        15608 :       || region->inner->cont == NULL)
     335              :     return;
     336              : 
     337              :   /* We only support parallel+for and parallel+sections.  */
     338        10886 :   if (region->type != GIMPLE_OMP_PARALLEL
     339        10886 :       || (region->inner->type != GIMPLE_OMP_FOR
     340          301 :           && region->inner->type != GIMPLE_OMP_SECTIONS))
     341              :     return;
     342              : 
     343              :   /* Check for perfect nesting PAR_ENTRY_BB -> WS_ENTRY_BB and
     344              :      WS_EXIT_BB -> PAR_EXIT_BB.  */
     345        10762 :   par_entry_bb = region->entry;
     346        10762 :   par_exit_bb = region->exit;
     347        10762 :   ws_entry_bb = region->inner->entry;
     348        10762 :   ws_exit_bb = region->inner->exit;
     349              : 
     350              :   /* Give up for task reductions on the parallel, while it is implementable,
     351              :      adding another big set of APIs or slowing down the normal paths is
     352              :      not acceptable.  */
     353        10762 :   tree pclauses
     354        10762 :     = gimple_omp_parallel_clauses (last_nondebug_stmt (par_entry_bb));
     355        10762 :   if (omp_find_clause (pclauses, OMP_CLAUSE__REDUCTEMP_))
     356              :     return;
     357              : 
     358        10737 :   if (single_succ (par_entry_bb) == ws_entry_bb
     359         9613 :       && single_succ (ws_exit_bb) == par_exit_bb
     360         9573 :       && workshare_safe_to_combine_p (ws_entry_bb)
     361        11992 :       && (gimple_omp_parallel_combined_p (last_nondebug_stmt (par_entry_bb))
     362            7 :           || (last_and_only_stmt (ws_entry_bb)
     363            2 :               && last_and_only_stmt (par_exit_bb))))
     364              :     {
     365         1248 :       gimple *par_stmt = last_nondebug_stmt (par_entry_bb);
     366         1248 :       gimple *ws_stmt = last_nondebug_stmt (ws_entry_bb);
     367              : 
     368         1248 :       if (region->inner->type == GIMPLE_OMP_FOR)
     369              :         {
     370              :           /* If this is a combined parallel loop, we need to determine
     371              :              whether or not to use the combined library calls.  There
     372              :              are two cases where we do not apply the transformation:
     373              :              static loops and any kind of ordered loop.  In the first
     374              :              case, we already open code the loop so there is no need
     375              :              to do anything else.  In the latter case, the combined
     376              :              parallel loop call would still need extra synchronization
     377              :              to implement ordered semantics, so there would not be any
     378              :              gain in using the combined call.  */
     379         1128 :           tree clauses = gimple_omp_for_clauses (ws_stmt);
     380         1128 :           tree c = omp_find_clause (clauses, OMP_CLAUSE_SCHEDULE);
     381         1128 :           if (c == NULL
     382         1110 :               || ((OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
     383              :                   == OMP_CLAUSE_SCHEDULE_STATIC)
     384         1096 :               || omp_find_clause (clauses, OMP_CLAUSE_ORDERED)
     385         1084 :               || omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_)
     386         2212 :               || ((c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_))
     387           14 :                   && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
     388              :             return;
     389              :         }
     390          120 :       else if (region->inner->type == GIMPLE_OMP_SECTIONS
     391          240 :                && (omp_find_clause (gimple_omp_sections_clauses (ws_stmt),
     392              :                                     OMP_CLAUSE__REDUCTEMP_)
     393          120 :                    || omp_find_clause (gimple_omp_sections_clauses (ws_stmt),
     394              :                                        OMP_CLAUSE__CONDTEMP_)))
     395              :         return;
     396              : 
     397         1200 :       region->is_combined_parallel = true;
     398         1200 :       region->inner->is_combined_parallel = true;
     399         1200 :       region->ws_args = get_ws_args_for (par_stmt, ws_stmt,
     400              :                                          is_in_offload_region (region));
     401              :     }
     402              : }
     403              : 
     404              : /* Debugging dumps for parallel regions.  */
     405              : void dump_omp_region (FILE *, struct omp_region *, int);
     406              : void debug_omp_region (struct omp_region *);
     407              : void debug_all_omp_regions (void);
     408              : 
     409              : /* Dump the parallel region tree rooted at REGION.  */
     410              : 
     411              : void
     412          402 : dump_omp_region (FILE *file, struct omp_region *region, int indent)
     413              : {
     414          473 :   fprintf (file, "%*sbb %d: %s\n", indent, "", region->entry->index,
     415          473 :            gimple_code_name[region->type]);
     416              : 
     417          473 :   if (region->inner)
     418          118 :     dump_omp_region (file, region->inner, indent + 4);
     419              : 
     420          473 :   if (region->cont)
     421              :     {
     422          223 :       fprintf (file, "%*sbb %d: GIMPLE_OMP_CONTINUE\n", indent, "",
     423              :                region->cont->index);
     424              :     }
     425              : 
     426          473 :   if (region->exit)
     427          450 :     fprintf (file, "%*sbb %d: GIMPLE_OMP_RETURN\n", indent, "",
     428              :              region->exit->index);
     429              :   else
     430           23 :     fprintf (file, "%*s[no exit marker]\n", indent, "");
     431              : 
     432          473 :   if (region->next)
     433              :     dump_omp_region (file, region->next, indent);
     434          402 : }
     435              : 
     436              : DEBUG_FUNCTION void
     437            0 : debug_omp_region (struct omp_region *region)
     438              : {
     439            0 :   dump_omp_region (stderr, region, 0);
     440            0 : }
     441              : 
     442              : DEBUG_FUNCTION void
     443            0 : debug_all_omp_regions (void)
     444              : {
     445            0 :   dump_omp_region (stderr, root_omp_region, 0);
     446            0 : }
     447              : 
     448              : /* Create a new parallel region starting at STMT inside region PARENT.  */
     449              : 
     450              : static struct omp_region *
     451       258915 : new_omp_region (basic_block bb, enum gimple_code type,
     452              :                 struct omp_region *parent)
     453              : {
     454       258915 :   struct omp_region *region = XCNEW (struct omp_region);
     455              : 
     456       258915 :   region->outer = parent;
     457       258915 :   region->entry = bb;
     458       258915 :   region->type = type;
     459              : 
     460       258915 :   if (parent)
     461              :     {
     462              :       /* This is a nested region.  Add it to the list of inner
     463              :          regions in PARENT.  */
     464       135724 :       region->next = parent->inner;
     465       135724 :       parent->inner = region;
     466              :     }
     467              :   else
     468              :     {
     469              :       /* This is a toplevel region.  Add it to the list of toplevel
     470              :          regions in ROOT_OMP_REGION.  */
     471       123191 :       region->next = root_omp_region;
     472       123191 :       root_omp_region = region;
     473              :     }
     474              : 
     475       258915 :   return region;
     476              : }
     477              : 
     478              : /* Release the memory associated with the region tree rooted at REGION.  */
     479              : 
     480              : static void
     481       258915 : free_omp_region_1 (struct omp_region *region)
     482              : {
     483       258915 :   struct omp_region *i, *n;
     484              : 
     485       394639 :   for (i = region->inner; i ; i = n)
     486              :     {
     487       135724 :       n = i->next;
     488       135724 :       free_omp_region_1 (i);
     489              :     }
     490              : 
     491       258915 :   free (region);
     492       258915 : }
     493              : 
     494              : /* Release the memory for the entire omp region tree.  */
     495              : 
     496              : void
     497      3045760 : omp_free_regions (void)
     498              : {
     499      3045760 :   struct omp_region *r, *n;
     500      3168951 :   for (r = root_omp_region; r ; r = n)
     501              :     {
     502       123191 :       n = r->next;
     503       123191 :       free_omp_region_1 (r);
     504              :     }
     505      3045760 :   root_omp_region = NULL;
     506      3045760 : }
     507              : 
     508              : /* A convenience function to build an empty GIMPLE_COND with just the
     509              :    condition.  */
     510              : 
     511              : static gcond *
     512       118164 : gimple_build_cond_empty (tree cond)
     513              : {
     514       118164 :   enum tree_code pred_code;
     515       118164 :   tree lhs, rhs;
     516              : 
     517       118164 :   gimple_cond_get_ops_from_tree (cond, &pred_code, &lhs, &rhs);
     518       118164 :   return gimple_build_cond (pred_code, lhs, rhs, NULL_TREE, NULL_TREE);
     519              : }
     520              : 
     521              : /* Change DECL_CONTEXT of CHILD_FNDECL to that of the parent function.
     522              :    Add CHILD_FNDECL to decl chain of the supercontext of the block
     523              :    ENTRY_BLOCK - this is the block which originally contained the
     524              :    code from which CHILD_FNDECL was created.
     525              : 
     526              :    Together, these actions ensure that the debug info for the outlined
     527              :    function will be emitted with the correct lexical scope.  */
     528              : 
     529              : static void
     530        45844 : adjust_context_and_scope (struct omp_region *region, tree entry_block,
     531              :                           tree child_fndecl)
     532              : {
     533        45844 :   tree parent_fndecl = NULL_TREE;
     534        45844 :   gimple *entry_stmt;
     535              :   /* OMP expansion expands inner regions before outer ones, so if
     536              :      we e.g. have explicit task region nested in parallel region, when
     537              :      expanding the task region current_function_decl will be the original
     538              :      source function, but we actually want to use as context the child
     539              :      function of the parallel.  */
     540        45844 :   for (region = region->outer;
     541        68960 :        region && parent_fndecl == NULL_TREE; region = region->outer)
     542        23116 :     switch (region->type)
     543              :       {
     544         7522 :       case GIMPLE_OMP_PARALLEL:
     545         7522 :       case GIMPLE_OMP_TASK:
     546         7522 :       case GIMPLE_OMP_TEAMS:
     547         7522 :         entry_stmt = last_nondebug_stmt (region->entry);
     548         7522 :         parent_fndecl = gimple_omp_taskreg_child_fn (entry_stmt);
     549         7522 :         break;
     550         5209 :       case GIMPLE_OMP_TARGET:
     551         5209 :         entry_stmt = last_nondebug_stmt (region->entry);
     552         5209 :         parent_fndecl
     553         5209 :           = gimple_omp_target_child_fn (as_a <gomp_target *> (entry_stmt));
     554         5209 :         break;
     555              :       default:
     556              :         break;
     557              :       }
     558              : 
     559        45844 :   if (parent_fndecl == NULL_TREE)
     560        36848 :     parent_fndecl = current_function_decl;
     561        45844 :   DECL_CONTEXT (child_fndecl) = parent_fndecl;
     562              : 
     563        45844 :   if (entry_block != NULL_TREE && TREE_CODE (entry_block) == BLOCK)
     564              :     {
     565        45810 :       tree b = BLOCK_SUPERCONTEXT (entry_block);
     566        45810 :       if (TREE_CODE (b) == BLOCK)
     567              :         {
     568        45678 :           DECL_CHAIN (child_fndecl) = BLOCK_VARS (b);
     569        45678 :           BLOCK_VARS (b) = child_fndecl;
     570              :         }
     571              :     }
     572        45844 : }
     573              : 
     574              : /* Build the function calls to GOMP_parallel etc to actually
     575              :    generate the parallel operation.  REGION is the parallel region
     576              :    being expanded.  BB is the block where to insert the code.  WS_ARGS
     577              :    will be set if this is a call to a combined parallel+workshare
     578              :    construct, it contains the list of additional arguments needed by
     579              :    the workshare construct.  */
     580              : 
     581              : static void
     582        17174 : expand_parallel_call (struct omp_region *region, basic_block bb,
     583              :                       gomp_parallel *entry_stmt,
     584              :                       vec<tree, va_gc> *ws_args)
     585              : {
     586        17174 :   tree t, t1, t2, val, cond, c, clauses, flags;
     587        17174 :   gimple_stmt_iterator gsi;
     588        17174 :   gimple *stmt;
     589        17174 :   enum built_in_function start_ix;
     590        17174 :   int start_ix2;
     591        17174 :   location_t clause_loc;
     592        17174 :   vec<tree, va_gc> *args;
     593              : 
     594        17174 :   clauses = gimple_omp_parallel_clauses (entry_stmt);
     595              : 
     596              :   /* Determine what flavor of GOMP_parallel we will be
     597              :      emitting.  */
     598        17174 :   start_ix = BUILT_IN_GOMP_PARALLEL;
     599        17174 :   tree rtmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
     600        17174 :   if (rtmp)
     601              :     start_ix = BUILT_IN_GOMP_PARALLEL_REDUCTIONS;
     602        17112 :   else if (is_combined_parallel (region))
     603              :     {
     604         1200 :       switch (region->inner->type)
     605              :         {
     606         1084 :         case GIMPLE_OMP_FOR:
     607         1084 :           gcc_assert (region->inner->sched_kind != OMP_CLAUSE_SCHEDULE_AUTO);
     608         1084 :           switch (region->inner->sched_kind)
     609              :             {
     610          546 :             case OMP_CLAUSE_SCHEDULE_RUNTIME:
     611              :               /* For lastprivate(conditional:), our implementation
     612              :                  requires monotonic behavior.  */
     613          546 :               if (region->inner->has_lastprivate_conditional != 0)
     614              :                 start_ix2 = 3;
     615          544 :               else if ((region->inner->sched_modifiers
     616              :                        & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0)
     617              :                 start_ix2 = 6;
     618          530 :               else if ((region->inner->sched_modifiers
     619              :                         & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0)
     620              :                 start_ix2 = 7;
     621              :               else
     622           16 :                 start_ix2 = 3;
     623              :               break;
     624          538 :             case OMP_CLAUSE_SCHEDULE_DYNAMIC:
     625          538 :             case OMP_CLAUSE_SCHEDULE_GUIDED:
     626          538 :               if ((region->inner->sched_modifiers
     627              :                    & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
     628          534 :                   && !region->inner->has_lastprivate_conditional)
     629              :                 {
     630          522 :                   start_ix2 = 3 + region->inner->sched_kind;
     631          522 :                   break;
     632              :                 }
     633              :               /* FALLTHRU */
     634           16 :             default:
     635           16 :               start_ix2 = region->inner->sched_kind;
     636           16 :               break;
     637              :             }
     638         1084 :           start_ix2 += (int) BUILT_IN_GOMP_PARALLEL_LOOP_STATIC;
     639         1084 :           start_ix = (enum built_in_function) start_ix2;
     640         1084 :           break;
     641              :         case GIMPLE_OMP_SECTIONS:
     642              :           start_ix = BUILT_IN_GOMP_PARALLEL_SECTIONS;
     643              :           break;
     644            0 :         default:
     645            0 :           gcc_unreachable ();
     646              :         }
     647              :     }
     648              : 
     649              :   /* By default, the value of NUM_THREADS is zero (selected at run time)
     650              :      and there is no conditional.  */
     651        17174 :   cond = NULL_TREE;
     652        17174 :   val = build_int_cst (unsigned_type_node, 0);
     653        17174 :   flags = build_int_cst (unsigned_type_node, 0);
     654              : 
     655        17174 :   c = omp_find_clause (clauses, OMP_CLAUSE_IF);
     656        17174 :   if (c)
     657          973 :     cond = OMP_CLAUSE_IF_EXPR (c);
     658              : 
     659        17174 :   c = omp_find_clause (clauses, OMP_CLAUSE_NUM_THREADS);
     660        17174 :   if (c)
     661              :     {
     662         2341 :       val = OMP_CLAUSE_NUM_THREADS_EXPR (c);
     663         2341 :       clause_loc = OMP_CLAUSE_LOCATION (c);
     664         2341 :       if (TREE_CODE (val) == TREE_LIST)
     665              :         {
     666          957 :           if (OMP_CLAUSE_NUM_THREADS_DIMS (c)
     667          957 :               && (TREE_CHAIN (val) || OMP_CLAUSE_NUM_THREADS_STRICT (c)))
     668              :             /* Accept 'dim(1),relaxed'. */
     669            1 :             sorry_at (clause_loc,
     670              :                       "%<num_threads%> clause with %<dims%> modifier");
     671          956 :           else if (TREE_CHAIN (val))
     672            1 :             sorry_at (clause_loc,
     673              :                       "%<num_threads%> clause with more than one argument");
     674          955 :           else if (OMP_CLAUSE_NUM_THREADS_STRICT (c))
     675            0 :             sorry_at (clause_loc,
     676              :                       "%<num_threads%> clause with %<strict%> modifier");
     677          957 :           val = TREE_VALUE (val);
     678              :         }
     679         1384 :       else if (OMP_CLAUSE_NUM_THREADS_STRICT (c))
     680            0 :         sorry_at (clause_loc,
     681              :                   "%<num_threads%> clause with %<strict%> modifier");
     682              :       /* FIXME: Handle OMP_CLAUSE_MESSAGE (only relevant with 'strict').  */
     683              :     }
     684              :   else
     685        14833 :     clause_loc = gimple_location (entry_stmt);
     686              : 
     687        17174 :   c = omp_find_clause (clauses, OMP_CLAUSE_PROC_BIND);
     688        17174 :   if (c)
     689          771 :     flags = build_int_cst (unsigned_type_node, OMP_CLAUSE_PROC_BIND_KIND (c));
     690              : 
     691              :   /* Ensure 'val' is of the correct type.  */
     692        17174 :   val = fold_convert_loc (clause_loc, unsigned_type_node, val);
     693              : 
     694              :   /* If we found the clause 'if (cond)', build either
     695              :      (cond != 0) or (cond ? val : 1u).  */
     696        17174 :   if (cond)
     697              :     {
     698          973 :       cond = gimple_boolify (cond);
     699              : 
     700          973 :       if (integer_zerop (val))
     701          186 :         val = fold_build2_loc (clause_loc,
     702              :                            EQ_EXPR, unsigned_type_node, cond,
     703          186 :                            build_int_cst (TREE_TYPE (cond), 0));
     704              :       else
     705              :         {
     706          787 :           basic_block cond_bb, then_bb, else_bb;
     707          787 :           edge e, e_then, e_else;
     708          787 :           tree tmp_then, tmp_else, tmp_join, tmp_var;
     709              : 
     710          787 :           tmp_var = create_tmp_var (TREE_TYPE (val));
     711          787 :           if (gimple_in_ssa_p (cfun))
     712              :             {
     713            0 :               tmp_then = make_ssa_name (tmp_var);
     714            0 :               tmp_else = make_ssa_name (tmp_var);
     715            0 :               tmp_join = make_ssa_name (tmp_var);
     716              :             }
     717              :           else
     718              :             {
     719              :               tmp_then = tmp_var;
     720              :               tmp_else = tmp_var;
     721              :               tmp_join = tmp_var;
     722              :             }
     723              : 
     724          787 :           e = split_block_after_labels (bb);
     725          787 :           cond_bb = e->src;
     726          787 :           bb = e->dest;
     727          787 :           remove_edge (e);
     728              : 
     729          787 :           then_bb = create_empty_bb (cond_bb);
     730          787 :           else_bb = create_empty_bb (then_bb);
     731          787 :           set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
     732          787 :           set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
     733              : 
     734          787 :           stmt = gimple_build_cond_empty (cond);
     735          787 :           gsi = gsi_start_bb (cond_bb);
     736          787 :           gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
     737              : 
     738          787 :           gsi = gsi_start_bb (then_bb);
     739          787 :           expand_omp_build_assign (&gsi, tmp_then, val, true);
     740              : 
     741          787 :           gsi = gsi_start_bb (else_bb);
     742          787 :           expand_omp_build_assign (&gsi, tmp_else,
     743              :                                    build_int_cst (unsigned_type_node, 1),
     744              :                                    true);
     745              : 
     746          787 :           make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
     747          787 :           make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
     748          787 :           add_bb_to_loop (then_bb, cond_bb->loop_father);
     749          787 :           add_bb_to_loop (else_bb, cond_bb->loop_father);
     750          787 :           e_then = make_edge (then_bb, bb, EDGE_FALLTHRU);
     751          787 :           e_else = make_edge (else_bb, bb, EDGE_FALLTHRU);
     752              : 
     753          787 :           if (gimple_in_ssa_p (cfun))
     754              :             {
     755            0 :               gphi *phi = create_phi_node (tmp_join, bb);
     756            0 :               add_phi_arg (phi, tmp_then, e_then, UNKNOWN_LOCATION);
     757            0 :               add_phi_arg (phi, tmp_else, e_else, UNKNOWN_LOCATION);
     758              :             }
     759              : 
     760          787 :           val = tmp_join;
     761              :         }
     762              : 
     763          973 :       gsi = gsi_start_bb (bb);
     764          973 :       val = force_gimple_operand_gsi (&gsi, val, true, NULL_TREE,
     765              :                                       false, GSI_CONTINUE_LINKING);
     766              :     }
     767              : 
     768        17174 :   gsi = gsi_last_nondebug_bb (bb);
     769        17174 :   t = gimple_omp_parallel_data_arg (entry_stmt);
     770        17174 :   if (t == NULL)
     771         2440 :     t1 = null_pointer_node;
     772              :   else
     773        14734 :     t1 = build_fold_addr_expr (t);
     774        17174 :   tree child_fndecl = gimple_omp_parallel_child_fn (entry_stmt);
     775        17174 :   t2 = build_fold_addr_expr (child_fndecl);
     776              : 
     777        18374 :   vec_alloc (args, 4 + vec_safe_length (ws_args));
     778        17174 :   args->quick_push (t2);
     779        17174 :   args->quick_push (t1);
     780        17174 :   args->quick_push (val);
     781        17174 :   if (ws_args)
     782         1200 :     args->splice (*ws_args);
     783        17174 :   args->quick_push (flags);
     784              : 
     785        17174 :   t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
     786              :                                builtin_decl_explicit (start_ix), args);
     787              : 
     788        17174 :   if (rtmp)
     789              :     {
     790           62 :       tree type = TREE_TYPE (OMP_CLAUSE_DECL (rtmp));
     791           62 :       t = build2 (MODIFY_EXPR, type, OMP_CLAUSE_DECL (rtmp),
     792              :                   fold_convert (type,
     793              :                                 fold_convert (pointer_sized_int_node, t)));
     794              :     }
     795        17174 :   force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
     796              :                             false, GSI_CONTINUE_LINKING);
     797        17174 : }
     798              : 
     799              : /* Build the function call to GOMP_task to actually
     800              :    generate the task operation.  BB is the block where to insert the code.  */
     801              : 
     802              : static void
     803         3844 : expand_task_call (struct omp_region *region, basic_block bb,
     804              :                   gomp_task *entry_stmt)
     805              : {
     806         3844 :   tree t1, t2, t3;
     807         3844 :   gimple_stmt_iterator gsi;
     808         3844 :   location_t loc = gimple_location (entry_stmt);
     809              : 
     810         3844 :   tree clauses = gimple_omp_task_clauses (entry_stmt);
     811              : 
     812         3844 :   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
     813         3844 :   tree untied = omp_find_clause (clauses, OMP_CLAUSE_UNTIED);
     814         3844 :   tree mergeable = omp_find_clause (clauses, OMP_CLAUSE_MERGEABLE);
     815         3844 :   tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
     816         3844 :   tree finalc = omp_find_clause (clauses, OMP_CLAUSE_FINAL);
     817         3844 :   tree priority = omp_find_clause (clauses, OMP_CLAUSE_PRIORITY);
     818         3844 :   tree detach = omp_find_clause (clauses, OMP_CLAUSE_DETACH);
     819              : 
     820         7688 :   unsigned int iflags
     821         3844 :     = (untied ? GOMP_TASK_FLAG_UNTIED : 0)
     822         3844 :       | (mergeable ? GOMP_TASK_FLAG_MERGEABLE : 0)
     823         3844 :       | (depend ? GOMP_TASK_FLAG_DEPEND : 0);
     824              : 
     825         3844 :   bool taskloop_p = gimple_omp_task_taskloop_p (entry_stmt);
     826         3844 :   tree startvar = NULL_TREE, endvar = NULL_TREE, step = NULL_TREE;
     827         3844 :   tree num_tasks = NULL_TREE;
     828         3844 :   bool ull = false;
     829         3844 :   if (taskloop_p)
     830              :     {
     831         1337 :       gimple *g = last_nondebug_stmt (region->outer->entry);
     832         1337 :       gcc_assert (gimple_code (g) == GIMPLE_OMP_FOR
     833              :                   && gimple_omp_for_kind (g) == GF_OMP_FOR_KIND_TASKLOOP);
     834         1337 :       struct omp_for_data fd;
     835         1337 :       omp_extract_for_data (as_a <gomp_for *> (g), &fd, NULL);
     836         1337 :       startvar = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
     837         1337 :       endvar = omp_find_clause (OMP_CLAUSE_CHAIN (startvar),
     838              :                                 OMP_CLAUSE__LOOPTEMP_);
     839         1337 :       startvar = OMP_CLAUSE_DECL (startvar);
     840         1337 :       endvar = OMP_CLAUSE_DECL (endvar);
     841         1337 :       step = fold_convert_loc (loc, fd.iter_type, fd.loop.step);
     842         1337 :       if (fd.loop.cond_code == LT_EXPR)
     843         1285 :         iflags |= GOMP_TASK_FLAG_UP;
     844         1337 :       tree tclauses = gimple_omp_for_clauses (g);
     845         1337 :       num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_NUM_TASKS);
     846         1337 :       if (num_tasks)
     847              :         {
     848          176 :           if (OMP_CLAUSE_NUM_TASKS_STRICT (num_tasks))
     849            3 :             iflags |= GOMP_TASK_FLAG_STRICT;
     850          176 :           num_tasks = OMP_CLAUSE_NUM_TASKS_EXPR (num_tasks);
     851              :         }
     852              :       else
     853              :         {
     854         1161 :           num_tasks = omp_find_clause (tclauses, OMP_CLAUSE_GRAINSIZE);
     855         1161 :           if (num_tasks)
     856              :             {
     857          220 :               iflags |= GOMP_TASK_FLAG_GRAINSIZE;
     858          220 :               if (OMP_CLAUSE_GRAINSIZE_STRICT (num_tasks))
     859            3 :                 iflags |= GOMP_TASK_FLAG_STRICT;
     860          220 :               num_tasks = OMP_CLAUSE_GRAINSIZE_EXPR (num_tasks);
     861              :             }
     862              :           else
     863          941 :             num_tasks = integer_zero_node;
     864              :         }
     865         1337 :       num_tasks = fold_convert_loc (loc, long_integer_type_node, num_tasks);
     866         1337 :       if (ifc == NULL_TREE)
     867          966 :         iflags |= GOMP_TASK_FLAG_IF;
     868         1337 :       if (omp_find_clause (tclauses, OMP_CLAUSE_NOGROUP))
     869           57 :         iflags |= GOMP_TASK_FLAG_NOGROUP;
     870         1337 :       ull = fd.iter_type == long_long_unsigned_type_node;
     871         1337 :       if (omp_find_clause (clauses, OMP_CLAUSE_REDUCTION))
     872          497 :         iflags |= GOMP_TASK_FLAG_REDUCTION;
     873              :     }
     874              :   else
     875              :     {
     876         2507 :       if (priority)
     877           18 :         iflags |= GOMP_TASK_FLAG_PRIORITY;
     878         2507 :       if (detach)
     879          166 :         iflags |= GOMP_TASK_FLAG_DETACH;
     880              :     }
     881              : 
     882         3844 :   tree flags = build_int_cst (unsigned_type_node, iflags);
     883              : 
     884         3844 :   tree cond = boolean_true_node;
     885         3844 :   if (ifc)
     886              :     {
     887          484 :       if (taskloop_p)
     888              :         {
     889          371 :           tree t = gimple_boolify (OMP_CLAUSE_IF_EXPR (ifc));
     890          371 :           t = fold_build3_loc (loc, COND_EXPR, unsigned_type_node, t,
     891              :                                build_int_cst (unsigned_type_node,
     892              :                                               GOMP_TASK_FLAG_IF),
     893              :                                build_int_cst (unsigned_type_node, 0));
     894          371 :           flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node,
     895              :                                    flags, t);
     896              :         }
     897              :       else
     898          113 :         cond = gimple_boolify (OMP_CLAUSE_IF_EXPR (ifc));
     899              :     }
     900              : 
     901         3844 :   if (finalc)
     902              :     {
     903          410 :       tree t = gimple_boolify (OMP_CLAUSE_FINAL_EXPR (finalc));
     904          410 :       t = fold_build3_loc (loc, COND_EXPR, unsigned_type_node, t,
     905              :                            build_int_cst (unsigned_type_node,
     906              :                                           GOMP_TASK_FLAG_FINAL),
     907              :                            build_int_cst (unsigned_type_node, 0));
     908          410 :       flags = fold_build2_loc (loc, PLUS_EXPR, unsigned_type_node, flags, t);
     909              :     }
     910         3844 :   if (depend)
     911         1105 :     depend = OMP_CLAUSE_DECL (depend);
     912              :   else
     913         2739 :     depend = build_int_cst (ptr_type_node, 0);
     914         3844 :   if (priority)
     915          372 :     priority = fold_convert (integer_type_node,
     916              :                              OMP_CLAUSE_PRIORITY_EXPR (priority));
     917              :   else
     918         3472 :     priority = integer_zero_node;
     919              : 
     920         3844 :   gsi = gsi_last_nondebug_bb (bb);
     921              : 
     922         3678 :   detach = (detach
     923         4010 :             ? build_fold_addr_expr (OMP_CLAUSE_DECL (detach))
     924              :             : null_pointer_node);
     925              : 
     926         3844 :   tree t = gimple_omp_task_data_arg (entry_stmt);
     927         3844 :   if (t == NULL)
     928          728 :     t2 = null_pointer_node;
     929              :   else
     930         3116 :     t2 = build_fold_addr_expr_loc (loc, t);
     931         3844 :   t1 = build_fold_addr_expr_loc (loc, gimple_omp_task_child_fn (entry_stmt));
     932         3844 :   t = gimple_omp_task_copy_fn (entry_stmt);
     933         3844 :   if (t == NULL)
     934         3330 :     t3 = null_pointer_node;
     935              :   else
     936          514 :     t3 = build_fold_addr_expr_loc (loc, t);
     937              : 
     938         3844 :   if (taskloop_p)
     939         2674 :     t = build_call_expr (ull
     940           42 :                          ? builtin_decl_explicit (BUILT_IN_GOMP_TASKLOOP_ULL)
     941         1295 :                          : builtin_decl_explicit (BUILT_IN_GOMP_TASKLOOP),
     942              :                          11, t1, t2, t3,
     943              :                          gimple_omp_task_arg_size (entry_stmt),
     944              :                          gimple_omp_task_arg_align (entry_stmt), flags,
     945              :                          num_tasks, priority, startvar, endvar, step);
     946              :   else
     947         2507 :     t = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_TASK),
     948              :                          10, t1, t2, t3,
     949              :                          gimple_omp_task_arg_size (entry_stmt),
     950              :                          gimple_omp_task_arg_align (entry_stmt), cond, flags,
     951              :                          depend, priority, detach);
     952              : 
     953         3844 :   force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
     954              :                             false, GSI_CONTINUE_LINKING);
     955         3844 : }
     956              : 
     957              : /* Build the function call to GOMP_taskwait_depend to actually
     958              :    generate the taskwait operation.  BB is the block where to insert the
     959              :    code.  */
     960              : 
     961              : static void
     962           84 : expand_taskwait_call (basic_block bb, gomp_task *entry_stmt)
     963              : {
     964           84 :   tree clauses = gimple_omp_task_clauses (entry_stmt);
     965           84 :   tree depend = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
     966           84 :   if (depend == NULL_TREE)
     967            0 :     return;
     968              : 
     969           84 :   depend = OMP_CLAUSE_DECL (depend);
     970              : 
     971           84 :   bool nowait = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT) != NULL_TREE;
     972           84 :   gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
     973           51 :   enum built_in_function f = (nowait
     974           84 :                               ? BUILT_IN_GOMP_TASKWAIT_DEPEND_NOWAIT
     975              :                               : BUILT_IN_GOMP_TASKWAIT_DEPEND);
     976           84 :   tree t = build_call_expr (builtin_decl_explicit (f), 1, depend);
     977              : 
     978           84 :   force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
     979              :                             false, GSI_CONTINUE_LINKING);
     980              : }
     981              : 
     982              : /* Build the function call to GOMP_teams_reg to actually
     983              :    generate the host teams operation.  REGION is the teams region
     984              :    being expanded.  BB is the block where to insert the code.  */
     985              : 
     986              : static void
     987         2578 : expand_teams_call (basic_block bb, gomp_teams *entry_stmt)
     988              : {
     989         2578 :   tree clauses = gimple_omp_teams_clauses (entry_stmt);
     990         2578 :   tree num_teams = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
     991         2578 :   tree thread_limit = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
     992         2578 :   tree message = omp_find_clause (clauses, OMP_CLAUSE_MESSAGE);
     993         2578 :   if (message
     994         2578 :       && ((thread_limit && OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit))
     995            1 :           || (num_teams && (TREE_CODE (num_teams) != INTEGER_CST
     996            0 :                             || tree_int_cst_sgn (num_teams) < 0))))
     997            0 :     sorry_at (OMP_CLAUSE_LOCATION (message), "%<message%> clause");
     998         2578 :   if (num_teams == NULL_TREE)
     999         2306 :     num_teams = build_int_cst (unsigned_type_node, 0);
    1000              :   else
    1001              :     {
    1002          272 :       tree expr = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
    1003              :       /* FIXME: Implement lower-bound handling as well; however, currently,
    1004              :          on the host the use-specified value is always used.  */
    1005          272 :       if (TREE_CODE (expr) == TREE_LIST
    1006            5 :           && OMP_CLAUSE_NUM_TEAMS_DIMS (num_teams)
    1007          277 :           && TREE_CHAIN (expr))
    1008              :         {
    1009            2 :           sorry_at (OMP_CLAUSE_LOCATION (num_teams),
    1010              :                     "%<num_teams%> clause with %<dims%> modifier");
    1011            2 :           expr = TREE_VALUE (expr);
    1012              :         }
    1013          270 :       else if (TREE_CODE (expr) == TREE_LIST)
    1014            3 :         expr = (TREE_CHAIN (expr)
    1015            3 :                 ? TREE_VALUE (TREE_CHAIN (expr)) : TREE_VALUE (expr));
    1016          272 :       num_teams = fold_convert (unsigned_type_node, expr);
    1017              :     }
    1018         2578 :   if (thread_limit == NULL_TREE)
    1019         2431 :     thread_limit = build_int_cst (unsigned_type_node, 0);
    1020              :   else
    1021              :     {
    1022          147 :       tree expr = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
    1023          147 :       if (TREE_CODE (expr) == TREE_LIST)
    1024              :         {
    1025           16 :           if (OMP_CLAUSE_THREAD_LIMIT_DIMS (thread_limit)
    1026           16 :               && (TREE_CHAIN (expr)
    1027            0 :                   || OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit)))
    1028            1 :             sorry_at (OMP_CLAUSE_LOCATION (thread_limit),
    1029              :                       "%<thread_limit%> clause with %<dims%> modifier");
    1030           15 :           else if (OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit))
    1031            1 :             sorry_at (OMP_CLAUSE_LOCATION (thread_limit),
    1032              :                       "%<thread_limit%> clause with %<strict%> modifier");
    1033           16 :           expr = TREE_VALUE (expr);
    1034              :         }
    1035          131 :       else if (OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit))
    1036            0 :         sorry_at (OMP_CLAUSE_LOCATION (thread_limit),
    1037              :                   "%<thread_limit%> clause with %<strict%> modifier");
    1038          147 :       thread_limit = fold_convert (unsigned_type_node, expr);
    1039              :     }
    1040              : 
    1041         2578 :   gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
    1042         2578 :   tree t = gimple_omp_teams_data_arg (entry_stmt), t1;
    1043         2578 :   if (t == NULL)
    1044         1381 :     t1 = null_pointer_node;
    1045              :   else
    1046         1197 :     t1 = build_fold_addr_expr (t);
    1047         2578 :   tree child_fndecl = gimple_omp_teams_child_fn (entry_stmt);
    1048         2578 :   tree t2 = build_fold_addr_expr (child_fndecl);
    1049              : 
    1050         2578 :   vec<tree, va_gc> *args;
    1051         2578 :   vec_alloc (args, 5);
    1052         2578 :   args->quick_push (t2);
    1053         2578 :   args->quick_push (t1);
    1054         2578 :   args->quick_push (num_teams);
    1055         2578 :   args->quick_push (thread_limit);
    1056              :   /* For future extensibility: flags.  */
    1057         2578 :   args->quick_push (build_zero_cst (unsigned_type_node));
    1058              : 
    1059         2578 :   t = build_call_expr_loc_vec (UNKNOWN_LOCATION,
    1060              :                                builtin_decl_explicit (BUILT_IN_GOMP_TEAMS_REG),
    1061              :                                args);
    1062              : 
    1063         2578 :   force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    1064              :                             false, GSI_CONTINUE_LINKING);
    1065         2578 : }
    1066              : 
    1067              : /* Chain all the DECLs in LIST by their TREE_CHAIN fields.  */
    1068              : 
    1069              : static tree
    1070        45844 : vec2chain (vec<tree, va_gc> *v)
    1071              : {
    1072        45844 :   tree chain = NULL_TREE, t;
    1073        45844 :   unsigned ix;
    1074              : 
    1075       470256 :   FOR_EACH_VEC_SAFE_ELT_REVERSE (v, ix, t)
    1076              :     {
    1077       384152 :       DECL_CHAIN (t) = chain;
    1078       384152 :       chain = t;
    1079              :     }
    1080              : 
    1081        45844 :   return chain;
    1082              : }
    1083              : 
    1084              : /* Remove barriers in REGION->EXIT's block.  Note that this is only
    1085              :    valid for GIMPLE_OMP_PARALLEL regions.  Since the end of a parallel region
    1086              :    is an implicit barrier, any workshare inside the GIMPLE_OMP_PARALLEL that
    1087              :    left a barrier at the end of the GIMPLE_OMP_PARALLEL region can now be
    1088              :    removed.  */
    1089              : 
    1090              : static void
    1091        12516 : remove_exit_barrier (struct omp_region *region)
    1092              : {
    1093        12516 :   gimple_stmt_iterator gsi;
    1094        12516 :   basic_block exit_bb;
    1095        12516 :   edge_iterator ei;
    1096        12516 :   edge e;
    1097        12516 :   gimple *stmt;
    1098        12516 :   int any_addressable_vars = -1;
    1099              : 
    1100        12516 :   exit_bb = region->exit;
    1101              : 
    1102              :   /* If the parallel region doesn't return, we don't have REGION->EXIT
    1103              :      block at all.  */
    1104        12516 :   if (! exit_bb)
    1105         1246 :     return;
    1106              : 
    1107              :   /* The last insn in the block will be the parallel's GIMPLE_OMP_RETURN.  The
    1108              :      workshare's GIMPLE_OMP_RETURN will be in a preceding block.  The kinds of
    1109              :      statements that can appear in between are extremely limited -- no
    1110              :      memory operations at all.  Here, we allow nothing at all, so the
    1111              :      only thing we allow to precede this GIMPLE_OMP_RETURN is a label.  */
    1112        12488 :   gsi = gsi_last_nondebug_bb (exit_bb);
    1113        12488 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
    1114        12488 :   gsi_prev_nondebug (&gsi);
    1115        12488 :   if (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
    1116              :     return;
    1117              : 
    1118        22929 :   FOR_EACH_EDGE (e, ei, exit_bb->preds)
    1119              :     {
    1120        11659 :       gsi = gsi_last_nondebug_bb (e->src);
    1121        11659 :       if (gsi_end_p (gsi))
    1122           46 :         continue;
    1123        11613 :       stmt = gsi_stmt (gsi);
    1124        11613 :       if (gimple_code (stmt) == GIMPLE_OMP_RETURN
    1125        11613 :           && !gimple_omp_return_nowait_p (stmt))
    1126              :         {
    1127              :           /* OpenMP 3.0 tasks unfortunately prevent this optimization
    1128              :              in many cases.  If there could be tasks queued, the barrier
    1129              :              might be needed to let the tasks run before some local
    1130              :              variable of the parallel that the task uses as shared
    1131              :              runs out of scope.  The task can be spawned either
    1132              :              from within current function (this would be easy to check)
    1133              :              or from some function it calls and gets passed an address
    1134              :              of such a variable.  */
    1135         1068 :           if (any_addressable_vars < 0)
    1136              :             {
    1137         1068 :               gomp_parallel *parallel_stmt
    1138         1068 :                 = as_a <gomp_parallel *> (last_nondebug_stmt (region->entry));
    1139         1068 :               tree child_fun = gimple_omp_parallel_child_fn (parallel_stmt);
    1140         1068 :               tree local_decls, block, decl;
    1141         1068 :               unsigned ix;
    1142              : 
    1143         1068 :               any_addressable_vars = 0;
    1144         8662 :               FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (child_fun), ix, decl)
    1145         7148 :                 if (TREE_ADDRESSABLE (decl))
    1146              :                   {
    1147              :                     any_addressable_vars = 1;
    1148              :                     break;
    1149              :                   }
    1150         1069 :               for (block = gimple_block (stmt);
    1151         1069 :                    !any_addressable_vars
    1152         1069 :                    && block
    1153         1069 :                    && TREE_CODE (block) == BLOCK;
    1154            1 :                    block = BLOCK_SUPERCONTEXT (block))
    1155              :                 {
    1156            2 :                   for (local_decls = BLOCK_VARS (block);
    1157            2 :                        local_decls;
    1158            0 :                        local_decls = DECL_CHAIN (local_decls))
    1159            0 :                     if (TREE_ADDRESSABLE (local_decls))
    1160              :                       {
    1161              :                         any_addressable_vars = 1;
    1162              :                         break;
    1163              :                       }
    1164            2 :                   if (block == gimple_block (parallel_stmt))
    1165              :                     break;
    1166              :                 }
    1167              :             }
    1168         1068 :           if (!any_addressable_vars)
    1169          532 :             gimple_omp_return_set_nowait (stmt);
    1170              :         }
    1171              :     }
    1172              : }
    1173              : 
    1174              : static void
    1175        73621 : remove_exit_barriers (struct omp_region *region)
    1176              : {
    1177        73621 :   if (region->type == GIMPLE_OMP_PARALLEL)
    1178        12516 :     remove_exit_barrier (region);
    1179              : 
    1180        73621 :   if (region->inner)
    1181              :     {
    1182        41053 :       region = region->inner;
    1183        41053 :       remove_exit_barriers (region);
    1184        88824 :       while (region->next)
    1185              :         {
    1186         6718 :           region = region->next;
    1187         6718 :           remove_exit_barriers (region);
    1188              :         }
    1189              :     }
    1190        73621 : }
    1191              : 
    1192              : /* Optimize omp_get_thread_num () and omp_get_num_threads ()
    1193              :    calls.  These can't be declared as const functions, but
    1194              :    within one parallel body they are constant, so they can be
    1195              :    transformed there into __builtin_omp_get_{thread_num,num_threads} ()
    1196              :    which are declared const.  Similarly for task body, except
    1197              :    that in untied task omp_get_thread_num () can change at any task
    1198              :    scheduling point.  */
    1199              : 
    1200              : static void
    1201        17696 : optimize_omp_library_calls (gimple *entry_stmt)
    1202              : {
    1203        17696 :   basic_block bb;
    1204        17696 :   gimple_stmt_iterator gsi;
    1205        17696 :   tree thr_num_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
    1206        17696 :   tree thr_num_id = DECL_ASSEMBLER_NAME (thr_num_tree);
    1207        17696 :   tree num_thr_tree = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
    1208        17696 :   tree num_thr_id = DECL_ASSEMBLER_NAME (num_thr_tree);
    1209        17696 :   bool untied_task = (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
    1210        20165 :                       && omp_find_clause (gimple_omp_task_clauses (entry_stmt),
    1211        17696 :                                           OMP_CLAUSE_UNTIED) != NULL);
    1212              : 
    1213       313124 :   FOR_EACH_BB_FN (bb, cfun)
    1214      1652629 :     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    1215              :       {
    1216      1061773 :         gimple *call = gsi_stmt (gsi);
    1217      1061773 :         tree decl;
    1218              : 
    1219      1061773 :         if (is_gimple_call (call)
    1220        78670 :             && (decl = gimple_call_fndecl (call))
    1221        69942 :             && DECL_EXTERNAL (decl)
    1222        58095 :             && TREE_PUBLIC (decl)
    1223      1119868 :             && DECL_INITIAL (decl) == NULL)
    1224              :           {
    1225        58094 :             tree built_in;
    1226              : 
    1227        58094 :             if (DECL_NAME (decl) == thr_num_id)
    1228              :               {
    1229              :                 /* In #pragma omp task untied omp_get_thread_num () can change
    1230              :                    during the execution of the task region.  */
    1231         1334 :                 if (untied_task)
    1232            0 :                   continue;
    1233         1334 :                 built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_THREAD_NUM);
    1234              :               }
    1235        56760 :             else if (DECL_NAME (decl) == num_thr_id)
    1236          415 :               built_in = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
    1237              :             else
    1238        56345 :               continue;
    1239              : 
    1240         1749 :             if (DECL_ASSEMBLER_NAME (decl) != DECL_ASSEMBLER_NAME (built_in)
    1241         1749 :                 || gimple_call_num_args (call) != 0)
    1242         1195 :               continue;
    1243              : 
    1244          554 :             if (flag_exceptions && !TREE_NOTHROW (decl))
    1245            0 :               continue;
    1246              : 
    1247          554 :             if (TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE
    1248         1108 :                 || !types_compatible_p (TREE_TYPE (TREE_TYPE (decl)),
    1249          554 :                                         TREE_TYPE (TREE_TYPE (built_in))))
    1250            0 :               continue;
    1251              : 
    1252          554 :             gimple_call_set_fndecl (call, built_in);
    1253              :           }
    1254              :       }
    1255        17696 : }
    1256              : 
    1257              : /* Callback for expand_omp_build_assign.  Return non-NULL if *tp needs to be
    1258              :    regimplified.  */
    1259              : 
    1260              : static tree
    1261       300186 : expand_omp_regimplify_p (tree *tp, int *walk_subtrees, void *)
    1262              : {
    1263       300186 :   tree t = *tp;
    1264              : 
    1265              :   /* Any variable with DECL_VALUE_EXPR needs to be regimplified.  */
    1266       300186 :   if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
    1267              :     return t;
    1268              : 
    1269       300090 :   if (TREE_CODE (t) == ADDR_EXPR)
    1270         1763 :     recompute_tree_invariant_for_addr_expr (t);
    1271              : 
    1272       300090 :   *walk_subtrees = !TYPE_P (t) && !DECL_P (t);
    1273       300090 :   return NULL_TREE;
    1274              : }
    1275              : 
    1276              : /* Prepend or append TO = FROM assignment before or after *GSI_P.  */
    1277              : 
    1278              : static void
    1279        74935 : expand_omp_build_assign (gimple_stmt_iterator *gsi_p, tree to, tree from,
    1280              :                          bool after)
    1281              : {
    1282        74935 :   bool simple_p = DECL_P (to) && TREE_ADDRESSABLE (to);
    1283       146377 :   from = force_gimple_operand_gsi (gsi_p, from, simple_p, NULL_TREE,
    1284              :                                    !after, after ? GSI_CONTINUE_LINKING
    1285              :                                                  : GSI_SAME_STMT);
    1286        74935 :   gimple *stmt = gimple_build_assign (to, from);
    1287        74935 :   if (after)
    1288         3493 :     gsi_insert_after (gsi_p, stmt, GSI_CONTINUE_LINKING);
    1289              :   else
    1290        71442 :     gsi_insert_before (gsi_p, stmt, GSI_SAME_STMT);
    1291        74935 :   if (walk_tree (&from, expand_omp_regimplify_p, NULL, NULL)
    1292        74935 :       || walk_tree (&to, expand_omp_regimplify_p, NULL, NULL))
    1293              :     {
    1294           80 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    1295           80 :       gimple_regimplify_operands (stmt, &gsi);
    1296              :     }
    1297        74935 : }
    1298              : 
    1299              : /* Prepend or append LHS CODE RHS condition before or after *GSI_P.  */
    1300              : 
    1301              : static gcond *
    1302         8837 : expand_omp_build_cond (gimple_stmt_iterator *gsi_p, enum tree_code code,
    1303              :                        tree lhs, tree rhs, bool after = false)
    1304              : {
    1305         8837 :   gcond *cond_stmt = gimple_build_cond (code, lhs, rhs, NULL_TREE, NULL_TREE);
    1306         8837 :   if (after)
    1307          236 :     gsi_insert_after (gsi_p, cond_stmt, GSI_CONTINUE_LINKING);
    1308              :   else
    1309         8601 :     gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT);
    1310         8837 :   if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
    1311              :                  NULL, NULL)
    1312         8837 :       || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
    1313              :                     NULL, NULL))
    1314              :     {
    1315            6 :       gimple_stmt_iterator gsi = gsi_for_stmt (cond_stmt);
    1316            6 :       gimple_regimplify_operands (cond_stmt, &gsi);
    1317              :     }
    1318         8837 :   return cond_stmt;
    1319              : }
    1320              : 
    1321              : /* Expand the OpenMP parallel or task directive starting at REGION.  */
    1322              : 
    1323              : static void
    1324        23680 : expand_omp_taskreg (struct omp_region *region)
    1325              : {
    1326        23680 :   basic_block entry_bb, exit_bb, new_bb;
    1327        23680 :   struct function *child_cfun;
    1328        23680 :   tree child_fn, block, t;
    1329        23680 :   gimple_stmt_iterator gsi;
    1330        23680 :   gimple *entry_stmt, *stmt;
    1331        23680 :   edge e;
    1332        23680 :   vec<tree, va_gc> *ws_args;
    1333              : 
    1334        23680 :   entry_stmt = last_nondebug_stmt (region->entry);
    1335        23680 :   if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK
    1336        23680 :       && gimple_omp_task_taskwait_p (entry_stmt))
    1337              :     {
    1338           84 :       new_bb = region->entry;
    1339           84 :       gsi = gsi_last_nondebug_bb (region->entry);
    1340           84 :       gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TASK);
    1341           84 :       gsi_remove (&gsi, true);
    1342           84 :       expand_taskwait_call (new_bb, as_a <gomp_task *> (entry_stmt));
    1343           84 :       return;
    1344              :     }
    1345              : 
    1346        23596 :   child_fn = gimple_omp_taskreg_child_fn (entry_stmt);
    1347        23596 :   child_cfun = DECL_STRUCT_FUNCTION (child_fn);
    1348              : 
    1349        23596 :   entry_bb = region->entry;
    1350        23596 :   if (gimple_code (entry_stmt) == GIMPLE_OMP_TASK)
    1351         3844 :     exit_bb = region->cont;
    1352              :   else
    1353        19752 :     exit_bb = region->exit;
    1354              : 
    1355        23596 :   if (is_combined_parallel (region))
    1356         1200 :     ws_args = region->ws_args;
    1357              :   else
    1358              :     ws_args = NULL;
    1359              : 
    1360        23596 :   if (child_cfun->cfg)
    1361              :     {
    1362              :       /* Due to inlining, it may happen that we have already outlined
    1363              :          the region, in which case all we need to do is make the
    1364              :          sub-graph unreachable and emit the parallel call.  */
    1365            0 :       edge entry_succ_e, exit_succ_e;
    1366              : 
    1367            0 :       entry_succ_e = single_succ_edge (entry_bb);
    1368              : 
    1369            0 :       gsi = gsi_last_nondebug_bb (entry_bb);
    1370            0 :       gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_PARALLEL
    1371              :                   || gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TASK
    1372              :                   || gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_TEAMS);
    1373            0 :       gsi_remove (&gsi, true);
    1374              : 
    1375            0 :       new_bb = entry_bb;
    1376            0 :       if (exit_bb)
    1377              :         {
    1378            0 :           exit_succ_e = single_succ_edge (exit_bb);
    1379            0 :           make_edge (new_bb, exit_succ_e->dest, EDGE_FALLTHRU);
    1380              :         }
    1381            0 :       remove_edge_and_dominated_blocks (entry_succ_e);
    1382              :     }
    1383              :   else
    1384              :     {
    1385        23596 :       unsigned srcidx, dstidx, num;
    1386              : 
    1387              :       /* If the parallel region needs data sent from the parent
    1388              :          function, then the very first statement (except possible
    1389              :          tree profile counter updates) of the parallel body
    1390              :          is a copy assignment .OMP_DATA_I = &.OMP_DATA_O.  Since
    1391              :          &.OMP_DATA_O is passed as an argument to the child function,
    1392              :          we need to replace it with the argument as seen by the child
    1393              :          function.
    1394              : 
    1395              :          In most cases, this will end up being the identity assignment
    1396              :          .OMP_DATA_I = .OMP_DATA_I.  However, if the parallel body had
    1397              :          a function call that has been inlined, the original PARM_DECL
    1398              :          .OMP_DATA_I may have been converted into a different local
    1399              :          variable.  In which case, we need to keep the assignment.  */
    1400        23596 :       if (gimple_omp_taskreg_data_arg (entry_stmt))
    1401              :         {
    1402        19047 :           basic_block entry_succ_bb
    1403        34978 :             = single_succ_p (entry_bb) ? single_succ (entry_bb)
    1404         3116 :                                        : FALLTHRU_EDGE (entry_bb)->dest;
    1405        19047 :           tree arg;
    1406        19047 :           gassign *parcopy_stmt = NULL;
    1407              : 
    1408        38094 :           for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
    1409              :             {
    1410        19047 :               gcc_assert (!gsi_end_p (gsi));
    1411        19047 :               gassign *stmt = dyn_cast<gassign *> (gsi_stmt (gsi));
    1412        19047 :               if (!stmt)
    1413            0 :                 continue;
    1414              : 
    1415        19047 :               if (gimple_num_ops (stmt) == 2)
    1416              :                 {
    1417        19047 :                   tree arg = gimple_assign_rhs1 (stmt);
    1418              : 
    1419              :                   /* We're ignore the subcode because we're
    1420              :                      effectively doing a STRIP_NOPS.  */
    1421              : 
    1422        19047 :                   if (TREE_CODE (arg) == ADDR_EXPR
    1423        19047 :                       && (TREE_OPERAND (arg, 0)
    1424        19047 :                           == gimple_omp_taskreg_data_arg (entry_stmt)))
    1425              :                     {
    1426        19047 :                       parcopy_stmt = stmt;
    1427        19047 :                       break;
    1428              :                     }
    1429              :                 }
    1430            0 :             }
    1431              : 
    1432        19047 :           gcc_assert (parcopy_stmt != NULL);
    1433        19047 :           arg = DECL_ARGUMENTS (child_fn);
    1434              : 
    1435        19047 :           if (!gimple_in_ssa_p (cfun))
    1436              :             {
    1437        18866 :               if (gimple_assign_lhs (parcopy_stmt) == arg)
    1438        18866 :                 gsi_remove (&gsi, true);
    1439              :               else
    1440              :                 {
    1441              :                   /* ?? Is setting the subcode really necessary ??  */
    1442            0 :                   gimple_omp_set_subcode (parcopy_stmt, TREE_CODE (arg));
    1443            0 :                   gimple_assign_set_rhs1 (parcopy_stmt, arg);
    1444              :                 }
    1445              :             }
    1446              :           else
    1447              :             {
    1448          181 :               tree lhs = gimple_assign_lhs (parcopy_stmt);
    1449          181 :               gcc_assert (SSA_NAME_VAR (lhs) == arg);
    1450              :               /* We'd like to set the rhs to the default def in the child_fn,
    1451              :                  but it's too early to create ssa names in the child_fn.
    1452              :                  Instead, we set the rhs to the parm.  In
    1453              :                  move_sese_region_to_fn, we introduce a default def for the
    1454              :                  parm, map the parm to it's default def, and once we encounter
    1455              :                  this stmt, replace the parm with the default def.  */
    1456          181 :               gimple_assign_set_rhs1 (parcopy_stmt, arg);
    1457          181 :               update_stmt (parcopy_stmt);
    1458              :             }
    1459              :         }
    1460              : 
    1461              :       /* Declare local variables needed in CHILD_CFUN.  */
    1462        23596 :       block = DECL_INITIAL (child_fn);
    1463        23596 :       BLOCK_VARS (block) = vec2chain (child_cfun->local_decls);
    1464              :       /* The gimplifier could record temporaries in parallel/task block
    1465              :          rather than in containing function's local_decls chain,
    1466              :          which would mean cgraph missed finalizing them.  Do it now.  */
    1467       267327 :       for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
    1468       243731 :         if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
    1469            1 :           varpool_node::finalize_decl (t);
    1470        23596 :       DECL_SAVED_TREE (child_fn) = NULL;
    1471              :       /* We'll create a CFG for child_fn, so no gimple body is needed.  */
    1472        23596 :       gimple_set_body (child_fn, NULL);
    1473        23596 :       TREE_USED (block) = 1;
    1474              : 
    1475              :       /* Reset DECL_CONTEXT on function arguments.  */
    1476        47192 :       for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
    1477        23596 :         DECL_CONTEXT (t) = child_fn;
    1478              : 
    1479              :       /* Split ENTRY_BB at GIMPLE_OMP_PARALLEL or GIMPLE_OMP_TASK,
    1480              :          so that it can be moved to the child function.  */
    1481        23596 :       gsi = gsi_last_nondebug_bb (entry_bb);
    1482        23596 :       stmt = gsi_stmt (gsi);
    1483        23596 :       gcc_assert (stmt && (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
    1484              :                            || gimple_code (stmt) == GIMPLE_OMP_TASK
    1485              :                            || gimple_code (stmt) == GIMPLE_OMP_TEAMS));
    1486        23596 :       e = split_block (entry_bb, stmt);
    1487        23596 :       gsi_remove (&gsi, true);
    1488        23596 :       entry_bb = e->dest;
    1489        23596 :       edge e2 = NULL;
    1490        23596 :       if (gimple_code (entry_stmt) != GIMPLE_OMP_TASK)
    1491        19752 :         single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
    1492              :       else
    1493              :         {
    1494         3844 :           e2 = make_edge (e->src, BRANCH_EDGE (entry_bb)->dest, EDGE_ABNORMAL);
    1495         3844 :           gcc_assert (e2->dest == region->exit);
    1496         3844 :           remove_edge (BRANCH_EDGE (entry_bb));
    1497         3844 :           set_immediate_dominator (CDI_DOMINATORS, e2->dest, e->src);
    1498         3844 :           gsi = gsi_last_nondebug_bb (region->exit);
    1499         3844 :           gcc_assert (!gsi_end_p (gsi)
    1500              :                       && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
    1501         3844 :           gsi_remove (&gsi, true);
    1502              :         }
    1503              : 
    1504              :       /* Convert GIMPLE_OMP_{RETURN,CONTINUE} into a RETURN_EXPR.  */
    1505        23596 :       if (exit_bb)
    1506              :         {
    1507        23551 :           gsi = gsi_last_nondebug_bb (exit_bb);
    1508        43275 :           gcc_assert (!gsi_end_p (gsi)
    1509              :                       && (gimple_code (gsi_stmt (gsi))
    1510              :                           == (e2 ? GIMPLE_OMP_CONTINUE : GIMPLE_OMP_RETURN)));
    1511        23551 :           stmt = gimple_build_return (NULL);
    1512        23551 :           gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
    1513        23551 :           gsi_remove (&gsi, true);
    1514              :         }
    1515              : 
    1516              :       /* Move the parallel region into CHILD_CFUN.  */
    1517              : 
    1518        23596 :       if (gimple_in_ssa_p (cfun))
    1519              :         {
    1520          193 :           init_tree_ssa (child_cfun);
    1521          193 :           init_ssa_operands (child_cfun);
    1522          193 :           child_cfun->gimple_df->in_ssa_p = true;
    1523          193 :           block = NULL_TREE;
    1524              :         }
    1525              :       else
    1526        23403 :         block = gimple_block (entry_stmt);
    1527              : 
    1528        23596 :       new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
    1529        23596 :       if (exit_bb)
    1530        23551 :         single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
    1531        23596 :       if (e2)
    1532              :         {
    1533         3844 :           basic_block dest_bb = e2->dest;
    1534         3844 :           if (!exit_bb)
    1535           17 :             make_edge (new_bb, dest_bb, EDGE_FALLTHRU);
    1536         3844 :           remove_edge (e2);
    1537         3844 :           set_immediate_dominator (CDI_DOMINATORS, dest_bb, new_bb);
    1538              :         }
    1539              :       /* When the OMP expansion process cannot guarantee an up-to-date
    1540              :          loop tree arrange for the child function to fixup loops.  */
    1541        23596 :       if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
    1542        23403 :         child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
    1543              : 
    1544              :       /* Remove non-local VAR_DECLs from child_cfun->local_decls list.  */
    1545        23596 :       num = vec_safe_length (child_cfun->local_decls);
    1546      1135957 :       for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
    1547              :         {
    1548      1112361 :           t = (*child_cfun->local_decls)[srcidx];
    1549      1112361 :           if (DECL_CONTEXT (t) == cfun->decl)
    1550       243731 :             continue;
    1551       868630 :           if (srcidx != dstidx)
    1552       864784 :             (*child_cfun->local_decls)[dstidx] = t;
    1553       868630 :           dstidx++;
    1554              :         }
    1555        23596 :       if (dstidx != num)
    1556        21383 :         vec_safe_truncate (child_cfun->local_decls, dstidx);
    1557              : 
    1558              :       /* Inform the callgraph about the new function.  */
    1559        23596 :       child_cfun->curr_properties = cfun->curr_properties;
    1560        23596 :       child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
    1561        23596 :       child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
    1562        23596 :       cgraph_node *node = cgraph_node::get_create (child_fn);
    1563        23596 :       node->parallelized_function = 1;
    1564        47192 :       node->has_omp_variant_constructs
    1565        23596 :         |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
    1566        23596 :       cgraph_node::add_new_function (child_fn, true);
    1567              : 
    1568        23596 :       bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
    1569        23596 :                       && !DECL_ASSEMBLER_NAME_SET_P (child_fn);
    1570              : 
    1571              :       /* Fix the callgraph edges for child_cfun.  Those for cfun will be
    1572              :          fixed in a following pass.  */
    1573        23596 :       push_cfun (child_cfun);
    1574        23596 :       if (need_asm)
    1575        23403 :         assign_assembler_name_if_needed (child_fn);
    1576              : 
    1577        23596 :       if (optimize)
    1578        17696 :         optimize_omp_library_calls (entry_stmt);
    1579        23596 :       update_max_bb_count ();
    1580        23596 :       cgraph_edge::rebuild_edges ();
    1581              : 
    1582              :       /* Some EH regions might become dead, see PR34608.  If
    1583              :          pass_cleanup_cfg isn't the first pass to happen with the
    1584              :          new child, these dead EH edges might cause problems.
    1585              :          Clean them up now.  */
    1586        23596 :       if (flag_exceptions)
    1587              :         {
    1588        10634 :           basic_block bb;
    1589        10634 :           bool changed = false;
    1590              : 
    1591       144233 :           FOR_EACH_BB_FN (bb, cfun)
    1592       133599 :             changed |= gimple_purge_dead_eh_edges (bb);
    1593        10634 :           if (changed)
    1594            0 :             cleanup_tree_cfg ();
    1595              :         }
    1596        23596 :       if (gimple_in_ssa_p (cfun))
    1597          193 :         update_ssa (TODO_update_ssa);
    1598        23596 :       if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
    1599          193 :         verify_loop_structure ();
    1600        23596 :       pop_cfun ();
    1601              : 
    1602        23596 :       if (dump_file && !gimple_in_ssa_p (cfun))
    1603              :         {
    1604           88 :           omp_any_child_fn_dumped = true;
    1605           88 :           dump_function_header (dump_file, child_fn, dump_flags);
    1606           88 :           dump_function_to_file (child_fn, dump_file, dump_flags);
    1607              :         }
    1608              :     }
    1609              : 
    1610        23596 :   adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
    1611              : 
    1612        23596 :   if (gimple_code (entry_stmt) == GIMPLE_OMP_PARALLEL)
    1613        17174 :     expand_parallel_call (region, new_bb,
    1614              :                           as_a <gomp_parallel *> (entry_stmt), ws_args);
    1615         6422 :   else if (gimple_code (entry_stmt) == GIMPLE_OMP_TEAMS)
    1616         2578 :     expand_teams_call (new_bb, as_a <gomp_teams *> (entry_stmt));
    1617              :   else
    1618         3844 :     expand_task_call (region, new_bb, as_a <gomp_task *> (entry_stmt));
    1619              : }
    1620              : 
    1621              : /* Information about members of an OpenACC collapsed loop nest.  */
    1622              : 
    1623              : struct oacc_collapse
    1624              : {
    1625              :   tree base;  /* Base value.  */
    1626              :   tree iters; /* Number of steps.  */
    1627              :   tree step;  /* Step size.  */
    1628              :   tree tile;  /* Tile increment (if tiled).  */
    1629              :   tree outer; /* Tile iterator var. */
    1630              : };
    1631              : 
    1632              : /* Helper for expand_oacc_for.  Determine collapsed loop information.
    1633              :    Fill in COUNTS array.  Emit any initialization code before GSI.
    1634              :    Return the calculated outer loop bound of BOUND_TYPE.  */
    1635              : 
    1636              : static tree
    1637          594 : expand_oacc_collapse_init (const struct omp_for_data *fd,
    1638              :                            gimple_stmt_iterator *gsi,
    1639              :                            oacc_collapse *counts, tree diff_type,
    1640              :                            tree bound_type, location_t loc)
    1641              : {
    1642          594 :   tree tiling = fd->tiling;
    1643          594 :   tree total = build_int_cst (bound_type, 1);
    1644          594 :   int ix;
    1645              : 
    1646          594 :   gcc_assert (integer_onep (fd->loop.step));
    1647          594 :   gcc_assert (integer_zerop (fd->loop.n1));
    1648              : 
    1649              :   /* When tiling, the first operand of the tile clause applies to the
    1650              :      innermost loop, and we work outwards from there.  Seems
    1651              :      backwards, but whatever.  */
    1652         1890 :   for (ix = fd->collapse; ix--;)
    1653              :     {
    1654         1296 :       const omp_for_data_loop *loop = &fd->loops[ix];
    1655              : 
    1656         1296 :       tree iter_type = TREE_TYPE (loop->v);
    1657         1296 :       tree plus_type = iter_type;
    1658              : 
    1659         1296 :       gcc_assert (loop->cond_code == LT_EXPR || loop->cond_code == GT_EXPR);
    1660              : 
    1661         1296 :       if (POINTER_TYPE_P (iter_type))
    1662            0 :         plus_type = sizetype;
    1663              : 
    1664         1296 :       if (tiling)
    1665              :         {
    1666          284 :           tree num = build_int_cst (integer_type_node, fd->collapse);
    1667          284 :           tree loop_no = build_int_cst (integer_type_node, ix);
    1668          284 :           tree tile = TREE_VALUE (tiling);
    1669          284 :           gcall *call
    1670          284 :             = gimple_build_call_internal (IFN_GOACC_TILE, 5, num, loop_no, tile,
    1671              :                                           /* gwv-outer=*/integer_zero_node,
    1672              :                                           /* gwv-inner=*/integer_zero_node);
    1673              : 
    1674          284 :           counts[ix].outer = create_tmp_var (iter_type, ".outer");
    1675          284 :           counts[ix].tile = create_tmp_var (diff_type, ".tile");
    1676          284 :           gimple_call_set_lhs (call, counts[ix].tile);
    1677          284 :           gimple_set_location (call, loc);
    1678          284 :           gsi_insert_before (gsi, call, GSI_SAME_STMT);
    1679              : 
    1680          284 :           tiling = TREE_CHAIN (tiling);
    1681              :         }
    1682              :       else
    1683              :         {
    1684         1012 :           counts[ix].tile = NULL;
    1685         1012 :           counts[ix].outer = loop->v;
    1686              :         }
    1687              : 
    1688         1296 :       tree b = loop->n1;
    1689         1296 :       tree e = loop->n2;
    1690         1296 :       tree s = loop->step;
    1691         1296 :       bool up = loop->cond_code == LT_EXPR;
    1692         1344 :       tree dir = build_int_cst (diff_type, up ? +1 : -1);
    1693         1296 :       bool negating;
    1694         1296 :       tree expr;
    1695              : 
    1696         1296 :       b = force_gimple_operand_gsi (gsi, b, true, NULL_TREE,
    1697              :                                     true, GSI_SAME_STMT);
    1698         1296 :       e = force_gimple_operand_gsi (gsi, e, true, NULL_TREE,
    1699              :                                     true, GSI_SAME_STMT);
    1700              : 
    1701              :       /* Convert the step, avoiding possible unsigned->signed overflow.  */
    1702         1296 :       negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
    1703            0 :       if (negating)
    1704            0 :         s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
    1705         1296 :       s = fold_convert (diff_type, s);
    1706         1296 :       if (negating)
    1707            0 :         s = fold_build1 (NEGATE_EXPR, diff_type, s);
    1708         1296 :       s = force_gimple_operand_gsi (gsi, s, true, NULL_TREE,
    1709              :                                     true, GSI_SAME_STMT);
    1710              : 
    1711              :       /* Determine the range, avoiding possible unsigned->signed overflow.  */
    1712         1296 :       negating = !up && TYPE_UNSIGNED (iter_type);
    1713         2592 :       expr = fold_build2 (MINUS_EXPR, plus_type,
    1714              :                           fold_convert (plus_type, negating ? b : e),
    1715              :                           fold_convert (plus_type, negating ? e : b));
    1716         1296 :       expr = fold_convert (diff_type, expr);
    1717         1296 :       if (negating)
    1718            0 :         expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
    1719         1296 :       tree range = force_gimple_operand_gsi
    1720         1296 :         (gsi, expr, true, NULL_TREE, true, GSI_SAME_STMT);
    1721              : 
    1722              :       /* Determine number of iterations.  */
    1723         1296 :       expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
    1724         1296 :       expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
    1725         1296 :       expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
    1726              : 
    1727         1296 :       tree iters = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE,
    1728              :                                              true, GSI_SAME_STMT);
    1729              : 
    1730         1296 :       counts[ix].base = b;
    1731         1296 :       counts[ix].iters = iters;
    1732         1296 :       counts[ix].step = s;
    1733              : 
    1734         1296 :       total = fold_build2 (MULT_EXPR, bound_type, total,
    1735              :                            fold_convert (bound_type, iters));
    1736              :     }
    1737              : 
    1738          594 :   return total;
    1739              : }
    1740              : 
    1741              : /* Emit initializers for collapsed loop members.  INNER is true if
    1742              :    this is for the element loop of a TILE.  IVAR is the outer
    1743              :    loop iteration variable, from which collapsed loop iteration values
    1744              :    are  calculated.  COUNTS array has been initialized by
    1745              :    expand_oacc_collapse_inits.  */
    1746              : 
    1747              : static void
    1748          771 : expand_oacc_collapse_vars (const struct omp_for_data *fd, bool inner,
    1749              :                            gimple_stmt_iterator *gsi,
    1750              :                            const oacc_collapse *counts, tree ivar,
    1751              :                            tree diff_type)
    1752              : {
    1753          771 :   tree ivar_type = TREE_TYPE (ivar);
    1754              : 
    1755              :   /*  The most rapidly changing iteration variable is the innermost
    1756              :       one.  */
    1757         2351 :   for (int ix = fd->collapse; ix--;)
    1758              :     {
    1759         1580 :       const omp_for_data_loop *loop = &fd->loops[ix];
    1760         1580 :       const oacc_collapse *collapse = &counts[ix];
    1761         1580 :       tree v = inner ? loop->v : collapse->outer;
    1762         1580 :       tree iter_type = TREE_TYPE (v);
    1763         1580 :       tree plus_type = iter_type;
    1764         1580 :       enum tree_code plus_code = PLUS_EXPR;
    1765         1580 :       tree expr;
    1766              : 
    1767         1580 :       if (POINTER_TYPE_P (iter_type))
    1768              :         {
    1769            0 :           plus_code = POINTER_PLUS_EXPR;
    1770            0 :           plus_type = sizetype;
    1771              :         }
    1772              : 
    1773         1580 :       expr = ivar;
    1774         1580 :       if (ix)
    1775              :         {
    1776          809 :           tree mod = fold_convert (ivar_type, collapse->iters);
    1777          809 :           ivar = fold_build2 (TRUNC_DIV_EXPR, ivar_type, expr, mod);
    1778          809 :           expr = fold_build2 (TRUNC_MOD_EXPR, ivar_type, expr, mod);
    1779          809 :           ivar = force_gimple_operand_gsi (gsi, ivar, true, NULL_TREE,
    1780              :                                            true, GSI_SAME_STMT);
    1781              :         }
    1782              : 
    1783         1580 :       expr = fold_build2 (MULT_EXPR, diff_type, fold_convert (diff_type, expr),
    1784              :                           fold_convert (diff_type, collapse->step));
    1785         1580 :       expr = fold_build2 (plus_code, iter_type,
    1786              :                           inner ? collapse->outer : collapse->base,
    1787              :                           fold_convert (plus_type, expr));
    1788         1580 :       expr = force_gimple_operand_gsi (gsi, expr, false, NULL_TREE,
    1789              :                                        true, GSI_SAME_STMT);
    1790         1580 :       gassign *ass = gimple_build_assign (v, expr);
    1791         1580 :       gsi_insert_before (gsi, ass, GSI_SAME_STMT);
    1792              :     }
    1793          771 : }
    1794              : 
    1795              : /* Helper function for expand_omp_{for_*,simd}.  If this is the outermost
    1796              :    of the combined collapse > 1 loop constructs, generate code like:
    1797              :         if (__builtin_expect (N32 cond3 N31, 0)) goto ZERO_ITER_BB;
    1798              :         if (cond3 is <)
    1799              :           adj = STEP3 - 1;
    1800              :         else
    1801              :           adj = STEP3 + 1;
    1802              :         count3 = (adj + N32 - N31) / STEP3;
    1803              :         if (__builtin_expect (N22 cond2 N21, 0)) goto ZERO_ITER_BB;
    1804              :         if (cond2 is <)
    1805              :           adj = STEP2 - 1;
    1806              :         else
    1807              :           adj = STEP2 + 1;
    1808              :         count2 = (adj + N22 - N21) / STEP2;
    1809              :         if (__builtin_expect (N12 cond1 N11, 0)) goto ZERO_ITER_BB;
    1810              :         if (cond1 is <)
    1811              :           adj = STEP1 - 1;
    1812              :         else
    1813              :           adj = STEP1 + 1;
    1814              :         count1 = (adj + N12 - N11) / STEP1;
    1815              :         count = count1 * count2 * count3;
    1816              :    Furthermore, if ZERO_ITER_BB is NULL, create a BB which does:
    1817              :         count = 0;
    1818              :    and set ZERO_ITER_BB to that bb.  If this isn't the outermost
    1819              :    of the combined loop constructs, just initialize COUNTS array
    1820              :    from the _looptemp_ clauses.  For loop nests with non-rectangular
    1821              :    loops, do this only for the rectangular loops.  Then pick
    1822              :    the loops which reference outer vars in their bound expressions
    1823              :    and the loops which they refer to and for this sub-nest compute
    1824              :    number of iterations.  For triangular loops use Faulhaber's formula,
    1825              :    otherwise as a fallback, compute by iterating the loops.
    1826              :    If e.g. the sub-nest is
    1827              :         for (I = N11; I COND1 N12; I += STEP1)
    1828              :         for (J = M21 * I + N21; J COND2 M22 * I + N22; J += STEP2)
    1829              :         for (K = M31 * J + N31; K COND3 M32 * J + N32; K += STEP3)
    1830              :    do:
    1831              :         COUNT = 0;
    1832              :         for (tmpi = N11; tmpi COND1 N12; tmpi += STEP1)
    1833              :         for (tmpj = M21 * tmpi + N21;
    1834              :              tmpj COND2 M22 * tmpi + N22; tmpj += STEP2)
    1835              :           {
    1836              :             int tmpk1 = M31 * tmpj + N31;
    1837              :             int tmpk2 = M32 * tmpj + N32;
    1838              :             if (tmpk1 COND3 tmpk2)
    1839              :               {
    1840              :                 if (COND3 is <)
    1841              :                   adj = STEP3 - 1;
    1842              :                 else
    1843              :                   adj = STEP3 + 1;
    1844              :                 COUNT += (adj + tmpk2 - tmpk1) / STEP3;
    1845              :               }
    1846              :           }
    1847              :    and finally multiply the counts of the rectangular loops not
    1848              :    in the sub-nest with COUNT.  Also, as counts[fd->last_nonrect]
    1849              :    store number of iterations of the loops from fd->first_nonrect
    1850              :    to fd->last_nonrect inclusive, i.e. the above COUNT multiplied
    1851              :    by the counts of rectangular loops not referenced in any non-rectangular
    1852              :    loops sandwiched in between those.  */
    1853              : 
    1854              : /* NOTE: It *could* be better to moosh all of the BBs together,
    1855              :    creating one larger BB with all the computation and the unexpected
    1856              :    jump at the end.  I.e.
    1857              : 
    1858              :    bool zero3, zero2, zero1, zero;
    1859              : 
    1860              :    zero3 = N32 c3 N31;
    1861              :    count3 = (N32 - N31) /[cl] STEP3;
    1862              :    zero2 = N22 c2 N21;
    1863              :    count2 = (N22 - N21) /[cl] STEP2;
    1864              :    zero1 = N12 c1 N11;
    1865              :    count1 = (N12 - N11) /[cl] STEP1;
    1866              :    zero = zero3 || zero2 || zero1;
    1867              :    count = count1 * count2 * count3;
    1868              :    if (__builtin_expect(zero, false)) goto zero_iter_bb;
    1869              : 
    1870              :    After all, we expect the zero=false, and thus we expect to have to
    1871              :    evaluate all of the comparison expressions, so short-circuiting
    1872              :    oughtn't be a win.  Since the condition isn't protecting a
    1873              :    denominator, we're not concerned about divide-by-zero, so we can
    1874              :    fully evaluate count even if a numerator turned out to be wrong.
    1875              : 
    1876              :    It seems like putting this all together would create much better
    1877              :    scheduling opportunities, and less pressure on the chip's branch
    1878              :    predictor.  */
    1879              : 
    1880              : static void
    1881        12442 : expand_omp_for_init_counts (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
    1882              :                             basic_block &entry_bb, tree *counts,
    1883              :                             basic_block &zero_iter1_bb, int &first_zero_iter1,
    1884              :                             basic_block &zero_iter2_bb, int &first_zero_iter2,
    1885              :                             basic_block &l2_dom_bb)
    1886              : {
    1887        12442 :   tree t, type = TREE_TYPE (fd->loop.v);
    1888        12442 :   edge e, ne;
    1889        12442 :   int i;
    1890              : 
    1891              :   /* Collapsed loops need work for expansion into SSA form.  */
    1892        12442 :   gcc_assert (!gimple_in_ssa_p (cfun));
    1893              : 
    1894        12442 :   if (gimple_omp_for_combined_into_p (fd->for_stmt)
    1895        12442 :       && TREE_CODE (fd->loop.n2) != INTEGER_CST)
    1896              :     {
    1897         3082 :       gcc_assert (fd->ordered == 0);
    1898              :       /* First two _looptemp_ clauses are for istart/iend, counts[0]
    1899              :          isn't supposed to be handled, as the inner loop doesn't
    1900              :          use it.  */
    1901         3082 :       tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    1902              :                                      OMP_CLAUSE__LOOPTEMP_);
    1903         3082 :       gcc_assert (innerc);
    1904        11767 :       for (i = 0; i < fd->collapse; i++)
    1905              :         {
    1906         8685 :           innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    1907              :                                     OMP_CLAUSE__LOOPTEMP_);
    1908         8685 :           gcc_assert (innerc);
    1909         8685 :           if (i)
    1910         5603 :             counts[i] = OMP_CLAUSE_DECL (innerc);
    1911              :           else
    1912         3082 :             counts[0] = NULL_TREE;
    1913              :         }
    1914         3082 :       if (fd->non_rect
    1915          112 :           && fd->last_nonrect == fd->first_nonrect + 1
    1916         3146 :           && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
    1917              :         {
    1918              :           tree c[4];
    1919          300 :           for (i = 0; i < 4; i++)
    1920              :             {
    1921          240 :               innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    1922              :                                         OMP_CLAUSE__LOOPTEMP_);
    1923          240 :               gcc_assert (innerc);
    1924          240 :               c[i] = OMP_CLAUSE_DECL (innerc);
    1925              :             }
    1926           60 :           counts[0] = c[0];
    1927           60 :           fd->first_inner_iterations = c[1];
    1928           60 :           fd->factor = c[2];
    1929           60 :           fd->adjn1 = c[3];
    1930              :         }
    1931              :       return;
    1932              :     }
    1933              : 
    1934        10253 :   for (i = fd->collapse; i < fd->ordered; i++)
    1935              :     {
    1936          893 :       tree itype = TREE_TYPE (fd->loops[i].v);
    1937          893 :       counts[i] = NULL_TREE;
    1938          893 :       t = fold_binary (fd->loops[i].cond_code, boolean_type_node,
    1939              :                        fold_convert (itype, fd->loops[i].n1),
    1940              :                        fold_convert (itype, fd->loops[i].n2));
    1941          893 :       if (t && integer_zerop (t))
    1942              :         {
    1943            0 :           for (i = fd->collapse; i < fd->ordered; i++)
    1944            0 :             counts[i] = build_int_cst (type, 0);
    1945              :           break;
    1946              :         }
    1947              :     }
    1948         9360 :   bool rect_count_seen = false;
    1949         9360 :   bool init_n2 = SSA_VAR_P (fd->loop.n2) && zero_iter1_bb;
    1950        35038 :   for (i = 0; i < (fd->ordered ? fd->ordered : fd->collapse); i++)
    1951              :     {
    1952        25678 :       tree itype = TREE_TYPE (fd->loops[i].v);
    1953              : 
    1954        25678 :       if (i >= fd->collapse && counts[i])
    1955            0 :         continue;
    1956        25678 :       if (fd->non_rect)
    1957              :         {
    1958              :           /* Skip loops that use outer iterators in their expressions
    1959              :              during this phase.  */
    1960         1050 :           if (fd->loops[i].m1 || fd->loops[i].m2)
    1961              :             {
    1962          432 :               counts[i] = build_zero_cst (type);
    1963          432 :               continue;
    1964              :             }
    1965              :         }
    1966        25246 :       if ((SSA_VAR_P (fd->loop.n2) || i >= fd->collapse)
    1967         9986 :           && ((t = fold_binary (fd->loops[i].cond_code, boolean_type_node,
    1968              :                                 fold_convert (itype, fd->loops[i].n1),
    1969              :                                 fold_convert (itype, fd->loops[i].n2)))
    1970         6371 :               == NULL_TREE || !integer_onep (t)))
    1971              :         {
    1972         6191 :           gcond *cond_stmt;
    1973         6191 :           tree n1, n2;
    1974         6191 :           if (init_n2 && i < fd->collapse && !rect_count_seen)
    1975              :             {
    1976              :               /* When called with non-NULL zero_iter1_bb, we won't clear
    1977              :                  fd->loop.n2 in the if (zero_iter_bb == NULL) code below
    1978              :                  and if it is prior to storing fd->loop.n2 where
    1979              :                  rect_count_seen is set, it could be used uninitialized.
    1980              :                  As zero_iter1_bb in that case can be reached also if there
    1981              :                  are non-zero iterations, the clearing can't be emitted
    1982              :                  to the zero_iter1_bb, but needs to be done before the
    1983              :                  condition.  */
    1984         1727 :               gassign *assign_stmt
    1985         1727 :                 = gimple_build_assign (fd->loop.n2, build_zero_cst (type));
    1986         1727 :               gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
    1987         1727 :               init_n2 = false;
    1988              :             }
    1989         6191 :           n1 = fold_convert (itype, unshare_expr (fd->loops[i].n1));
    1990         6191 :           n1 = force_gimple_operand_gsi (gsi, n1, true, NULL_TREE,
    1991              :                                          true, GSI_SAME_STMT);
    1992         6191 :           n2 = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    1993         6191 :           n2 = force_gimple_operand_gsi (gsi, n2, true, NULL_TREE,
    1994              :                                          true, GSI_SAME_STMT);
    1995         6191 :           cond_stmt = expand_omp_build_cond (gsi, fd->loops[i].cond_code,
    1996              :                                              n1, n2);
    1997         6191 :           e = split_block (entry_bb, cond_stmt);
    1998          216 :           basic_block &zero_iter_bb
    1999         6191 :             = i < fd->collapse ? zero_iter1_bb : zero_iter2_bb;
    2000          216 :           int &first_zero_iter
    2001         6191 :             = i < fd->collapse ? first_zero_iter1 : first_zero_iter2;
    2002         6191 :           if (zero_iter_bb == NULL)
    2003              :             {
    2004          626 :               gassign *assign_stmt;
    2005          626 :               first_zero_iter = i;
    2006          626 :               zero_iter_bb = create_empty_bb (entry_bb);
    2007          626 :               add_bb_to_loop (zero_iter_bb, entry_bb->loop_father);
    2008          626 :               *gsi = gsi_after_labels (zero_iter_bb);
    2009          626 :               if (i < fd->collapse)
    2010          508 :                 assign_stmt = gimple_build_assign (fd->loop.n2,
    2011              :                                                    build_zero_cst (type));
    2012              :               else
    2013              :                 {
    2014          118 :                   counts[i] = create_tmp_reg (type, ".count");
    2015          118 :                   assign_stmt
    2016          118 :                     = gimple_build_assign (counts[i], build_zero_cst (type));
    2017              :                 }
    2018          626 :               gsi_insert_before (gsi, assign_stmt, GSI_SAME_STMT);
    2019          626 :               set_immediate_dominator (CDI_DOMINATORS, zero_iter_bb,
    2020              :                                        entry_bb);
    2021              :             }
    2022         6191 :           ne = make_edge (entry_bb, zero_iter_bb, EDGE_FALSE_VALUE);
    2023         6191 :           ne->probability = profile_probability::very_unlikely ();
    2024         6191 :           e->flags = EDGE_TRUE_VALUE;
    2025         6191 :           e->probability = ne->probability.invert ();
    2026         6191 :           if (l2_dom_bb == NULL)
    2027         2813 :             l2_dom_bb = entry_bb;
    2028         6191 :           entry_bb = e->dest;
    2029         6191 :           *gsi = gsi_last_nondebug_bb (entry_bb);
    2030              :         }
    2031              : 
    2032        25246 :       if (POINTER_TYPE_P (itype))
    2033         1667 :         itype = signed_type_for (itype);
    2034        25246 :       t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
    2035        30434 :                                  ? -1 : 1));
    2036        25246 :       t = fold_build2 (PLUS_EXPR, itype,
    2037              :                        fold_convert (itype, fd->loops[i].step), t);
    2038        25246 :       t = fold_build2 (PLUS_EXPR, itype, t,
    2039              :                        fold_convert (itype, fd->loops[i].n2));
    2040        25246 :       t = fold_build2 (MINUS_EXPR, itype, t,
    2041              :                        fold_convert (itype, fd->loops[i].n1));
    2042              :       /* ?? We could probably use CEIL_DIV_EXPR instead of
    2043              :          TRUNC_DIV_EXPR and adjusting by hand.  Unless we can't
    2044              :          generate the same code in the end because generically we
    2045              :          don't know that the values involved must be negative for
    2046              :          GT??  */
    2047        25246 :       if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
    2048         2300 :         t = fold_build2 (TRUNC_DIV_EXPR, itype,
    2049              :                          fold_build1 (NEGATE_EXPR, itype, t),
    2050              :                          fold_build1 (NEGATE_EXPR, itype,
    2051              :                                       fold_convert (itype,
    2052              :                                                     fd->loops[i].step)));
    2053              :       else
    2054        22946 :         t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
    2055              :                          fold_convert (itype, fd->loops[i].step));
    2056        25246 :       t = fold_convert (type, t);
    2057        25246 :       if (TREE_CODE (t) == INTEGER_CST)
    2058        18985 :         counts[i] = t;
    2059              :       else
    2060              :         {
    2061         6261 :           if (i < fd->collapse || i != first_zero_iter2)
    2062         6143 :             counts[i] = create_tmp_reg (type, ".count");
    2063         6261 :           expand_omp_build_assign (gsi, counts[i], t);
    2064              :         }
    2065        25246 :       if (SSA_VAR_P (fd->loop.n2) && i < fd->collapse)
    2066              :         {
    2067         9093 :           if (fd->non_rect && i >= fd->first_nonrect && i <= fd->last_nonrect)
    2068          376 :             continue;
    2069         8717 :           if (!rect_count_seen)
    2070              :             {
    2071         3217 :               t = counts[i];
    2072         3217 :               rect_count_seen = true;
    2073              :             }
    2074              :           else
    2075         5500 :             t = fold_build2 (MULT_EXPR, type, fd->loop.n2, counts[i]);
    2076         8717 :           expand_omp_build_assign (gsi, fd->loop.n2, t);
    2077              :         }
    2078              :     }
    2079         9360 :   if (fd->non_rect && SSA_VAR_P (fd->loop.n2))
    2080              :     {
    2081          284 :       gcc_assert (fd->last_nonrect != -1);
    2082              : 
    2083          284 :       counts[fd->last_nonrect] = create_tmp_reg (type, ".count");
    2084          284 :       expand_omp_build_assign (gsi, counts[fd->last_nonrect],
    2085              :                                build_zero_cst (type));
    2086          366 :       for (i = fd->first_nonrect + 1; i < fd->last_nonrect; i++)
    2087          113 :         if (fd->loops[i].m1
    2088           83 :             || fd->loops[i].m2
    2089           82 :             || fd->loops[i].non_rect_referenced)
    2090              :           break;
    2091          284 :       if (i == fd->last_nonrect
    2092          253 :           && fd->loops[i].outer == fd->last_nonrect - fd->first_nonrect
    2093          253 :           && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
    2094          511 :           && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[i].v)))
    2095              :         {
    2096          225 :           int o = fd->first_nonrect;
    2097          225 :           tree itype = TREE_TYPE (fd->loops[o].v);
    2098          225 :           tree n1o = create_tmp_reg (itype, ".n1o");
    2099          225 :           t = fold_convert (itype, unshare_expr (fd->loops[o].n1));
    2100          225 :           expand_omp_build_assign (gsi, n1o, t);
    2101          225 :           tree n2o = create_tmp_reg (itype, ".n2o");
    2102          225 :           t = fold_convert (itype, unshare_expr (fd->loops[o].n2));
    2103          225 :           expand_omp_build_assign (gsi, n2o, t);
    2104          225 :           if (fd->loops[i].m1 && fd->loops[i].m2)
    2105           43 :             t = fold_build2 (MINUS_EXPR, itype, unshare_expr (fd->loops[i].m2),
    2106              :                              unshare_expr (fd->loops[i].m1));
    2107          182 :           else if (fd->loops[i].m1)
    2108          162 :             t = fold_build1 (NEGATE_EXPR, itype,
    2109              :                              unshare_expr (fd->loops[i].m1));
    2110              :           else
    2111           20 :             t = unshare_expr (fd->loops[i].m2);
    2112          225 :           tree m2minusm1
    2113          225 :             = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
    2114              :                                         true, GSI_SAME_STMT);
    2115              : 
    2116          225 :           gimple_stmt_iterator gsi2 = *gsi;
    2117          225 :           gsi_prev (&gsi2);
    2118          225 :           e = split_block (entry_bb, gsi_stmt (gsi2));
    2119          225 :           e = split_block (e->dest, (gimple *) NULL);
    2120          225 :           basic_block bb1 = e->src;
    2121          225 :           entry_bb = e->dest;
    2122          225 :           *gsi = gsi_after_labels (entry_bb);
    2123              : 
    2124          225 :           gsi2 = gsi_after_labels (bb1);
    2125          225 :           tree ostep = fold_convert (itype, fd->loops[o].step);
    2126          225 :           t = build_int_cst (itype, (fd->loops[o].cond_code
    2127          241 :                                      == LT_EXPR ? -1 : 1));
    2128          225 :           t = fold_build2 (PLUS_EXPR, itype, ostep, t);
    2129          225 :           t = fold_build2 (PLUS_EXPR, itype, t, n2o);
    2130          225 :           t = fold_build2 (MINUS_EXPR, itype, t, n1o);
    2131          225 :           if (TYPE_UNSIGNED (itype)
    2132          225 :               && fd->loops[o].cond_code == GT_EXPR)
    2133            0 :             t = fold_build2 (TRUNC_DIV_EXPR, itype,
    2134              :                              fold_build1 (NEGATE_EXPR, itype, t),
    2135              :                              fold_build1 (NEGATE_EXPR, itype, ostep));
    2136              :           else
    2137          225 :             t = fold_build2 (TRUNC_DIV_EXPR, itype, t, ostep);
    2138          225 :           tree outer_niters
    2139          225 :             = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2140              :                                         true, GSI_SAME_STMT);
    2141          225 :           t = fold_build2 (MINUS_EXPR, itype, outer_niters,
    2142              :                            build_one_cst (itype));
    2143          225 :           t = fold_build2 (MULT_EXPR, itype, t, ostep);
    2144          225 :           t = fold_build2 (PLUS_EXPR, itype, n1o, t);
    2145          225 :           tree last = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2146              :                                                 true, GSI_SAME_STMT);
    2147          225 :           tree n1, n2, n1e, n2e;
    2148          225 :           t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
    2149          225 :           if (fd->loops[i].m1)
    2150              :             {
    2151          205 :               n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
    2152          205 :               n1 = fold_build2 (MULT_EXPR, itype, n1o, n1);
    2153          205 :               n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
    2154              :             }
    2155              :           else
    2156              :             n1 = t;
    2157          225 :           n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
    2158              :                                          true, GSI_SAME_STMT);
    2159          225 :           t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    2160          225 :           if (fd->loops[i].m2)
    2161              :             {
    2162           63 :               n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
    2163           63 :               n2 = fold_build2 (MULT_EXPR, itype, n1o, n2);
    2164           63 :               n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
    2165              :             }
    2166              :           else
    2167              :             n2 = t;
    2168          225 :           n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
    2169              :                                          true, GSI_SAME_STMT);
    2170          225 :           t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
    2171          225 :           if (fd->loops[i].m1)
    2172              :             {
    2173          205 :               n1e = fold_convert (itype, unshare_expr (fd->loops[i].m1));
    2174          205 :               n1e = fold_build2 (MULT_EXPR, itype, last, n1e);
    2175          205 :               n1e = fold_build2 (PLUS_EXPR, itype, n1e, t);
    2176              :             }
    2177              :           else
    2178              :             n1e = t;
    2179          225 :           n1e = force_gimple_operand_gsi (&gsi2, n1e, true, NULL_TREE,
    2180              :                                           true, GSI_SAME_STMT);
    2181          225 :           t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    2182          225 :           if (fd->loops[i].m2)
    2183              :             {
    2184           63 :               n2e = fold_convert (itype, unshare_expr (fd->loops[i].m2));
    2185           63 :               n2e = fold_build2 (MULT_EXPR, itype, last, n2e);
    2186           63 :               n2e = fold_build2 (PLUS_EXPR, itype, n2e, t);
    2187              :             }
    2188              :           else
    2189              :             n2e = t;
    2190          225 :           n2e = force_gimple_operand_gsi (&gsi2, n2e, true, NULL_TREE,
    2191              :                                           true, GSI_SAME_STMT);
    2192          225 :           gcond *cond_stmt
    2193          225 :             = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
    2194              :                                      n1, n2);
    2195          225 :           e = split_block (bb1, cond_stmt);
    2196          225 :           e->flags = EDGE_TRUE_VALUE;
    2197          225 :           e->probability = profile_probability::likely ().guessed ();
    2198          225 :           basic_block bb2 = e->dest;
    2199          225 :           gsi2 = gsi_after_labels (bb2);
    2200              : 
    2201          225 :           cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
    2202              :                                              n1e, n2e);
    2203          225 :           e = split_block (bb2, cond_stmt);
    2204          225 :           e->flags = EDGE_TRUE_VALUE;
    2205          225 :           e->probability = profile_probability::likely ().guessed ();
    2206          225 :           gsi2 = gsi_after_labels (e->dest);
    2207              : 
    2208          225 :           tree step = fold_convert (itype, fd->loops[i].step);
    2209          225 :           t = build_int_cst (itype, (fd->loops[i].cond_code
    2210          241 :                                      == LT_EXPR ? -1 : 1));
    2211          225 :           t = fold_build2 (PLUS_EXPR, itype, step, t);
    2212          225 :           t = fold_build2 (PLUS_EXPR, itype, t, n2);
    2213          225 :           t = fold_build2 (MINUS_EXPR, itype, t, n1);
    2214          225 :           if (TYPE_UNSIGNED (itype)
    2215          225 :               && fd->loops[i].cond_code == GT_EXPR)
    2216            0 :             t = fold_build2 (TRUNC_DIV_EXPR, itype,
    2217              :                              fold_build1 (NEGATE_EXPR, itype, t),
    2218              :                              fold_build1 (NEGATE_EXPR, itype, step));
    2219              :           else
    2220          225 :             t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    2221          225 :           tree first_inner_iterations
    2222          225 :             = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2223              :                                         true, GSI_SAME_STMT);
    2224          225 :           t = fold_build2 (MULT_EXPR, itype, m2minusm1, ostep);
    2225          225 :           if (TYPE_UNSIGNED (itype)
    2226          225 :               && fd->loops[i].cond_code == GT_EXPR)
    2227            0 :             t = fold_build2 (TRUNC_DIV_EXPR, itype,
    2228              :                              fold_build1 (NEGATE_EXPR, itype, t),
    2229              :                              fold_build1 (NEGATE_EXPR, itype, step));
    2230              :           else
    2231          225 :             t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    2232          225 :           tree factor
    2233          225 :             = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2234              :                                         true, GSI_SAME_STMT);
    2235          225 :           t = fold_build2 (MINUS_EXPR, itype, outer_niters,
    2236              :                            build_one_cst (itype));
    2237          225 :           t = fold_build2 (MULT_EXPR, itype, t, outer_niters);
    2238          225 :           t = fold_build2 (RSHIFT_EXPR, itype, t, integer_one_node);
    2239          225 :           t = fold_build2 (MULT_EXPR, itype, factor, t);
    2240          225 :           t = fold_build2 (PLUS_EXPR, itype,
    2241              :                            fold_build2 (MULT_EXPR, itype, outer_niters,
    2242              :                                         first_inner_iterations), t);
    2243          225 :           expand_omp_build_assign (&gsi2, counts[fd->last_nonrect],
    2244              :                                    fold_convert (type, t));
    2245              : 
    2246          225 :           basic_block bb3 = create_empty_bb (bb1);
    2247          225 :           add_bb_to_loop (bb3, bb1->loop_father);
    2248              : 
    2249          225 :           e = make_edge (bb1, bb3, EDGE_FALSE_VALUE);
    2250          225 :           e->probability = profile_probability::unlikely ().guessed ();
    2251              : 
    2252          225 :           gsi2 = gsi_after_labels (bb3);
    2253          225 :           cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
    2254              :                                              n1e, n2e);
    2255          225 :           e = split_block (bb3, cond_stmt);
    2256          225 :           e->flags = EDGE_TRUE_VALUE;
    2257          225 :           e->probability = profile_probability::likely ().guessed ();
    2258          225 :           basic_block bb4 = e->dest;
    2259              : 
    2260          225 :           ne = make_edge (bb3, entry_bb, EDGE_FALSE_VALUE);
    2261          225 :           ne->probability = e->probability.invert ();
    2262              : 
    2263          225 :           basic_block bb5 = create_empty_bb (bb2);
    2264          225 :           add_bb_to_loop (bb5, bb2->loop_father);
    2265              : 
    2266          225 :           ne = make_edge (bb2, bb5, EDGE_FALSE_VALUE);
    2267          225 :           ne->probability = profile_probability::unlikely ().guessed ();
    2268              : 
    2269          675 :           for (int j = 0; j < 2; j++)
    2270              :             {
    2271          450 :               gsi2 = gsi_after_labels (j ? bb5 : bb4);
    2272          450 :               t = fold_build2 (MINUS_EXPR, itype,
    2273              :                                unshare_expr (fd->loops[i].n1),
    2274              :                                unshare_expr (fd->loops[i].n2));
    2275          450 :               t = fold_build2 (TRUNC_DIV_EXPR, itype, t, m2minusm1);
    2276          450 :               tree tem
    2277          450 :                 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2278              :                                             true, GSI_SAME_STMT);
    2279          450 :               t = fold_build2 (MINUS_EXPR, itype, tem, n1o);
    2280          450 :               t = fold_build2 (TRUNC_MOD_EXPR, itype, t, ostep);
    2281          450 :               t = fold_build2 (MINUS_EXPR, itype, tem, t);
    2282          450 :               tem = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2283              :                                               true, GSI_SAME_STMT);
    2284          450 :               t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
    2285          450 :               if (fd->loops[i].m1)
    2286              :                 {
    2287          410 :                   n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
    2288          410 :                   n1 = fold_build2 (MULT_EXPR, itype, tem, n1);
    2289          410 :                   n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
    2290              :                 }
    2291              :               else
    2292              :                 n1 = t;
    2293          450 :               n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
    2294              :                                              true, GSI_SAME_STMT);
    2295          450 :               t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    2296          450 :               if (fd->loops[i].m2)
    2297              :                 {
    2298          126 :                   n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
    2299          126 :                   n2 = fold_build2 (MULT_EXPR, itype, tem, n2);
    2300          126 :                   n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
    2301              :                 }
    2302              :               else
    2303              :                 n2 = t;
    2304          450 :               n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
    2305              :                                              true, GSI_SAME_STMT);
    2306          675 :               expand_omp_build_assign (&gsi2, j ? n2o : n1o, tem);
    2307              : 
    2308          450 :               cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
    2309              :                                                  n1, n2);
    2310          450 :               e = split_block (gsi_bb (gsi2), cond_stmt);
    2311          450 :               e->flags = j ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE;
    2312          450 :               e->probability = profile_probability::unlikely ().guessed ();
    2313          675 :               ne = make_edge (e->src, bb1,
    2314              :                               j ? EDGE_FALSE_VALUE : EDGE_TRUE_VALUE);
    2315          450 :               ne->probability = e->probability.invert ();
    2316          450 :               gsi2 = gsi_after_labels (e->dest);
    2317              : 
    2318          450 :               t = fold_build2 (PLUS_EXPR, itype, tem, ostep);
    2319          450 :               expand_omp_build_assign (&gsi2, j ? n2o : n1o, t);
    2320              : 
    2321          450 :               make_edge (e->dest, bb1, EDGE_FALLTHRU);
    2322              :             }
    2323              : 
    2324          225 :           set_immediate_dominator (CDI_DOMINATORS, bb3, bb1);
    2325          225 :           set_immediate_dominator (CDI_DOMINATORS, bb5, bb2);
    2326          225 :           set_immediate_dominator (CDI_DOMINATORS, entry_bb, bb1);
    2327              : 
    2328          225 :           if (fd->first_nonrect + 1 == fd->last_nonrect)
    2329              :             {
    2330          153 :               fd->first_inner_iterations = first_inner_iterations;
    2331          153 :               fd->factor = factor;
    2332          153 :               fd->adjn1 = n1o;
    2333              :             }
    2334              :         }
    2335              :       else
    2336              :         {
    2337              :           /* Fallback implementation.  Evaluate the loops with m1/m2
    2338              :              non-NULL as well as their outer loops at runtime using temporaries
    2339              :              instead of the original iteration variables, and in the
    2340              :              body just bump the counter.  */
    2341           59 :           gimple_stmt_iterator gsi2 = *gsi;
    2342           59 :           gsi_prev (&gsi2);
    2343           59 :           e = split_block (entry_bb, gsi_stmt (gsi2));
    2344           59 :           e = split_block (e->dest, (gimple *) NULL);
    2345           59 :           basic_block cur_bb = e->src;
    2346           59 :           basic_block next_bb = e->dest;
    2347           59 :           entry_bb = e->dest;
    2348           59 :           *gsi = gsi_after_labels (entry_bb);
    2349              : 
    2350           59 :           tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
    2351           59 :           memset (vs, 0, fd->last_nonrect * sizeof (tree));
    2352              : 
    2353          189 :           for (i = 0; i <= fd->last_nonrect; i++)
    2354              :             {
    2355          189 :               if (fd->loops[i].m1 == NULL_TREE
    2356          108 :                   && fd->loops[i].m2 == NULL_TREE
    2357           99 :                   && !fd->loops[i].non_rect_referenced)
    2358           40 :                 continue;
    2359              : 
    2360          149 :               tree itype = TREE_TYPE (fd->loops[i].v);
    2361              : 
    2362          149 :               gsi2 = gsi_after_labels (cur_bb);
    2363          149 :               tree n1, n2;
    2364          149 :               t = fold_convert (itype, unshare_expr (fd->loops[i].n1));
    2365          149 :               if (fd->loops[i].m1 == NULL_TREE)
    2366              :                 n1 = t;
    2367           81 :               else if (POINTER_TYPE_P (itype))
    2368              :                 {
    2369           30 :                   gcc_assert (integer_onep (fd->loops[i].m1));
    2370           30 :                   t = unshare_expr (fd->loops[i].n1);
    2371           30 :                   n1 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
    2372              :                 }
    2373              :               else
    2374              :                 {
    2375           51 :                   n1 = fold_convert (itype, unshare_expr (fd->loops[i].m1));
    2376           51 :                   n1 = fold_build2 (MULT_EXPR, itype,
    2377              :                                     vs[i - fd->loops[i].outer], n1);
    2378           51 :                   n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
    2379              :                 }
    2380          149 :               n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
    2381              :                                              true, GSI_SAME_STMT);
    2382          149 :               if (i < fd->last_nonrect)
    2383              :                 {
    2384           90 :                   vs[i] = create_tmp_reg (itype, ".it");
    2385           90 :                   expand_omp_build_assign (&gsi2, vs[i], n1);
    2386              :                 }
    2387          149 :               t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    2388          149 :               if (fd->loops[i].m2 == NULL_TREE)
    2389              :                 n2 = t;
    2390           85 :               else if (POINTER_TYPE_P (itype))
    2391              :                 {
    2392           34 :                   gcc_assert (integer_onep (fd->loops[i].m2));
    2393           34 :                   t = unshare_expr (fd->loops[i].n2);
    2394           34 :                   n2 = fold_build_pointer_plus (vs[i - fd->loops[i].outer], t);
    2395              :                 }
    2396              :               else
    2397              :                 {
    2398           51 :                   n2 = fold_convert (itype, unshare_expr (fd->loops[i].m2));
    2399           51 :                   n2 = fold_build2 (MULT_EXPR, itype,
    2400              :                                     vs[i - fd->loops[i].outer], n2);
    2401           51 :                   n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
    2402              :                 }
    2403          149 :               n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
    2404              :                                              true, GSI_SAME_STMT);
    2405          149 :               if (POINTER_TYPE_P (itype))
    2406           70 :                 itype = signed_type_for (itype);
    2407          149 :               if (i == fd->last_nonrect)
    2408              :                 {
    2409           59 :                   gcond *cond_stmt
    2410           59 :                     = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
    2411              :                                              n1, n2);
    2412           59 :                   e = split_block (cur_bb, cond_stmt);
    2413           59 :                   e->flags = EDGE_TRUE_VALUE;
    2414           59 :                   ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
    2415           59 :                   e->probability = profile_probability::likely ().guessed ();
    2416           59 :                   ne->probability = e->probability.invert ();
    2417           59 :                   gsi2 = gsi_after_labels (e->dest);
    2418              : 
    2419           59 :                   t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
    2420           79 :                                              ? -1 : 1));
    2421           59 :                   t = fold_build2 (PLUS_EXPR, itype,
    2422              :                                    fold_convert (itype, fd->loops[i].step), t);
    2423           59 :                   t = fold_build2 (PLUS_EXPR, itype, t,
    2424              :                                    fold_convert (itype, n2));
    2425           59 :                   t = fold_build2 (MINUS_EXPR, itype, t,
    2426              :                                    fold_convert (itype, n1));
    2427           59 :                   tree step = fold_convert (itype, fd->loops[i].step);
    2428           59 :                   if (TYPE_UNSIGNED (itype)
    2429           59 :                       && fd->loops[i].cond_code == GT_EXPR)
    2430            0 :                     t = fold_build2 (TRUNC_DIV_EXPR, itype,
    2431              :                                      fold_build1 (NEGATE_EXPR, itype, t),
    2432              :                                      fold_build1 (NEGATE_EXPR, itype, step));
    2433              :                   else
    2434           59 :                     t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    2435           59 :                   t = fold_convert (type, t);
    2436           59 :                   t = fold_build2 (PLUS_EXPR, type,
    2437              :                                    counts[fd->last_nonrect], t);
    2438           59 :                   t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2439              :                                                 true, GSI_SAME_STMT);
    2440           59 :                   expand_omp_build_assign (&gsi2, counts[fd->last_nonrect], t);
    2441           59 :                   e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
    2442           59 :                   set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
    2443           59 :                   break;
    2444              :                 }
    2445           90 :               e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
    2446              : 
    2447           90 :               basic_block new_cur_bb = create_empty_bb (cur_bb);
    2448           90 :               add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
    2449              : 
    2450           90 :               gsi2 = gsi_after_labels (e->dest);
    2451           90 :               tree step = fold_convert (itype,
    2452              :                                         unshare_expr (fd->loops[i].step));
    2453           90 :               if (POINTER_TYPE_P (TREE_TYPE (vs[i])))
    2454           38 :                 t = fold_build_pointer_plus (vs[i], step);
    2455              :               else
    2456           52 :                 t = fold_build2 (PLUS_EXPR, itype, vs[i], step);
    2457           90 :               t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2458              :                                             true, GSI_SAME_STMT);
    2459           90 :               expand_omp_build_assign (&gsi2, vs[i], t);
    2460              : 
    2461           90 :               ne = split_block (e->dest, last_nondebug_stmt (e->dest));
    2462           90 :               gsi2 = gsi_after_labels (ne->dest);
    2463              : 
    2464           90 :               expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, vs[i], n2);
    2465           90 :               edge e3, e4;
    2466           90 :               if (next_bb == entry_bb)
    2467              :                 {
    2468           59 :                   e3 = find_edge (ne->dest, next_bb);
    2469           59 :                   e3->flags = EDGE_FALSE_VALUE;
    2470              :                 }
    2471              :               else
    2472           31 :                 e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
    2473           90 :               e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
    2474           90 :               e4->probability = profile_probability::likely ().guessed ();
    2475           90 :               e3->probability = e4->probability.invert ();
    2476           90 :               basic_block esrc = e->src;
    2477           90 :               make_edge (e->src, ne->dest, EDGE_FALLTHRU);
    2478           90 :               cur_bb = new_cur_bb;
    2479           90 :               basic_block latch_bb = next_bb;
    2480           90 :               next_bb = e->dest;
    2481           90 :               remove_edge (e);
    2482           90 :               set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
    2483           90 :               set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
    2484           90 :               set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
    2485              :             }
    2486              :         }
    2487          284 :       t = NULL_TREE;
    2488          691 :       for (i = fd->first_nonrect; i < fd->last_nonrect; i++)
    2489          407 :         if (!fd->loops[i].non_rect_referenced
    2490           92 :             && fd->loops[i].m1 == NULL_TREE
    2491           92 :             && fd->loops[i].m2 == NULL_TREE)
    2492              :           {
    2493           92 :             if (t == NULL_TREE)
    2494           82 :               t = counts[i];
    2495              :             else
    2496           10 :               t = fold_build2 (MULT_EXPR, type, t, counts[i]);
    2497              :           }
    2498          284 :       if (t)
    2499              :         {
    2500           82 :           t = fold_build2 (MULT_EXPR, type, counts[fd->last_nonrect], t);
    2501           82 :           expand_omp_build_assign (gsi, counts[fd->last_nonrect], t);
    2502              :         }
    2503          284 :       if (!rect_count_seen)
    2504          186 :         t = counts[fd->last_nonrect];
    2505              :       else
    2506           98 :         t = fold_build2 (MULT_EXPR, type, fd->loop.n2,
    2507              :                          counts[fd->last_nonrect]);
    2508          284 :       expand_omp_build_assign (gsi, fd->loop.n2, t);
    2509          284 :     }
    2510         9076 :   else if (fd->non_rect)
    2511              :     {
    2512          117 :       tree t = fd->loop.n2;
    2513          117 :       gcc_assert (TREE_CODE (t) == INTEGER_CST);
    2514              :       int non_rect_referenced = 0, non_rect = 0;
    2515          358 :       for (i = 0; i < fd->collapse; i++)
    2516              :         {
    2517          240 :           if ((i < fd->first_nonrect || i > fd->last_nonrect)
    2518          246 :               && !integer_zerop (counts[i]))
    2519            6 :             t = fold_build2 (TRUNC_DIV_EXPR, type, t, counts[i]);
    2520          241 :           if (fd->loops[i].non_rect_referenced)
    2521          117 :             non_rect_referenced++;
    2522          241 :           if (fd->loops[i].m1 || fd->loops[i].m2)
    2523          117 :             non_rect++;
    2524              :         }
    2525          117 :       gcc_assert (non_rect == 1 && non_rect_referenced == 1);
    2526          117 :       counts[fd->last_nonrect] = t;
    2527              :     }
    2528              : }
    2529              : 
    2530              : /* Helper function for expand_omp_{for_*,simd}.  Generate code like:
    2531              :         T = V;
    2532              :         V3 = N31 + (T % count3) * STEP3;
    2533              :         T = T / count3;
    2534              :         V2 = N21 + (T % count2) * STEP2;
    2535              :         T = T / count2;
    2536              :         V1 = N11 + T * STEP1;
    2537              :    if this loop doesn't have an inner loop construct combined with it.
    2538              :    If it does have an inner loop construct combined with it and the
    2539              :    iteration count isn't known constant, store values from counts array
    2540              :    into its _looptemp_ temporaries instead.
    2541              :    For non-rectangular loops (between fd->first_nonrect and fd->last_nonrect
    2542              :    inclusive), use the count of all those loops together, and either
    2543              :    find quadratic etc. equation roots, or as a fallback, do:
    2544              :         COUNT = 0;
    2545              :         for (tmpi = N11; tmpi COND1 N12; tmpi += STEP1)
    2546              :         for (tmpj = M21 * tmpi + N21;
    2547              :              tmpj COND2 M22 * tmpi + N22; tmpj += STEP2)
    2548              :           {
    2549              :             int tmpk1 = M31 * tmpj + N31;
    2550              :             int tmpk2 = M32 * tmpj + N32;
    2551              :             if (tmpk1 COND3 tmpk2)
    2552              :               {
    2553              :                 if (COND3 is <)
    2554              :                   adj = STEP3 - 1;
    2555              :                 else
    2556              :                   adj = STEP3 + 1;
    2557              :                 int temp = (adj + tmpk2 - tmpk1) / STEP3;
    2558              :                 if (COUNT + temp > T)
    2559              :                   {
    2560              :                     V1 = tmpi;
    2561              :                     V2 = tmpj;
    2562              :                     V3 = tmpk1 + (T - COUNT) * STEP3;
    2563              :                     goto done;
    2564              :                   }
    2565              :                 else
    2566              :                   COUNT += temp;
    2567              :               }
    2568              :           }
    2569              :         done:;
    2570              :    but for optional innermost or outermost rectangular loops that aren't
    2571              :    referenced by other loop expressions keep doing the division/modulo.  */
    2572              : 
    2573              : static void
    2574        12254 : expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
    2575              :                           tree *counts, tree *nonrect_bounds,
    2576              :                           gimple *inner_stmt, tree startvar)
    2577              : {
    2578        12254 :   int i;
    2579        12254 :   if (gimple_omp_for_combined_p (fd->for_stmt))
    2580              :     {
    2581              :       /* If fd->loop.n2 is constant, then no propagation of the counts
    2582              :          is needed, they are constant.  */
    2583         6165 :       if (TREE_CODE (fd->loop.n2) == INTEGER_CST)
    2584              :         return;
    2585              : 
    2586         3082 :       tree clauses = gimple_code (inner_stmt) != GIMPLE_OMP_FOR
    2587         4716 :                      ? gimple_omp_taskreg_clauses (inner_stmt)
    2588         1448 :                      : gimple_omp_for_clauses (inner_stmt);
    2589              :       /* First two _looptemp_ clauses are for istart/iend, counts[0]
    2590              :          isn't supposed to be handled, as the inner loop doesn't
    2591              :          use it.  */
    2592         3082 :       tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
    2593         3082 :       gcc_assert (innerc);
    2594         3082 :       int count = 0;
    2595         3082 :       if (fd->non_rect
    2596          112 :           && fd->last_nonrect == fd->first_nonrect + 1
    2597         3146 :           && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
    2598              :         count = 4;
    2599        12007 :       for (i = 0; i < fd->collapse + count; i++)
    2600              :         {
    2601         8925 :           innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    2602              :                                     OMP_CLAUSE__LOOPTEMP_);
    2603         8925 :           gcc_assert (innerc);
    2604         8925 :           if (i)
    2605              :             {
    2606         5843 :               tree tem = OMP_CLAUSE_DECL (innerc);
    2607         5843 :               tree t;
    2608         5843 :               if (i < fd->collapse)
    2609         5603 :                 t = counts[i];
    2610              :               else
    2611          240 :                 switch (i - fd->collapse)
    2612              :                   {
    2613           60 :                   case 0: t = counts[0]; break;
    2614           60 :                   case 1: t = fd->first_inner_iterations; break;
    2615           60 :                   case 2: t = fd->factor; break;
    2616           60 :                   case 3: t = fd->adjn1; break;
    2617            0 :                   default: gcc_unreachable ();
    2618              :                   }
    2619         5843 :               t = fold_convert (TREE_TYPE (tem), t);
    2620         5843 :               t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
    2621              :                                             false, GSI_CONTINUE_LINKING);
    2622         5843 :               gassign *stmt = gimple_build_assign (tem, t);
    2623         5843 :               gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
    2624              :             }
    2625              :         }
    2626              :       return;
    2627              :     }
    2628              : 
    2629         6089 :   tree type = TREE_TYPE (fd->loop.v);
    2630         6089 :   tree tem = create_tmp_reg (type, ".tem");
    2631         6089 :   gassign *stmt = gimple_build_assign (tem, startvar);
    2632         6089 :   gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
    2633              : 
    2634        22211 :   for (i = fd->collapse - 1; i >= 0; i--)
    2635              :     {
    2636        16122 :       tree vtype = TREE_TYPE (fd->loops[i].v), itype, t;
    2637        16122 :       itype = vtype;
    2638        16122 :       if (POINTER_TYPE_P (vtype))
    2639         1655 :         itype = signed_type_for (vtype);
    2640        16122 :       if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
    2641        10033 :         t = fold_build2 (TRUNC_MOD_EXPR, type, tem, counts[i]);
    2642              :       else
    2643              :         t = tem;
    2644        16122 :       if (i == fd->last_nonrect)
    2645              :         {
    2646          376 :           t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
    2647              :                                         false, GSI_CONTINUE_LINKING);
    2648          376 :           tree stopval = t;
    2649          376 :           tree idx = create_tmp_reg (type, ".count");
    2650          376 :           expand_omp_build_assign (gsi, idx,
    2651              :                                    build_zero_cst (type), true);
    2652          376 :           basic_block bb_triang = NULL, bb_triang_dom = NULL;
    2653          376 :           if (fd->first_nonrect + 1 == fd->last_nonrect
    2654          272 :               && (TREE_CODE (fd->loop.n2) == INTEGER_CST
    2655          181 :                   || fd->first_inner_iterations)
    2656          488 :               && (optab_handler (sqrt_optab, TYPE_MODE (double_type_node))
    2657              :                   != CODE_FOR_nothing)
    2658          620 :               && !integer_zerop (fd->loop.n2))
    2659              :             {
    2660          236 :               tree outer_n1 = fd->adjn1 ? fd->adjn1 : fd->loops[i - 1].n1;
    2661          236 :               tree itype = TREE_TYPE (fd->loops[i].v);
    2662          236 :               tree first_inner_iterations = fd->first_inner_iterations;
    2663          236 :               tree factor = fd->factor;
    2664          236 :               gcond *cond_stmt
    2665          236 :                 = expand_omp_build_cond (gsi, NE_EXPR, factor,
    2666          236 :                                          build_zero_cst (TREE_TYPE (factor)),
    2667              :                                          true);
    2668          236 :               edge e = split_block (gsi_bb (*gsi), cond_stmt);
    2669          236 :               basic_block bb0 = e->src;
    2670          236 :               e->flags = EDGE_TRUE_VALUE;
    2671          236 :               e->probability = profile_probability::likely ();
    2672          236 :               bb_triang_dom = bb0;
    2673          236 :               *gsi = gsi_after_labels (e->dest);
    2674          236 :               tree slltype = long_long_integer_type_node;
    2675          236 :               tree ulltype = long_long_unsigned_type_node;
    2676          236 :               tree stopvalull = fold_convert (ulltype, stopval);
    2677          236 :               stopvalull
    2678          236 :                 = force_gimple_operand_gsi (gsi, stopvalull, true, NULL_TREE,
    2679              :                                             false, GSI_CONTINUE_LINKING);
    2680          236 :               first_inner_iterations
    2681          236 :                 = fold_convert (slltype, first_inner_iterations);
    2682          236 :               first_inner_iterations
    2683          236 :                 = force_gimple_operand_gsi (gsi, first_inner_iterations, true,
    2684              :                                             NULL_TREE, false,
    2685              :                                             GSI_CONTINUE_LINKING);
    2686          236 :               factor = fold_convert (slltype, factor);
    2687          236 :               factor
    2688          236 :                 = force_gimple_operand_gsi (gsi, factor, true, NULL_TREE,
    2689              :                                             false, GSI_CONTINUE_LINKING);
    2690          236 :               tree first_inner_iterationsd
    2691          236 :                 = fold_build1 (FLOAT_EXPR, double_type_node,
    2692              :                                first_inner_iterations);
    2693          236 :               first_inner_iterationsd
    2694          236 :                 = force_gimple_operand_gsi (gsi, first_inner_iterationsd, true,
    2695              :                                             NULL_TREE, false,
    2696              :                                             GSI_CONTINUE_LINKING);
    2697          236 :               tree factord = fold_build1 (FLOAT_EXPR, double_type_node,
    2698              :                                           factor);
    2699          236 :               factord = force_gimple_operand_gsi (gsi, factord, true,
    2700              :                                                   NULL_TREE, false,
    2701              :                                                   GSI_CONTINUE_LINKING);
    2702          236 :               tree stopvald = fold_build1 (FLOAT_EXPR, double_type_node,
    2703              :                                            stopvalull);
    2704          236 :               stopvald = force_gimple_operand_gsi (gsi, stopvald, true,
    2705              :                                                    NULL_TREE, false,
    2706              :                                                    GSI_CONTINUE_LINKING);
    2707              :               /* Temporarily disable flag_rounding_math, values will be
    2708              :                  decimal numbers divided by 2 and worst case imprecisions
    2709              :                  due to too large values ought to be caught later by the
    2710              :                  checks for fallback.  */
    2711          236 :               int save_flag_rounding_math = flag_rounding_math;
    2712          236 :               flag_rounding_math = 0;
    2713          236 :               t = fold_build2 (RDIV_EXPR, double_type_node, factord,
    2714              :                                build_real (double_type_node, dconst2));
    2715          236 :               tree t3 = fold_build2 (MINUS_EXPR, double_type_node,
    2716              :                                      first_inner_iterationsd, t);
    2717          236 :               t3 = force_gimple_operand_gsi (gsi, t3, true, NULL_TREE, false,
    2718              :                                              GSI_CONTINUE_LINKING);
    2719          236 :               t = fold_build2 (MULT_EXPR, double_type_node, factord,
    2720              :                                build_real (double_type_node, dconst2));
    2721          236 :               t = fold_build2 (MULT_EXPR, double_type_node, t, stopvald);
    2722          236 :               t = fold_build2 (PLUS_EXPR, double_type_node, t,
    2723              :                                fold_build2 (MULT_EXPR, double_type_node,
    2724              :                                             t3, t3));
    2725          236 :               flag_rounding_math = save_flag_rounding_math;
    2726          236 :               t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
    2727              :                                             GSI_CONTINUE_LINKING);
    2728          236 :               if (flag_exceptions
    2729           34 :                   && cfun->can_throw_non_call_exceptions
    2730          244 :                   && operation_could_trap_p (LT_EXPR, true, false, NULL_TREE))
    2731              :                 {
    2732            8 :                   tree tem = fold_build2 (LT_EXPR, boolean_type_node, t,
    2733              :                                           build_zero_cst (double_type_node));
    2734            8 :                   tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE,
    2735              :                                                   false, GSI_CONTINUE_LINKING);
    2736            8 :                   cond_stmt = gimple_build_cond (NE_EXPR, tem,
    2737              :                                                  boolean_false_node,
    2738              :                                                  NULL_TREE, NULL_TREE);
    2739              :                 }
    2740              :               else
    2741          228 :                 cond_stmt
    2742          228 :                   = gimple_build_cond (LT_EXPR, t,
    2743              :                                        build_zero_cst (double_type_node),
    2744              :                                        NULL_TREE, NULL_TREE);
    2745          236 :               gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
    2746          236 :               e = split_block (gsi_bb (*gsi), cond_stmt);
    2747          236 :               basic_block bb1 = e->src;
    2748          236 :               e->flags = EDGE_FALSE_VALUE;
    2749          236 :               e->probability = profile_probability::very_likely ();
    2750          236 :               *gsi = gsi_after_labels (e->dest);
    2751          236 :               gcall *call = gimple_build_call_internal (IFN_SQRT, 1, t);
    2752          236 :               tree sqrtr = create_tmp_var (double_type_node);
    2753          236 :               gimple_call_set_lhs (call, sqrtr);
    2754          236 :               gsi_insert_after (gsi, call, GSI_CONTINUE_LINKING);
    2755          236 :               t = fold_build2 (MINUS_EXPR, double_type_node, sqrtr, t3);
    2756          236 :               t = fold_build2 (RDIV_EXPR, double_type_node, t, factord);
    2757          236 :               t = fold_build1 (FIX_TRUNC_EXPR, ulltype, t);
    2758          236 :               tree c = create_tmp_var (ulltype);
    2759          236 :               tree d = create_tmp_var (ulltype);
    2760          236 :               expand_omp_build_assign (gsi, c, t, true);
    2761          236 :               t = fold_build2 (MINUS_EXPR, ulltype, c,
    2762              :                                build_one_cst (ulltype));
    2763          236 :               t = fold_build2 (MULT_EXPR, ulltype, c, t);
    2764          236 :               t = fold_build2 (RSHIFT_EXPR, ulltype, t, integer_one_node);
    2765          236 :               t = fold_build2 (MULT_EXPR, ulltype,
    2766              :                                fold_convert (ulltype, fd->factor), t);
    2767          236 :               tree t2
    2768          236 :                 = fold_build2 (MULT_EXPR, ulltype, c,
    2769              :                                fold_convert (ulltype,
    2770              :                                              fd->first_inner_iterations));
    2771          236 :               t = fold_build2 (PLUS_EXPR, ulltype, t, t2);
    2772          236 :               expand_omp_build_assign (gsi, d, t, true);
    2773          236 :               t = fold_build2 (MULT_EXPR, ulltype,
    2774              :                                fold_convert (ulltype, fd->factor), c);
    2775          236 :               t = fold_build2 (PLUS_EXPR, ulltype,
    2776              :                                t, fold_convert (ulltype,
    2777              :                                                 fd->first_inner_iterations));
    2778          236 :               t2 = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
    2779              :                                              GSI_CONTINUE_LINKING);
    2780          236 :               cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, d,
    2781              :                                              NULL_TREE, NULL_TREE);
    2782          236 :               gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
    2783          236 :               e = split_block (gsi_bb (*gsi), cond_stmt);
    2784          236 :               basic_block bb2 = e->src;
    2785          236 :               e->flags = EDGE_TRUE_VALUE;
    2786          236 :               e->probability = profile_probability::very_likely ();
    2787          236 :               *gsi = gsi_after_labels (e->dest);
    2788          236 :               t = fold_build2 (PLUS_EXPR, ulltype, d, t2);
    2789          236 :               t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
    2790              :                                             GSI_CONTINUE_LINKING);
    2791          236 :               cond_stmt = gimple_build_cond (GE_EXPR, stopvalull, t,
    2792              :                                              NULL_TREE, NULL_TREE);
    2793          236 :               gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);
    2794          236 :               e = split_block (gsi_bb (*gsi), cond_stmt);
    2795          236 :               basic_block bb3 = e->src;
    2796          236 :               e->flags = EDGE_FALSE_VALUE;
    2797          236 :               e->probability = profile_probability::very_likely ();
    2798          236 :               *gsi = gsi_after_labels (e->dest);
    2799          236 :               t = fold_convert (itype, c);
    2800          236 :               t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i - 1].step);
    2801          236 :               t = fold_build2 (PLUS_EXPR, itype, outer_n1, t);
    2802          236 :               t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, false,
    2803              :                                             GSI_CONTINUE_LINKING);
    2804          236 :               expand_omp_build_assign (gsi, fd->loops[i - 1].v, t, true);
    2805          236 :               t2 = fold_build2 (MINUS_EXPR, ulltype, stopvalull, d);
    2806          236 :               t2 = fold_convert (itype, t2);
    2807          236 :               t2 = fold_build2 (MULT_EXPR, itype, t2, fd->loops[i].step);
    2808          236 :               t2 = fold_build2 (PLUS_EXPR, itype, t2, fd->loops[i].n1);
    2809          236 :               if (fd->loops[i].m1)
    2810              :                 {
    2811          195 :                   t = fold_build2 (MULT_EXPR, itype, t, fd->loops[i].m1);
    2812          195 :                   t2 = fold_build2 (PLUS_EXPR, itype, t2, t);
    2813              :                 }
    2814          236 :               expand_omp_build_assign (gsi, fd->loops[i].v, t2, true);
    2815          236 :               e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
    2816          236 :               bb_triang = e->src;
    2817          236 :               *gsi = gsi_after_labels (e->dest);
    2818          236 :               remove_edge (e);
    2819          236 :               e = make_edge (bb1, gsi_bb (*gsi), EDGE_TRUE_VALUE);
    2820          236 :               e->probability = profile_probability::very_unlikely ();
    2821          236 :               e = make_edge (bb2, gsi_bb (*gsi), EDGE_FALSE_VALUE);
    2822          236 :               e->probability = profile_probability::very_unlikely ();
    2823          236 :               e = make_edge (bb3, gsi_bb (*gsi), EDGE_TRUE_VALUE);
    2824          236 :               e->probability = profile_probability::very_unlikely ();
    2825              : 
    2826          236 :               basic_block bb4 = create_empty_bb (bb0);
    2827          236 :               add_bb_to_loop (bb4, bb0->loop_father);
    2828          236 :               e = make_edge (bb0, bb4, EDGE_FALSE_VALUE);
    2829          236 :               e->probability = profile_probability::unlikely ();
    2830          236 :               make_edge (bb4, gsi_bb (*gsi), EDGE_FALLTHRU);
    2831          236 :               set_immediate_dominator (CDI_DOMINATORS, bb4, bb0);
    2832          236 :               set_immediate_dominator (CDI_DOMINATORS, gsi_bb (*gsi), bb0);
    2833          236 :               gimple_stmt_iterator gsi2 = gsi_after_labels (bb4);
    2834          236 :               t2 = fold_build2 (TRUNC_DIV_EXPR, type,
    2835              :                                 counts[i], counts[i - 1]);
    2836          236 :               t2 = force_gimple_operand_gsi (&gsi2, t2, true, NULL_TREE, false,
    2837              :                                              GSI_CONTINUE_LINKING);
    2838          236 :               t = fold_build2 (TRUNC_MOD_EXPR, type, stopval, t2);
    2839          236 :               t2 = fold_build2 (TRUNC_DIV_EXPR, type, stopval, t2);
    2840          236 :               t = fold_convert (itype, t);
    2841          236 :               t2 = fold_convert (itype, t2);
    2842          236 :               t = fold_build2 (MULT_EXPR, itype, t,
    2843              :                                fold_convert (itype, fd->loops[i].step));
    2844          236 :               t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
    2845          236 :               t2 = fold_build2 (MULT_EXPR, itype, t2,
    2846              :                                 fold_convert (itype, fd->loops[i - 1].step));
    2847          236 :               t2 = fold_build2 (PLUS_EXPR, itype, fd->loops[i - 1].n1, t2);
    2848          236 :               t2 = force_gimple_operand_gsi (&gsi2, t2, false, NULL_TREE,
    2849              :                                              false, GSI_CONTINUE_LINKING);
    2850          236 :               stmt = gimple_build_assign (fd->loops[i - 1].v, t2);
    2851          236 :               gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
    2852          236 :               if (fd->loops[i].m1)
    2853              :                 {
    2854          195 :                   t2 = fold_build2 (MULT_EXPR, itype, fd->loops[i].m1,
    2855              :                                     fd->loops[i - 1].v);
    2856          195 :                   t = fold_build2 (PLUS_EXPR, itype, t, t2);
    2857              :                 }
    2858          236 :               t = force_gimple_operand_gsi (&gsi2, t, false, NULL_TREE,
    2859              :                                             false, GSI_CONTINUE_LINKING);
    2860          236 :               stmt = gimple_build_assign (fd->loops[i].v, t);
    2861          236 :               gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
    2862              :             }
    2863              :           /* Fallback implementation.  Evaluate the loops in between
    2864              :              (inclusive) fd->first_nonrect and fd->last_nonrect at
    2865              :              runtime using temporaries instead of the original iteration
    2866              :              variables, in the body just bump the counter and compare
    2867              :              with the desired value.  */
    2868          376 :           gimple_stmt_iterator gsi2 = *gsi;
    2869          376 :           basic_block entry_bb = gsi_bb (gsi2);
    2870          376 :           edge e = split_block (entry_bb, gsi_stmt (gsi2));
    2871          376 :           e = split_block (e->dest, (gimple *) NULL);
    2872          376 :           basic_block dom_bb = NULL;
    2873          376 :           basic_block cur_bb = e->src;
    2874          376 :           basic_block next_bb = e->dest;
    2875          376 :           entry_bb = e->dest;
    2876          376 :           *gsi = gsi_after_labels (entry_bb);
    2877              : 
    2878          376 :           tree *vs = XALLOCAVEC (tree, fd->last_nonrect);
    2879          376 :           tree n1 = NULL_TREE, n2 = NULL_TREE;
    2880          376 :           memset (vs, 0, fd->last_nonrect * sizeof (tree));
    2881              : 
    2882          876 :           for (int j = fd->first_nonrect; j <= fd->last_nonrect; j++)
    2883              :             {
    2884          876 :               tree itype = TREE_TYPE (fd->loops[j].v);
    2885         1752 :               bool rect_p = (fd->loops[j].m1 == NULL_TREE
    2886          527 :                              && fd->loops[j].m2 == NULL_TREE
    2887         1345 :                              && !fd->loops[j].non_rect_referenced);
    2888          876 :               gsi2 = gsi_after_labels (cur_bb);
    2889          876 :               t = fold_convert (itype, unshare_expr (fd->loops[j].n1));
    2890          876 :               if (fd->loops[j].m1 == NULL_TREE)
    2891          527 :                 n1 = rect_p ? build_zero_cst (type) : t;
    2892          349 :               else if (POINTER_TYPE_P (itype))
    2893              :                 {
    2894           30 :                   gcc_assert (integer_onep (fd->loops[j].m1));
    2895           30 :                   t = unshare_expr (fd->loops[j].n1);
    2896           30 :                   n1 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
    2897              :                 }
    2898              :               else
    2899              :                 {
    2900          319 :                   n1 = fold_convert (itype, unshare_expr (fd->loops[j].m1));
    2901          319 :                   n1 = fold_build2 (MULT_EXPR, itype,
    2902              :                                     vs[j - fd->loops[j].outer], n1);
    2903          319 :                   n1 = fold_build2 (PLUS_EXPR, itype, n1, t);
    2904              :                 }
    2905          876 :               n1 = force_gimple_operand_gsi (&gsi2, n1, true, NULL_TREE,
    2906              :                                              true, GSI_SAME_STMT);
    2907          876 :               if (j < fd->last_nonrect)
    2908              :                 {
    2909          907 :                   vs[j] = create_tmp_reg (rect_p ? type : itype, ".it");
    2910          500 :                   expand_omp_build_assign (&gsi2, vs[j], n1);
    2911              :                 }
    2912          876 :               t = fold_convert (itype, unshare_expr (fd->loops[j].n2));
    2913          876 :               if (fd->loops[j].m2 == NULL_TREE)
    2914          646 :                 n2 = rect_p ? counts[j] : t;
    2915          230 :               else if (POINTER_TYPE_P (itype))
    2916              :                 {
    2917           34 :                   gcc_assert (integer_onep (fd->loops[j].m2));
    2918           34 :                   t = unshare_expr (fd->loops[j].n2);
    2919           34 :                   n2 = fold_build_pointer_plus (vs[j - fd->loops[j].outer], t);
    2920              :                 }
    2921              :               else
    2922              :                 {
    2923          196 :                   n2 = fold_convert (itype, unshare_expr (fd->loops[j].m2));
    2924          196 :                   n2 = fold_build2 (MULT_EXPR, itype,
    2925              :                                     vs[j - fd->loops[j].outer], n2);
    2926          196 :                   n2 = fold_build2 (PLUS_EXPR, itype, n2, t);
    2927              :                 }
    2928          876 :               n2 = force_gimple_operand_gsi (&gsi2, n2, true, NULL_TREE,
    2929              :                                              true, GSI_SAME_STMT);
    2930          876 :               if (POINTER_TYPE_P (itype))
    2931           74 :                 itype = signed_type_for (itype);
    2932          876 :               if (j == fd->last_nonrect)
    2933              :                 {
    2934          376 :                   gcond *cond_stmt
    2935          376 :                     = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code,
    2936              :                                              n1, n2);
    2937          376 :                   e = split_block (cur_bb, cond_stmt);
    2938          376 :                   e->flags = EDGE_TRUE_VALUE;
    2939          376 :                   edge ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE);
    2940          376 :                   e->probability = profile_probability::likely ().guessed ();
    2941          376 :                   ne->probability = e->probability.invert ();
    2942          376 :                   gsi2 = gsi_after_labels (e->dest);
    2943              : 
    2944          376 :                   t = build_int_cst (itype, (fd->loops[j].cond_code == LT_EXPR
    2945          428 :                                              ? -1 : 1));
    2946          376 :                   t = fold_build2 (PLUS_EXPR, itype,
    2947              :                                    fold_convert (itype, fd->loops[j].step), t);
    2948          376 :                   t = fold_build2 (PLUS_EXPR, itype, t,
    2949              :                                    fold_convert (itype, n2));
    2950          376 :                   t = fold_build2 (MINUS_EXPR, itype, t,
    2951              :                                    fold_convert (itype, n1));
    2952          376 :                   tree step = fold_convert (itype, fd->loops[j].step);
    2953          376 :                   if (TYPE_UNSIGNED (itype)
    2954          376 :                       && fd->loops[j].cond_code == GT_EXPR)
    2955            0 :                     t = fold_build2 (TRUNC_DIV_EXPR, itype,
    2956              :                                      fold_build1 (NEGATE_EXPR, itype, t),
    2957              :                                      fold_build1 (NEGATE_EXPR, itype, step));
    2958              :                   else
    2959          376 :                     t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    2960          376 :                   t = fold_convert (type, t);
    2961          376 :                   t = fold_build2 (PLUS_EXPR, type, idx, t);
    2962          376 :                   t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2963              :                                                 true, GSI_SAME_STMT);
    2964          376 :                   e = make_edge (e->dest, next_bb, EDGE_FALLTHRU);
    2965          376 :                   set_immediate_dominator (CDI_DOMINATORS, next_bb, cur_bb);
    2966          376 :                   cond_stmt
    2967          376 :                     = gimple_build_cond (LE_EXPR, t, stopval, NULL_TREE,
    2968              :                                          NULL_TREE);
    2969          376 :                   gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
    2970          376 :                   e = split_block (gsi_bb (gsi2), cond_stmt);
    2971          376 :                   e->flags = EDGE_TRUE_VALUE;
    2972          376 :                   e->probability = profile_probability::likely ().guessed ();
    2973          376 :                   ne = make_edge (e->src, entry_bb, EDGE_FALSE_VALUE);
    2974          376 :                   ne->probability = e->probability.invert ();
    2975          376 :                   gsi2 = gsi_after_labels (e->dest);
    2976          376 :                   expand_omp_build_assign (&gsi2, idx, t);
    2977          376 :                   set_immediate_dominator (CDI_DOMINATORS, entry_bb, dom_bb);
    2978          376 :                   break;
    2979              :                 }
    2980          500 :               e = split_block (cur_bb, last_nondebug_stmt (cur_bb));
    2981              : 
    2982          500 :               basic_block new_cur_bb = create_empty_bb (cur_bb);
    2983          500 :               add_bb_to_loop (new_cur_bb, cur_bb->loop_father);
    2984              : 
    2985          500 :               gsi2 = gsi_after_labels (e->dest);
    2986          500 :               if (rect_p)
    2987           93 :                 t = fold_build2 (PLUS_EXPR, type, vs[j],
    2988              :                                  build_one_cst (type));
    2989              :               else
    2990              :                 {
    2991          407 :                   tree step
    2992          407 :                     = fold_convert (itype, unshare_expr (fd->loops[j].step));
    2993          407 :                   if (POINTER_TYPE_P (vtype))
    2994           38 :                     t = fold_build_pointer_plus (vs[j], step);
    2995              :                   else
    2996          369 :                     t = fold_build2 (PLUS_EXPR, itype, vs[j], step);
    2997              :                 }
    2998          500 :               t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    2999              :                                             true, GSI_SAME_STMT);
    3000          500 :               expand_omp_build_assign (&gsi2, vs[j], t);
    3001              : 
    3002          500 :               edge ne = split_block (e->dest, last_nondebug_stmt (e->dest));
    3003          500 :               gsi2 = gsi_after_labels (ne->dest);
    3004              : 
    3005          500 :               gcond *cond_stmt;
    3006          500 :               if (next_bb == entry_bb)
    3007              :                 /* No need to actually check the outermost condition.  */
    3008          376 :                 cond_stmt
    3009          376 :                   = gimple_build_cond (EQ_EXPR, boolean_true_node,
    3010              :                                        boolean_true_node,
    3011              :                                        NULL_TREE, NULL_TREE);
    3012              :               else
    3013          124 :                 cond_stmt
    3014          155 :                   = gimple_build_cond (rect_p ? LT_EXPR
    3015           31 :                                               : fd->loops[j].cond_code,
    3016              :                                        vs[j], n2, NULL_TREE, NULL_TREE);
    3017          500 :               gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT);
    3018          500 :               edge e3, e4;
    3019          500 :               if (next_bb == entry_bb)
    3020              :                 {
    3021          376 :                   e3 = find_edge (ne->dest, next_bb);
    3022          376 :                   e3->flags = EDGE_FALSE_VALUE;
    3023          376 :                   dom_bb = ne->dest;
    3024              :                 }
    3025              :               else
    3026          124 :                 e3 = make_edge (ne->dest, next_bb, EDGE_FALSE_VALUE);
    3027          500 :               e4 = make_edge (ne->dest, new_cur_bb, EDGE_TRUE_VALUE);
    3028          500 :               e4->probability = profile_probability::likely ().guessed ();
    3029          500 :               e3->probability = e4->probability.invert ();
    3030          500 :               basic_block esrc = e->src;
    3031          500 :               make_edge (e->src, ne->dest, EDGE_FALLTHRU);
    3032          500 :               cur_bb = new_cur_bb;
    3033          500 :               basic_block latch_bb = next_bb;
    3034          500 :               next_bb = e->dest;
    3035          500 :               remove_edge (e);
    3036          500 :               set_immediate_dominator (CDI_DOMINATORS, ne->dest, esrc);
    3037          500 :               set_immediate_dominator (CDI_DOMINATORS, latch_bb, ne->dest);
    3038          500 :               set_immediate_dominator (CDI_DOMINATORS, cur_bb, ne->dest);
    3039              :             }
    3040         1252 :           for (int j = fd->last_nonrect; j >= fd->first_nonrect; j--)
    3041              :             {
    3042          876 :               tree vtype = TREE_TYPE (fd->loops[j].v);
    3043          876 :               tree itype = vtype;
    3044          876 :               if (POINTER_TYPE_P (itype))
    3045           74 :                 itype = signed_type_for (itype);
    3046         1752 :               bool rect_p = (fd->loops[j].m1 == NULL_TREE
    3047          527 :                              && fd->loops[j].m2 == NULL_TREE
    3048         1345 :                              && !fd->loops[j].non_rect_referenced);
    3049          876 :               if (j == fd->last_nonrect)
    3050              :                 {
    3051          376 :                   t = fold_build2 (MINUS_EXPR, type, stopval, idx);
    3052          376 :                   t = fold_convert (itype, t);
    3053          376 :                   tree t2
    3054          376 :                     = fold_convert (itype, unshare_expr (fd->loops[j].step));
    3055          376 :                   t = fold_build2 (MULT_EXPR, itype, t, t2);
    3056          376 :                   if (POINTER_TYPE_P (vtype))
    3057           32 :                     t = fold_build_pointer_plus (n1, t);
    3058              :                   else
    3059          344 :                     t = fold_build2 (PLUS_EXPR, itype, n1, t);
    3060              :                 }
    3061          500 :               else if (rect_p)
    3062              :                 {
    3063           93 :                   t = fold_convert (itype, vs[j]);
    3064           93 :                   t = fold_build2 (MULT_EXPR, itype, t,
    3065              :                                    fold_convert (itype, fd->loops[j].step));
    3066           93 :                   if (POINTER_TYPE_P (vtype))
    3067            4 :                     t = fold_build_pointer_plus (fd->loops[j].n1, t);
    3068              :                   else
    3069           89 :                     t = fold_build2 (PLUS_EXPR, itype, fd->loops[j].n1, t);
    3070              :                 }
    3071              :               else
    3072          407 :                 t = vs[j];
    3073          876 :               t = force_gimple_operand_gsi (gsi, t, false,
    3074              :                                             NULL_TREE, true,
    3075              :                                             GSI_SAME_STMT);
    3076          876 :               stmt = gimple_build_assign (fd->loops[j].v, t);
    3077          876 :               gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
    3078              :             }
    3079          376 :           if (gsi_end_p (*gsi))
    3080          752 :             *gsi = gsi_last_bb (gsi_bb (*gsi));
    3081              :           else
    3082            0 :             gsi_prev (gsi);
    3083          376 :           if (bb_triang)
    3084              :             {
    3085          236 :               e = split_block (gsi_bb (*gsi), gsi_stmt (*gsi));
    3086          236 :               make_edge (bb_triang, e->dest, EDGE_FALLTHRU);
    3087          236 :               *gsi = gsi_after_labels (e->dest);
    3088          236 :               if (!gsi_end_p (*gsi))
    3089            0 :                 gsi_insert_before (gsi, gimple_build_nop (), GSI_NEW_STMT);
    3090          236 :               set_immediate_dominator (CDI_DOMINATORS, e->dest, bb_triang_dom);
    3091              :             }
    3092              :         }
    3093              :       else
    3094              :         {
    3095        15746 :           t = fold_convert (itype, t);
    3096        15746 :           t = fold_build2 (MULT_EXPR, itype, t,
    3097              :                            fold_convert (itype, fd->loops[i].step));
    3098        15746 :           if (POINTER_TYPE_P (vtype))
    3099         1623 :             t = fold_build_pointer_plus (fd->loops[i].n1, t);
    3100              :           else
    3101        14123 :             t = fold_build2 (PLUS_EXPR, itype, fd->loops[i].n1, t);
    3102        15746 :           t = force_gimple_operand_gsi (gsi, t,
    3103        15746 :                                         DECL_P (fd->loops[i].v)
    3104        15746 :                                         && TREE_ADDRESSABLE (fd->loops[i].v),
    3105              :                                         NULL_TREE, false,
    3106              :                                         GSI_CONTINUE_LINKING);
    3107        15746 :           stmt = gimple_build_assign (fd->loops[i].v, t);
    3108        15746 :           gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
    3109              :         }
    3110        16122 :       if (i != 0 && (i != fd->last_nonrect || fd->first_nonrect))
    3111              :         {
    3112        10033 :           t = fold_build2 (TRUNC_DIV_EXPR, type, tem, counts[i]);
    3113        10033 :           t = force_gimple_operand_gsi (gsi, t, false, NULL_TREE,
    3114              :                                         false, GSI_CONTINUE_LINKING);
    3115        10033 :           stmt = gimple_build_assign (tem, t);
    3116        10033 :           gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
    3117              :         }
    3118        16122 :       if (i == fd->last_nonrect)
    3119          376 :         i = fd->first_nonrect;
    3120              :     }
    3121         6089 :   if (fd->non_rect)
    3122         1351 :     for (i = 0; i <= fd->last_nonrect; i++)
    3123          975 :       if (fd->loops[i].m2)
    3124              :         {
    3125          230 :           tree itype = TREE_TYPE (fd->loops[i].v);
    3126              : 
    3127          230 :           tree t;
    3128          230 :           if (POINTER_TYPE_P (itype))
    3129              :             {
    3130           34 :               gcc_assert (integer_onep (fd->loops[i].m2));
    3131           34 :               t = fold_build_pointer_plus (fd->loops[i - fd->loops[i].outer].v,
    3132              :                                            unshare_expr (fd->loops[i].n2));
    3133              :             }
    3134              :           else
    3135              :             {
    3136          196 :               t = fold_convert (itype, unshare_expr (fd->loops[i].m2));
    3137          196 :               t = fold_build2 (MULT_EXPR, itype,
    3138              :                                fd->loops[i - fd->loops[i].outer].v, t);
    3139          196 :               t = fold_build2 (PLUS_EXPR, itype, t,
    3140              :                                fold_convert (itype,
    3141              :                                              unshare_expr (fd->loops[i].n2)));
    3142              :             }
    3143          230 :           nonrect_bounds[i] = create_tmp_reg (itype, ".bound");
    3144          230 :           t = force_gimple_operand_gsi (gsi, t, false,
    3145              :                                         NULL_TREE, false,
    3146              :                                         GSI_CONTINUE_LINKING);
    3147          230 :           stmt = gimple_build_assign (nonrect_bounds[i], t);
    3148          230 :           gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
    3149              :         }
    3150              : }
    3151              : 
    3152              : /* Helper function for expand_omp_for_*.  Generate code like:
    3153              :     L10:
    3154              :         V3 += STEP3;
    3155              :         if (V3 cond3 N32) goto BODY_BB; else goto L11;
    3156              :     L11:
    3157              :         V3 = N31;
    3158              :         V2 += STEP2;
    3159              :         if (V2 cond2 N22) goto BODY_BB; else goto L12;
    3160              :     L12:
    3161              :         V2 = N21;
    3162              :         V1 += STEP1;
    3163              :         goto BODY_BB;
    3164              :    For non-rectangular loops, use temporaries stored in nonrect_bounds
    3165              :    for the upper bounds if M?2 multiplier is present.  Given e.g.
    3166              :    for (V1 = N11; V1 cond1 N12; V1 += STEP1)
    3167              :    for (V2 = N21; V2 cond2 N22; V2 += STEP2)
    3168              :    for (V3 = N31; V3 cond3 N32; V3 += STEP3)
    3169              :    for (V4 = N41 + M41 * V2; V4 cond4 N42 + M42 * V2; V4 += STEP4)
    3170              :    do:
    3171              :     L10:
    3172              :         V4 += STEP4;
    3173              :         if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L11;
    3174              :     L11:
    3175              :         V4 = N41 + M41 * V2; // This can be left out if the loop
    3176              :                              // refers to the immediate parent loop
    3177              :         V3 += STEP3;
    3178              :         if (V3 cond3 N32) goto BODY_BB; else goto L12;
    3179              :     L12:
    3180              :         V3 = N31;
    3181              :         V2 += STEP2;
    3182              :         if (V2 cond2 N22) goto L120; else goto L13;
    3183              :     L120:
    3184              :         V4 = N41 + M41 * V2;
    3185              :         NONRECT_BOUND4 = N42 + M42 * V2;
    3186              :         if (V4 cond4 NONRECT_BOUND4) goto BODY_BB; else goto L12;
    3187              :     L13:
    3188              :         V2 = N21;
    3189              :         V1 += STEP1;
    3190              :         goto L120;  */
    3191              : 
    3192              : static basic_block
    3193         2593 : extract_omp_for_update_vars (struct omp_for_data *fd, tree *nonrect_bounds,
    3194              :                              basic_block cont_bb, basic_block body_bb)
    3195              : {
    3196         2593 :   basic_block last_bb, bb, collapse_bb = NULL;
    3197         2593 :   int i;
    3198         2593 :   gimple_stmt_iterator gsi;
    3199         2593 :   edge e;
    3200         2593 :   tree t;
    3201         2593 :   gimple *stmt;
    3202              : 
    3203         2593 :   last_bb = cont_bb;
    3204         9736 :   for (i = fd->collapse - 1; i >= 0; i--)
    3205              :     {
    3206         7143 :       tree vtype = TREE_TYPE (fd->loops[i].v);
    3207              : 
    3208         7143 :       bb = create_empty_bb (last_bb);
    3209         7143 :       add_bb_to_loop (bb, last_bb->loop_father);
    3210         7143 :       gsi = gsi_start_bb (bb);
    3211              : 
    3212         7143 :       if (i < fd->collapse - 1)
    3213              :         {
    3214         4550 :           e = make_edge (last_bb, bb, EDGE_FALSE_VALUE);
    3215         4550 :           e->probability = profile_probability::guessed_always () / 8;
    3216              : 
    3217         4550 :           struct omp_for_data_loop *l = &fd->loops[i + 1];
    3218         4550 :           if (l->m1 == NULL_TREE || l->outer != 1)
    3219              :             {
    3220         4342 :               t = l->n1;
    3221         4342 :               if (l->m1)
    3222              :                 {
    3223           56 :                   if (POINTER_TYPE_P (TREE_TYPE (l->v)))
    3224            3 :                     t = fold_build_pointer_plus (fd->loops[i + 1 - l->outer].v,
    3225              :                                                  t);
    3226              :                   else
    3227              :                     {
    3228           53 :                       tree t2
    3229           53 :                         = fold_build2 (MULT_EXPR, TREE_TYPE (t),
    3230              :                                        fd->loops[i + 1 - l->outer].v, l->m1);
    3231           53 :                       t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t2, t);
    3232              :                     }
    3233              :                 }
    3234         4342 :               t = force_gimple_operand_gsi (&gsi, t,
    3235         4342 :                                             DECL_P (l->v)
    3236         4342 :                                             && TREE_ADDRESSABLE (l->v),
    3237              :                                             NULL_TREE, false,
    3238              :                                             GSI_CONTINUE_LINKING);
    3239         4342 :               stmt = gimple_build_assign (l->v, t);
    3240         4342 :               gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
    3241              :             }
    3242              :         }
    3243              :       else
    3244              :         collapse_bb = bb;
    3245              : 
    3246         7143 :       set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
    3247              : 
    3248         7143 :       if (POINTER_TYPE_P (vtype))
    3249          902 :         t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
    3250              :       else
    3251         6241 :         t = fold_build2 (PLUS_EXPR, vtype, fd->loops[i].v, fd->loops[i].step);
    3252         7143 :       t = force_gimple_operand_gsi (&gsi, t,
    3253         7143 :                                     DECL_P (fd->loops[i].v)
    3254         7143 :                                     && TREE_ADDRESSABLE (fd->loops[i].v),
    3255              :                                     NULL_TREE, false, GSI_CONTINUE_LINKING);
    3256         7143 :       stmt = gimple_build_assign (fd->loops[i].v, t);
    3257         7143 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
    3258              : 
    3259         7143 :       if (fd->loops[i].non_rect_referenced)
    3260              :         {
    3261          308 :           basic_block update_bb = NULL, prev_bb = NULL;
    3262          714 :           for (int j = i + 1; j <= fd->last_nonrect; j++)
    3263          406 :             if (j - fd->loops[j].outer == i)
    3264              :               {
    3265          308 :                 tree n1, n2;
    3266          308 :                 struct omp_for_data_loop *l = &fd->loops[j];
    3267          308 :                 basic_block this_bb = create_empty_bb (last_bb);
    3268          308 :                 add_bb_to_loop (this_bb, last_bb->loop_father);
    3269          308 :                 gimple_stmt_iterator gsi2 = gsi_start_bb (this_bb);
    3270          308 :                 if (prev_bb)
    3271              :                   {
    3272            0 :                     e = make_edge (prev_bb, this_bb, EDGE_TRUE_VALUE);
    3273            0 :                     e->probability
    3274            0 :                       = profile_probability::guessed_always ().apply_scale (7,
    3275              :                                                                             8);
    3276            0 :                     set_immediate_dominator (CDI_DOMINATORS, this_bb, prev_bb);
    3277              :                   }
    3278          308 :                 if (l->m1)
    3279              :                   {
    3280          264 :                     if (POINTER_TYPE_P (TREE_TYPE (l->v)))
    3281           26 :                       t = fold_build_pointer_plus (fd->loops[i].v, l->n1);
    3282              :                     else
    3283              :                       {
    3284          238 :                         t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m1), l->m1,
    3285              :                                          fd->loops[i].v);
    3286          238 :                         t = fold_build2 (PLUS_EXPR, TREE_TYPE (l->v),
    3287              :                                          t, l->n1);
    3288              :                       }
    3289          264 :                     n1 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    3290              :                                                    false,
    3291              :                                                    GSI_CONTINUE_LINKING);
    3292          264 :                     stmt = gimple_build_assign (l->v, n1);
    3293          264 :                     gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
    3294          264 :                     n1 = l->v;
    3295              :                   }
    3296              :                 else
    3297           44 :                   n1 = force_gimple_operand_gsi (&gsi2, l->n1, true,
    3298              :                                                  NULL_TREE, false,
    3299              :                                                  GSI_CONTINUE_LINKING);
    3300          308 :                 if (l->m2)
    3301              :                   {
    3302          204 :                     if (POINTER_TYPE_P (TREE_TYPE (l->v)))
    3303           30 :                       t = fold_build_pointer_plus (fd->loops[i].v, l->n2);
    3304              :                     else
    3305              :                       {
    3306          174 :                         t = fold_build2 (MULT_EXPR, TREE_TYPE (l->m2), l->m2,
    3307              :                                          fd->loops[i].v);
    3308          174 :                         t = fold_build2 (PLUS_EXPR,
    3309              :                                          TREE_TYPE (nonrect_bounds[j]),
    3310              :                                          t, unshare_expr (l->n2));
    3311              :                       }
    3312          204 :                     n2 = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    3313              :                                                    false,
    3314              :                                                    GSI_CONTINUE_LINKING);
    3315          204 :                     stmt = gimple_build_assign (nonrect_bounds[j], n2);
    3316          204 :                     gsi_insert_after (&gsi2, stmt, GSI_CONTINUE_LINKING);
    3317          204 :                     n2 = nonrect_bounds[j];
    3318              :                   }
    3319              :                 else
    3320          104 :                   n2 = force_gimple_operand_gsi (&gsi2, unshare_expr (l->n2),
    3321              :                                                  true, NULL_TREE, false,
    3322              :                                                  GSI_CONTINUE_LINKING);
    3323          308 :                 gcond *cond_stmt
    3324          308 :                   = gimple_build_cond (l->cond_code, n1, n2,
    3325              :                                        NULL_TREE, NULL_TREE);
    3326          308 :                 gsi_insert_after (&gsi2, cond_stmt, GSI_CONTINUE_LINKING);
    3327          308 :                 if (update_bb == NULL)
    3328          308 :                   update_bb = this_bb;
    3329          308 :                 e = make_edge (this_bb, bb, EDGE_FALSE_VALUE);
    3330          308 :                 e->probability = profile_probability::guessed_always () / 8;
    3331          308 :                 if (prev_bb == NULL)
    3332          308 :                   set_immediate_dominator (CDI_DOMINATORS, this_bb, bb);
    3333          308 :                 prev_bb = this_bb;
    3334              :               }
    3335          308 :           e = make_edge (prev_bb, body_bb, EDGE_TRUE_VALUE);
    3336          308 :           e->probability
    3337          308 :             = profile_probability::guessed_always ().apply_scale (7, 8);
    3338          308 :           body_bb = update_bb;
    3339              :         }
    3340              : 
    3341         7143 :       if (i > 0)
    3342              :         {
    3343         4550 :           if (fd->loops[i].m2)
    3344          204 :             t = nonrect_bounds[i];
    3345              :           else
    3346         4346 :             t = unshare_expr (fd->loops[i].n2);
    3347         4550 :           t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    3348              :                                         false, GSI_CONTINUE_LINKING);
    3349         4550 :           tree v = fd->loops[i].v;
    3350         4550 :           if (DECL_P (v) && TREE_ADDRESSABLE (v))
    3351            0 :             v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
    3352              :                                           false, GSI_CONTINUE_LINKING);
    3353         4550 :           t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
    3354         4550 :           stmt = gimple_build_cond_empty (t);
    3355         4550 :           gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
    3356         4550 :           if (walk_tree (gimple_cond_lhs_ptr (as_a <gcond *> (stmt)),
    3357              :                          expand_omp_regimplify_p, NULL, NULL)
    3358         4550 :               || walk_tree (gimple_cond_rhs_ptr (as_a <gcond *> (stmt)),
    3359              :                             expand_omp_regimplify_p, NULL, NULL))
    3360            4 :             gimple_regimplify_operands (stmt, &gsi);
    3361         4550 :           e = make_edge (bb, body_bb, EDGE_TRUE_VALUE);
    3362         4550 :           e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
    3363              :         }
    3364              :       else
    3365         2593 :         make_edge (bb, body_bb, EDGE_FALLTHRU);
    3366         7143 :       set_immediate_dominator (CDI_DOMINATORS, bb, last_bb);
    3367         7143 :       last_bb = bb;
    3368              :     }
    3369              : 
    3370         2593 :   return collapse_bb;
    3371              : }
    3372              : 
    3373              : /* Expand #pragma omp ordered depend(source).  */
    3374              : 
    3375              : static void
    3376          335 : expand_omp_ordered_source (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
    3377              :                            tree *counts, location_t loc)
    3378              : {
    3379           56 :   enum built_in_function source_ix
    3380          335 :     = fd->iter_type == long_integer_type_node
    3381          335 :       ? BUILT_IN_GOMP_DOACROSS_POST : BUILT_IN_GOMP_DOACROSS_ULL_POST;
    3382          335 :   gimple *g
    3383          335 :     = gimple_build_call (builtin_decl_explicit (source_ix), 1,
    3384          335 :                          build_fold_addr_expr (counts[fd->ordered]));
    3385          335 :   gimple_set_location (g, loc);
    3386          335 :   gsi_insert_before (gsi, g, GSI_SAME_STMT);
    3387          335 : }
    3388              : 
    3389              : /* Expand a single depend from #pragma omp ordered depend(sink:...).  */
    3390              : 
    3391              : static void
    3392          466 : expand_omp_ordered_sink (gimple_stmt_iterator *gsi, struct omp_for_data *fd,
    3393              :                          tree *counts, tree c, location_t loc,
    3394              :                          basic_block cont_bb)
    3395              : {
    3396          466 :   auto_vec<tree, 10> args;
    3397           73 :   enum built_in_function sink_ix
    3398          466 :     = fd->iter_type == long_integer_type_node
    3399          466 :       ? BUILT_IN_GOMP_DOACROSS_WAIT : BUILT_IN_GOMP_DOACROSS_ULL_WAIT;
    3400          466 :   tree t, off, coff = NULL_TREE, deps = OMP_CLAUSE_DECL (c), cond = NULL_TREE;
    3401          466 :   int i;
    3402          466 :   gimple_stmt_iterator gsi2 = *gsi;
    3403          466 :   bool warned_step = false;
    3404              : 
    3405          466 :   if (deps == NULL)
    3406              :     {
    3407              :       /* Handle doacross(sink: omp_cur_iteration - 1).  */
    3408           52 :       gsi_prev (&gsi2);
    3409           52 :       edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
    3410           52 :       edge e2 = split_block_after_labels (e1->dest);
    3411           52 :       gsi2 = gsi_after_labels (e1->dest);
    3412           52 :       *gsi = gsi_last_bb (e1->src);
    3413           52 :       gimple_stmt_iterator gsi3 = *gsi;
    3414              : 
    3415           52 :       if (counts[fd->collapse - 1])
    3416              :         {
    3417            8 :           gcc_assert (fd->collapse == 1);
    3418            8 :           t = counts[fd->collapse - 1];
    3419              :         }
    3420           44 :       else if (fd->collapse > 1)
    3421           28 :         t = fd->loop.v;
    3422              :       else
    3423              :         {
    3424           16 :           t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
    3425              :                            fd->loops[0].v, fd->loops[0].n1);
    3426           16 :           t = fold_convert (fd->iter_type, t);
    3427              :         }
    3428              : 
    3429           52 :       t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE,
    3430              :                                     false, GSI_CONTINUE_LINKING);
    3431           52 :       gsi_insert_after (gsi, gimple_build_cond (NE_EXPR, t,
    3432           52 :                                                 build_zero_cst (TREE_TYPE (t)),
    3433              :                                                 NULL_TREE, NULL_TREE),
    3434              :                         GSI_NEW_STMT);
    3435              : 
    3436           52 :       t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
    3437              :                        build_minus_one_cst (TREE_TYPE (t)));
    3438           52 :       t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    3439              :                                     true, GSI_SAME_STMT);
    3440           52 :       args.safe_push (t);
    3441          281 :       for (i = fd->collapse; i < fd->ordered; i++)
    3442              :         {
    3443          229 :           t = counts[fd->ordered + 2 + (i - fd->collapse)];
    3444          229 :           t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,
    3445              :                            build_minus_one_cst (TREE_TYPE (t)));
    3446          229 :           t = fold_convert (fd->iter_type, t);
    3447          229 :           t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    3448              :                                         true, GSI_SAME_STMT);
    3449          229 :           args.safe_push (t);
    3450              :         }
    3451              : 
    3452           52 :       gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix),
    3453              :                                          args);
    3454           52 :       gimple_set_location (g, loc);
    3455           52 :       gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
    3456              : 
    3457           52 :       edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
    3458           52 :       e3->probability = profile_probability::guessed_always () / 8;
    3459           52 :       e1->probability = e3->probability.invert ();
    3460           52 :       e1->flags = EDGE_TRUE_VALUE;
    3461           52 :       set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
    3462              : 
    3463           52 :       if (fd->ordered > fd->collapse && cont_bb)
    3464              :         {
    3465           33 :           if (counts[fd->ordered + 1] == NULL_TREE)
    3466           33 :             counts[fd->ordered + 1]
    3467           33 :               = create_tmp_var (boolean_type_node, ".first");
    3468              : 
    3469           33 :           edge e4;
    3470           33 :           if (gsi_end_p (gsi3))
    3471           25 :             e4 = split_block_after_labels (e1->src);
    3472              :           else
    3473              :             {
    3474            8 :               gsi_prev (&gsi3);
    3475            8 :               e4 = split_block (gsi_bb (gsi3), gsi_stmt (gsi3));
    3476              :             }
    3477           33 :           gsi3 = gsi_last_bb (e4->src);
    3478              : 
    3479           33 :           gsi_insert_after (&gsi3,
    3480           33 :                             gimple_build_cond (NE_EXPR,
    3481           33 :                                                counts[fd->ordered + 1],
    3482              :                                                boolean_false_node,
    3483              :                                                NULL_TREE, NULL_TREE),
    3484              :                             GSI_NEW_STMT);
    3485              : 
    3486           33 :           edge e5 = make_edge (e4->src, e2->dest, EDGE_FALSE_VALUE);
    3487           33 :           e4->probability = profile_probability::guessed_always () / 8;
    3488           33 :           e5->probability = e4->probability.invert ();
    3489           33 :           e4->flags = EDGE_TRUE_VALUE;
    3490           33 :           set_immediate_dominator (CDI_DOMINATORS, e2->dest, e4->src);
    3491              :         }
    3492              : 
    3493           52 :       *gsi = gsi_after_labels (e2->dest);
    3494           52 :       return;
    3495              :     }
    3496          504 :   for (i = 0; i < fd->ordered; i++)
    3497              :     {
    3498          502 :       tree step = NULL_TREE;
    3499          502 :       off = TREE_PURPOSE (deps);
    3500          502 :       if (TREE_CODE (off) == TRUNC_DIV_EXPR)
    3501              :         {
    3502           83 :           step = TREE_OPERAND (off, 1);
    3503           83 :           off = TREE_OPERAND (off, 0);
    3504              :         }
    3505          502 :       if (!integer_zerop (off))
    3506              :         {
    3507          412 :           gcc_assert (fd->loops[i].cond_code == LT_EXPR
    3508              :                       || fd->loops[i].cond_code == GT_EXPR);
    3509          412 :           bool forward = fd->loops[i].cond_code == LT_EXPR;
    3510          412 :           if (step)
    3511              :             {
    3512              :               /* Non-simple Fortran DO loops.  If step is variable,
    3513              :                  we don't know at compile even the direction, so can't
    3514              :                  warn.  */
    3515           83 :               if (TREE_CODE (step) != INTEGER_CST)
    3516              :                 break;
    3517            0 :               forward = tree_int_cst_sgn (step) != -1;
    3518              :             }
    3519          329 :           if (forward ^ OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3520           17 :             warning_at (loc, OPT_Wopenmp,
    3521              :                         "%qs clause with %<sink%> modifier "
    3522              :                         "waiting for lexically later iteration",
    3523           17 :                         OMP_CLAUSE_DOACROSS_DEPEND (c)
    3524              :                         ? "depend" : "doacross");
    3525              :           break;
    3526              :         }
    3527           90 :       deps = TREE_CHAIN (deps);
    3528              :     }
    3529              :   /* If all offsets corresponding to the collapsed loops are zero,
    3530              :      this depend clause can be ignored.  FIXME: but there is still a
    3531              :      flush needed.  We need to emit one __sync_synchronize () for it
    3532              :      though (perhaps conditionally)?  Solve this together with the
    3533              :      conservative dependence folding optimization.
    3534              :   if (i >= fd->collapse)
    3535              :     return;  */
    3536              : 
    3537          414 :   deps = OMP_CLAUSE_DECL (c);
    3538          414 :   gsi_prev (&gsi2);
    3539          414 :   edge e1 = split_block (gsi_bb (gsi2), gsi_stmt (gsi2));
    3540          414 :   edge e2 = split_block_after_labels (e1->dest);
    3541              : 
    3542          414 :   gsi2 = gsi_after_labels (e1->dest);
    3543          414 :   *gsi = gsi_last_bb (e1->src);
    3544         2806 :   for (i = 0; i < fd->ordered; i++)
    3545              :     {
    3546         2392 :       tree itype = TREE_TYPE (fd->loops[i].v);
    3547         2392 :       tree step = NULL_TREE;
    3548         2392 :       tree orig_off = NULL_TREE;
    3549         2392 :       if (POINTER_TYPE_P (itype))
    3550            9 :         itype = sizetype;
    3551         2392 :       if (i)
    3552         1978 :         deps = TREE_CHAIN (deps);
    3553         2392 :       off = TREE_PURPOSE (deps);
    3554         2392 :       if (TREE_CODE (off) == TRUNC_DIV_EXPR)
    3555              :         {
    3556          204 :           step = TREE_OPERAND (off, 1);
    3557          204 :           off = TREE_OPERAND (off, 0);
    3558          204 :           gcc_assert (fd->loops[i].cond_code == LT_EXPR
    3559              :                       && integer_onep (fd->loops[i].step)
    3560              :                       && !POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)));
    3561              :         }
    3562         2392 :       tree s = fold_convert_loc (loc, itype, step ? step : fd->loops[i].step);
    3563         2392 :       if (step)
    3564              :         {
    3565          204 :           off = fold_convert_loc (loc, itype, off);
    3566          204 :           orig_off = off;
    3567          204 :           off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
    3568              :         }
    3569              : 
    3570         2392 :       if (integer_zerop (off))
    3571         1516 :         t = boolean_true_node;
    3572              :       else
    3573              :         {
    3574          876 :           tree a;
    3575          876 :           tree co = fold_convert_loc (loc, itype, off);
    3576          876 :           if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
    3577              :             {
    3578            9 :               if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3579            0 :                 co = fold_build1_loc (loc, NEGATE_EXPR, itype, co);
    3580            9 :               a = fold_build2_loc (loc, POINTER_PLUS_EXPR,
    3581            9 :                                    TREE_TYPE (fd->loops[i].v), fd->loops[i].v,
    3582              :                                    co);
    3583              :             }
    3584          867 :           else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3585          640 :             a = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
    3586              :                                  fd->loops[i].v, co);
    3587              :           else
    3588          227 :             a = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
    3589              :                                  fd->loops[i].v, co);
    3590          876 :           if (step)
    3591              :             {
    3592          204 :               tree t1, t2;
    3593          204 :               if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3594          149 :                 t1 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
    3595          149 :                                       fd->loops[i].n1);
    3596              :               else
    3597           55 :                 t1 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
    3598           55 :                                       fd->loops[i].n2);
    3599          204 :               if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3600          149 :                 t2 = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
    3601          149 :                                       fd->loops[i].n2);
    3602              :               else
    3603           55 :                 t2 = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
    3604           55 :                                       fd->loops[i].n1);
    3605          204 :               t = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
    3606          204 :                                    step, build_int_cst (TREE_TYPE (step), 0));
    3607          204 :               if (TREE_CODE (step) != INTEGER_CST)
    3608              :                 {
    3609          204 :                   t1 = unshare_expr (t1);
    3610          204 :                   t1 = force_gimple_operand_gsi (gsi, t1, true, NULL_TREE,
    3611              :                                                  false, GSI_CONTINUE_LINKING);
    3612          204 :                   t2 = unshare_expr (t2);
    3613          204 :                   t2 = force_gimple_operand_gsi (gsi, t2, true, NULL_TREE,
    3614              :                                                  false, GSI_CONTINUE_LINKING);
    3615              :                 }
    3616          204 :               t = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
    3617              :                                    t, t2, t1);
    3618              :             }
    3619          672 :           else if (fd->loops[i].cond_code == LT_EXPR)
    3620              :             {
    3621          520 :               if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3622          433 :                 t = fold_build2_loc (loc, GE_EXPR, boolean_type_node, a,
    3623              :                                      fd->loops[i].n1);
    3624              :               else
    3625           87 :                 t = fold_build2_loc (loc, LT_EXPR, boolean_type_node, a,
    3626              :                                      fd->loops[i].n2);
    3627              :             }
    3628          152 :           else if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3629           58 :             t = fold_build2_loc (loc, GT_EXPR, boolean_type_node, a,
    3630              :                                  fd->loops[i].n2);
    3631              :           else
    3632           94 :             t = fold_build2_loc (loc, LE_EXPR, boolean_type_node, a,
    3633              :                                  fd->loops[i].n1);
    3634              :         }
    3635         2392 :       if (cond)
    3636         1978 :         cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node, cond, t);
    3637              :       else
    3638          414 :         cond = t;
    3639              : 
    3640         2392 :       off = fold_convert_loc (loc, itype, off);
    3641              : 
    3642         2392 :       if (step
    3643         4411 :           || (fd->loops[i].cond_code == LT_EXPR
    3644         2188 :               ? !integer_onep (fd->loops[i].step)
    3645          169 :               : !integer_minus_onep (fd->loops[i].step)))
    3646              :         {
    3647          382 :           if (step == NULL_TREE
    3648          178 :               && TYPE_UNSIGNED (itype)
    3649          393 :               && fd->loops[i].cond_code == GT_EXPR)
    3650            9 :             t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype, off,
    3651              :                                  fold_build1_loc (loc, NEGATE_EXPR, itype,
    3652              :                                                   s));
    3653              :           else
    3654          542 :             t = fold_build2_loc (loc, TRUNC_MOD_EXPR, itype,
    3655              :                                  orig_off ? orig_off : off, s);
    3656          382 :           t = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, t,
    3657              :                                build_int_cst (itype, 0));
    3658          382 :           if (integer_zerop (t) && !warned_step)
    3659              :             {
    3660            5 :               warning_at (loc, OPT_Wopenmp,
    3661              :                           "%qs clause with %<sink%> modifier refers to "
    3662              :                           "iteration never in the iteration space",
    3663            5 :                           OMP_CLAUSE_DOACROSS_DEPEND (c)
    3664              :                           ? "depend" : "doacross");
    3665            5 :               warned_step = true;
    3666              :             }
    3667          382 :           cond = fold_build2_loc (loc, BIT_AND_EXPR, boolean_type_node,
    3668              :                                   cond, t);
    3669              :         }
    3670              : 
    3671         2392 :       if (i <= fd->collapse - 1 && fd->collapse > 1)
    3672          322 :         t = fd->loop.v;
    3673         2070 :       else if (counts[i])
    3674          166 :         t = counts[i];
    3675              :       else
    3676              :         {
    3677         1904 :           t = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
    3678         1904 :                                fd->loops[i].v, fd->loops[i].n1);
    3679         1904 :           t = fold_convert_loc (loc, fd->iter_type, t);
    3680              :         }
    3681         2392 :       if (step)
    3682              :         /* We have divided off by step already earlier.  */;
    3683         2188 :       else if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
    3684            9 :         off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off,
    3685              :                                fold_build1_loc (loc, NEGATE_EXPR, itype,
    3686              :                                                 s));
    3687              :       else
    3688         2179 :         off = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, off, s);
    3689         2392 :       if (OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (deps))
    3690          640 :         off = fold_build1_loc (loc, NEGATE_EXPR, itype, off);
    3691         2392 :       off = fold_convert_loc (loc, fd->iter_type, off);
    3692         2392 :       if (i <= fd->collapse - 1 && fd->collapse > 1)
    3693              :         {
    3694          322 :           if (i)
    3695          161 :             off = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, coff,
    3696              :                                    off);
    3697          322 :           if (i < fd->collapse - 1)
    3698              :             {
    3699          322 :               coff = fold_build2_loc (loc, MULT_EXPR, fd->iter_type, off,
    3700          161 :                                       counts[i]);
    3701          161 :               continue;
    3702              :             }
    3703              :         }
    3704         2231 :       off = unshare_expr (off);
    3705         2231 :       t = fold_build2_loc (loc, PLUS_EXPR, fd->iter_type, t, off);
    3706         2231 :       t = force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    3707              :                                     true, GSI_SAME_STMT);
    3708         2231 :       args.safe_push (t);
    3709              :     }
    3710          414 :   gimple *g = gimple_build_call_vec (builtin_decl_explicit (sink_ix), args);
    3711          414 :   gimple_set_location (g, loc);
    3712          414 :   gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
    3713              : 
    3714          414 :   cond = unshare_expr (cond);
    3715          414 :   cond = force_gimple_operand_gsi (gsi, cond, true, NULL_TREE, false,
    3716              :                                    GSI_CONTINUE_LINKING);
    3717          414 :   gsi_insert_after (gsi, gimple_build_cond_empty (cond), GSI_NEW_STMT);
    3718          414 :   edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
    3719          414 :   e3->probability = profile_probability::guessed_always () / 8;
    3720          414 :   e1->probability = e3->probability.invert ();
    3721          414 :   e1->flags = EDGE_TRUE_VALUE;
    3722          414 :   set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
    3723              : 
    3724          414 :   *gsi = gsi_after_labels (e2->dest);
    3725          466 : }
    3726              : 
    3727              : /* Expand all #pragma omp ordered depend(source) and
    3728              :    #pragma omp ordered depend(sink:...) constructs in the current
    3729              :    #pragma omp for ordered(n) region.  */
    3730              : 
    3731              : static void
    3732          335 : expand_omp_ordered_source_sink (struct omp_region *region,
    3733              :                                 struct omp_for_data *fd, tree *counts,
    3734              :                                 basic_block cont_bb)
    3735              : {
    3736          335 :   struct omp_region *inner;
    3737          335 :   int i;
    3738         1563 :   for (i = fd->collapse - 1; i < fd->ordered; i++)
    3739         1228 :     if (i == fd->collapse - 1 && fd->collapse > 1)
    3740          147 :       counts[i] = NULL_TREE;
    3741         1081 :     else if (i >= fd->collapse && !cont_bb)
    3742            0 :       counts[i] = build_zero_cst (fd->iter_type);
    3743         2154 :     else if (!POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v))
    3744         2154 :              && integer_onep (fd->loops[i].step))
    3745          987 :       counts[i] = NULL_TREE;
    3746              :     else
    3747           94 :       counts[i] = create_tmp_var (fd->iter_type, ".orditer");
    3748          335 :   tree atype
    3749          335 :     = build_array_type_nelts (fd->iter_type, fd->ordered - fd->collapse + 1);
    3750          335 :   counts[fd->ordered] = create_tmp_var (atype, ".orditera");
    3751          335 :   TREE_ADDRESSABLE (counts[fd->ordered]) = 1;
    3752          335 :   counts[fd->ordered + 1] = NULL_TREE;
    3753              : 
    3754         1615 :   for (inner = region->inner; inner; inner = inner->next)
    3755         1280 :     if (inner->type == GIMPLE_OMP_ORDERED)
    3756              :       {
    3757          709 :         gomp_ordered *ord_stmt = inner->ord_stmt;
    3758          709 :         gimple_stmt_iterator gsi = gsi_for_stmt (ord_stmt);
    3759          709 :         location_t loc = gimple_location (ord_stmt);
    3760          709 :         tree c;
    3761         1175 :         for (c = gimple_omp_ordered_clauses (ord_stmt);
    3762         1175 :              c; c = OMP_CLAUSE_CHAIN (c))
    3763          801 :           if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
    3764              :             break;
    3765          709 :         if (c)
    3766          335 :           expand_omp_ordered_source (&gsi, fd, counts, loc);
    3767         1510 :         for (c = gimple_omp_ordered_clauses (ord_stmt);
    3768         1510 :              c; c = OMP_CLAUSE_CHAIN (c))
    3769          801 :           if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
    3770          466 :             expand_omp_ordered_sink (&gsi, fd, counts, c, loc, cont_bb);
    3771          709 :         gsi_remove (&gsi, true);
    3772              :       }
    3773          335 : }
    3774              : 
    3775              : /* Wrap the body into fd->ordered - fd->collapse loops that aren't
    3776              :    collapsed.  */
    3777              : 
    3778              : static basic_block
    3779          335 : expand_omp_for_ordered_loops (struct omp_for_data *fd, tree *counts,
    3780              :                               basic_block cont_bb, basic_block body_bb,
    3781              :                               basic_block l0_bb, bool ordered_lastprivate)
    3782              : {
    3783          335 :   if (fd->ordered == fd->collapse)
    3784              :     return cont_bb;
    3785              : 
    3786          171 :   if (!cont_bb)
    3787              :     {
    3788            0 :       gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
    3789            0 :       for (int i = fd->collapse; i < fd->ordered; i++)
    3790              :         {
    3791            0 :           tree type = TREE_TYPE (fd->loops[i].v);
    3792            0 :           tree n1 = fold_convert (type, fd->loops[i].n1);
    3793            0 :           expand_omp_build_assign (&gsi, fd->loops[i].v, n1);
    3794            0 :           tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
    3795            0 :                               size_int (i - fd->collapse + 1),
    3796              :                               NULL_TREE, NULL_TREE);
    3797            0 :           expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
    3798              :         }
    3799            0 :       return NULL;
    3800              :     }
    3801              : 
    3802         1064 :   for (int i = fd->ordered - 1; i >= fd->collapse; i--)
    3803              :     {
    3804          893 :       tree t, type = TREE_TYPE (fd->loops[i].v);
    3805          893 :       gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
    3806          893 :       if (counts[fd->ordered + 1] && i == fd->collapse)
    3807           33 :         expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
    3808              :                                  boolean_true_node);
    3809          893 :       expand_omp_build_assign (&gsi, fd->loops[i].v,
    3810          893 :                                fold_convert (type, fd->loops[i].n1));
    3811          893 :       if (counts[i])
    3812           57 :         expand_omp_build_assign (&gsi, counts[i],
    3813              :                                  build_zero_cst (fd->iter_type));
    3814          893 :       tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
    3815          893 :                           size_int (i - fd->collapse + 1),
    3816              :                           NULL_TREE, NULL_TREE);
    3817          893 :       expand_omp_build_assign (&gsi, aref, build_zero_cst (fd->iter_type));
    3818          893 :       if (!gsi_end_p (gsi))
    3819          893 :         gsi_prev (&gsi);
    3820              :       else
    3821            0 :         gsi = gsi_last_bb (body_bb);
    3822          893 :       edge e1 = split_block (body_bb, gsi_stmt (gsi));
    3823          893 :       basic_block new_body = e1->dest;
    3824          893 :       if (body_bb == cont_bb)
    3825            0 :         cont_bb = new_body;
    3826          893 :       edge e2 = NULL;
    3827          893 :       basic_block new_header;
    3828          893 :       if (EDGE_COUNT (cont_bb->preds) > 0)
    3829              :         {
    3830          864 :           gsi = gsi_last_bb (cont_bb);
    3831          864 :           if (POINTER_TYPE_P (type))
    3832            0 :             t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
    3833              :           else
    3834          864 :             t = fold_build2 (PLUS_EXPR, type, fd->loops[i].v,
    3835              :                              fold_convert (type, fd->loops[i].step));
    3836          864 :           expand_omp_build_assign (&gsi, fd->loops[i].v, t);
    3837          864 :           if (counts[i])
    3838              :             {
    3839           57 :               t = fold_build2 (PLUS_EXPR, fd->iter_type, counts[i],
    3840              :                                build_int_cst (fd->iter_type, 1));
    3841           57 :               expand_omp_build_assign (&gsi, counts[i], t);
    3842           57 :               t = counts[i];
    3843              :             }
    3844              :           else
    3845              :             {
    3846          807 :               t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[i].v),
    3847              :                                fd->loops[i].v, fd->loops[i].n1);
    3848          807 :               t = fold_convert (fd->iter_type, t);
    3849          807 :               t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    3850              :                                             true, GSI_SAME_STMT);
    3851              :             }
    3852          864 :           aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
    3853          864 :                          size_int (i - fd->collapse + 1),
    3854              :                          NULL_TREE, NULL_TREE);
    3855          864 :           expand_omp_build_assign (&gsi, aref, t);
    3856          864 :           if (counts[fd->ordered + 1] && i == fd->ordered - 1)
    3857           30 :             expand_omp_build_assign (&gsi, counts[fd->ordered + 1],
    3858              :                                      boolean_false_node);
    3859          864 :           gsi_prev (&gsi);
    3860          864 :           e2 = split_block (cont_bb, gsi_stmt (gsi));
    3861          864 :           new_header = e2->dest;
    3862              :         }
    3863              :       else
    3864              :         new_header = cont_bb;
    3865          893 :       gsi = gsi_after_labels (new_header);
    3866          893 :       tree v = force_gimple_operand_gsi (&gsi, fd->loops[i].v, true, NULL_TREE,
    3867              :                                          true, GSI_SAME_STMT);
    3868          893 :       tree n2
    3869          893 :         = force_gimple_operand_gsi (&gsi, fold_convert (type, fd->loops[i].n2),
    3870              :                                     true, NULL_TREE, true, GSI_SAME_STMT);
    3871          893 :       t = build2 (fd->loops[i].cond_code, boolean_type_node, v, n2);
    3872          893 :       gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_NEW_STMT);
    3873          893 :       edge e3 = split_block (new_header, gsi_stmt (gsi));
    3874          893 :       cont_bb = e3->dest;
    3875          893 :       remove_edge (e1);
    3876          893 :       make_edge (body_bb, new_header, EDGE_FALLTHRU);
    3877          893 :       e3->flags = EDGE_FALSE_VALUE;
    3878          893 :       e3->probability = profile_probability::guessed_always () / 8;
    3879          893 :       e1 = make_edge (new_header, new_body, EDGE_TRUE_VALUE);
    3880          893 :       e1->probability = e3->probability.invert ();
    3881              : 
    3882          893 :       set_immediate_dominator (CDI_DOMINATORS, new_header, body_bb);
    3883          893 :       set_immediate_dominator (CDI_DOMINATORS, new_body, new_header);
    3884              : 
    3885          893 :       if (e2)
    3886              :         {
    3887          864 :           class loop *loop = alloc_loop ();
    3888          864 :           loop->header = new_header;
    3889          864 :           loop->latch = e2->src;
    3890          864 :           add_loop (loop, l0_bb->loop_father);
    3891              :         }
    3892              :     }
    3893              : 
    3894              :   /* If there are any lastprivate clauses and it is possible some loops
    3895              :      might have zero iterations, ensure all the decls are initialized,
    3896              :      otherwise we could crash evaluating C++ class iterators with lastprivate
    3897              :      clauses.  */
    3898              :   bool need_inits = false;
    3899          171 :   for (int i = fd->collapse; ordered_lastprivate && i < fd->ordered; i++)
    3900            0 :     if (need_inits)
    3901              :       {
    3902            0 :         tree type = TREE_TYPE (fd->loops[i].v);
    3903            0 :         gimple_stmt_iterator gsi = gsi_after_labels (body_bb);
    3904            0 :         expand_omp_build_assign (&gsi, fd->loops[i].v,
    3905              :                                  fold_convert (type, fd->loops[i].n1));
    3906              :       }
    3907              :     else
    3908              :       {
    3909            0 :         tree type = TREE_TYPE (fd->loops[i].v);
    3910            0 :         tree this_cond = fold_build2 (fd->loops[i].cond_code,
    3911              :                                       boolean_type_node,
    3912              :                                       fold_convert (type, fd->loops[i].n1),
    3913              :                                       fold_convert (type, fd->loops[i].n2));
    3914            0 :         if (!integer_onep (this_cond))
    3915            0 :           need_inits = true;
    3916              :       }
    3917              : 
    3918              :   return cont_bb;
    3919              : }
    3920              : 
    3921              : /* A subroutine of expand_omp_for.  Generate code for a parallel
    3922              :    loop with any schedule.  Given parameters:
    3923              : 
    3924              :         for (V = N1; V cond N2; V += STEP) BODY;
    3925              : 
    3926              :    where COND is "<" or ">", we generate pseudocode
    3927              : 
    3928              :         more = GOMP_loop_foo_start (N1, N2, STEP, CHUNK, &istart0, &iend0);
    3929              :         if (more) goto L0; else goto L3;
    3930              :     L0:
    3931              :         V = istart0;
    3932              :         iend = iend0;
    3933              :     L1:
    3934              :         BODY;
    3935              :         V += STEP;
    3936              :         if (V cond iend) goto L1; else goto L2;
    3937              :     L2:
    3938              :         if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
    3939              :     L3:
    3940              : 
    3941              :     If this is a combined omp parallel loop, instead of the call to
    3942              :     GOMP_loop_foo_start, we call GOMP_loop_foo_next.
    3943              :     If this is gimple_omp_for_combined_p loop, then instead of assigning
    3944              :     V and iend in L0 we assign the first two _looptemp_ clause decls of the
    3945              :     inner GIMPLE_OMP_FOR and V += STEP; and
    3946              :     if (V cond iend) goto L1; else goto L2; are removed.
    3947              : 
    3948              :     For collapsed loops, given parameters:
    3949              :       collapse(3)
    3950              :       for (V1 = N11; V1 cond1 N12; V1 += STEP1)
    3951              :         for (V2 = N21; V2 cond2 N22; V2 += STEP2)
    3952              :           for (V3 = N31; V3 cond3 N32; V3 += STEP3)
    3953              :             BODY;
    3954              : 
    3955              :     we generate pseudocode
    3956              : 
    3957              :         if (__builtin_expect (N32 cond3 N31, 0)) goto Z0;
    3958              :         if (cond3 is <)
    3959              :           adj = STEP3 - 1;
    3960              :         else
    3961              :           adj = STEP3 + 1;
    3962              :         count3 = (adj + N32 - N31) / STEP3;
    3963              :         if (__builtin_expect (N22 cond2 N21, 0)) goto Z0;
    3964              :         if (cond2 is <)
    3965              :           adj = STEP2 - 1;
    3966              :         else
    3967              :           adj = STEP2 + 1;
    3968              :         count2 = (adj + N22 - N21) / STEP2;
    3969              :         if (__builtin_expect (N12 cond1 N11, 0)) goto Z0;
    3970              :         if (cond1 is <)
    3971              :           adj = STEP1 - 1;
    3972              :         else
    3973              :           adj = STEP1 + 1;
    3974              :         count1 = (adj + N12 - N11) / STEP1;
    3975              :         count = count1 * count2 * count3;
    3976              :         goto Z1;
    3977              :     Z0:
    3978              :         count = 0;
    3979              :     Z1:
    3980              :         more = GOMP_loop_foo_start (0, count, 1, CHUNK, &istart0, &iend0);
    3981              :         if (more) goto L0; else goto L3;
    3982              :     L0:
    3983              :         V = istart0;
    3984              :         T = V;
    3985              :         V3 = N31 + (T % count3) * STEP3;
    3986              :         T = T / count3;
    3987              :         V2 = N21 + (T % count2) * STEP2;
    3988              :         T = T / count2;
    3989              :         V1 = N11 + T * STEP1;
    3990              :         iend = iend0;
    3991              :     L1:
    3992              :         BODY;
    3993              :         V += 1;
    3994              :         if (V < iend) goto L10; else goto L2;
    3995              :     L10:
    3996              :         V3 += STEP3;
    3997              :         if (V3 cond3 N32) goto L1; else goto L11;
    3998              :     L11:
    3999              :         V3 = N31;
    4000              :         V2 += STEP2;
    4001              :         if (V2 cond2 N22) goto L1; else goto L12;
    4002              :     L12:
    4003              :         V2 = N21;
    4004              :         V1 += STEP1;
    4005              :         goto L1;
    4006              :     L2:
    4007              :         if (GOMP_loop_foo_next (&istart0, &iend0)) goto L0; else goto L3;
    4008              :     L3:
    4009              : 
    4010              :       */
    4011              : 
    4012              : static void
    4013         4136 : expand_omp_for_generic (struct omp_region *region,
    4014              :                         struct omp_for_data *fd,
    4015              :                         enum built_in_function start_fn,
    4016              :                         enum built_in_function next_fn,
    4017              :                         tree sched_arg,
    4018              :                         gimple *inner_stmt)
    4019              : {
    4020         4136 :   tree type, istart0, iend0, iend;
    4021         4136 :   tree t, vmain, vback, bias = NULL_TREE;
    4022         4136 :   basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, collapse_bb;
    4023         4136 :   basic_block l2_bb = NULL, l3_bb = NULL;
    4024         4136 :   gimple_stmt_iterator gsi;
    4025         4136 :   gassign *assign_stmt;
    4026         4136 :   bool in_combined_parallel = is_combined_parallel (region);
    4027         4136 :   bool broken_loop = region->cont == NULL;
    4028         4136 :   edge e, ne;
    4029         4136 :   tree *counts = NULL;
    4030         4136 :   int i;
    4031         4136 :   bool ordered_lastprivate = false;
    4032         4136 :   bool offload = is_in_offload_region (region);
    4033              : 
    4034         4136 :   gcc_assert (!broken_loop || !in_combined_parallel);
    4035         4136 :   gcc_assert (fd->iter_type == long_integer_type_node
    4036              :               || !in_combined_parallel);
    4037              : 
    4038         4136 :   entry_bb = region->entry;
    4039         4136 :   cont_bb = region->cont;
    4040         4136 :   collapse_bb = NULL;
    4041         4136 :   gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
    4042         4136 :   gcc_assert (broken_loop
    4043              :               || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
    4044         4136 :   l0_bb = split_edge (FALLTHRU_EDGE (entry_bb));
    4045         4136 :   l1_bb = single_succ (l0_bb);
    4046         4136 :   if (!broken_loop)
    4047              :     {
    4048         3729 :       l2_bb = create_empty_bb (cont_bb);
    4049         3729 :       gcc_assert (BRANCH_EDGE (cont_bb)->dest == l1_bb
    4050              :                   || (single_succ_edge (BRANCH_EDGE (cont_bb)->dest)->dest
    4051              :                       == l1_bb));
    4052         3729 :       gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
    4053              :     }
    4054              :   else
    4055              :     l2_bb = NULL;
    4056         4136 :   l3_bb = BRANCH_EDGE (entry_bb)->dest;
    4057         4136 :   exit_bb = region->exit;
    4058              : 
    4059         4136 :   gsi = gsi_last_nondebug_bb (entry_bb);
    4060              : 
    4061         4136 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    4062         4136 :   if (fd->ordered
    4063         4136 :       && omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    4064              :                           OMP_CLAUSE_LASTPRIVATE))
    4065         4136 :     ordered_lastprivate = false;
    4066         4136 :   tree reductions = NULL_TREE;
    4067         4136 :   tree mem = NULL_TREE, cond_var = NULL_TREE, condtemp = NULL_TREE;
    4068         4136 :   tree memv = NULL_TREE;
    4069         4136 :   if (fd->lastprivate_conditional)
    4070              :     {
    4071           46 :       tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    4072              :                                 OMP_CLAUSE__CONDTEMP_);
    4073           46 :       if (fd->have_pointer_condtemp)
    4074           28 :         condtemp = OMP_CLAUSE_DECL (c);
    4075           46 :       c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
    4076           46 :       cond_var = OMP_CLAUSE_DECL (c);
    4077              :     }
    4078         4136 :   if (sched_arg)
    4079              :     {
    4080          169 :       if (fd->have_reductemp)
    4081              :         {
    4082          149 :           tree c = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    4083              :                                     OMP_CLAUSE__REDUCTEMP_);
    4084          149 :           reductions = OMP_CLAUSE_DECL (c);
    4085          149 :           gcc_assert (TREE_CODE (reductions) == SSA_NAME);
    4086          149 :           gimple *g = SSA_NAME_DEF_STMT (reductions);
    4087          149 :           reductions = gimple_assign_rhs1 (g);
    4088          149 :           OMP_CLAUSE_DECL (c) = reductions;
    4089          149 :           entry_bb = gimple_bb (g);
    4090          149 :           edge e = split_block (entry_bb, g);
    4091          149 :           if (region->entry == entry_bb)
    4092            8 :             region->entry = e->dest;
    4093          298 :           gsi = gsi_last_bb (entry_bb);
    4094              :         }
    4095              :       else
    4096           20 :         reductions = null_pointer_node;
    4097          169 :       if (fd->have_pointer_condtemp)
    4098              :         {
    4099           28 :           tree type = TREE_TYPE (condtemp);
    4100           28 :           memv = create_tmp_var (type);
    4101           28 :           TREE_ADDRESSABLE (memv) = 1;
    4102           28 :           unsigned HOST_WIDE_INT sz
    4103           28 :             = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
    4104           28 :           sz *= fd->lastprivate_conditional;
    4105           28 :           expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
    4106              :                                    false);
    4107           28 :           mem = build_fold_addr_expr (memv);
    4108              :         }
    4109              :       else
    4110          141 :         mem = null_pointer_node;
    4111              :     }
    4112         4136 :   if (fd->collapse > 1 || fd->ordered)
    4113              :     {
    4114         1669 :       int first_zero_iter1 = -1, first_zero_iter2 = -1;
    4115         1669 :       basic_block zero_iter1_bb = NULL, zero_iter2_bb = NULL, l2_dom_bb = NULL;
    4116              : 
    4117         1669 :       counts = XALLOCAVEC (tree, fd->ordered
    4118              :                                  ? fd->ordered + 2
    4119              :                                    + (fd->ordered - fd->collapse)
    4120              :                                  : fd->collapse);
    4121         1669 :       expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
    4122              :                                   zero_iter1_bb, first_zero_iter1,
    4123              :                                   zero_iter2_bb, first_zero_iter2, l2_dom_bb);
    4124              : 
    4125         1669 :       if (zero_iter1_bb)
    4126              :         {
    4127              :           /* Some counts[i] vars might be uninitialized if
    4128              :              some loop has zero iterations.  But the body shouldn't
    4129              :              be executed in that case, so just avoid uninit warnings.  */
    4130         1913 :           for (i = first_zero_iter1;
    4131         1913 :                i < (fd->ordered ? fd->ordered : fd->collapse); i++)
    4132         1502 :             if (SSA_VAR_P (counts[i]))
    4133          950 :               suppress_warning (counts[i], OPT_Wuninitialized);
    4134          411 :           gsi_prev (&gsi);
    4135          411 :           e = split_block (entry_bb, gsi_stmt (gsi));
    4136          411 :           entry_bb = e->dest;
    4137          411 :           make_edge (zero_iter1_bb, entry_bb, EDGE_FALLTHRU);
    4138          411 :           gsi = gsi_last_nondebug_bb (entry_bb);
    4139          411 :           set_immediate_dominator (CDI_DOMINATORS, entry_bb,
    4140              :                                    get_immediate_dominator (CDI_DOMINATORS,
    4141              :                                                             zero_iter1_bb));
    4142              :         }
    4143         1669 :       if (zero_iter2_bb)
    4144              :         {
    4145              :           /* Some counts[i] vars might be uninitialized if
    4146              :              some loop has zero iterations.  But the body shouldn't
    4147              :              be executed in that case, so just avoid uninit warnings.  */
    4148          394 :           for (i = first_zero_iter2; i < fd->ordered; i++)
    4149          276 :             if (SSA_VAR_P (counts[i]))
    4150          216 :               suppress_warning (counts[i], OPT_Wuninitialized);
    4151          118 :           if (zero_iter1_bb)
    4152          103 :             make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
    4153              :           else
    4154              :             {
    4155           15 :               gsi_prev (&gsi);
    4156           15 :               e = split_block (entry_bb, gsi_stmt (gsi));
    4157           15 :               entry_bb = e->dest;
    4158           15 :               make_edge (zero_iter2_bb, entry_bb, EDGE_FALLTHRU);
    4159           15 :               gsi = gsi_last_nondebug_bb (entry_bb);
    4160           15 :               set_immediate_dominator (CDI_DOMINATORS, entry_bb,
    4161              :                                        get_immediate_dominator
    4162              :                                          (CDI_DOMINATORS, zero_iter2_bb));
    4163              :             }
    4164              :         }
    4165         1669 :       if (fd->collapse == 1)
    4166              :         {
    4167          188 :           counts[0] = fd->loop.n2;
    4168          188 :           fd->loop = fd->loops[0];
    4169              :         }
    4170              :     }
    4171              : 
    4172         4136 :   type = TREE_TYPE (fd->loop.v);
    4173         4136 :   istart0 = create_tmp_var (fd->iter_type, ".istart0");
    4174         4136 :   iend0 = create_tmp_var (fd->iter_type, ".iend0");
    4175         4136 :   TREE_ADDRESSABLE (istart0) = 1;
    4176         4136 :   TREE_ADDRESSABLE (iend0) = 1;
    4177              : 
    4178              :   /* See if we need to bias by LLONG_MIN.  */
    4179         4136 :   if (fd->iter_type == long_long_unsigned_type_node
    4180          778 :       && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
    4181          530 :       && !TYPE_UNSIGNED (type)
    4182         4136 :       && fd->ordered == 0)
    4183              :     {
    4184            0 :       tree n1, n2;
    4185              : 
    4186            0 :       if (fd->loop.cond_code == LT_EXPR)
    4187              :         {
    4188            0 :           n1 = fd->loop.n1;
    4189            0 :           n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
    4190              :         }
    4191              :       else
    4192              :         {
    4193            0 :           n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
    4194            0 :           n2 = fd->loop.n1;
    4195              :         }
    4196            0 :       if (TREE_CODE (n1) != INTEGER_CST
    4197            0 :           || TREE_CODE (n2) != INTEGER_CST
    4198            0 :           || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
    4199            0 :         bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
    4200              :     }
    4201              : 
    4202         4136 :   gimple_stmt_iterator gsif = gsi;
    4203         4136 :   gsi_prev (&gsif);
    4204              : 
    4205         4136 :   tree arr = NULL_TREE;
    4206         4136 :   if (in_combined_parallel)
    4207              :     {
    4208         1084 :       gcc_assert (fd->ordered == 0);
    4209              :       /* In a combined parallel loop, emit a call to
    4210              :          GOMP_loop_foo_next.  */
    4211         1084 :       t = build_call_expr (builtin_decl_explicit (next_fn), 2,
    4212              :                            build_fold_addr_expr (istart0),
    4213              :                            build_fold_addr_expr (iend0));
    4214              :     }
    4215              :   else
    4216              :     {
    4217         3052 :       tree t0, t1, t2, t3, t4;
    4218              :       /* If this is not a combined parallel loop, emit a call to
    4219              :          GOMP_loop_foo_start in ENTRY_BB.  */
    4220         3052 :       t4 = build_fold_addr_expr (iend0);
    4221         3052 :       t3 = build_fold_addr_expr (istart0);
    4222         3052 :       if (fd->ordered)
    4223              :         {
    4224          670 :           t0 = build_int_cst (unsigned_type_node,
    4225          335 :                               fd->ordered - fd->collapse + 1);
    4226          335 :           arr = create_tmp_var (build_array_type_nelts (fd->iter_type,
    4227          335 :                                                         fd->ordered
    4228          335 :                                                         - fd->collapse + 1),
    4229              :                                 ".omp_counts");
    4230          335 :           DECL_NAMELESS (arr) = 1;
    4231          335 :           TREE_ADDRESSABLE (arr) = 1;
    4232          335 :           TREE_STATIC (arr) = 1;
    4233          335 :           vec<constructor_elt, va_gc> *v;
    4234          335 :           vec_alloc (v, fd->ordered - fd->collapse + 1);
    4235          335 :           int idx;
    4236              : 
    4237         1898 :           for (idx = 0; idx < fd->ordered - fd->collapse + 1; idx++)
    4238              :             {
    4239         1228 :               tree c;
    4240         1228 :               if (idx == 0 && fd->collapse > 1)
    4241          147 :                 c = fd->loop.n2;
    4242              :               else
    4243         1081 :                 c = counts[idx + fd->collapse - 1];
    4244         1228 :               tree purpose = size_int (idx);
    4245         1228 :               CONSTRUCTOR_APPEND_ELT (v, purpose, c);
    4246         1228 :               if (TREE_CODE (c) != INTEGER_CST)
    4247          433 :                 TREE_STATIC (arr) = 0;
    4248              :             }
    4249              : 
    4250          335 :           DECL_INITIAL (arr) = build_constructor (TREE_TYPE (arr), v);
    4251          335 :           if (!TREE_STATIC (arr))
    4252          232 :             force_gimple_operand_gsi (&gsi, build1 (DECL_EXPR,
    4253              :                                                     void_type_node, arr),
    4254              :                                       true, NULL_TREE, true, GSI_SAME_STMT);
    4255          335 :           t1 = build_fold_addr_expr (arr);
    4256          335 :           t2 = NULL_TREE;
    4257              :         }
    4258              :       else
    4259              :         {
    4260         2717 :           t2 = fold_convert (fd->iter_type, fd->loop.step);
    4261         2717 :           t1 = fd->loop.n2;
    4262         2717 :           t0 = fd->loop.n1;
    4263         2717 :           if (gimple_omp_for_combined_into_p (fd->for_stmt))
    4264              :             {
    4265         1152 :               tree innerc
    4266         1152 :                 = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    4267              :                                    OMP_CLAUSE__LOOPTEMP_);
    4268         1152 :               gcc_assert (innerc);
    4269         1152 :               t0 = OMP_CLAUSE_DECL (innerc);
    4270         1152 :               innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    4271              :                                         OMP_CLAUSE__LOOPTEMP_);
    4272         1152 :               gcc_assert (innerc);
    4273         1152 :               t1 = OMP_CLAUSE_DECL (innerc);
    4274              :             }
    4275         5322 :           if (POINTER_TYPE_P (TREE_TYPE (t0))
    4276         2717 :               && TYPE_PRECISION (TREE_TYPE (t0))
    4277          112 :                  != TYPE_PRECISION (fd->iter_type))
    4278              :             {
    4279              :               /* Avoid casting pointers to integer of a different size.  */
    4280            0 :               tree itype = signed_type_for (type);
    4281            0 :               t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
    4282            0 :               t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
    4283              :             }
    4284              :           else
    4285              :             {
    4286         2717 :               t1 = fold_convert (fd->iter_type, t1);
    4287         2717 :               t0 = fold_convert (fd->iter_type, t0);
    4288              :             }
    4289         2717 :           if (bias)
    4290              :             {
    4291            0 :               t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
    4292            0 :               t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
    4293              :             }
    4294              :         }
    4295         3052 :       if (fd->iter_type == long_integer_type_node || fd->ordered)
    4296              :         {
    4297         2330 :           if (fd->chunk_size)
    4298              :             {
    4299         1581 :               t = fold_convert (fd->iter_type, fd->chunk_size);
    4300         1581 :               t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
    4301         1581 :               if (sched_arg)
    4302              :                 {
    4303          135 :                   if (fd->ordered)
    4304           36 :                     t = build_call_expr (builtin_decl_explicit (start_fn),
    4305              :                                          8, t0, t1, sched_arg, t, t3, t4,
    4306              :                                          reductions, mem);
    4307              :                   else
    4308           99 :                     t = build_call_expr (builtin_decl_explicit (start_fn),
    4309              :                                          9, t0, t1, t2, sched_arg, t, t3, t4,
    4310              :                                          reductions, mem);
    4311              :                 }
    4312         1446 :               else if (fd->ordered)
    4313          299 :                 t = build_call_expr (builtin_decl_explicit (start_fn),
    4314              :                                      5, t0, t1, t, t3, t4);
    4315              :               else
    4316         1147 :                 t = build_call_expr (builtin_decl_explicit (start_fn),
    4317              :                                      6, t0, t1, t2, t, t3, t4);
    4318              :             }
    4319          749 :           else if (fd->ordered)
    4320            0 :             t = build_call_expr (builtin_decl_explicit (start_fn),
    4321              :                                  4, t0, t1, t3, t4);
    4322              :           else
    4323          749 :             t = build_call_expr (builtin_decl_explicit (start_fn),
    4324              :                                  5, t0, t1, t2, t3, t4);
    4325              :         }
    4326              :       else
    4327              :         {
    4328          722 :           tree t5;
    4329          722 :           tree c_bool_type;
    4330          722 :           tree bfn_decl;
    4331              : 
    4332              :           /* The GOMP_loop_ull_*start functions have additional boolean
    4333              :              argument, true for < loops and false for > loops.
    4334              :              In Fortran, the C bool type can be different from
    4335              :              boolean_type_node.  */
    4336          722 :           bfn_decl = builtin_decl_explicit (start_fn);
    4337          722 :           c_bool_type = TREE_TYPE (TREE_TYPE (bfn_decl));
    4338          722 :           t5 = build_int_cst (c_bool_type,
    4339          942 :                               fd->loop.cond_code == LT_EXPR ? 1 : 0);
    4340          722 :           if (fd->chunk_size)
    4341              :             {
    4342          390 :               tree bfn_decl = builtin_decl_explicit (start_fn);
    4343          390 :               t = fold_convert (fd->iter_type, fd->chunk_size);
    4344          390 :               t = omp_adjust_chunk_size (t, fd->simd_schedule, offload);
    4345          390 :               if (sched_arg)
    4346           34 :                 t = build_call_expr (bfn_decl, 10, t5, t0, t1, t2, sched_arg,
    4347              :                                      t, t3, t4, reductions, mem);
    4348              :               else
    4349          356 :                 t = build_call_expr (bfn_decl, 7, t5, t0, t1, t2, t, t3, t4);
    4350              :             }
    4351              :           else
    4352          332 :             t = build_call_expr (builtin_decl_explicit (start_fn),
    4353              :                                  6, t5, t0, t1, t2, t3, t4);
    4354              :         }
    4355              :     }
    4356         4136 :   if (TREE_TYPE (t) != boolean_type_node)
    4357            0 :     t = fold_build2 (NE_EXPR, boolean_type_node,
    4358              :                      t, build_int_cst (TREE_TYPE (t), 0));
    4359         4136 :   t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    4360              :                                 true, GSI_SAME_STMT);
    4361         4136 :   if (arr && !TREE_STATIC (arr))
    4362              :     {
    4363          232 :       tree clobber = build_clobber (TREE_TYPE (arr));
    4364          232 :       gsi_insert_before (&gsi, gimple_build_assign (arr, clobber),
    4365              :                          GSI_SAME_STMT);
    4366              :     }
    4367         4136 :   if (fd->have_pointer_condtemp)
    4368           28 :     expand_omp_build_assign (&gsi, condtemp, memv, false);
    4369         4136 :   if (fd->have_reductemp)
    4370              :     {
    4371          149 :       gimple *g = gsi_stmt (gsi);
    4372          149 :       gsi_remove (&gsi, true);
    4373          149 :       release_ssa_name (gimple_assign_lhs (g));
    4374              : 
    4375          149 :       entry_bb = region->entry;
    4376          149 :       gsi = gsi_last_nondebug_bb (entry_bb);
    4377              : 
    4378          149 :       gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    4379              :     }
    4380         4136 :   gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
    4381              : 
    4382              :   /* Remove the GIMPLE_OMP_FOR statement.  */
    4383         4136 :   gsi_remove (&gsi, true);
    4384              : 
    4385         4136 :   if (gsi_end_p (gsif))
    4386         1203 :     gsif = gsi_after_labels (gsi_bb (gsif));
    4387         4136 :   gsi_next (&gsif);
    4388              : 
    4389              :   /* Iteration setup for sequential loop goes in L0_BB.  */
    4390         4136 :   tree startvar = fd->loop.v;
    4391         4136 :   tree endvar = NULL_TREE;
    4392              : 
    4393         4136 :   if (gimple_omp_for_combined_p (fd->for_stmt))
    4394              :     {
    4395         1389 :       gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_FOR
    4396              :                   && gimple_omp_for_kind (inner_stmt)
    4397              :                      == GF_OMP_FOR_KIND_SIMD);
    4398         1389 :       tree innerc = omp_find_clause (gimple_omp_for_clauses (inner_stmt),
    4399              :                                      OMP_CLAUSE__LOOPTEMP_);
    4400         1389 :       gcc_assert (innerc);
    4401         1389 :       startvar = OMP_CLAUSE_DECL (innerc);
    4402         1389 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    4403              :                                 OMP_CLAUSE__LOOPTEMP_);
    4404         1389 :       gcc_assert (innerc);
    4405         1389 :       endvar = OMP_CLAUSE_DECL (innerc);
    4406              :     }
    4407              : 
    4408         4136 :   gsi = gsi_start_bb (l0_bb);
    4409         4136 :   t = istart0;
    4410         4136 :   if (fd->ordered && fd->collapse == 1)
    4411          188 :     t = fold_build2 (MULT_EXPR, fd->iter_type, t,
    4412              :                      fold_convert (fd->iter_type, fd->loop.step));
    4413         3948 :   else if (bias)
    4414            0 :     t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
    4415         4136 :   if (fd->ordered && fd->collapse == 1)
    4416              :     {
    4417          188 :       if (POINTER_TYPE_P (TREE_TYPE (startvar)))
    4418            8 :         t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
    4419              :                          fd->loop.n1, fold_convert (sizetype, t));
    4420              :       else
    4421              :         {
    4422          180 :           t = fold_convert (TREE_TYPE (startvar), t);
    4423          180 :           t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
    4424              :                            fd->loop.n1, t);
    4425              :         }
    4426              :     }
    4427              :   else
    4428              :     {
    4429         3948 :       if (POINTER_TYPE_P (TREE_TYPE (startvar)))
    4430          240 :         t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
    4431         3948 :       t = fold_convert (TREE_TYPE (startvar), t);
    4432              :     }
    4433         4136 :   t = force_gimple_operand_gsi (&gsi, t,
    4434         4136 :                                 DECL_P (startvar)
    4435         4123 :                                 && TREE_ADDRESSABLE (startvar),
    4436              :                                 NULL_TREE, false, GSI_CONTINUE_LINKING);
    4437         4136 :   assign_stmt = gimple_build_assign (startvar, t);
    4438         4136 :   gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    4439         4136 :   if (cond_var)
    4440              :     {
    4441           46 :       tree itype = TREE_TYPE (cond_var);
    4442              :       /* For lastprivate(conditional:) itervar, we need some iteration
    4443              :          counter that starts at unsigned non-zero and increases.
    4444              :          Prefer as few IVs as possible, so if we can use startvar
    4445              :          itself, use that, or startvar + constant (those would be
    4446              :          incremented with step), and as last resort use the s0 + 1
    4447              :          incremented by 1.  */
    4448           46 :       if ((fd->ordered && fd->collapse == 1)
    4449           46 :           || bias
    4450           46 :           || POINTER_TYPE_P (type)
    4451           46 :           || TREE_CODE (fd->loop.n1) != INTEGER_CST
    4452           40 :           || fd->loop.cond_code != LT_EXPR)
    4453            6 :         t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, istart0),
    4454              :                          build_int_cst (itype, 1));
    4455           40 :       else if (tree_int_cst_sgn (fd->loop.n1) == 1)
    4456            9 :         t = fold_convert (itype, t);
    4457              :       else
    4458              :         {
    4459           31 :           tree c = fold_convert (itype, fd->loop.n1);
    4460           31 :           c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
    4461           31 :           t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
    4462              :         }
    4463           46 :       t = force_gimple_operand_gsi (&gsi, t, false,
    4464              :                                     NULL_TREE, false, GSI_CONTINUE_LINKING);
    4465           46 :       assign_stmt = gimple_build_assign (cond_var, t);
    4466           46 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    4467              :     }
    4468              : 
    4469         4136 :   t = iend0;
    4470         4136 :   if (fd->ordered && fd->collapse == 1)
    4471          188 :     t = fold_build2 (MULT_EXPR, fd->iter_type, t,
    4472              :                      fold_convert (fd->iter_type, fd->loop.step));
    4473         3948 :   else if (bias)
    4474            0 :     t = fold_build2 (MINUS_EXPR, fd->iter_type, t, bias);
    4475         4136 :   if (fd->ordered && fd->collapse == 1)
    4476              :     {
    4477          188 :       if (POINTER_TYPE_P (TREE_TYPE (startvar)))
    4478            8 :         t = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (startvar),
    4479              :                          fd->loop.n1, fold_convert (sizetype, t));
    4480              :       else
    4481              :         {
    4482          180 :           t = fold_convert (TREE_TYPE (startvar), t);
    4483          180 :           t = fold_build2 (PLUS_EXPR, TREE_TYPE (startvar),
    4484              :                            fd->loop.n1, t);
    4485              :         }
    4486              :     }
    4487              :   else
    4488              :     {
    4489         3948 :       if (POINTER_TYPE_P (TREE_TYPE (startvar)))
    4490          240 :         t = fold_convert (signed_type_for (TREE_TYPE (startvar)), t);
    4491         3948 :       t = fold_convert (TREE_TYPE (startvar), t);
    4492              :     }
    4493         4136 :   iend = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    4494              :                                    false, GSI_CONTINUE_LINKING);
    4495         4136 :   if (endvar)
    4496              :     {
    4497         1389 :       assign_stmt = gimple_build_assign (endvar, iend);
    4498         1389 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    4499         1389 :       if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (iend)))
    4500          857 :         assign_stmt = gimple_build_assign (fd->loop.v, iend);
    4501              :       else
    4502          532 :         assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, iend);
    4503         1389 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    4504              :     }
    4505              :   /* Handle linear clause adjustments.  */
    4506         4136 :   tree itercnt = NULL_TREE;
    4507         4136 :   if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
    4508        27027 :     for (tree c = gimple_omp_for_clauses (fd->for_stmt);
    4509        27027 :          c; c = OMP_CLAUSE_CHAIN (c))
    4510        22891 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    4511        22891 :           && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
    4512              :         {
    4513           72 :           tree d = OMP_CLAUSE_DECL (c);
    4514           72 :           tree t = d, a, dest;
    4515           72 :           if (omp_privatize_by_reference (t))
    4516            2 :             t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
    4517           72 :           tree type = TREE_TYPE (t);
    4518           72 :           if (POINTER_TYPE_P (type))
    4519            0 :             type = sizetype;
    4520           72 :           dest = unshare_expr (t);
    4521           72 :           tree v = create_tmp_var (TREE_TYPE (t), NULL);
    4522           72 :           expand_omp_build_assign (&gsif, v, t);
    4523           72 :           if (itercnt == NULL_TREE)
    4524              :             {
    4525           72 :               itercnt = startvar;
    4526           72 :               tree n1 = fd->loop.n1;
    4527           72 :               if (POINTER_TYPE_P (TREE_TYPE (itercnt)))
    4528              :                 {
    4529            0 :                   itercnt
    4530            0 :                     = fold_convert (signed_type_for (TREE_TYPE (itercnt)),
    4531              :                                     itercnt);
    4532            0 :                   n1 = fold_convert (TREE_TYPE (itercnt), n1);
    4533              :                 }
    4534           72 :               itercnt = fold_build2 (MINUS_EXPR, TREE_TYPE (itercnt),
    4535              :                                      itercnt, n1);
    4536           72 :               itercnt = fold_build2 (EXACT_DIV_EXPR, TREE_TYPE (itercnt),
    4537              :                                      itercnt, fd->loop.step);
    4538           72 :               itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
    4539              :                                                   NULL_TREE, false,
    4540              :                                                   GSI_CONTINUE_LINKING);
    4541              :             }
    4542           72 :           a = fold_build2 (MULT_EXPR, type,
    4543              :                            fold_convert (type, itercnt),
    4544              :                            fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
    4545           72 :           t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
    4546              :                            : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
    4547           72 :           t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    4548              :                                         false, GSI_CONTINUE_LINKING);
    4549           72 :           expand_omp_build_assign (&gsi, dest, t, true);
    4550              :         }
    4551         4136 :   if (fd->collapse > 1)
    4552         1481 :     expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
    4553              : 
    4554         4136 :   if (fd->ordered)
    4555              :     {
    4556              :       /* Until now, counts array contained number of iterations or
    4557              :          variable containing it for ith loop.  From now on, we usually need
    4558              :          those counts only for collapsed loops, and only for the 2nd
    4559              :          till the last collapsed one.  Move those one element earlier,
    4560              :          we'll use counts[fd->collapse - 1] for the first source/sink
    4561              :          iteration counter and so on and counts[fd->ordered]
    4562              :          as the array holding the current counter values for
    4563              :          depend(source).  For doacross(sink:omp_cur_iteration - 1) we need
    4564              :          the counts from fd->collapse to fd->ordered - 1; make a copy of
    4565              :          those to counts[fd->ordered + 2] and onwards.
    4566              :          counts[fd->ordered + 1] can be a flag whether it is the first
    4567              :          iteration with a new collapsed counter (used only if
    4568              :          fd->ordered > fd->collapse).  */
    4569          335 :       if (fd->ordered > fd->collapse)
    4570          171 :         memcpy (counts + fd->ordered + 2, counts + fd->collapse,
    4571          171 :                 (fd->ordered - fd->collapse) * sizeof (counts[0]));
    4572          335 :       if (fd->collapse > 1)
    4573          147 :         memmove (counts, counts + 1, (fd->collapse - 1) * sizeof (counts[0]));
    4574          335 :       if (broken_loop)
    4575              :         {
    4576              :           int i;
    4577           41 :           for (i = fd->collapse; i < fd->ordered; i++)
    4578              :             {
    4579           33 :               tree type = TREE_TYPE (fd->loops[i].v);
    4580           33 :               tree this_cond
    4581           33 :                 = fold_build2 (fd->loops[i].cond_code, boolean_type_node,
    4582              :                                fold_convert (type, fd->loops[i].n1),
    4583              :                                fold_convert (type, fd->loops[i].n2));
    4584           33 :               if (!integer_onep (this_cond))
    4585              :                 break;
    4586              :             }
    4587           37 :           if (i < fd->ordered)
    4588              :             {
    4589           29 :               if (entry_bb->loop_father != l0_bb->loop_father)
    4590              :                 {
    4591            4 :                   remove_bb_from_loops (l0_bb);
    4592            4 :                   add_bb_to_loop (l0_bb, entry_bb->loop_father);
    4593            4 :                   gcc_assert (single_succ (l0_bb) == l1_bb);
    4594              :                 }
    4595           29 :               cont_bb
    4596           29 :                 = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
    4597           29 :               add_bb_to_loop (cont_bb, l0_bb->loop_father);
    4598           29 :               gimple_stmt_iterator gsi = gsi_after_labels (cont_bb);
    4599           29 :               gimple *g = gimple_build_omp_continue (fd->loop.v, fd->loop.v);
    4600           29 :               gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    4601           29 :               make_edge (cont_bb, l3_bb, EDGE_FALLTHRU);
    4602           29 :               make_edge (cont_bb, l1_bb, 0);
    4603           29 :               l2_bb = create_empty_bb (cont_bb);
    4604           29 :               broken_loop = false;
    4605              :             }
    4606              :         }
    4607          335 :       expand_omp_ordered_source_sink (region, fd, counts, cont_bb);
    4608          335 :       cont_bb = expand_omp_for_ordered_loops (fd, counts, cont_bb, l1_bb,
    4609              :                                               l0_bb, ordered_lastprivate);
    4610          335 :       if (counts[fd->collapse - 1])
    4611              :         {
    4612           37 :           gcc_assert (fd->collapse == 1);
    4613           37 :           gsi = gsi_last_bb (l0_bb);
    4614           37 :           expand_omp_build_assign (&gsi, counts[fd->collapse - 1],
    4615              :                                    istart0, true);
    4616           37 :           if (cont_bb)
    4617              :             {
    4618           33 :               gsi = gsi_last_bb (cont_bb);
    4619           33 :               t = fold_build2 (PLUS_EXPR, fd->iter_type,
    4620              :                                counts[fd->collapse - 1],
    4621              :                                build_int_cst (fd->iter_type, 1));
    4622           33 :               expand_omp_build_assign (&gsi, counts[fd->collapse - 1], t);
    4623           66 :               tree aref = build4 (ARRAY_REF, fd->iter_type,
    4624           33 :                                   counts[fd->ordered], size_zero_node,
    4625              :                                   NULL_TREE, NULL_TREE);
    4626           33 :               expand_omp_build_assign (&gsi, aref, counts[fd->collapse - 1]);
    4627              :             }
    4628           37 :           t = counts[fd->collapse - 1];
    4629              :         }
    4630          298 :       else if (fd->collapse > 1)
    4631          147 :         t = fd->loop.v;
    4632              :       else
    4633              :         {
    4634          151 :           t = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
    4635              :                            fd->loops[0].v, fd->loops[0].n1);
    4636          151 :           t = fold_convert (fd->iter_type, t);
    4637              :         }
    4638          335 :       gsi = gsi_last_bb (l0_bb);
    4639          335 :       tree aref = build4 (ARRAY_REF, fd->iter_type, counts[fd->ordered],
    4640              :                           size_zero_node, NULL_TREE, NULL_TREE);
    4641          335 :       t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    4642              :                                     false, GSI_CONTINUE_LINKING);
    4643          335 :       expand_omp_build_assign (&gsi, aref, t, true);
    4644              :     }
    4645              : 
    4646         4136 :   if (!broken_loop)
    4647              :     {
    4648              :       /* Code to control the increment and predicate for the sequential
    4649              :          loop goes in the CONT_BB.  */
    4650         3758 :       gsi = gsi_last_nondebug_bb (cont_bb);
    4651         3758 :       gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
    4652         3758 :       gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
    4653         3758 :       vmain = gimple_omp_continue_control_use (cont_stmt);
    4654         3758 :       vback = gimple_omp_continue_control_def (cont_stmt);
    4655              : 
    4656         3758 :       if (cond_var)
    4657              :         {
    4658           46 :           tree itype = TREE_TYPE (cond_var);
    4659           46 :           tree t2;
    4660           46 :           if ((fd->ordered && fd->collapse == 1)
    4661           46 :                || bias
    4662           46 :                || POINTER_TYPE_P (type)
    4663           46 :                || TREE_CODE (fd->loop.n1) != INTEGER_CST
    4664           40 :                || fd->loop.cond_code != LT_EXPR)
    4665            6 :             t2 = build_int_cst (itype, 1);
    4666              :           else
    4667           40 :             t2 = fold_convert (itype, fd->loop.step);
    4668           46 :           t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
    4669           46 :           t2 = force_gimple_operand_gsi (&gsi, t2, false,
    4670              :                                          NULL_TREE, true, GSI_SAME_STMT);
    4671           46 :           assign_stmt = gimple_build_assign (cond_var, t2);
    4672           46 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    4673              :         }
    4674              : 
    4675         3758 :       if (!gimple_omp_for_combined_p (fd->for_stmt))
    4676              :         {
    4677         2369 :           if (POINTER_TYPE_P (type))
    4678          160 :             t = fold_build_pointer_plus (vmain, fd->loop.step);
    4679              :           else
    4680         2209 :             t = fold_build2 (PLUS_EXPR, type, vmain, fd->loop.step);
    4681         2369 :           t = force_gimple_operand_gsi (&gsi, t,
    4682         2369 :                                         DECL_P (vback)
    4683         2356 :                                         && TREE_ADDRESSABLE (vback),
    4684              :                                         NULL_TREE, true, GSI_SAME_STMT);
    4685         2369 :           assign_stmt = gimple_build_assign (vback, t);
    4686         2369 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    4687              : 
    4688         2369 :           if (fd->ordered && counts[fd->collapse - 1] == NULL_TREE)
    4689              :             {
    4690          294 :               tree tem;
    4691          294 :               if (fd->collapse > 1)
    4692          143 :                 tem = fd->loop.v;
    4693              :               else
    4694              :                 {
    4695          151 :                   tem = fold_build2 (MINUS_EXPR, TREE_TYPE (fd->loops[0].v),
    4696              :                                      fd->loops[0].v, fd->loops[0].n1);
    4697          151 :                   tem = fold_convert (fd->iter_type, tem);
    4698              :                 }
    4699          588 :               tree aref = build4 (ARRAY_REF, fd->iter_type,
    4700          294 :                                   counts[fd->ordered], size_zero_node,
    4701              :                                   NULL_TREE, NULL_TREE);
    4702          294 :               tem = force_gimple_operand_gsi (&gsi, tem, true, NULL_TREE,
    4703              :                                               true, GSI_SAME_STMT);
    4704          294 :               expand_omp_build_assign (&gsi, aref, tem);
    4705              :             }
    4706              : 
    4707         2369 :           t = build2 (fd->loop.cond_code, boolean_type_node,
    4708         2369 :                       DECL_P (vback) && TREE_ADDRESSABLE (vback) ? t : vback,
    4709              :                       iend);
    4710         2369 :           gcond *cond_stmt = gimple_build_cond_empty (t);
    4711         2369 :           gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
    4712              :         }
    4713              : 
    4714              :       /* Remove GIMPLE_OMP_CONTINUE.  */
    4715         3758 :       gsi_remove (&gsi, true);
    4716              : 
    4717         3758 :       if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
    4718          681 :         collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, l1_bb);
    4719              : 
    4720              :       /* Emit code to get the next parallel iteration in L2_BB.  */
    4721         3758 :       gsi = gsi_start_bb (l2_bb);
    4722              : 
    4723         3758 :       t = build_call_expr (builtin_decl_explicit (next_fn), 2,
    4724              :                            build_fold_addr_expr (istart0),
    4725              :                            build_fold_addr_expr (iend0));
    4726         3758 :       t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    4727              :                                     false, GSI_CONTINUE_LINKING);
    4728         3758 :       if (TREE_TYPE (t) != boolean_type_node)
    4729            0 :         t = fold_build2 (NE_EXPR, boolean_type_node,
    4730              :                          t, build_int_cst (TREE_TYPE (t), 0));
    4731         3758 :       gcond *cond_stmt = gimple_build_cond_empty (t);
    4732         3758 :       gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
    4733              :     }
    4734              : 
    4735              :   /* Add the loop cleanup function.  */
    4736         4136 :   gsi = gsi_last_nondebug_bb (exit_bb);
    4737         4136 :   if (gimple_omp_return_nowait_p (gsi_stmt (gsi)))
    4738         3047 :     t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
    4739         1089 :   else if (gimple_omp_return_lhs (gsi_stmt (gsi)))
    4740            2 :     t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
    4741              :   else
    4742         1087 :     t = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
    4743         4136 :   gcall *call_stmt = gimple_build_call (t, 0);
    4744         4136 :   if (fd->ordered)
    4745              :     {
    4746          335 :       tree arr = counts[fd->ordered];
    4747          335 :       tree clobber = build_clobber (TREE_TYPE (arr));
    4748          335 :       gsi_insert_after (&gsi, gimple_build_assign (arr, clobber),
    4749              :                         GSI_SAME_STMT);
    4750              :     }
    4751         4136 :   if (gimple_omp_return_lhs (gsi_stmt (gsi)))
    4752              :     {
    4753            2 :       gimple_call_set_lhs (call_stmt, gimple_omp_return_lhs (gsi_stmt (gsi)));
    4754            2 :       if (fd->have_reductemp)
    4755              :         {
    4756            0 :           gimple *g = gimple_build_assign (reductions, NOP_EXPR,
    4757              :                                            gimple_call_lhs (call_stmt));
    4758            0 :           gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    4759              :         }
    4760              :     }
    4761         4136 :   gsi_insert_after (&gsi, call_stmt, GSI_SAME_STMT);
    4762         4136 :   gsi_remove (&gsi, true);
    4763              : 
    4764              :   /* Connect the new blocks.  */
    4765         4136 :   find_edge (entry_bb, l0_bb)->flags = EDGE_TRUE_VALUE;
    4766         4136 :   find_edge (entry_bb, l3_bb)->flags = EDGE_FALSE_VALUE;
    4767              : 
    4768         4136 :   if (!broken_loop)
    4769              :     {
    4770         3758 :       gimple_seq phis;
    4771              : 
    4772         3758 :       e = find_edge (cont_bb, l3_bb);
    4773         3758 :       ne = make_edge (l2_bb, l3_bb, EDGE_FALSE_VALUE);
    4774              : 
    4775         3758 :       phis = phi_nodes (l3_bb);
    4776         3780 :       for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
    4777              :         {
    4778           11 :           gimple *phi = gsi_stmt (gsi);
    4779           11 :           SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, ne),
    4780              :                    PHI_ARG_DEF_FROM_EDGE (phi, e));
    4781              :         }
    4782         3758 :       remove_edge (e);
    4783              : 
    4784         3758 :       make_edge (cont_bb, l2_bb, EDGE_FALSE_VALUE);
    4785         3758 :       e = find_edge (cont_bb, l1_bb);
    4786         3758 :       if (e == NULL)
    4787              :         {
    4788           13 :           e = BRANCH_EDGE (cont_bb);
    4789           13 :           gcc_assert (single_succ (e->dest) == l1_bb);
    4790              :         }
    4791         3758 :       if (gimple_omp_for_combined_p (fd->for_stmt))
    4792              :         {
    4793         1389 :           remove_edge (e);
    4794         1389 :           e = NULL;
    4795              :         }
    4796         2369 :       else if (fd->collapse > 1)
    4797              :         {
    4798          681 :           remove_edge (e);
    4799          681 :           e = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
    4800              :         }
    4801              :       else
    4802         1688 :         e->flags = EDGE_TRUE_VALUE;
    4803         3758 :       if (e)
    4804              :         {
    4805         2369 :           e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
    4806         2369 :           find_edge (cont_bb, l2_bb)->probability = e->probability.invert ();
    4807              :         }
    4808              :       else
    4809              :         {
    4810         1389 :           e = find_edge (cont_bb, l2_bb);
    4811         1389 :           e->flags = EDGE_FALLTHRU;
    4812              :         }
    4813         3758 :       make_edge (l2_bb, l0_bb, EDGE_TRUE_VALUE);
    4814              : 
    4815         3758 :       if (gimple_in_ssa_p (cfun))
    4816              :         {
    4817              :           /* Add phis to the outer loop that connect to the phis in the inner,
    4818              :              original loop, and move the loop entry value of the inner phi to
    4819              :              the loop entry value of the outer phi.  */
    4820           13 :           gphi_iterator psi;
    4821           24 :           for (psi = gsi_start_phis (l3_bb); !gsi_end_p (psi); gsi_next (&psi))
    4822              :             {
    4823           11 :               location_t locus;
    4824           11 :               gphi *nphi;
    4825           11 :               gphi *exit_phi = psi.phi ();
    4826              : 
    4827           22 :               if (virtual_operand_p (gimple_phi_result (exit_phi)))
    4828            6 :                 continue;
    4829              : 
    4830            5 :               edge l2_to_l3 = find_edge (l2_bb, l3_bb);
    4831            5 :               tree exit_res = PHI_ARG_DEF_FROM_EDGE (exit_phi, l2_to_l3);
    4832              : 
    4833            5 :               basic_block latch = BRANCH_EDGE (cont_bb)->dest;
    4834            5 :               edge latch_to_l1 = find_edge (latch, l1_bb);
    4835            5 :               gphi *inner_phi
    4836            5 :                 = find_phi_with_arg_on_edge (exit_res, latch_to_l1);
    4837              : 
    4838            5 :               tree t = gimple_phi_result (exit_phi);
    4839            5 :               tree new_res = copy_ssa_name (t, NULL);
    4840            5 :               nphi = create_phi_node (new_res, l0_bb);
    4841              : 
    4842            5 :               edge l0_to_l1 = find_edge (l0_bb, l1_bb);
    4843            5 :               t = PHI_ARG_DEF_FROM_EDGE (inner_phi, l0_to_l1);
    4844            5 :               locus = gimple_phi_arg_location_from_edge (inner_phi, l0_to_l1);
    4845            5 :               edge entry_to_l0 = find_edge (entry_bb, l0_bb);
    4846            5 :               add_phi_arg (nphi, t, entry_to_l0, locus);
    4847              : 
    4848            5 :               edge l2_to_l0 = find_edge (l2_bb, l0_bb);
    4849            5 :               add_phi_arg (nphi, exit_res, l2_to_l0, UNKNOWN_LOCATION);
    4850              : 
    4851            5 :               add_phi_arg (inner_phi, new_res, l0_to_l1, UNKNOWN_LOCATION);
    4852              :             }
    4853              :         }
    4854              : 
    4855         3758 :       set_immediate_dominator (CDI_DOMINATORS, l2_bb,
    4856              :                                recompute_dominator (CDI_DOMINATORS, l2_bb));
    4857         3758 :       set_immediate_dominator (CDI_DOMINATORS, l3_bb,
    4858              :                                recompute_dominator (CDI_DOMINATORS, l3_bb));
    4859         3758 :       set_immediate_dominator (CDI_DOMINATORS, l0_bb,
    4860              :                                recompute_dominator (CDI_DOMINATORS, l0_bb));
    4861         3758 :       set_immediate_dominator (CDI_DOMINATORS, l1_bb,
    4862              :                                recompute_dominator (CDI_DOMINATORS, l1_bb));
    4863              : 
    4864              :       /* We enter expand_omp_for_generic with a loop.  This original loop may
    4865              :          have its own loop struct, or it may be part of an outer loop struct
    4866              :          (which may be the fake loop).  */
    4867         3758 :       class loop *outer_loop = entry_bb->loop_father;
    4868         3758 :       bool orig_loop_has_loop_struct = l1_bb->loop_father != outer_loop;
    4869              : 
    4870         3758 :       add_bb_to_loop (l2_bb, outer_loop);
    4871              : 
    4872              :       /* We've added a new loop around the original loop.  Allocate the
    4873              :          corresponding loop struct.  */
    4874         3758 :       class loop *new_loop = alloc_loop ();
    4875         3758 :       new_loop->header = l0_bb;
    4876         3758 :       new_loop->latch = l2_bb;
    4877         3758 :       add_loop (new_loop, outer_loop);
    4878              : 
    4879              :       /* Allocate a loop structure for the original loop unless we already
    4880              :          had one.  */
    4881         3758 :       if (!orig_loop_has_loop_struct
    4882         3758 :           && !gimple_omp_for_combined_p (fd->for_stmt))
    4883              :         {
    4884         2352 :           class loop *orig_loop = alloc_loop ();
    4885         2352 :           orig_loop->header = l1_bb;
    4886              :           /* The loop may have multiple latches.  */
    4887         2352 :           add_loop (orig_loop, new_loop);
    4888              :         }
    4889              :     }
    4890         4136 : }
    4891              : 
    4892              : /* Helper function for expand_omp_for_static_nochunk.  If PTR is NULL,
    4893              :    compute needed allocation size.  If !ALLOC of team allocations,
    4894              :    if ALLOC of thread allocation.  SZ is the initial needed size for
    4895              :    other purposes, ALLOC_ALIGN guaranteed alignment of allocation in bytes,
    4896              :    CNT number of elements of each array, for !ALLOC this is
    4897              :    omp_get_num_threads (), for ALLOC number of iterations handled by the
    4898              :    current thread.  If PTR is non-NULL, it is the start of the allocation
    4899              :    and this routine shall assign to OMP_CLAUSE_DECL (c) of those _scantemp_
    4900              :    clauses pointers to the corresponding arrays.  */
    4901              : 
    4902              : static tree
    4903          748 : expand_omp_scantemp_alloc (tree clauses, tree ptr, unsigned HOST_WIDE_INT sz,
    4904              :                            unsigned HOST_WIDE_INT alloc_align, tree cnt,
    4905              :                            gimple_stmt_iterator *gsi, bool alloc)
    4906              : {
    4907          748 :   tree eltsz = NULL_TREE;
    4908          748 :   unsigned HOST_WIDE_INT preval = 0;
    4909          748 :   if (ptr && sz)
    4910            5 :     ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
    4911              :                        ptr, size_int (sz));
    4912         6168 :   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    4913         5420 :     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
    4914         3248 :         && !OMP_CLAUSE__SCANTEMP__CONTROL (c)
    4915         7172 :         && (!OMP_CLAUSE__SCANTEMP__ALLOC (c)) != alloc)
    4916              :       {
    4917          876 :         tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
    4918          876 :         unsigned HOST_WIDE_INT al = TYPE_ALIGN_UNIT (pointee_type);
    4919          876 :         if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
    4920              :           {
    4921          876 :             unsigned HOST_WIDE_INT szl
    4922          876 :               = tree_to_uhwi (TYPE_SIZE_UNIT (pointee_type));
    4923          876 :             szl = least_bit_hwi (szl);
    4924          876 :             if (szl)
    4925          876 :               al = MIN (al, szl);
    4926              :           }
    4927          876 :         if (ptr == NULL_TREE)
    4928              :           {
    4929          438 :             if (eltsz == NULL_TREE)
    4930          374 :               eltsz = TYPE_SIZE_UNIT (pointee_type);
    4931              :             else
    4932           64 :               eltsz = size_binop (PLUS_EXPR, eltsz,
    4933              :                                   TYPE_SIZE_UNIT (pointee_type));
    4934              :           }
    4935          876 :         if (preval == 0 && al <= alloc_align)
    4936              :           {
    4937          748 :             unsigned HOST_WIDE_INT diff = ROUND_UP (sz, al) - sz;
    4938          748 :             sz += diff;
    4939          748 :             if (diff && ptr)
    4940            0 :               ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr),
    4941              :                                  ptr, size_int (diff));
    4942              :           }
    4943          128 :         else if (al > preval)
    4944              :           {
    4945           32 :             if (ptr)
    4946              :               {
    4947           16 :                 ptr = fold_convert (pointer_sized_int_node, ptr);
    4948           16 :                 ptr = fold_build2 (PLUS_EXPR, pointer_sized_int_node, ptr,
    4949              :                                    build_int_cst (pointer_sized_int_node,
    4950              :                                                   al - 1));
    4951           16 :                 ptr = fold_build2 (BIT_AND_EXPR, pointer_sized_int_node, ptr,
    4952              :                                    build_int_cst (pointer_sized_int_node,
    4953              :                                                   -(HOST_WIDE_INT) al));
    4954           16 :                 ptr = fold_convert (ptr_type_node, ptr);
    4955              :               }
    4956              :             else
    4957           16 :               sz += al - 1;
    4958              :           }
    4959          876 :         if (tree_fits_uhwi_p (TYPE_SIZE_UNIT (pointee_type)))
    4960              :           preval = al;
    4961              :         else
    4962            0 :           preval = 1;
    4963          876 :         if (ptr)
    4964              :           {
    4965          438 :             expand_omp_build_assign (gsi, OMP_CLAUSE_DECL (c), ptr, false);
    4966          438 :             ptr = OMP_CLAUSE_DECL (c);
    4967          438 :             ptr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
    4968              :                                size_binop (MULT_EXPR, cnt,
    4969              :                                            TYPE_SIZE_UNIT (pointee_type)));
    4970              :           }
    4971              :       }
    4972              : 
    4973          748 :   if (ptr == NULL_TREE)
    4974              :     {
    4975          374 :       eltsz = size_binop (MULT_EXPR, eltsz, cnt);
    4976          374 :       if (sz)
    4977           13 :         eltsz = size_binop (PLUS_EXPR, eltsz, size_int (sz));
    4978              :       return eltsz;
    4979              :     }
    4980              :   else
    4981              :     return ptr;
    4982              : }
    4983              : 
    4984              : /* Return the last _looptemp_ clause if one has been created for
    4985              :    lastprivate on distribute parallel for{, simd} or taskloop.
    4986              :    FD is the loop data and INNERC should be the second _looptemp_
    4987              :    clause (the one holding the end of the range).
    4988              :    This is followed by collapse - 1 _looptemp_ clauses for the
    4989              :    counts[1] and up, and for triangular loops followed by 4
    4990              :    further _looptemp_ clauses (one for counts[0], one first_inner_iterations,
    4991              :    one factor and one adjn1).  After this there is optionally one
    4992              :    _looptemp_ clause that this function returns.  */
    4993              : 
    4994              : static tree
    4995         1802 : find_lastprivate_looptemp (struct omp_for_data *fd, tree innerc)
    4996              : {
    4997         1802 :   gcc_assert (innerc);
    4998         1802 :   int count = fd->collapse - 1;
    4999         1802 :   if (fd->non_rect
    5000           24 :       && fd->last_nonrect == fd->first_nonrect + 1
    5001         1814 :       && !TYPE_UNSIGNED (TREE_TYPE (fd->loops[fd->last_nonrect].v)))
    5002           12 :     count += 4;
    5003         5163 :   for (int i = 0; i < count; i++)
    5004              :     {
    5005         3361 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    5006              :                                 OMP_CLAUSE__LOOPTEMP_);
    5007         3361 :       gcc_assert (innerc);
    5008              :     }
    5009         1802 :   return omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    5010         1802 :                           OMP_CLAUSE__LOOPTEMP_);
    5011              : }
    5012              : 
    5013              : /* A subroutine of expand_omp_for.  Generate code for a parallel
    5014              :    loop with static schedule and no specified chunk size.  Given
    5015              :    parameters:
    5016              : 
    5017              :         for (V = N1; V cond N2; V += STEP) BODY;
    5018              : 
    5019              :    where COND is "<" or ">", we generate pseudocode
    5020              : 
    5021              :         if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
    5022              :         if (cond is <)
    5023              :           adj = STEP - 1;
    5024              :         else
    5025              :           adj = STEP + 1;
    5026              :         if ((__typeof (V)) -1 > 0 && cond is >)
    5027              :           n = -(adj + N2 - N1) / -STEP;
    5028              :         else
    5029              :           n = (adj + N2 - N1) / STEP;
    5030              :         q = n / nthreads;
    5031              :         tt = n % nthreads;
    5032              :         if (threadid < tt) goto L3; else goto L4;
    5033              :     L3:
    5034              :         tt = 0;
    5035              :         q = q + 1;
    5036              :     L4:
    5037              :         s0 = q * threadid + tt;
    5038              :         e0 = s0 + q;
    5039              :         V = s0 * STEP + N1;
    5040              :         if (s0 >= e0) goto L2; else goto L0;
    5041              :     L0:
    5042              :         e = e0 * STEP + N1;
    5043              :     L1:
    5044              :         BODY;
    5045              :         V += STEP;
    5046              :         if (V cond e) goto L1;
    5047              :     L2:
    5048              : */
    5049              : 
    5050              : static void
    5051        15363 : expand_omp_for_static_nochunk (struct omp_region *region,
    5052              :                                struct omp_for_data *fd,
    5053              :                                gimple *inner_stmt)
    5054              : {
    5055        15363 :   tree n, q, s0, e0, e, t, tt, nthreads = NULL_TREE, threadid;
    5056        15363 :   tree type, itype, vmain, vback;
    5057        15363 :   basic_block entry_bb, second_bb, third_bb, exit_bb, seq_start_bb;
    5058        15363 :   basic_block body_bb, cont_bb, collapse_bb = NULL;
    5059        15363 :   basic_block fin_bb, fourth_bb = NULL, fifth_bb = NULL, sixth_bb = NULL;
    5060        15363 :   basic_block exit1_bb = NULL, exit2_bb = NULL, exit3_bb = NULL;
    5061        15363 :   gimple_stmt_iterator gsi, gsip;
    5062        15363 :   edge ep;
    5063        15363 :   bool broken_loop = region->cont == NULL;
    5064        15363 :   tree *counts = NULL;
    5065        15363 :   tree n1, n2, step;
    5066        15363 :   tree reductions = NULL_TREE;
    5067        15363 :   tree cond_var = NULL_TREE, condtemp = NULL_TREE;
    5068              : 
    5069        15363 :   itype = type = TREE_TYPE (fd->loop.v);
    5070        15363 :   if (POINTER_TYPE_P (type))
    5071          606 :     itype = signed_type_for (type);
    5072              : 
    5073        15363 :   entry_bb = region->entry;
    5074        15363 :   cont_bb = region->cont;
    5075        15363 :   gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
    5076        15363 :   fin_bb = BRANCH_EDGE (entry_bb)->dest;
    5077        15363 :   gcc_assert (broken_loop
    5078              :               || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
    5079        15363 :   seq_start_bb = split_edge (FALLTHRU_EDGE (entry_bb));
    5080        15363 :   body_bb = single_succ (seq_start_bb);
    5081        15363 :   if (!broken_loop)
    5082              :     {
    5083        14867 :       gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
    5084              :                   || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
    5085        14867 :       gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
    5086              :     }
    5087        15363 :   exit_bb = region->exit;
    5088              : 
    5089              :   /* Iteration space partitioning goes in ENTRY_BB.  */
    5090        15363 :   gsi = gsi_last_nondebug_bb (entry_bb);
    5091        15363 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    5092        15363 :   gsip = gsi;
    5093        15363 :   gsi_prev (&gsip);
    5094              : 
    5095        15363 :   if (fd->collapse > 1)
    5096              :     {
    5097         5195 :       int first_zero_iter = -1, dummy = -1;
    5098         5195 :       basic_block l2_dom_bb = NULL, dummy_bb = NULL;
    5099              : 
    5100         5195 :       counts = XALLOCAVEC (tree, fd->collapse);
    5101         5195 :       expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
    5102              :                                   fin_bb, first_zero_iter,
    5103              :                                   dummy_bb, dummy, l2_dom_bb);
    5104         5195 :       t = NULL_TREE;
    5105              :     }
    5106        10168 :   else if (gimple_omp_for_combined_into_p (fd->for_stmt))
    5107         2037 :     t = integer_one_node;
    5108              :   else
    5109         8131 :     t = fold_binary (fd->loop.cond_code, boolean_type_node,
    5110              :                      fold_convert (type, fd->loop.n1),
    5111              :                      fold_convert (type, fd->loop.n2));
    5112        15363 :   if (fd->collapse == 1
    5113        10168 :       && TYPE_UNSIGNED (type)
    5114        17379 :       && (t == NULL_TREE || !integer_onep (t)))
    5115              :     {
    5116          524 :       n1 = fold_convert (type, unshare_expr (fd->loop.n1));
    5117          524 :       n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
    5118              :                                      true, GSI_SAME_STMT);
    5119          524 :       n2 = fold_convert (type, unshare_expr (fd->loop.n2));
    5120          524 :       n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
    5121              :                                      true, GSI_SAME_STMT);
    5122          524 :       gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
    5123              :                                                 n1, n2);
    5124          524 :       ep = split_block (entry_bb, cond_stmt);
    5125          524 :       ep->flags = EDGE_TRUE_VALUE;
    5126          524 :       entry_bb = ep->dest;
    5127          524 :       ep->probability = profile_probability::very_likely ();
    5128          524 :       ep = make_edge (ep->src, fin_bb, EDGE_FALSE_VALUE);
    5129          524 :       ep->probability = profile_probability::very_unlikely ();
    5130          524 :       if (gimple_in_ssa_p (cfun))
    5131              :         {
    5132           21 :           int dest_idx = find_edge (entry_bb, fin_bb)->dest_idx;
    5133           21 :           for (gphi_iterator gpi = gsi_start_phis (fin_bb);
    5134           41 :                !gsi_end_p (gpi); gsi_next (&gpi))
    5135              :             {
    5136           20 :               gphi *phi = gpi.phi ();
    5137           20 :               add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
    5138              :                            ep, UNKNOWN_LOCATION);
    5139              :             }
    5140              :         }
    5141         1048 :       gsi = gsi_last_bb (entry_bb);
    5142              :     }
    5143              : 
    5144        15363 :   if (fd->lastprivate_conditional)
    5145              :     {
    5146          112 :       tree clauses = gimple_omp_for_clauses (fd->for_stmt);
    5147          112 :       tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
    5148          112 :       if (fd->have_pointer_condtemp)
    5149           38 :         condtemp = OMP_CLAUSE_DECL (c);
    5150          112 :       c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
    5151          112 :       cond_var = OMP_CLAUSE_DECL (c);
    5152              :     }
    5153        15363 :   if (fd->have_reductemp
    5154              :       /* For scan, we don't want to reinitialize condtemp before the
    5155              :          second loop.  */
    5156        15310 :       || (fd->have_pointer_condtemp && !fd->have_scantemp)
    5157        15292 :       || fd->have_nonctrl_scantemp)
    5158              :     {
    5159          258 :       tree t1 = build_int_cst (long_integer_type_node, 0);
    5160          258 :       tree t2 = build_int_cst (long_integer_type_node, 1);
    5161          258 :       tree t3 = build_int_cstu (long_integer_type_node,
    5162              :                                 (HOST_WIDE_INT_1U << 31) + 1);
    5163          258 :       tree clauses = gimple_omp_for_clauses (fd->for_stmt);
    5164          258 :       gimple_stmt_iterator gsi2 = gsi_none ();
    5165          258 :       gimple *g = NULL;
    5166          258 :       tree mem = null_pointer_node, memv = NULL_TREE;
    5167          258 :       unsigned HOST_WIDE_INT condtemp_sz = 0;
    5168          258 :       unsigned HOST_WIDE_INT alloc_align = 0;
    5169          258 :       if (fd->have_reductemp)
    5170              :         {
    5171           53 :           gcc_assert (!fd->have_nonctrl_scantemp);
    5172           53 :           tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
    5173           53 :           reductions = OMP_CLAUSE_DECL (c);
    5174           53 :           gcc_assert (TREE_CODE (reductions) == SSA_NAME);
    5175           53 :           g = SSA_NAME_DEF_STMT (reductions);
    5176           53 :           reductions = gimple_assign_rhs1 (g);
    5177           53 :           OMP_CLAUSE_DECL (c) = reductions;
    5178           53 :           gsi2 = gsi_for_stmt (g);
    5179              :         }
    5180              :       else
    5181              :         {
    5182          205 :           if (gsi_end_p (gsip))
    5183            0 :             gsi2 = gsi_after_labels (region->entry);
    5184              :           else
    5185          205 :             gsi2 = gsip;
    5186              :           reductions = null_pointer_node;
    5187              :         }
    5188          258 :       if (fd->have_pointer_condtemp || fd->have_nonctrl_scantemp)
    5189              :         {
    5190          215 :           tree type;
    5191          215 :           if (fd->have_pointer_condtemp)
    5192           33 :             type = TREE_TYPE (condtemp);
    5193              :           else
    5194          182 :             type = ptr_type_node;
    5195          215 :           memv = create_tmp_var (type);
    5196          215 :           TREE_ADDRESSABLE (memv) = 1;
    5197          215 :           unsigned HOST_WIDE_INT sz = 0;
    5198          215 :           tree size = NULL_TREE;
    5199          215 :           if (fd->have_pointer_condtemp)
    5200              :             {
    5201           33 :               sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
    5202           33 :               sz *= fd->lastprivate_conditional;
    5203           33 :               condtemp_sz = sz;
    5204              :             }
    5205          215 :           if (fd->have_nonctrl_scantemp)
    5206              :             {
    5207          187 :               nthreads = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_THREADS);
    5208          187 :               gimple *g = gimple_build_call (nthreads, 0);
    5209          187 :               nthreads = create_tmp_var (integer_type_node);
    5210          187 :               gimple_call_set_lhs (g, nthreads);
    5211          187 :               gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
    5212          187 :               nthreads = fold_convert (sizetype, nthreads);
    5213          187 :               alloc_align = TYPE_ALIGN_UNIT (long_long_integer_type_node);
    5214          187 :               size = expand_omp_scantemp_alloc (clauses, NULL_TREE, sz,
    5215              :                                                 alloc_align, nthreads, NULL,
    5216              :                                                 false);
    5217          187 :               size = fold_convert (type, size);
    5218              :             }
    5219              :           else
    5220           28 :             size = build_int_cst (type, sz);
    5221          215 :           expand_omp_build_assign (&gsi2, memv, size, false);
    5222          215 :           mem = build_fold_addr_expr (memv);
    5223              :         }
    5224          258 :       tree t
    5225          258 :         = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
    5226              :                            9, t1, t2, t2, t3, t1, null_pointer_node,
    5227              :                            null_pointer_node, reductions, mem);
    5228          258 :       force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    5229              :                                 true, GSI_SAME_STMT);
    5230          258 :       if (fd->have_pointer_condtemp)
    5231           33 :         expand_omp_build_assign (&gsi2, condtemp, memv, false);
    5232          258 :       if (fd->have_nonctrl_scantemp)
    5233              :         {
    5234          187 :           tree ptr = fd->have_pointer_condtemp ? condtemp : memv;
    5235          187 :           expand_omp_scantemp_alloc (clauses, ptr, condtemp_sz,
    5236              :                                      alloc_align, nthreads, &gsi2, false);
    5237              :         }
    5238          258 :       if (fd->have_reductemp)
    5239              :         {
    5240           53 :           gsi_remove (&gsi2, true);
    5241           53 :           release_ssa_name (gimple_assign_lhs (g));
    5242              :         }
    5243              :     }
    5244              : 
    5245        15363 :   n1 = fd->loop.n1;
    5246        15363 :   n2 = fd->loop.n2;
    5247        15363 :   step = fd->loop.step;
    5248        15363 :   if (gimple_omp_for_combined_into_p (fd->for_stmt))
    5249              :     {
    5250         3688 :       tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    5251              :                                      OMP_CLAUSE__LOOPTEMP_);
    5252         3688 :       gcc_assert (innerc);
    5253         3688 :       n1 = OMP_CLAUSE_DECL (innerc);
    5254         3688 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    5255              :                                 OMP_CLAUSE__LOOPTEMP_);
    5256         3688 :       gcc_assert (innerc);
    5257         3688 :       n2 = OMP_CLAUSE_DECL (innerc);
    5258              :     }
    5259        15363 :   n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
    5260              :                                  true, NULL_TREE, true, GSI_SAME_STMT);
    5261        15363 :   n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
    5262              :                                  true, NULL_TREE, true, GSI_SAME_STMT);
    5263        15363 :   step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
    5264              :                                    true, NULL_TREE, true, GSI_SAME_STMT);
    5265              : 
    5266        16382 :   t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
    5267        15363 :   t = fold_build2 (PLUS_EXPR, itype, step, t);
    5268        15363 :   t = fold_build2 (PLUS_EXPR, itype, t, n2);
    5269        15363 :   t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
    5270        15363 :   if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
    5271          414 :     t = fold_build2 (TRUNC_DIV_EXPR, itype,
    5272              :                      fold_build1 (NEGATE_EXPR, itype, t),
    5273              :                      fold_build1 (NEGATE_EXPR, itype, step));
    5274              :   else
    5275        14949 :     t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    5276        15363 :   t = fold_convert (itype, t);
    5277        15363 :   n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
    5278              : 
    5279              :   /* When GOMP_loop_end (or one of its variants) is emitted (e.g. with the
    5280              :      inscan modifier), which already implies the end of the scope, _start
    5281              :      variants of GOMP builtin calls have to be used and _end can be skipped.  */
    5282        30726 :   bool has_gomp_loop_end = fd->have_reductemp
    5283        15363 :                            || ((fd->have_pointer_condtemp || fd->have_scantemp)
    5284          392 :                                && !fd->have_nonctrl_scantemp);
    5285        15363 :   {
    5286              :     /* Fetch the thread/team id and the number of threads/teams in a single
    5287              :        call to GOMP_loop_static_worksharing or
    5288              :        GOMP_distribute_static_worksharing. The helper returns both values packed
    5289              :        into one complex int, with the id as the imaginary part and the count as
    5290              :        the real part.  Returning (rather than writing through pointers) keeps
    5291              :        both values as plain SSA names, which lets later passes - notably IPA-CP
    5292              :        propagating constants into the outlined kernel - reason about them.
    5293              :        Also pass the total number of iterations for OMPT.  */
    5294        15363 :     tree decl;
    5295        15363 :     switch (gimple_omp_for_kind (fd->for_stmt))
    5296              :       {
    5297        10184 :       case GF_OMP_FOR_KIND_FOR:
    5298        20368 :         decl = builtin_decl_explicit (
    5299        10184 :           flag_openmp_ompt || has_gomp_loop_end
    5300              :             ? BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_START
    5301              :             : BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING);
    5302        10184 :         break;
    5303         5179 :       case GF_OMP_FOR_KIND_DISTRIBUTE:
    5304        10358 :         decl = builtin_decl_explicit (
    5305         5179 :           flag_openmp_ompt || has_gomp_loop_end
    5306              :             ? BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_START
    5307              :             : BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING);
    5308         5179 :         break;
    5309            0 :       default:
    5310            0 :         gcc_unreachable ();
    5311              :       }
    5312        15363 :     tree n_ull = fold_convert (long_long_unsigned_type_node, n);
    5313        15363 :     tree packed = build_call_expr (decl, 1, n_ull);
    5314        15363 :     packed = force_gimple_operand_gsi (&gsi, packed, true, NULL_TREE,
    5315              :                                        true, GSI_SAME_STMT);
    5316        15363 :     threadid = fold_build1 (IMAGPART_EXPR, integer_type_node, packed);
    5317        15363 :     threadid = fold_convert (itype, threadid);
    5318        15363 :     threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE,
    5319              :                                          true, GSI_SAME_STMT);
    5320        15363 :     nthreads = fold_build1 (REALPART_EXPR, integer_type_node, packed);
    5321        15363 :     nthreads = fold_convert (itype, nthreads);
    5322        15363 :     nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE,
    5323              :                                          true, GSI_SAME_STMT);
    5324              :   }
    5325              : 
    5326        15363 :   q = create_tmp_reg (itype, "q");
    5327        15363 :   t = fold_build2 (TRUNC_DIV_EXPR, itype, n, nthreads);
    5328        15363 :   t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
    5329        15363 :   gsi_insert_before (&gsi, gimple_build_assign (q, t), GSI_SAME_STMT);
    5330              : 
    5331        15363 :   tt = create_tmp_reg (itype, "tt");
    5332        15363 :   t = fold_build2 (TRUNC_MOD_EXPR, itype, n, nthreads);
    5333        15363 :   t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, true, GSI_SAME_STMT);
    5334        15363 :   gsi_insert_before (&gsi, gimple_build_assign (tt, t), GSI_SAME_STMT);
    5335              : 
    5336        15363 :   t = build2 (LT_EXPR, boolean_type_node, threadid, tt);
    5337        15363 :   gcond *cond_stmt = gimple_build_cond_empty (t);
    5338        15363 :   gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
    5339              : 
    5340        15363 :   second_bb = split_block (entry_bb, cond_stmt)->dest;
    5341        15363 :   gsi = gsi_last_nondebug_bb (second_bb);
    5342        15363 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    5343              : 
    5344        15363 :   gsi_insert_before (&gsi, gimple_build_assign (tt, build_int_cst (itype, 0)),
    5345              :                      GSI_SAME_STMT);
    5346        15363 :   gassign *assign_stmt
    5347        15363 :     = gimple_build_assign (q, PLUS_EXPR, q, build_int_cst (itype, 1));
    5348        15363 :   gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    5349              : 
    5350        15363 :   third_bb = split_block (second_bb, assign_stmt)->dest;
    5351        15363 :   gsi = gsi_last_nondebug_bb (third_bb);
    5352        15363 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    5353              : 
    5354        15363 :   if (fd->have_nonctrl_scantemp)
    5355              :     {
    5356          187 :       tree clauses = gimple_omp_for_clauses (fd->for_stmt);
    5357          187 :       tree controlp = NULL_TREE, controlb = NULL_TREE;
    5358          812 :       for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    5359          812 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
    5360          812 :             && OMP_CLAUSE__SCANTEMP__CONTROL (c))
    5361              :           {
    5362          374 :             if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
    5363          187 :               controlb = OMP_CLAUSE_DECL (c);
    5364              :             else
    5365          187 :               controlp = OMP_CLAUSE_DECL (c);
    5366          374 :             if (controlb && controlp)
    5367              :               break;
    5368              :           }
    5369          187 :       gcc_assert (controlp && controlb);
    5370          187 :       tree cnt = create_tmp_var (sizetype);
    5371          187 :       gimple *g = gimple_build_assign (cnt, NOP_EXPR, q);
    5372          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5373          187 :       unsigned HOST_WIDE_INT alloc_align = TYPE_ALIGN_UNIT (ptr_type_node);
    5374          187 :       tree sz = expand_omp_scantemp_alloc (clauses, NULL_TREE, 0,
    5375              :                                            alloc_align, cnt, NULL, true);
    5376          187 :       tree size = create_tmp_var (sizetype);
    5377          187 :       expand_omp_build_assign (&gsi, size, sz, false);
    5378          187 :       tree cmp = fold_build2 (GT_EXPR, boolean_type_node,
    5379              :                               size, size_int (16384));
    5380          187 :       expand_omp_build_assign (&gsi, controlb, cmp);
    5381          187 :       g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
    5382              :                              NULL_TREE, NULL_TREE);
    5383          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5384          187 :       fourth_bb = split_block (third_bb, g)->dest;
    5385          187 :       gsi = gsi_last_nondebug_bb (fourth_bb);
    5386              :       /* FIXME: Once we have allocators, this should use allocator.  */
    5387          187 :       g = gimple_build_call (builtin_decl_explicit (BUILT_IN_MALLOC), 1, size);
    5388          187 :       gimple_call_set_lhs (g, controlp);
    5389          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5390          187 :       expand_omp_scantemp_alloc (clauses, controlp, 0, alloc_align, cnt,
    5391              :                                  &gsi, true);
    5392          187 :       gsi_prev (&gsi);
    5393          187 :       g = gsi_stmt (gsi);
    5394          187 :       fifth_bb = split_block (fourth_bb, g)->dest;
    5395          187 :       gsi = gsi_last_nondebug_bb (fifth_bb);
    5396              : 
    5397          374 :       g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
    5398          187 :       gimple_call_set_lhs (g, controlp);
    5399          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5400          187 :       tree alloca_decl = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
    5401         1542 :       for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    5402         1355 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
    5403         1355 :             && OMP_CLAUSE__SCANTEMP__ALLOC (c))
    5404              :           {
    5405          219 :             tree tmp = create_tmp_var (sizetype);
    5406          219 :             tree pointee_type = TREE_TYPE (TREE_TYPE (OMP_CLAUSE_DECL (c)));
    5407          219 :             g = gimple_build_assign (tmp, MULT_EXPR, cnt,
    5408          219 :                                      TYPE_SIZE_UNIT (pointee_type));
    5409          219 :             gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5410          219 :             g = gimple_build_call (alloca_decl, 2, tmp,
    5411          219 :                                    size_int (TYPE_ALIGN (pointee_type)));
    5412          219 :             gimple_call_set_lhs (g, OMP_CLAUSE_DECL (c));
    5413          219 :             gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5414              :           }
    5415              : 
    5416          187 :       sixth_bb = split_block (fifth_bb, g)->dest;
    5417          187 :       gsi = gsi_last_nondebug_bb (sixth_bb);
    5418              :     }
    5419              : 
    5420        15363 :   t = build2 (MULT_EXPR, itype, q, threadid);
    5421        15363 :   t = build2 (PLUS_EXPR, itype, t, tt);
    5422        15363 :   s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
    5423              : 
    5424        15363 :   t = fold_build2 (PLUS_EXPR, itype, s0, q);
    5425        15363 :   e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
    5426              : 
    5427        15363 :   t = build2 (GE_EXPR, boolean_type_node, s0, e0);
    5428        15363 :   gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
    5429              : 
    5430              :   /* Remove the GIMPLE_OMP_FOR statement.  */
    5431        15363 :   gsi_remove (&gsi, true);
    5432              : 
    5433              :   /* Setup code for sequential iteration goes in SEQ_START_BB.  */
    5434        15363 :   gsi = gsi_start_bb (seq_start_bb);
    5435              : 
    5436        15363 :   tree startvar = fd->loop.v;
    5437        15363 :   tree endvar = NULL_TREE;
    5438              : 
    5439        15363 :   if (gimple_omp_for_combined_p (fd->for_stmt))
    5440              :     {
    5441         8405 :       tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
    5442        12566 :                      ? gimple_omp_parallel_clauses (inner_stmt)
    5443         4244 :                      : gimple_omp_for_clauses (inner_stmt);
    5444         8405 :       tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
    5445         8405 :       gcc_assert (innerc);
    5446         8405 :       startvar = OMP_CLAUSE_DECL (innerc);
    5447         8405 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    5448              :                                 OMP_CLAUSE__LOOPTEMP_);
    5449         8405 :       gcc_assert (innerc);
    5450         8405 :       endvar = OMP_CLAUSE_DECL (innerc);
    5451         3585 :       if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
    5452        10117 :           && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
    5453              :         {
    5454          997 :           innerc = find_lastprivate_looptemp (fd, innerc);
    5455          997 :           if (innerc)
    5456              :             {
    5457              :               /* If needed (distribute parallel for with lastprivate),
    5458              :                  propagate down the total number of iterations.  */
    5459          499 :               tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
    5460              :                                      fd->loop.n2);
    5461          499 :               t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
    5462              :                                             GSI_CONTINUE_LINKING);
    5463          499 :               assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
    5464          499 :               gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    5465              :             }
    5466              :         }
    5467              :     }
    5468        15363 :   t = fold_convert (itype, s0);
    5469        15363 :   t = fold_build2 (MULT_EXPR, itype, t, step);
    5470        15363 :   if (POINTER_TYPE_P (type))
    5471              :     {
    5472          606 :       t = fold_build_pointer_plus (n1, t);
    5473          670 :       if (!POINTER_TYPE_P (TREE_TYPE (startvar))
    5474          670 :           && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
    5475            0 :         t = fold_convert (signed_type_for (type), t);
    5476              :     }
    5477              :   else
    5478        14757 :     t = fold_build2 (PLUS_EXPR, type, t, n1);
    5479        15363 :   t = fold_convert (TREE_TYPE (startvar), t);
    5480        15363 :   t = force_gimple_operand_gsi (&gsi, t,
    5481        15363 :                                 DECL_P (startvar)
    5482        15195 :                                 && TREE_ADDRESSABLE (startvar),
    5483              :                                 NULL_TREE, false, GSI_CONTINUE_LINKING);
    5484        15363 :   assign_stmt = gimple_build_assign (startvar, t);
    5485        15363 :   gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    5486        15363 :   if (cond_var)
    5487              :     {
    5488          112 :       tree itype = TREE_TYPE (cond_var);
    5489              :       /* For lastprivate(conditional:) itervar, we need some iteration
    5490              :          counter that starts at unsigned non-zero and increases.
    5491              :          Prefer as few IVs as possible, so if we can use startvar
    5492              :          itself, use that, or startvar + constant (those would be
    5493              :          incremented with step), and as last resort use the s0 + 1
    5494              :          incremented by 1.  */
    5495          112 :       if (POINTER_TYPE_P (type)
    5496          112 :           || TREE_CODE (n1) != INTEGER_CST
    5497          106 :           || fd->loop.cond_code != LT_EXPR)
    5498            6 :         t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
    5499              :                          build_int_cst (itype, 1));
    5500          106 :       else if (tree_int_cst_sgn (n1) == 1)
    5501           12 :         t = fold_convert (itype, t);
    5502              :       else
    5503              :         {
    5504           94 :           tree c = fold_convert (itype, n1);
    5505           94 :           c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
    5506           94 :           t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
    5507              :         }
    5508          112 :       t = force_gimple_operand_gsi (&gsi, t, false,
    5509              :                                     NULL_TREE, false, GSI_CONTINUE_LINKING);
    5510          112 :       assign_stmt = gimple_build_assign (cond_var, t);
    5511          112 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    5512              :     }
    5513              : 
    5514        15363 :   t = fold_convert (itype, e0);
    5515        15363 :   t = fold_build2 (MULT_EXPR, itype, t, step);
    5516        15363 :   if (POINTER_TYPE_P (type))
    5517              :     {
    5518          606 :       t = fold_build_pointer_plus (n1, t);
    5519          670 :       if (!POINTER_TYPE_P (TREE_TYPE (startvar))
    5520          670 :           && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
    5521            0 :         t = fold_convert (signed_type_for (type), t);
    5522              :     }
    5523              :   else
    5524        14757 :     t = fold_build2 (PLUS_EXPR, type, t, n1);
    5525        15363 :   t = fold_convert (TREE_TYPE (startvar), t);
    5526        15363 :   e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    5527              :                                 false, GSI_CONTINUE_LINKING);
    5528        15363 :   if (endvar)
    5529              :     {
    5530         8405 :       assign_stmt = gimple_build_assign (endvar, e);
    5531         8405 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    5532         8405 :       if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
    5533         7636 :         assign_stmt = gimple_build_assign (fd->loop.v, e);
    5534              :       else
    5535          769 :         assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
    5536         8405 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    5537              :     }
    5538              :   /* Handle linear clause adjustments.  */
    5539        15363 :   tree itercnt = NULL_TREE;
    5540        15363 :   tree *nonrect_bounds = NULL;
    5541        15363 :   if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
    5542        50489 :     for (tree c = gimple_omp_for_clauses (fd->for_stmt);
    5543        50489 :          c; c = OMP_CLAUSE_CHAIN (c))
    5544        40305 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    5545        40305 :           && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
    5546              :         {
    5547           87 :           tree d = OMP_CLAUSE_DECL (c);
    5548           87 :           tree t = d, a, dest;
    5549           87 :           if (omp_privatize_by_reference (t))
    5550           13 :             t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
    5551           87 :           if (itercnt == NULL_TREE)
    5552              :             {
    5553           87 :               if (gimple_omp_for_combined_into_p (fd->for_stmt))
    5554              :                 {
    5555            0 :                   itercnt = fold_build2 (MINUS_EXPR, itype,
    5556              :                                          fold_convert (itype, n1),
    5557              :                                          fold_convert (itype, fd->loop.n1));
    5558            0 :                   itercnt = fold_build2 (EXACT_DIV_EXPR, itype, itercnt, step);
    5559            0 :                   itercnt = fold_build2 (PLUS_EXPR, itype, itercnt, s0);
    5560            0 :                   itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
    5561              :                                                       NULL_TREE, false,
    5562              :                                                       GSI_CONTINUE_LINKING);
    5563              :                 }
    5564              :               else
    5565              :                 itercnt = s0;
    5566              :             }
    5567           87 :           tree type = TREE_TYPE (t);
    5568           87 :           if (POINTER_TYPE_P (type))
    5569            0 :             type = sizetype;
    5570           87 :           a = fold_build2 (MULT_EXPR, type,
    5571              :                            fold_convert (type, itercnt),
    5572              :                            fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
    5573           87 :           dest = unshare_expr (t);
    5574           87 :           t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
    5575              :                            : POINTER_PLUS_EXPR, TREE_TYPE (t), t, a);
    5576           87 :           t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    5577              :                                         false, GSI_CONTINUE_LINKING);
    5578           87 :           expand_omp_build_assign (&gsi, dest, t, true);
    5579              :         }
    5580        15363 :   if (fd->collapse > 1)
    5581              :     {
    5582         5195 :       if (fd->non_rect)
    5583              :         {
    5584          376 :           nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
    5585          376 :           memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
    5586              :         }
    5587         5195 :       expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
    5588              :                                 startvar);
    5589              :     }
    5590              : 
    5591        15363 :   if (!broken_loop)
    5592              :     {
    5593              :       /* The code controlling the sequential loop replaces the
    5594              :          GIMPLE_OMP_CONTINUE.  */
    5595        14867 :       gsi = gsi_last_nondebug_bb (cont_bb);
    5596        14867 :       gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
    5597        14867 :       gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
    5598        14867 :       vmain = gimple_omp_continue_control_use (cont_stmt);
    5599        14867 :       vback = gimple_omp_continue_control_def (cont_stmt);
    5600              : 
    5601        14867 :       if (cond_var)
    5602              :         {
    5603          112 :           tree itype = TREE_TYPE (cond_var);
    5604          112 :           tree t2;
    5605          112 :           if (POINTER_TYPE_P (type)
    5606          112 :               || TREE_CODE (n1) != INTEGER_CST
    5607          106 :               || fd->loop.cond_code != LT_EXPR)
    5608            6 :             t2 = build_int_cst (itype, 1);
    5609              :           else
    5610          106 :             t2 = fold_convert (itype, step);
    5611          112 :           t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
    5612          112 :           t2 = force_gimple_operand_gsi (&gsi, t2, false,
    5613              :                                          NULL_TREE, true, GSI_SAME_STMT);
    5614          112 :           assign_stmt = gimple_build_assign (cond_var, t2);
    5615          112 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    5616              :         }
    5617              : 
    5618        14867 :       if (!gimple_omp_for_combined_p (fd->for_stmt))
    5619              :         {
    5620         6462 :           if (POINTER_TYPE_P (type))
    5621          316 :             t = fold_build_pointer_plus (vmain, step);
    5622              :           else
    5623         6146 :             t = fold_build2 (PLUS_EXPR, type, vmain, step);
    5624         6462 :           t = force_gimple_operand_gsi (&gsi, t,
    5625         6462 :                                         DECL_P (vback)
    5626         6294 :                                         && TREE_ADDRESSABLE (vback),
    5627              :                                         NULL_TREE, true, GSI_SAME_STMT);
    5628         6462 :           assign_stmt = gimple_build_assign (vback, t);
    5629         6462 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    5630              : 
    5631         6462 :           t = build2 (fd->loop.cond_code, boolean_type_node,
    5632         6462 :                       DECL_P (vback) && TREE_ADDRESSABLE (vback)
    5633              :                       ? t : vback, e);
    5634         6462 :           gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
    5635              :         }
    5636              : 
    5637              :       /* Remove the GIMPLE_OMP_CONTINUE statement.  */
    5638        14867 :       gsi_remove (&gsi, true);
    5639              : 
    5640        14867 :       if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
    5641         1382 :         collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
    5642              :                                                    cont_bb, body_bb);
    5643              :     }
    5644              : 
    5645        15363 :   if (flag_openmp_ompt == OMP_OMPT_LEVEL_EXTENDED)
    5646              :     {
    5647              :       /* Insert call to GOMP_*_static_worksharing_dispatch at the end of
    5648              :          seq_start_bb.  */
    5649            8 :       gsi = gsi_last_nondebug_bb (seq_start_bb);
    5650            8 :       tree decl;
    5651            8 :       switch (gimple_omp_for_kind (fd->for_stmt))
    5652              :         {
    5653            4 :         case GF_OMP_FOR_KIND_FOR:
    5654            4 :           decl = builtin_decl_explicit (
    5655              :             BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_DISPATCH);
    5656            4 :           break;
    5657            4 :         case GF_OMP_FOR_KIND_DISTRIBUTE:
    5658            4 :           decl = builtin_decl_explicit (
    5659              :             BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_DISPATCH);
    5660            4 :           break;
    5661            0 :         default:
    5662            0 :           gcc_unreachable ();
    5663              :         }
    5664            8 :       tree s0_ull = fold_convert (long_long_unsigned_type_node, s0);
    5665            8 :       tree q_ull = fold_convert (long_long_unsigned_type_node, q);
    5666            8 :       tree call = build_call_expr (decl, 2, s0_ull, q_ull);
    5667            8 :       force_gimple_operand_gsi (&gsi, call, true, NULL_TREE, true,
    5668              :                                 GSI_SAME_STMT);
    5669              :     }
    5670              : 
    5671        15363 :   gsi = gsi_last_nondebug_bb (exit_bb);
    5672        15363 :   if (flag_openmp_ompt && !has_gomp_loop_end)
    5673              :     {
    5674              :       /* Insert call to GOMP_*_static_worksharing_end at the end of exit_bb.
    5675              :        */
    5676           20 :       tree decl;
    5677           20 :       switch (gimple_omp_for_kind (fd->for_stmt))
    5678              :         {
    5679           12 :         case GF_OMP_FOR_KIND_FOR:
    5680           12 :           decl
    5681           12 :             = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_END);
    5682           12 :           break;
    5683            8 :         case GF_OMP_FOR_KIND_DISTRIBUTE:
    5684            8 :           decl = builtin_decl_explicit (
    5685              :             BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_END);
    5686            8 :           break;
    5687            0 :         default:
    5688            0 :           gcc_unreachable ();
    5689              :         }
    5690           20 :       gcall *g = gimple_build_call (decl, 0);
    5691           20 :       gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    5692              :     }
    5693              : 
    5694              :   /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing.  */
    5695        15363 :   if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
    5696              :     {
    5697         2408 :       t = gimple_omp_return_lhs (gsi_stmt (gsi));
    5698         2408 :       if (fd->have_reductemp
    5699         2355 :           || ((fd->have_pointer_condtemp || fd->have_scantemp)
    5700           63 :               && !fd->have_nonctrl_scantemp))
    5701              :         {
    5702          116 :           tree fn;
    5703          116 :           if (t)
    5704            4 :             fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
    5705              :           else
    5706          112 :             fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
    5707          116 :           gcall *g = gimple_build_call (fn, 0);
    5708          116 :           if (t)
    5709              :             {
    5710            4 :               gimple_call_set_lhs (g, t);
    5711            4 :               if (fd->have_reductemp)
    5712            4 :                 gsi_insert_after (&gsi, gimple_build_assign (reductions,
    5713              :                                                              NOP_EXPR, t),
    5714              :                                   GSI_SAME_STMT);
    5715              :             }
    5716          116 :           gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    5717          116 :         }
    5718              :       else
    5719         2292 :         gsi_insert_after (&gsi,
    5720              :                           omp_build_barrier (t,
    5721              :                                              GOMP_BARRIER_IMPLICIT_WORKSHARE),
    5722              :                           GSI_SAME_STMT);
    5723              :     }
    5724        12955 :   else if ((fd->have_pointer_condtemp || fd->have_scantemp)
    5725          329 :            && !fd->have_nonctrl_scantemp)
    5726              :     {
    5727          142 :       tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
    5728          142 :       gcall *g = gimple_build_call (fn, 0);
    5729          142 :       gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    5730              :     }
    5731        15363 :   if (fd->have_scantemp && !fd->have_nonctrl_scantemp)
    5732              :     {
    5733          187 :       tree clauses = gimple_omp_for_clauses (fd->for_stmt);
    5734          187 :       tree controlp = NULL_TREE, controlb = NULL_TREE;
    5735          374 :       for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    5736          374 :         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE__SCANTEMP_
    5737          374 :             && OMP_CLAUSE__SCANTEMP__CONTROL (c))
    5738              :           {
    5739          374 :             if (TREE_TYPE (OMP_CLAUSE_DECL (c)) == boolean_type_node)
    5740          187 :               controlb = OMP_CLAUSE_DECL (c);
    5741              :             else
    5742          187 :               controlp = OMP_CLAUSE_DECL (c);
    5743          374 :             if (controlb && controlp)
    5744              :               break;
    5745              :           }
    5746          187 :       gcc_assert (controlp && controlb);
    5747          187 :       gimple *g = gimple_build_cond (NE_EXPR, controlb, boolean_false_node,
    5748              :                                      NULL_TREE, NULL_TREE);
    5749          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5750          187 :       exit1_bb = split_block (exit_bb, g)->dest;
    5751          187 :       gsi = gsi_after_labels (exit1_bb);
    5752          187 :       g = gimple_build_call (builtin_decl_explicit (BUILT_IN_FREE), 1,
    5753              :                              controlp);
    5754          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5755          187 :       exit2_bb = split_block (exit1_bb, g)->dest;
    5756          187 :       gsi = gsi_after_labels (exit2_bb);
    5757          374 :       g = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE), 1,
    5758              :                              controlp);
    5759          187 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5760          187 :       exit3_bb = split_block (exit2_bb, g)->dest;
    5761          187 :       gsi = gsi_after_labels (exit3_bb);
    5762              :     }
    5763              : 
    5764        15363 :   gsi_remove (&gsi, true);
    5765              : 
    5766              :   /* Connect all the blocks.  */
    5767        15363 :   ep = make_edge (entry_bb, third_bb, EDGE_FALSE_VALUE);
    5768        15363 :   ep->probability = profile_probability::guessed_always ().apply_scale (3, 4);
    5769        15363 :   ep = find_edge (entry_bb, second_bb);
    5770        15363 :   ep->flags = EDGE_TRUE_VALUE;
    5771        15363 :   ep->probability = profile_probability::guessed_always () / 4;
    5772        15363 :   if (fourth_bb)
    5773              :     {
    5774          187 :       ep = make_edge (third_bb, fifth_bb, EDGE_FALSE_VALUE);
    5775          187 :       ep->probability = profile_probability::guessed_always () / 2;
    5776          187 :       ep = find_edge (third_bb, fourth_bb);
    5777          187 :       ep->flags = EDGE_TRUE_VALUE;
    5778          187 :       ep->probability = profile_probability::guessed_always () / 2;
    5779          187 :       ep = find_edge (fourth_bb, fifth_bb);
    5780          187 :       redirect_edge_and_branch (ep, sixth_bb);
    5781              :     }
    5782              :   else
    5783              :     sixth_bb = third_bb;
    5784        15363 :   find_edge (sixth_bb, seq_start_bb)->flags = EDGE_FALSE_VALUE;
    5785        15363 :   find_edge (sixth_bb, fin_bb)->flags = EDGE_TRUE_VALUE;
    5786        15363 :   if (exit1_bb)
    5787              :     {
    5788          187 :       ep = make_edge (exit_bb, exit2_bb, EDGE_FALSE_VALUE);
    5789          187 :       ep->probability = profile_probability::guessed_always () / 2;
    5790          187 :       ep = find_edge (exit_bb, exit1_bb);
    5791          187 :       ep->flags = EDGE_TRUE_VALUE;
    5792          187 :       ep->probability = profile_probability::guessed_always () / 2;
    5793          187 :       ep = find_edge (exit1_bb, exit2_bb);
    5794          187 :       redirect_edge_and_branch (ep, exit3_bb);
    5795              :     }
    5796              : 
    5797        15363 :   if (!broken_loop)
    5798              :     {
    5799        14867 :       ep = find_edge (cont_bb, body_bb);
    5800        14867 :       if (ep == NULL)
    5801              :         {
    5802          168 :           ep = BRANCH_EDGE (cont_bb);
    5803          168 :           gcc_assert (single_succ (ep->dest) == body_bb);
    5804              :         }
    5805        14867 :       if (gimple_omp_for_combined_p (fd->for_stmt))
    5806              :         {
    5807         8405 :           remove_edge (ep);
    5808         8405 :           ep = NULL;
    5809              :         }
    5810         6462 :       else if (fd->collapse > 1)
    5811              :         {
    5812         1382 :           remove_edge (ep);
    5813         1382 :           ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
    5814              :         }
    5815              :       else
    5816         5080 :         ep->flags = EDGE_TRUE_VALUE;
    5817        23272 :       find_edge (cont_bb, fin_bb)->flags
    5818        16249 :         = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
    5819              :     }
    5820              : 
    5821        15363 :   set_immediate_dominator (CDI_DOMINATORS, second_bb, entry_bb);
    5822        15363 :   set_immediate_dominator (CDI_DOMINATORS, third_bb, entry_bb);
    5823        15363 :   if (fourth_bb)
    5824              :     {
    5825          187 :       set_immediate_dominator (CDI_DOMINATORS, fifth_bb, third_bb);
    5826          187 :       set_immediate_dominator (CDI_DOMINATORS, sixth_bb, third_bb);
    5827              :     }
    5828        15363 :   set_immediate_dominator (CDI_DOMINATORS, seq_start_bb, sixth_bb);
    5829              : 
    5830        15363 :   set_immediate_dominator (CDI_DOMINATORS, body_bb,
    5831              :                            recompute_dominator (CDI_DOMINATORS, body_bb));
    5832        15363 :   set_immediate_dominator (CDI_DOMINATORS, fin_bb,
    5833              :                            recompute_dominator (CDI_DOMINATORS, fin_bb));
    5834        15363 :   if (exit1_bb)
    5835              :     {
    5836          187 :       set_immediate_dominator (CDI_DOMINATORS, exit2_bb, exit_bb);
    5837          187 :       set_immediate_dominator (CDI_DOMINATORS, exit3_bb, exit_bb);
    5838              :     }
    5839              : 
    5840        15363 :   class loop *loop = body_bb->loop_father;
    5841        15363 :   if (loop != entry_bb->loop_father)
    5842              :     {
    5843          330 :       gcc_assert (broken_loop || loop->header == body_bb);
    5844          168 :       gcc_assert (broken_loop
    5845              :                   || loop->latch == region->cont
    5846              :                   || single_pred (loop->latch) == region->cont);
    5847          330 :       return;
    5848              :     }
    5849              : 
    5850        15033 :   if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
    5851              :     {
    5852         6294 :       loop = alloc_loop ();
    5853         6294 :       loop->header = body_bb;
    5854         6294 :       if (collapse_bb == NULL)
    5855         4912 :         loop->latch = cont_bb;
    5856         6294 :       add_loop (loop, body_bb->loop_father);
    5857              :     }
    5858              : }
    5859              : 
    5860              : /* Return phi in E->DEST with ARG on edge E.  */
    5861              : 
    5862              : static gphi *
    5863           14 : find_phi_with_arg_on_edge (tree arg, edge e)
    5864              : {
    5865           14 :   basic_block bb = e->dest;
    5866              : 
    5867           14 :   for (gphi_iterator gpi = gsi_start_phis (bb);
    5868           14 :        !gsi_end_p (gpi);
    5869            0 :        gsi_next (&gpi))
    5870              :     {
    5871           14 :       gphi *phi = gpi.phi ();
    5872           14 :       if (PHI_ARG_DEF_FROM_EDGE (phi, e) == arg)
    5873           14 :         return phi;
    5874              :     }
    5875              : 
    5876            0 :   return NULL;
    5877              : }
    5878              : 
    5879              : /* A subroutine of expand_omp_for.  Generate code for a parallel
    5880              :    loop with static schedule and a specified chunk size.  Given
    5881              :    parameters:
    5882              : 
    5883              :         for (V = N1; V cond N2; V += STEP) BODY;
    5884              : 
    5885              :    where COND is "<" or ">", we generate pseudocode
    5886              : 
    5887              :         if ((__typeof (V)) -1 > 0 && N2 cond N1) goto L2;
    5888              :         if (cond is <)
    5889              :           adj = STEP - 1;
    5890              :         else
    5891              :           adj = STEP + 1;
    5892              :         if ((__typeof (V)) -1 > 0 && cond is >)
    5893              :           n = -(adj + N2 - N1) / -STEP;
    5894              :         else
    5895              :           n = (adj + N2 - N1) / STEP;
    5896              :         trip = 0;
    5897              :         V = threadid * CHUNK * STEP + N1;  -- this extra definition of V is
    5898              :                                               here so that V is defined
    5899              :                                               if the loop is not entered
    5900              :     L0:
    5901              :         s0 = (trip * nthreads + threadid) * CHUNK;
    5902              :         e0 = min (s0 + CHUNK, n);
    5903              :         if (s0 < n) goto L1; else goto L4;
    5904              :     L1:
    5905              :         V = s0 * STEP + N1;
    5906              :         e = e0 * STEP + N1;
    5907              :     L2:
    5908              :         BODY;
    5909              :         V += STEP;
    5910              :         if (V cond e) goto L2; else goto L3;
    5911              :     L3:
    5912              :         trip += 1;
    5913              :         goto L0;
    5914              :     L4:
    5915              : */
    5916              : 
    5917              : static void
    5918         5881 : expand_omp_for_static_chunk (struct omp_region *region,
    5919              :                              struct omp_for_data *fd, gimple *inner_stmt)
    5920              : {
    5921         5881 :   tree n, s0, e0, e, t;
    5922         5881 :   tree trip_var, trip_init, trip_main, trip_back, nthreads, threadid;
    5923         5881 :   tree type, itype, vmain, vback, vextra;
    5924         5881 :   basic_block entry_bb, exit_bb, body_bb, seq_start_bb, iter_part_bb;
    5925         5881 :   basic_block trip_update_bb = NULL, cont_bb, collapse_bb = NULL, fin_bb;
    5926         5881 :   gimple_stmt_iterator gsi, gsip;
    5927         5881 :   edge se;
    5928         5881 :   bool broken_loop = region->cont == NULL;
    5929         5881 :   tree *counts = NULL;
    5930         5881 :   tree n1, n2, step;
    5931         5881 :   tree reductions = NULL_TREE;
    5932         5881 :   tree cond_var = NULL_TREE, condtemp = NULL_TREE;
    5933              : 
    5934         5881 :   itype = type = TREE_TYPE (fd->loop.v);
    5935         5881 :   if (POINTER_TYPE_P (type))
    5936          316 :     itype = signed_type_for (type);
    5937              : 
    5938         5881 :   entry_bb = region->entry;
    5939         5881 :   se = split_block (entry_bb, last_nondebug_stmt (entry_bb));
    5940         5881 :   entry_bb = se->src;
    5941         5881 :   iter_part_bb = se->dest;
    5942         5881 :   cont_bb = region->cont;
    5943         5881 :   gcc_assert (EDGE_COUNT (iter_part_bb->succs) == 2);
    5944         5881 :   fin_bb = BRANCH_EDGE (iter_part_bb)->dest;
    5945         5881 :   gcc_assert (broken_loop
    5946              :               || fin_bb == FALLTHRU_EDGE (cont_bb)->dest);
    5947         5881 :   seq_start_bb = split_edge (FALLTHRU_EDGE (iter_part_bb));
    5948         5881 :   body_bb = single_succ (seq_start_bb);
    5949         5881 :   if (!broken_loop)
    5950              :     {
    5951         5624 :       gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb
    5952              :                   || single_succ (BRANCH_EDGE (cont_bb)->dest) == body_bb);
    5953         5624 :       gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
    5954         5624 :       trip_update_bb = split_edge (FALLTHRU_EDGE (cont_bb));
    5955              :     }
    5956         5881 :   exit_bb = region->exit;
    5957              : 
    5958              :   /* Trip and adjustment setup goes in ENTRY_BB.  */
    5959         5881 :   gsi = gsi_last_nondebug_bb (entry_bb);
    5960         5881 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    5961         5881 :   gsip = gsi;
    5962         5881 :   gsi_prev (&gsip);
    5963              : 
    5964         5881 :   if (fd->collapse > 1)
    5965              :     {
    5966         2256 :       int first_zero_iter = -1, dummy = -1;
    5967         2256 :       basic_block l2_dom_bb = NULL, dummy_bb = NULL;
    5968              : 
    5969         2256 :       counts = XALLOCAVEC (tree, fd->collapse);
    5970         2256 :       expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
    5971              :                                   fin_bb, first_zero_iter,
    5972              :                                   dummy_bb, dummy, l2_dom_bb);
    5973         2256 :       t = NULL_TREE;
    5974              :     }
    5975         3625 :   else if (gimple_omp_for_combined_into_p (fd->for_stmt))
    5976          813 :     t = integer_one_node;
    5977              :   else
    5978         2812 :     t = fold_binary (fd->loop.cond_code, boolean_type_node,
    5979              :                      fold_convert (type, fd->loop.n1),
    5980              :                      fold_convert (type, fd->loop.n2));
    5981         5881 :   if (fd->collapse == 1
    5982         3625 :       && TYPE_UNSIGNED (type)
    5983         7068 :       && (t == NULL_TREE || !integer_onep (t)))
    5984              :     {
    5985          236 :       n1 = fold_convert (type, unshare_expr (fd->loop.n1));
    5986          236 :       n1 = force_gimple_operand_gsi (&gsi, n1, true, NULL_TREE,
    5987              :                                      true, GSI_SAME_STMT);
    5988          236 :       n2 = fold_convert (type, unshare_expr (fd->loop.n2));
    5989          236 :       n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE,
    5990              :                                      true, GSI_SAME_STMT);
    5991          236 :       gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code,
    5992              :                                                 n1, n2);
    5993          236 :       se = split_block (entry_bb, cond_stmt);
    5994          236 :       se->flags = EDGE_TRUE_VALUE;
    5995          236 :       entry_bb = se->dest;
    5996          236 :       se->probability = profile_probability::very_likely ();
    5997          236 :       se = make_edge (se->src, fin_bb, EDGE_FALSE_VALUE);
    5998          236 :       se->probability = profile_probability::very_unlikely ();
    5999          236 :       if (gimple_in_ssa_p (cfun))
    6000              :         {
    6001            1 :           int dest_idx = find_edge (iter_part_bb, fin_bb)->dest_idx;
    6002            1 :           for (gphi_iterator gpi = gsi_start_phis (fin_bb);
    6003            2 :                !gsi_end_p (gpi); gsi_next (&gpi))
    6004              :             {
    6005            1 :               gphi *phi = gpi.phi ();
    6006            1 :               add_phi_arg (phi, gimple_phi_arg_def (phi, dest_idx),
    6007              :                            se, UNKNOWN_LOCATION);
    6008              :             }
    6009              :         }
    6010          472 :       gsi = gsi_last_bb (entry_bb);
    6011              :     }
    6012              : 
    6013         5881 :   if (fd->lastprivate_conditional)
    6014              :     {
    6015           49 :       tree clauses = gimple_omp_for_clauses (fd->for_stmt);
    6016           49 :       tree c = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
    6017           49 :       if (fd->have_pointer_condtemp)
    6018           29 :         condtemp = OMP_CLAUSE_DECL (c);
    6019           49 :       c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE__CONDTEMP_);
    6020           49 :       cond_var = OMP_CLAUSE_DECL (c);
    6021              :     }
    6022         5881 :   if (fd->have_reductemp || fd->have_pointer_condtemp)
    6023              :     {
    6024           45 :       tree t1 = build_int_cst (long_integer_type_node, 0);
    6025           45 :       tree t2 = build_int_cst (long_integer_type_node, 1);
    6026           45 :       tree t3 = build_int_cstu (long_integer_type_node,
    6027              :                                 (HOST_WIDE_INT_1U << 31) + 1);
    6028           45 :       tree clauses = gimple_omp_for_clauses (fd->for_stmt);
    6029           45 :       gimple_stmt_iterator gsi2 = gsi_none ();
    6030           45 :       gimple *g = NULL;
    6031           45 :       tree mem = null_pointer_node, memv = NULL_TREE;
    6032           45 :       if (fd->have_reductemp)
    6033              :         {
    6034           26 :           tree c = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
    6035           26 :           reductions = OMP_CLAUSE_DECL (c);
    6036           26 :           gcc_assert (TREE_CODE (reductions) == SSA_NAME);
    6037           26 :           g = SSA_NAME_DEF_STMT (reductions);
    6038           26 :           reductions = gimple_assign_rhs1 (g);
    6039           26 :           OMP_CLAUSE_DECL (c) = reductions;
    6040           26 :           gsi2 = gsi_for_stmt (g);
    6041              :         }
    6042              :       else
    6043              :         {
    6044           19 :           if (gsi_end_p (gsip))
    6045            0 :             gsi2 = gsi_after_labels (region->entry);
    6046              :           else
    6047           19 :             gsi2 = gsip;
    6048              :           reductions = null_pointer_node;
    6049              :         }
    6050           45 :       if (fd->have_pointer_condtemp)
    6051              :         {
    6052           29 :           tree type = TREE_TYPE (condtemp);
    6053           29 :           memv = create_tmp_var (type);
    6054           29 :           TREE_ADDRESSABLE (memv) = 1;
    6055           29 :           unsigned HOST_WIDE_INT sz
    6056           29 :             = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
    6057           29 :           sz *= fd->lastprivate_conditional;
    6058           29 :           expand_omp_build_assign (&gsi2, memv, build_int_cst (type, sz),
    6059              :                                    false);
    6060           29 :           mem = build_fold_addr_expr (memv);
    6061              :         }
    6062           45 :       tree t
    6063           45 :         = build_call_expr (builtin_decl_explicit (BUILT_IN_GOMP_LOOP_START),
    6064              :                            9, t1, t2, t2, t3, t1, null_pointer_node,
    6065              :                            null_pointer_node, reductions, mem);
    6066           45 :       force_gimple_operand_gsi (&gsi2, t, true, NULL_TREE,
    6067              :                                 true, GSI_SAME_STMT);
    6068           45 :       if (fd->have_pointer_condtemp)
    6069           29 :         expand_omp_build_assign (&gsi2, condtemp, memv, false);
    6070           45 :       if (fd->have_reductemp)
    6071              :         {
    6072           26 :           gsi_remove (&gsi2, true);
    6073           26 :           release_ssa_name (gimple_assign_lhs (g));
    6074              :         }
    6075              :     }
    6076              : 
    6077         5881 :   n1 = fd->loop.n1;
    6078         5881 :   n2 = fd->loop.n2;
    6079         5881 :   step = fd->loop.step;
    6080         5881 :   if (gimple_omp_for_combined_into_p (fd->for_stmt))
    6081              :     {
    6082         1309 :       tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6083              :                                      OMP_CLAUSE__LOOPTEMP_);
    6084         1309 :       gcc_assert (innerc);
    6085         1309 :       n1 = OMP_CLAUSE_DECL (innerc);
    6086         1309 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    6087              :                                 OMP_CLAUSE__LOOPTEMP_);
    6088         1309 :       gcc_assert (innerc);
    6089         1309 :       n2 = OMP_CLAUSE_DECL (innerc);
    6090              :     }
    6091         5881 :   n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
    6092              :                                  true, NULL_TREE, true, GSI_SAME_STMT);
    6093         5881 :   n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
    6094              :                                  true, NULL_TREE, true, GSI_SAME_STMT);
    6095         5881 :   step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
    6096              :                                    true, NULL_TREE, true, GSI_SAME_STMT);
    6097         5881 :   tree chunk_size = fold_convert (itype, fd->chunk_size);
    6098         5881 :   chunk_size = omp_adjust_chunk_size (chunk_size, fd->simd_schedule,
    6099              :                                       is_in_offload_region (region));
    6100         5881 :   chunk_size
    6101         5881 :     = force_gimple_operand_gsi (&gsi, chunk_size, true, NULL_TREE, true,
    6102              :                                 GSI_SAME_STMT);
    6103              : 
    6104         6487 :   t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
    6105         5881 :   t = fold_build2 (PLUS_EXPR, itype, step, t);
    6106         5881 :   t = fold_build2 (PLUS_EXPR, itype, t, n2);
    6107         5881 :   t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, n1));
    6108         5881 :   if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
    6109          284 :     t = fold_build2 (TRUNC_DIV_EXPR, itype,
    6110              :                      fold_build1 (NEGATE_EXPR, itype, t),
    6111              :                      fold_build1 (NEGATE_EXPR, itype, step));
    6112              :   else
    6113         5597 :     t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    6114         5881 :   t = fold_convert (itype, t);
    6115         5881 :   n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    6116              :                                 true, GSI_SAME_STMT);
    6117              : 
    6118              :   /* When GOMP_loop_end (or one of its variants) is emitted (e.g. with the
    6119              :      inscan modifier), which already implies the end of the scope, _start
    6120              :      variants of GOMP builtin calls have to be used and _end can be skipped.  */
    6121         5881 :   bool has_gomp_loop_end = fd->have_reductemp || fd->have_pointer_condtemp;
    6122         5881 :   {
    6123              :     /* Fetch the thread/team id and the number of threads/teams in a single
    6124              :        call to GOMP_loop_static_worksharing or
    6125              :        GOMP_distribute_static_worksharing. The helper returns both values packed
    6126              :        into one complex int, with the id as the imaginary part and the count as
    6127              :        the real part.  Returning (rather than writing through pointers) keeps
    6128              :        both values as plain SSA names, which lets later passes - notably IPA-CP
    6129              :        propagating constants into the outlined kernel - reason about them.
    6130              :        Also pass the total number of iterations for OMPT.  */
    6131         5881 :     tree decl;
    6132         5881 :     switch (gimple_omp_for_kind (fd->for_stmt))
    6133              :       {
    6134         2395 :       case GF_OMP_FOR_KIND_FOR:
    6135         4790 :         decl = builtin_decl_explicit (
    6136         2395 :           flag_openmp_ompt || has_gomp_loop_end
    6137              :             ? BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_START
    6138              :             : BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING);
    6139         2395 :         break;
    6140         3486 :       case GF_OMP_FOR_KIND_DISTRIBUTE:
    6141         6972 :         decl = builtin_decl_explicit (
    6142         3486 :           flag_openmp_ompt || has_gomp_loop_end
    6143              :             ? BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_START
    6144              :             : BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING);
    6145         3486 :         break;
    6146            0 :       default:
    6147            0 :         gcc_unreachable ();
    6148              :       }
    6149         5881 :     tree n_ull = fold_convert (long_long_unsigned_type_node, n);
    6150         5881 :     tree packed = build_call_expr (decl, 1, n_ull);
    6151         5881 :     packed = force_gimple_operand_gsi (&gsi, packed, true, NULL_TREE, true,
    6152              :                                        GSI_SAME_STMT);
    6153         5881 :     threadid = fold_build1 (IMAGPART_EXPR, integer_type_node, packed);
    6154         5881 :     threadid = fold_convert (itype, threadid);
    6155         5881 :     threadid = force_gimple_operand_gsi (&gsi, threadid, true, NULL_TREE, true,
    6156              :                                          GSI_SAME_STMT);
    6157         5881 :     nthreads = fold_build1 (REALPART_EXPR, integer_type_node, packed);
    6158         5881 :     nthreads = fold_convert (itype, nthreads);
    6159         5881 :     nthreads = force_gimple_operand_gsi (&gsi, nthreads, true, NULL_TREE, true,
    6160              :                                          GSI_SAME_STMT);
    6161              :   }
    6162              : 
    6163         5881 :   trip_var = create_tmp_reg (itype, ".trip");
    6164         5881 :   if (gimple_in_ssa_p (cfun))
    6165              :     {
    6166           12 :       trip_init = make_ssa_name (trip_var);
    6167           12 :       trip_main = make_ssa_name (trip_var);
    6168           12 :       trip_back = make_ssa_name (trip_var);
    6169              :     }
    6170              :   else
    6171              :     {
    6172              :       trip_init = trip_var;
    6173              :       trip_main = trip_var;
    6174              :       trip_back = trip_var;
    6175              :     }
    6176              : 
    6177         5881 :   gassign *assign_stmt
    6178         5881 :     = gimple_build_assign (trip_init, build_int_cst (itype, 0));
    6179         5881 :   gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    6180              : 
    6181         5881 :   t = fold_build2 (MULT_EXPR, itype, threadid, chunk_size);
    6182         5881 :   t = fold_build2 (MULT_EXPR, itype, t, step);
    6183         5881 :   if (POINTER_TYPE_P (type))
    6184          316 :     t = fold_build_pointer_plus (n1, t);
    6185              :   else
    6186         5565 :     t = fold_build2 (PLUS_EXPR, type, t, n1);
    6187         5881 :   vextra = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    6188              :                                      true, GSI_SAME_STMT);
    6189              : 
    6190              :   /* Remove the GIMPLE_OMP_FOR.  */
    6191         5881 :   gsi_remove (&gsi, true);
    6192              : 
    6193         5881 :   gimple_stmt_iterator gsif = gsi;
    6194              : 
    6195              :   /* Iteration space partitioning goes in ITER_PART_BB.  */
    6196         5881 :   gsi = gsi_last_bb (iter_part_bb);
    6197              : 
    6198         5881 :   t = fold_build2 (MULT_EXPR, itype, trip_main, nthreads);
    6199         5881 :   t = fold_build2 (PLUS_EXPR, itype, t, threadid);
    6200         5881 :   t = fold_build2 (MULT_EXPR, itype, t, chunk_size);
    6201         5881 :   s0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    6202              :                                  false, GSI_CONTINUE_LINKING);
    6203              : 
    6204         5881 :   t = fold_build2 (PLUS_EXPR, itype, s0, chunk_size);
    6205         5881 :   t = fold_build2 (MIN_EXPR, itype, t, n);
    6206         5881 :   e0 = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    6207              :                                  false, GSI_CONTINUE_LINKING);
    6208              : 
    6209         5881 :   t = build2 (LT_EXPR, boolean_type_node, s0, n);
    6210         5881 :   gsi_insert_after (&gsi, gimple_build_cond_empty (t), GSI_CONTINUE_LINKING);
    6211              : 
    6212              :   /* Setup code for sequential iteration goes in SEQ_START_BB.  */
    6213         5881 :   gsi = gsi_start_bb (seq_start_bb);
    6214              : 
    6215         5881 :   tree startvar = fd->loop.v;
    6216         5881 :   tree endvar = NULL_TREE;
    6217              : 
    6218         5881 :   if (gimple_omp_for_combined_p (fd->for_stmt))
    6219              :     {
    6220         4226 :       tree clauses = gimple_code (inner_stmt) == GIMPLE_OMP_PARALLEL
    6221         6983 :                      ? gimple_omp_parallel_clauses (inner_stmt)
    6222         1469 :                      : gimple_omp_for_clauses (inner_stmt);
    6223         4226 :       tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
    6224         4226 :       gcc_assert (innerc);
    6225         4226 :       startvar = OMP_CLAUSE_DECL (innerc);
    6226         4226 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    6227              :                                 OMP_CLAUSE__LOOPTEMP_);
    6228         4226 :       gcc_assert (innerc);
    6229         4226 :       endvar = OMP_CLAUSE_DECL (innerc);
    6230         1687 :       if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST
    6231         5111 :           && gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_DISTRIBUTE)
    6232              :         {
    6233          704 :           innerc = find_lastprivate_looptemp (fd, innerc);
    6234          704 :           if (innerc)
    6235              :             {
    6236              :               /* If needed (distribute parallel for with lastprivate),
    6237              :                  propagate down the total number of iterations.  */
    6238          320 :               tree t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (innerc)),
    6239              :                                      fd->loop.n2);
    6240          320 :               t = force_gimple_operand_gsi (&gsi, t, false, NULL_TREE, false,
    6241              :                                             GSI_CONTINUE_LINKING);
    6242          320 :               assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
    6243          320 :               gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    6244              :             }
    6245              :         }
    6246              :     }
    6247              : 
    6248         5881 :   t = fold_convert (itype, s0);
    6249         5881 :   t = fold_build2 (MULT_EXPR, itype, t, step);
    6250         5881 :   if (POINTER_TYPE_P (type))
    6251              :     {
    6252          316 :       t = fold_build_pointer_plus (n1, t);
    6253          380 :       if (!POINTER_TYPE_P (TREE_TYPE (startvar))
    6254          380 :           && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
    6255            0 :         t = fold_convert (signed_type_for (type), t);
    6256              :     }
    6257              :   else
    6258         5565 :     t = fold_build2 (PLUS_EXPR, type, t, n1);
    6259         5881 :   t = fold_convert (TREE_TYPE (startvar), t);
    6260         5881 :   t = force_gimple_operand_gsi (&gsi, t,
    6261         5881 :                                 DECL_P (startvar)
    6262         5869 :                                 && TREE_ADDRESSABLE (startvar),
    6263              :                                 NULL_TREE, false, GSI_CONTINUE_LINKING);
    6264         5881 :   assign_stmt = gimple_build_assign (startvar, t);
    6265         5881 :   gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    6266         5881 :   if (cond_var)
    6267              :     {
    6268           49 :       tree itype = TREE_TYPE (cond_var);
    6269              :       /* For lastprivate(conditional:) itervar, we need some iteration
    6270              :          counter that starts at unsigned non-zero and increases.
    6271              :          Prefer as few IVs as possible, so if we can use startvar
    6272              :          itself, use that, or startvar + constant (those would be
    6273              :          incremented with step), and as last resort use the s0 + 1
    6274              :          incremented by 1.  */
    6275           49 :       if (POINTER_TYPE_P (type)
    6276           49 :           || TREE_CODE (n1) != INTEGER_CST
    6277           43 :           || fd->loop.cond_code != LT_EXPR)
    6278            6 :         t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, s0),
    6279              :                          build_int_cst (itype, 1));
    6280           43 :       else if (tree_int_cst_sgn (n1) == 1)
    6281           21 :         t = fold_convert (itype, t);
    6282              :       else
    6283              :         {
    6284           22 :           tree c = fold_convert (itype, n1);
    6285           22 :           c = fold_build2 (MINUS_EXPR, itype, build_int_cst (itype, 1), c);
    6286           22 :           t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, t), c);
    6287              :         }
    6288           49 :       t = force_gimple_operand_gsi (&gsi, t, false,
    6289              :                                     NULL_TREE, false, GSI_CONTINUE_LINKING);
    6290           49 :       assign_stmt = gimple_build_assign (cond_var, t);
    6291           49 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    6292              :     }
    6293              : 
    6294         5881 :   t = fold_convert (itype, e0);
    6295         5881 :   t = fold_build2 (MULT_EXPR, itype, t, step);
    6296         5881 :   if (POINTER_TYPE_P (type))
    6297              :     {
    6298          316 :       t = fold_build_pointer_plus (n1, t);
    6299          380 :       if (!POINTER_TYPE_P (TREE_TYPE (startvar))
    6300          380 :           && TYPE_PRECISION (TREE_TYPE (startvar)) > TYPE_PRECISION (type))
    6301            0 :         t = fold_convert (signed_type_for (type), t);
    6302              :     }
    6303              :   else
    6304         5565 :     t = fold_build2 (PLUS_EXPR, type, t, n1);
    6305         5881 :   t = fold_convert (TREE_TYPE (startvar), t);
    6306         5881 :   e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    6307              :                                 false, GSI_CONTINUE_LINKING);
    6308         5881 :   if (endvar)
    6309              :     {
    6310         4226 :       assign_stmt = gimple_build_assign (endvar, e);
    6311         4226 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    6312         4226 :       if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
    6313         3458 :         assign_stmt = gimple_build_assign (fd->loop.v, e);
    6314              :       else
    6315          768 :         assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
    6316         4226 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    6317              :     }
    6318              :   /* Handle linear clause adjustments.  */
    6319         5881 :   tree itercnt = NULL_TREE, itercntbias = NULL_TREE;
    6320         5881 :   if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_FOR)
    6321        15938 :     for (tree c = gimple_omp_for_clauses (fd->for_stmt);
    6322        15938 :          c; c = OMP_CLAUSE_CHAIN (c))
    6323        13543 :       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
    6324        13543 :           && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
    6325              :         {
    6326           68 :           tree d = OMP_CLAUSE_DECL (c);
    6327           68 :           tree t = d, a, dest;
    6328           68 :           if (omp_privatize_by_reference (t))
    6329            3 :             t = build_simple_mem_ref_loc (OMP_CLAUSE_LOCATION (c), t);
    6330           68 :           tree type = TREE_TYPE (t);
    6331           68 :           if (POINTER_TYPE_P (type))
    6332            0 :             type = sizetype;
    6333           68 :           dest = unshare_expr (t);
    6334           68 :           tree v = create_tmp_var (TREE_TYPE (t), NULL);
    6335           68 :           expand_omp_build_assign (&gsif, v, t);
    6336           68 :           if (itercnt == NULL_TREE)
    6337              :             {
    6338           68 :               if (gimple_omp_for_combined_into_p (fd->for_stmt))
    6339              :                 {
    6340            0 :                   itercntbias
    6341            0 :                     = fold_build2 (MINUS_EXPR, itype, fold_convert (itype, n1),
    6342              :                                    fold_convert (itype, fd->loop.n1));
    6343            0 :                   itercntbias = fold_build2 (EXACT_DIV_EXPR, itype,
    6344              :                                              itercntbias, step);
    6345            0 :                   itercntbias
    6346            0 :                     = force_gimple_operand_gsi (&gsif, itercntbias, true,
    6347              :                                                 NULL_TREE, true,
    6348              :                                                 GSI_SAME_STMT);
    6349            0 :                   itercnt = fold_build2 (PLUS_EXPR, itype, itercntbias, s0);
    6350            0 :                   itercnt = force_gimple_operand_gsi (&gsi, itercnt, true,
    6351              :                                                       NULL_TREE, false,
    6352              :                                                       GSI_CONTINUE_LINKING);
    6353              :                 }
    6354              :               else
    6355              :                 itercnt = s0;
    6356              :             }
    6357           68 :           a = fold_build2 (MULT_EXPR, type,
    6358              :                            fold_convert (type, itercnt),
    6359              :                            fold_convert (type, OMP_CLAUSE_LINEAR_STEP (c)));
    6360           68 :           t = fold_build2 (type == TREE_TYPE (t) ? PLUS_EXPR
    6361              :                            : POINTER_PLUS_EXPR, TREE_TYPE (t), v, a);
    6362           68 :           t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    6363              :                                         false, GSI_CONTINUE_LINKING);
    6364           68 :           expand_omp_build_assign (&gsi, dest, t, true);
    6365              :         }
    6366         5881 :   if (fd->collapse > 1)
    6367         2256 :     expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
    6368              : 
    6369         5881 :   if (!broken_loop)
    6370              :     {
    6371              :       /* The code controlling the sequential loop goes in CONT_BB,
    6372              :          replacing the GIMPLE_OMP_CONTINUE.  */
    6373         5624 :       gsi = gsi_last_nondebug_bb (cont_bb);
    6374         5624 :       gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
    6375         5624 :       vmain = gimple_omp_continue_control_use (cont_stmt);
    6376         5624 :       vback = gimple_omp_continue_control_def (cont_stmt);
    6377              : 
    6378         5624 :       if (cond_var)
    6379              :         {
    6380           49 :           tree itype = TREE_TYPE (cond_var);
    6381           49 :           tree t2;
    6382           49 :           if (POINTER_TYPE_P (type)
    6383           49 :               || TREE_CODE (n1) != INTEGER_CST
    6384           43 :               || fd->loop.cond_code != LT_EXPR)
    6385            6 :             t2 = build_int_cst (itype, 1);
    6386              :           else
    6387           43 :             t2 = fold_convert (itype, step);
    6388           49 :           t2 = fold_build2 (PLUS_EXPR, itype, cond_var, t2);
    6389           49 :           t2 = force_gimple_operand_gsi (&gsi, t2, false,
    6390              :                                          NULL_TREE, true, GSI_SAME_STMT);
    6391           49 :           assign_stmt = gimple_build_assign (cond_var, t2);
    6392           49 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    6393              :         }
    6394              : 
    6395         5624 :       if (!gimple_omp_for_combined_p (fd->for_stmt))
    6396              :         {
    6397         1398 :           if (POINTER_TYPE_P (type))
    6398           96 :             t = fold_build_pointer_plus (vmain, step);
    6399              :           else
    6400         1302 :             t = fold_build2 (PLUS_EXPR, type, vmain, step);
    6401         1398 :           t = force_gimple_operand_gsi (&gsi, t,
    6402         1398 :                                         DECL_P (vback)
    6403         1386 :                                          && TREE_ADDRESSABLE (vback), NULL_TREE,
    6404              :                                         true, GSI_SAME_STMT);
    6405         1398 :           assign_stmt = gimple_build_assign (vback, t);
    6406         1398 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    6407              : 
    6408         1398 :           if (tree_int_cst_equal (fd->chunk_size, integer_one_node))
    6409           96 :             t = build2 (EQ_EXPR, boolean_type_node,
    6410              :                         build_int_cst (itype, 0),
    6411              :                         build_int_cst (itype, 1));
    6412              :           else
    6413         1302 :             t = build2 (fd->loop.cond_code, boolean_type_node,
    6414         1302 :                         DECL_P (vback) && TREE_ADDRESSABLE (vback)
    6415              :                         ? t : vback, e);
    6416         1398 :           gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
    6417              :         }
    6418              : 
    6419              :       /* Remove GIMPLE_OMP_CONTINUE.  */
    6420         5624 :       gsi_remove (&gsi, true);
    6421              : 
    6422         5624 :       if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
    6423          449 :         collapse_bb = extract_omp_for_update_vars (fd, NULL, cont_bb, body_bb);
    6424              : 
    6425              :       /* Trip update code goes into TRIP_UPDATE_BB.  */
    6426         5624 :       gsi = gsi_start_bb (trip_update_bb);
    6427              : 
    6428         5624 :       t = build_int_cst (itype, 1);
    6429         5624 :       t = build2 (PLUS_EXPR, itype, trip_main, t);
    6430         5624 :       assign_stmt = gimple_build_assign (trip_back, t);
    6431         5624 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    6432              :     }
    6433              : 
    6434         5881 :   gsi = gsi_last_nondebug_bb (exit_bb);
    6435         5881 :   if (flag_openmp_ompt && !has_gomp_loop_end)
    6436              :     {
    6437              :       /* Insert call to GOMP_*_static_worksharing_end at the end of exit_bb.
    6438              :        */
    6439           16 :       tree decl;
    6440           16 :       switch (gimple_omp_for_kind (fd->for_stmt))
    6441              :         {
    6442            8 :         case GF_OMP_FOR_KIND_FOR:
    6443            8 :           decl
    6444            8 :             = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_END);
    6445            8 :           break;
    6446            8 :         case GF_OMP_FOR_KIND_DISTRIBUTE:
    6447            8 :           decl = builtin_decl_explicit (
    6448              :             BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_END);
    6449            8 :           break;
    6450            0 :         default:
    6451            0 :           gcc_unreachable ();
    6452              :         }
    6453           16 :       gcall *g = gimple_build_call (decl, 0);
    6454           16 :       gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    6455              :     }
    6456              : 
    6457              :   /* Replace the GIMPLE_OMP_RETURN with a barrier, or nothing.  */
    6458         5881 :   if (!gimple_omp_return_nowait_p (gsi_stmt (gsi)))
    6459              :     {
    6460          322 :       t = gimple_omp_return_lhs (gsi_stmt (gsi));
    6461          322 :       if (fd->have_reductemp || fd->have_pointer_condtemp)
    6462              :         {
    6463           36 :           tree fn;
    6464           36 :           if (t)
    6465            0 :             fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_CANCEL);
    6466              :           else
    6467           36 :             fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END);
    6468           36 :           gcall *g = gimple_build_call (fn, 0);
    6469           36 :           if (t)
    6470              :             {
    6471            0 :               gimple_call_set_lhs (g, t);
    6472            0 :               if (fd->have_reductemp)
    6473            0 :                 gsi_insert_after (&gsi, gimple_build_assign (reductions,
    6474              :                                                              NOP_EXPR, t),
    6475              :                                   GSI_SAME_STMT);
    6476              :             }
    6477           36 :           gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    6478           36 :         }
    6479              :       else
    6480          286 :         gsi_insert_after (&gsi,
    6481              :                           omp_build_barrier (t,
    6482              :                                              GOMP_BARRIER_IMPLICIT_WORKSHARE),
    6483              :                           GSI_SAME_STMT);
    6484              :     }
    6485         5559 :   else if (fd->have_pointer_condtemp)
    6486              :     {
    6487            9 :       tree fn = builtin_decl_explicit (BUILT_IN_GOMP_LOOP_END_NOWAIT);
    6488            9 :       gcall *g = gimple_build_call (fn, 0);
    6489            9 :       gsi_insert_after (&gsi, g, GSI_SAME_STMT);
    6490              :     }
    6491         5881 :   gsi_remove (&gsi, true);
    6492              : 
    6493         5881 :   if (flag_openmp_ompt == OMP_OMPT_LEVEL_EXTENDED)
    6494              :     {
    6495              :       /* Insert call to GOMP_*_static_worksharing_dispatch at the end of
    6496              :          seq_start_bb.  */
    6497            8 :       gsi = gsi_last_nondebug_bb (seq_start_bb);
    6498            8 :       tree decl;
    6499            8 :       switch (gimple_omp_for_kind (fd->for_stmt))
    6500              :         {
    6501            4 :         case GF_OMP_FOR_KIND_FOR:
    6502            4 :           decl = builtin_decl_explicit (
    6503              :             BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_DISPATCH);
    6504            4 :           break;
    6505            4 :         case GF_OMP_FOR_KIND_DISTRIBUTE:
    6506            4 :           decl = builtin_decl_explicit (
    6507              :             BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_DISPATCH);
    6508            4 :           break;
    6509            0 :         default:
    6510            0 :           gcc_unreachable ();
    6511              :         }
    6512            8 :       tree s0_ull = fold_convert (long_long_unsigned_type_node, s0);
    6513            8 :       tree iterations = fold_build2 (MINUS_EXPR, itype, e0, s0);
    6514            8 :       iterations = fold_convert (long_long_unsigned_type_node, iterations);
    6515            8 :       tree call = build_call_expr (decl, 2, s0_ull, iterations);
    6516            8 :       force_gimple_operand_gsi (&gsi, call, true, NULL_TREE, true,
    6517              :                                 GSI_SAME_STMT);
    6518              :     }
    6519              : 
    6520              :   /* Connect the new blocks.  */
    6521         5881 :   find_edge (iter_part_bb, seq_start_bb)->flags = EDGE_TRUE_VALUE;
    6522         5881 :   find_edge (iter_part_bb, fin_bb)->flags = EDGE_FALSE_VALUE;
    6523              : 
    6524         5881 :   if (!broken_loop)
    6525              :     {
    6526         5624 :       se = find_edge (cont_bb, body_bb);
    6527         5624 :       if (se == NULL)
    6528              :         {
    6529           12 :           se = BRANCH_EDGE (cont_bb);
    6530           12 :           gcc_assert (single_succ (se->dest) == body_bb);
    6531              :         }
    6532         5624 :       if (gimple_omp_for_combined_p (fd->for_stmt))
    6533              :         {
    6534         4226 :           remove_edge (se);
    6535         4226 :           se = NULL;
    6536              :         }
    6537         1398 :       else if (fd->collapse > 1)
    6538              :         {
    6539          449 :           remove_edge (se);
    6540          449 :           se = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
    6541              :         }
    6542              :       else
    6543          949 :         se->flags = EDGE_TRUE_VALUE;
    6544         9850 :       find_edge (cont_bb, trip_update_bb)->flags
    6545          449 :         = se ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
    6546              : 
    6547         5624 :       redirect_edge_and_branch (single_succ_edge (trip_update_bb),
    6548              :                                 iter_part_bb);
    6549              :     }
    6550              : 
    6551         5881 :   if (gimple_in_ssa_p (cfun))
    6552              :     {
    6553           12 :       gphi_iterator psi;
    6554           12 :       gphi *phi;
    6555           12 :       edge re, ene;
    6556           12 :       edge_var_map *vm;
    6557           12 :       size_t i;
    6558              : 
    6559           12 :       gcc_assert (fd->collapse == 1 && !broken_loop);
    6560              : 
    6561              :       /* When we redirect the edge from trip_update_bb to iter_part_bb, we
    6562              :          remove arguments of the phi nodes in fin_bb.  We need to create
    6563              :          appropriate phi nodes in iter_part_bb instead.  */
    6564           12 :       se = find_edge (iter_part_bb, fin_bb);
    6565           12 :       re = single_succ_edge (trip_update_bb);
    6566           12 :       vec<edge_var_map> *head = redirect_edge_var_map_vector (re);
    6567           12 :       ene = single_succ_edge (entry_bb);
    6568              : 
    6569           12 :       psi = gsi_start_phis (fin_bb);
    6570           33 :       for (i = 0; !gsi_end_p (psi) && head->iterate (i, &vm);
    6571            9 :            gsi_next (&psi), ++i)
    6572              :         {
    6573            9 :           gphi *nphi;
    6574            9 :           location_t locus;
    6575              : 
    6576            9 :           phi = psi.phi ();
    6577            9 :           if (operand_equal_p (gimple_phi_arg_def (phi, 0),
    6578            9 :                                redirect_edge_var_map_def (vm), 0))
    6579            0 :             continue;
    6580              : 
    6581            9 :           t = gimple_phi_result (phi);
    6582            9 :           gcc_assert (t == redirect_edge_var_map_result (vm));
    6583              : 
    6584            9 :           if (!single_pred_p (fin_bb))
    6585            1 :             t = copy_ssa_name (t, phi);
    6586              : 
    6587            9 :           nphi = create_phi_node (t, iter_part_bb);
    6588              : 
    6589            9 :           t = PHI_ARG_DEF_FROM_EDGE (phi, se);
    6590            9 :           locus = gimple_phi_arg_location_from_edge (phi, se);
    6591              : 
    6592              :           /* A special case -- fd->loop.v is not yet computed in
    6593              :              iter_part_bb, we need to use vextra instead.  */
    6594            9 :           if (t == fd->loop.v)
    6595            0 :             t = vextra;
    6596            9 :           add_phi_arg (nphi, t, ene, locus);
    6597            9 :           locus = redirect_edge_var_map_location (vm);
    6598            9 :           tree back_arg = redirect_edge_var_map_def (vm);
    6599            9 :           add_phi_arg (nphi, back_arg, re, locus);
    6600            9 :           edge ce = find_edge (cont_bb, body_bb);
    6601            9 :           if (ce == NULL)
    6602              :             {
    6603            9 :               ce = BRANCH_EDGE (cont_bb);
    6604            9 :               gcc_assert (single_succ (ce->dest) == body_bb);
    6605              :               ce = single_succ_edge (ce->dest);
    6606              :             }
    6607            9 :           gphi *inner_loop_phi = find_phi_with_arg_on_edge (back_arg, ce);
    6608            9 :           gcc_assert (inner_loop_phi != NULL);
    6609            9 :           add_phi_arg (inner_loop_phi, gimple_phi_result (nphi),
    6610              :                        find_edge (seq_start_bb, body_bb), locus);
    6611              : 
    6612            9 :           if (!single_pred_p (fin_bb))
    6613            1 :             add_phi_arg (phi, gimple_phi_result (nphi), se, locus);
    6614              :         }
    6615           21 :       gcc_assert (gsi_end_p (psi) && (head == NULL || i == head->length ()));
    6616           12 :       redirect_edge_var_map_clear (re);
    6617           12 :       if (single_pred_p (fin_bb))
    6618           27 :         while (1)
    6619              :           {
    6620           19 :             psi = gsi_start_phis (fin_bb);
    6621           19 :             if (gsi_end_p (psi))
    6622              :               break;
    6623            8 :             remove_phi_node (&psi, false);
    6624              :           }
    6625              : 
    6626              :       /* Make phi node for trip.  */
    6627           12 :       phi = create_phi_node (trip_main, iter_part_bb);
    6628           12 :       add_phi_arg (phi, trip_back, single_succ_edge (trip_update_bb),
    6629              :                    UNKNOWN_LOCATION);
    6630           12 :       add_phi_arg (phi, trip_init, single_succ_edge (entry_bb),
    6631              :                    UNKNOWN_LOCATION);
    6632              :     }
    6633              : 
    6634         5881 :   if (!broken_loop)
    6635         5624 :     set_immediate_dominator (CDI_DOMINATORS, trip_update_bb, cont_bb);
    6636         5881 :   set_immediate_dominator (CDI_DOMINATORS, iter_part_bb,
    6637              :                            recompute_dominator (CDI_DOMINATORS, iter_part_bb));
    6638         5881 :   set_immediate_dominator (CDI_DOMINATORS, fin_bb,
    6639              :                            recompute_dominator (CDI_DOMINATORS, fin_bb));
    6640         5881 :   set_immediate_dominator (CDI_DOMINATORS, seq_start_bb,
    6641              :                            recompute_dominator (CDI_DOMINATORS, seq_start_bb));
    6642         5881 :   set_immediate_dominator (CDI_DOMINATORS, body_bb,
    6643              :                            recompute_dominator (CDI_DOMINATORS, body_bb));
    6644              : 
    6645         5881 :   if (!broken_loop)
    6646              :     {
    6647         5624 :       class loop *loop = body_bb->loop_father;
    6648         5624 :       class loop *trip_loop = alloc_loop ();
    6649         5624 :       trip_loop->header = iter_part_bb;
    6650         5624 :       trip_loop->latch = trip_update_bb;
    6651         5624 :       add_loop (trip_loop, iter_part_bb->loop_father);
    6652              : 
    6653         5624 :       if (loop != entry_bb->loop_father)
    6654              :         {
    6655           12 :           gcc_assert (loop->header == body_bb);
    6656           12 :           gcc_assert (loop->latch == region->cont
    6657              :                       || single_pred (loop->latch) == region->cont);
    6658           12 :           trip_loop->inner = loop;
    6659           12 :           return;
    6660              :         }
    6661              : 
    6662         5612 :       if (!gimple_omp_for_combined_p (fd->for_stmt))
    6663              :         {
    6664         1386 :           loop = alloc_loop ();
    6665         1386 :           loop->header = body_bb;
    6666         1386 :           if (collapse_bb == NULL)
    6667          937 :             loop->latch = cont_bb;
    6668         1386 :           add_loop (loop, trip_loop);
    6669              :         }
    6670              :     }
    6671              : }
    6672              : 
    6673              : /* A subroutine of expand_omp_for.  Generate code for a simd non-worksharing
    6674              :    loop.  Given parameters:
    6675              : 
    6676              :         for (V = N1; V cond N2; V += STEP) BODY;
    6677              : 
    6678              :    where COND is "<" or ">", we generate pseudocode
    6679              : 
    6680              :         V = N1;
    6681              :         goto L1;
    6682              :     L0:
    6683              :         BODY;
    6684              :         V += STEP;
    6685              :     L1:
    6686              :         if (V cond N2) goto L0; else goto L2;
    6687              :     L2:
    6688              : 
    6689              :     For collapsed loops, emit the outer loops as scalar
    6690              :     and only try to vectorize the innermost loop.  */
    6691              : 
    6692              : static void
    6693        10428 : expand_omp_simd (struct omp_region *region, struct omp_for_data *fd)
    6694              : {
    6695        10428 :   tree type, t;
    6696        10428 :   basic_block entry_bb, cont_bb, exit_bb, l0_bb, l1_bb, l2_bb, l2_dom_bb;
    6697        10428 :   gimple_stmt_iterator gsi;
    6698        10428 :   gimple *stmt;
    6699        10428 :   gcond *cond_stmt;
    6700        10428 :   bool broken_loop = region->cont == NULL;
    6701        10428 :   edge e, ne;
    6702        10428 :   tree *counts = NULL;
    6703        10428 :   int i;
    6704        10428 :   int safelen_int = INT_MAX;
    6705        10428 :   bool dont_vectorize = false;
    6706        10428 :   tree safelen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6707              :                                   OMP_CLAUSE_SAFELEN);
    6708        10428 :   tree simduid = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6709              :                                   OMP_CLAUSE__SIMDUID_);
    6710        10428 :   tree ifc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6711              :                               OMP_CLAUSE_IF);
    6712        10428 :   tree simdlen = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6713              :                                   OMP_CLAUSE_SIMDLEN);
    6714        10428 :   tree condtemp = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6715              :                                    OMP_CLAUSE__CONDTEMP_);
    6716        10428 :   tree n1, n2;
    6717        10529 :   tree cond_var = condtemp ? OMP_CLAUSE_DECL (condtemp) : NULL_TREE;
    6718              : 
    6719        10428 :   if (safelen)
    6720              :     {
    6721         6022 :       poly_uint64 val;
    6722         6022 :       safelen = OMP_CLAUSE_SAFELEN_EXPR (safelen);
    6723         6022 :       if (!poly_int_tree_p (safelen, &val))
    6724              :         safelen_int = 0;
    6725              :       else
    6726         6022 :         safelen_int = MIN (constant_lower_bound (val), INT_MAX);
    6727         6022 :       if (safelen_int == 1)
    6728         1807 :         safelen_int = 0;
    6729              :     }
    6730          759 :   if ((ifc && integer_zerop (OMP_CLAUSE_IF_EXPR (ifc)))
    6731        11063 :       || (simdlen && integer_onep (OMP_CLAUSE_SIMDLEN_EXPR (simdlen))))
    6732              :     {
    6733              :       safelen_int = 0;
    6734              :       dont_vectorize = true;
    6735              :     }
    6736        10428 :   type = TREE_TYPE (fd->loop.v);
    6737        10428 :   entry_bb = region->entry;
    6738        10428 :   cont_bb = region->cont;
    6739        10428 :   gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
    6740        10428 :   gcc_assert (broken_loop
    6741              :               || BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
    6742        10428 :   l0_bb = FALLTHRU_EDGE (entry_bb)->dest;
    6743        10428 :   if (!broken_loop)
    6744              :     {
    6745         9356 :       gcc_assert (BRANCH_EDGE (cont_bb)->dest == l0_bb);
    6746         9356 :       gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
    6747         9356 :       l1_bb = split_block (cont_bb, last_nondebug_stmt (cont_bb))->dest;
    6748         9356 :       l2_bb = BRANCH_EDGE (entry_bb)->dest;
    6749              :     }
    6750              :   else
    6751              :     {
    6752         1072 :       BRANCH_EDGE (entry_bb)->flags &= ~EDGE_ABNORMAL;
    6753         1072 :       l1_bb = split_edge (BRANCH_EDGE (entry_bb));
    6754         1072 :       l2_bb = single_succ (l1_bb);
    6755              :     }
    6756        10428 :   exit_bb = region->exit;
    6757        10428 :   l2_dom_bb = NULL;
    6758              : 
    6759        10428 :   gsi = gsi_last_nondebug_bb (entry_bb);
    6760              : 
    6761        10428 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    6762              :   /* Not needed in SSA form right now.  */
    6763        10428 :   gcc_assert (!gimple_in_ssa_p (cfun));
    6764        10428 :   if (fd->collapse > 1
    6765        10428 :       && (gimple_omp_for_combined_into_p (fd->for_stmt)
    6766          810 :           || broken_loop))
    6767              :     {
    6768         2960 :       int first_zero_iter = -1, dummy = -1;
    6769         2960 :       basic_block zero_iter_bb = l2_bb, dummy_bb = NULL;
    6770              : 
    6771         2960 :       counts = XALLOCAVEC (tree, fd->collapse);
    6772         2960 :       expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
    6773              :                                   zero_iter_bb, first_zero_iter,
    6774              :                                   dummy_bb, dummy, l2_dom_bb);
    6775              :     }
    6776        10428 :   if (l2_dom_bb == NULL)
    6777        10420 :     l2_dom_bb = l1_bb;
    6778              : 
    6779        10428 :   n1 = fd->loop.n1;
    6780        10428 :   n2 = fd->loop.n2;
    6781        10428 :   if (gimple_omp_for_combined_into_p (fd->for_stmt))
    6782              :     {
    6783         7722 :       tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6784              :                                      OMP_CLAUSE__LOOPTEMP_);
    6785         7722 :       gcc_assert (innerc);
    6786         7722 :       n1 = OMP_CLAUSE_DECL (innerc);
    6787         7722 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    6788              :                                 OMP_CLAUSE__LOOPTEMP_);
    6789         7722 :       gcc_assert (innerc);
    6790         7722 :       n2 = OMP_CLAUSE_DECL (innerc);
    6791              :     }
    6792        10428 :   tree step = fd->loop.step;
    6793        10428 :   tree orig_step = step; /* May be different from step if is_simt.  */
    6794              : 
    6795        10428 :   bool is_simt = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    6796        10428 :                                   OMP_CLAUSE__SIMT_);
    6797        10428 :   if (is_simt)
    6798              :     {
    6799            0 :       cfun->curr_properties &= ~PROP_gimple_lomp_dev;
    6800            0 :       is_simt = safelen_int > 1;
    6801              :     }
    6802        10428 :   tree simt_lane = NULL_TREE, simt_maxlane = NULL_TREE;
    6803        10428 :   tree vf = NULL_TREE;
    6804        10428 :   if (is_simt)
    6805              :     {
    6806            0 :       simt_lane = create_tmp_var (unsigned_type_node);
    6807            0 :       gimple *g = gimple_build_call_internal (IFN_GOMP_SIMT_LANE, 0);
    6808            0 :       gimple_call_set_lhs (g, simt_lane);
    6809            0 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    6810            0 :       if (fd->collapse == 1)
    6811              :         {
    6812              :           /* For non-collapsed SIMT loops, add the lane offset to the low
    6813              :              bound.  */
    6814            0 :           tree offset
    6815            0 :             = fold_build2 (MULT_EXPR, TREE_TYPE (step), step,
    6816              :                            fold_convert (TREE_TYPE (step), simt_lane));
    6817            0 :           n1 = fold_convert (type, n1);
    6818            0 :           if (POINTER_TYPE_P (type))
    6819            0 :             n1 = fold_build_pointer_plus (n1, offset);
    6820              :           else
    6821            0 :             n1 = fold_build2 (PLUS_EXPR, type, n1, fold_convert (type, offset));
    6822              :         }
    6823              : 
    6824            0 :       if (fd->collapse > 1 && broken_loop)
    6825            0 :         simt_maxlane = build_one_cst (unsigned_type_node);
    6826            0 :       else if (safelen_int < omp_max_simt_vf ())
    6827            0 :         simt_maxlane = build_int_cst (unsigned_type_node, safelen_int);
    6828            0 :       vf = build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_GOMP_SIMT_VF,
    6829              :                                          unsigned_type_node, 0);
    6830            0 :       if (simt_maxlane)
    6831            0 :         vf = fold_build2 (MIN_EXPR, unsigned_type_node, vf, simt_maxlane);
    6832            0 :       vf = fold_convert (TREE_TYPE (step), vf);
    6833            0 :       step = fold_build2 (MULT_EXPR, TREE_TYPE (step), step, vf);
    6834              :     }
    6835              : 
    6836        10428 :   tree n2var = NULL_TREE;
    6837        10428 :   tree n2v = NULL_TREE;
    6838        10428 :   tree *nonrect_bounds = NULL;
    6839        10428 :   tree min_arg1 = NULL_TREE, min_arg2 = NULL_TREE;
    6840        10428 :   if (fd->collapse > 1)
    6841              :     {
    6842         3750 :       if (broken_loop || gimple_omp_for_combined_into_p (fd->for_stmt))
    6843              :         {
    6844         2960 :           if (fd->non_rect)
    6845              :             {
    6846           99 :               nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
    6847           99 :               memset (nonrect_bounds, 0,
    6848              :                       sizeof (tree) * (fd->last_nonrect + 1));
    6849              :             }
    6850         2960 :           expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
    6851         2960 :           gcc_assert (entry_bb == gsi_bb (gsi));
    6852         2960 :           gcc_assert (fd->for_stmt == gsi_stmt (gsi));
    6853         2960 :           gsi_prev (&gsi);
    6854         2960 :           entry_bb = split_block (entry_bb, gsi_stmt (gsi))->dest;
    6855         2960 :           expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds,
    6856              :                                     NULL, n1);
    6857         2960 :           gsi = gsi_for_stmt (fd->for_stmt);
    6858              :         }
    6859         3750 :       if (broken_loop)
    6860              :         ;
    6861         3214 :       else if (gimple_omp_for_combined_into_p (fd->for_stmt))
    6862              :         {
    6863              :           /* Compute in n2var the limit for the first innermost loop,
    6864              :              i.e. fd->loop.v + MIN (n2 - fd->loop.v, cnt)
    6865              :              where cnt is how many iterations would the loop have if
    6866              :              all further iterations were assigned to the current task.  */
    6867         2424 :           n2var = create_tmp_var (type);
    6868         2424 :           i = fd->collapse - 1;
    6869         2424 :           tree itype = TREE_TYPE (fd->loops[i].v);
    6870         2424 :           if (POINTER_TYPE_P (itype))
    6871          266 :             itype = signed_type_for (itype);
    6872         2424 :           t = build_int_cst (itype, (fd->loops[i].cond_code == LT_EXPR
    6873         3230 :                                      ? -1 : 1));
    6874         2424 :           t = fold_build2 (PLUS_EXPR, itype,
    6875              :                            fold_convert (itype, fd->loops[i].step), t);
    6876         2424 :           t = fold_build2 (PLUS_EXPR, itype, t,
    6877              :                            fold_convert (itype, fd->loops[i].n2));
    6878         2424 :           if (fd->loops[i].m2)
    6879              :             {
    6880           26 :               tree t2 = fold_convert (itype,
    6881              :                                       fd->loops[i - fd->loops[i].outer].v);
    6882           26 :               tree t3 = fold_convert (itype, fd->loops[i].m2);
    6883           26 :               t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
    6884           26 :               t = fold_build2 (PLUS_EXPR, itype, t, t2);
    6885              :             }
    6886         2424 :           t = fold_build2 (MINUS_EXPR, itype, t,
    6887              :                            fold_convert (itype, fd->loops[i].v));
    6888         2424 :           if (TYPE_UNSIGNED (itype) && fd->loops[i].cond_code == GT_EXPR)
    6889          276 :             t = fold_build2 (TRUNC_DIV_EXPR, itype,
    6890              :                              fold_build1 (NEGATE_EXPR, itype, t),
    6891              :                              fold_build1 (NEGATE_EXPR, itype,
    6892              :                                           fold_convert (itype,
    6893              :                                                         fd->loops[i].step)));
    6894              :           else
    6895         2148 :             t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
    6896              :                              fold_convert (itype, fd->loops[i].step));
    6897         2424 :           t = fold_convert (type, t);
    6898         2424 :           tree t2 = fold_build2 (MINUS_EXPR, type, n2, n1);
    6899         2424 :           min_arg1 = create_tmp_var (type);
    6900         2424 :           expand_omp_build_assign (&gsi, min_arg1, t2);
    6901         2424 :           min_arg2 = create_tmp_var (type);
    6902         2424 :           expand_omp_build_assign (&gsi, min_arg2, t);
    6903              :         }
    6904              :       else
    6905              :         {
    6906          790 :           if (TREE_CODE (n2) == INTEGER_CST)
    6907              :             {
    6908              :               /* Indicate for lastprivate handling that at least one iteration
    6909              :                  has been performed, without wasting runtime.  */
    6910          273 :               if (integer_nonzerop (n2))
    6911          265 :                 expand_omp_build_assign (&gsi, fd->loop.v,
    6912              :                                          fold_convert (type, n2));
    6913              :               else
    6914              :                 /* Indicate that no iteration has been performed.  */
    6915            8 :                 expand_omp_build_assign (&gsi, fd->loop.v,
    6916              :                                          build_one_cst (type));
    6917              :             }
    6918              :           else
    6919              :             {
    6920          517 :               expand_omp_build_assign (&gsi, fd->loop.v,
    6921              :                                        build_zero_cst (type));
    6922          517 :               expand_omp_build_assign (&gsi, n2, build_one_cst (type));
    6923              :             }
    6924          790 :           for (i = 0; i < fd->collapse; i++)
    6925              :             {
    6926          790 :               t = fold_convert (TREE_TYPE (fd->loops[i].v), fd->loops[i].n1);
    6927          790 :               if (fd->loops[i].m1)
    6928              :                 {
    6929            0 :                   tree t2
    6930            0 :                     = fold_convert (TREE_TYPE (t),
    6931              :                                     fd->loops[i - fd->loops[i].outer].v);
    6932            0 :                   tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i].m1);
    6933            0 :                   t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
    6934            0 :                   t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
    6935              :                 }
    6936          790 :               expand_omp_build_assign (&gsi, fd->loops[i].v, t);
    6937              :               /* For normal non-combined collapsed loops just initialize
    6938              :                  the outermost iterator in the entry_bb.  */
    6939          790 :               if (!broken_loop)
    6940              :                 break;
    6941              :             }
    6942              :         }
    6943              :     }
    6944              :   else
    6945         6678 :     expand_omp_build_assign (&gsi, fd->loop.v, fold_convert (type, n1));
    6946        10428 :   tree altv = NULL_TREE, altn2 = NULL_TREE;
    6947        10428 :   if (fd->collapse == 1
    6948         6678 :       && !broken_loop
    6949         6142 :       && TREE_CODE (orig_step) != INTEGER_CST)
    6950              :     {
    6951              :       /* The vectorizer currently punts on loops with non-constant steps
    6952              :          for the main IV (can't compute number of iterations and gives up
    6953              :          because of that).  As for OpenMP loops it is always possible to
    6954              :          compute the number of iterations upfront, use an alternate IV
    6955              :          as the loop iterator:
    6956              :          altn2 = n1 < n2 ? (n2 - n1 + step - 1) / step : 0;
    6957              :          for (i = n1, altv = 0; altv < altn2; altv++, i += step)  */
    6958          176 :       altv = create_tmp_var (unsigned_type_for (TREE_TYPE (fd->loops[0].v)));
    6959          176 :       expand_omp_build_assign (&gsi, altv, build_zero_cst (TREE_TYPE (altv)));
    6960          176 :       tree itype = TREE_TYPE (fd->loop.v);
    6961          176 :       if (POINTER_TYPE_P (itype))
    6962            0 :         itype = signed_type_for (itype);
    6963          334 :       t = build_int_cst (itype, (fd->loop.cond_code == LT_EXPR ? -1 : 1));
    6964          176 :       t = fold_build2 (PLUS_EXPR, itype,
    6965              :                        fold_convert (itype, step), t);
    6966          176 :       t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, n2));
    6967          176 :       t = fold_build2 (MINUS_EXPR, itype, t,
    6968              :                        fold_convert (itype, fd->loop.v));
    6969          176 :       if (TYPE_UNSIGNED (itype) && fd->loop.cond_code == GT_EXPR)
    6970            0 :         t = fold_build2 (TRUNC_DIV_EXPR, itype,
    6971              :                          fold_build1 (NEGATE_EXPR, itype, t),
    6972              :                          fold_build1 (NEGATE_EXPR, itype,
    6973              :                                       fold_convert (itype, step)));
    6974              :       else
    6975          176 :         t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
    6976              :                          fold_convert (itype, step));
    6977          176 :       t = fold_convert (TREE_TYPE (altv), t);
    6978          176 :       altn2 = create_tmp_var (TREE_TYPE (altv));
    6979          176 :       expand_omp_build_assign (&gsi, altn2, t);
    6980          176 :       tree t2 = fold_convert (TREE_TYPE (fd->loop.v), n2);
    6981          176 :       t2 = fold_build2 (fd->loop.cond_code, boolean_type_node, fd->loop.v, t2);
    6982          176 :       t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
    6983              :                                      true, GSI_SAME_STMT);
    6984          176 :       gassign *g = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
    6985          176 :                                         build_zero_cst (TREE_TYPE (altv)));
    6986          176 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    6987          176 :     }
    6988        10252 :   else if (fd->collapse > 1
    6989         3750 :            && !broken_loop
    6990         3214 :            && !gimple_omp_for_combined_into_p (fd->for_stmt)
    6991        11042 :            && TREE_CODE (fd->loops[fd->collapse - 1].step) != INTEGER_CST)
    6992              :     {
    6993           52 :       tree utype
    6994           52 :         = unsigned_type_for (TREE_TYPE (fd->loops[fd->collapse - 1].v));
    6995              :       /* For SIMT, altv counts each lane's remaining iterations in steps of the
    6996              :          vectorization factor, so it can pass altn2 by up to VF - 1.  64-bit
    6997              :          leaves room for that with narrower loop variables.  */
    6998           52 :       altv = create_tmp_var (
    6999            0 :         is_simt && TYPE_PRECISION (utype) < 64 ? uint64_type_node : utype);
    7000           52 :       altn2 = create_tmp_var (TREE_TYPE (altv));
    7001              :     }
    7002        10428 :   if (cond_var)
    7003              :     {
    7004          101 :       if (POINTER_TYPE_P (type)
    7005          101 :           || TREE_CODE (n1) != INTEGER_CST
    7006           35 :           || fd->loop.cond_code != LT_EXPR
    7007          136 :           || tree_int_cst_sgn (n1) != 1)
    7008           80 :         expand_omp_build_assign (&gsi, cond_var,
    7009           80 :                                  build_one_cst (TREE_TYPE (cond_var)));
    7010              :       else
    7011           21 :         expand_omp_build_assign (&gsi, cond_var,
    7012           21 :                                  fold_convert (TREE_TYPE (cond_var), n1));
    7013              :     }
    7014              : 
    7015              :   /* Remove the GIMPLE_OMP_FOR statement.  */
    7016        10428 :   gsi_remove (&gsi, true);
    7017              : 
    7018        10428 :   if (!broken_loop)
    7019              :     {
    7020              :       /* Code to control the increment goes in the CONT_BB.  */
    7021         9356 :       gsi = gsi_last_nondebug_bb (cont_bb);
    7022         9356 :       stmt = gsi_stmt (gsi);
    7023         9356 :       gcc_assert (gimple_code (stmt) == GIMPLE_OMP_CONTINUE);
    7024              : 
    7025         9356 :       if (fd->collapse == 1
    7026         9356 :           || gimple_omp_for_combined_into_p (fd->for_stmt))
    7027              :         {
    7028         8566 :           if (POINTER_TYPE_P (type))
    7029          286 :             t = fold_build_pointer_plus (fd->loop.v, unshare_expr (step));
    7030              :           else
    7031         8280 :             t = fold_build2 (PLUS_EXPR, type, fd->loop.v, unshare_expr (step));
    7032         8566 :           expand_omp_build_assign (&gsi, fd->loop.v, t);
    7033              :         }
    7034          790 :       else if (TREE_CODE (n2) != INTEGER_CST)
    7035          517 :         expand_omp_build_assign (&gsi, fd->loop.v, build_one_cst (type));
    7036         9356 :       if (altv)
    7037              :         {
    7038          228 :           tree step;
    7039          228 :           if (is_simt && fd->collapse > 1)
    7040            0 :             step = fold_convert (TREE_TYPE (altv), unshare_expr (vf));
    7041              :           else
    7042          228 :             step = build_one_cst (TREE_TYPE (altv));
    7043          228 :           t = fold_build2 (PLUS_EXPR, TREE_TYPE (altv), altv, step);
    7044          228 :           expand_omp_build_assign (&gsi, altv, t);
    7045              :         }
    7046              : 
    7047         9356 :       if (fd->collapse > 1)
    7048              :         {
    7049         3214 :           i = fd->collapse - 1;
    7050         3214 :           tree step = fd->loops[i].step;
    7051         3214 :           if (is_simt)
    7052              :             /* For collapsed SIMT loops, scale the step by the vectorisation
    7053              :                factor.  */
    7054            0 :             step = fold_build2 (MULT_EXPR, TREE_TYPE (fd->loops[i].v),
    7055              :                                 fold_convert (TREE_TYPE (fd->loops[i].v), step),
    7056              :                                 fold_convert (TREE_TYPE (fd->loops[i].v),
    7057              :                                               unshare_expr (vf)));
    7058         3214 :           if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
    7059          280 :             t = fold_build_pointer_plus (fd->loops[i].v, step);
    7060              :           else
    7061              :             {
    7062         2934 :               t = fold_convert (TREE_TYPE (fd->loops[i].v), step);
    7063         2934 :               t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
    7064              :                                fd->loops[i].v, t);
    7065              :             }
    7066         3214 :           expand_omp_build_assign (&gsi, fd->loops[i].v, t);
    7067              :         }
    7068         9356 :       if (cond_var)
    7069              :         {
    7070          101 :           if (POINTER_TYPE_P (type)
    7071          101 :               || TREE_CODE (n1) != INTEGER_CST
    7072           35 :               || fd->loop.cond_code != LT_EXPR
    7073          136 :               || tree_int_cst_sgn (n1) != 1)
    7074           80 :             t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
    7075              :                              build_one_cst (TREE_TYPE (cond_var)));
    7076              :           else
    7077           21 :             t = fold_build2 (PLUS_EXPR, TREE_TYPE (cond_var), cond_var,
    7078              :                              fold_convert (TREE_TYPE (cond_var), step));
    7079          101 :           expand_omp_build_assign (&gsi, cond_var, t);
    7080              :         }
    7081              : 
    7082              :       /* Remove GIMPLE_OMP_CONTINUE.  */
    7083         9356 :       gsi_remove (&gsi, true);
    7084              :     }
    7085              : 
    7086              :   /* Emit the condition in L1_BB.  */
    7087        10428 :   gsi = gsi_start_bb (l1_bb);
    7088              : 
    7089        10428 :   if (altv)
    7090          228 :     t = build2 (LT_EXPR, boolean_type_node, altv, altn2);
    7091        10200 :   else if (fd->collapse > 1
    7092         3698 :            && !gimple_omp_for_combined_into_p (fd->for_stmt)
    7093        10958 :            && !broken_loop)
    7094              :     {
    7095          738 :       i = fd->collapse - 1;
    7096          738 :       tree itype = TREE_TYPE (fd->loops[i].v);
    7097          738 :       if (fd->loops[i].m2)
    7098          203 :         t = n2v = create_tmp_var (itype);
    7099              :       else
    7100          535 :         t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    7101          738 :       t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    7102              :                                     false, GSI_CONTINUE_LINKING);
    7103          738 :       tree v = fd->loops[i].v;
    7104          738 :       if (DECL_P (v) && TREE_ADDRESSABLE (v))
    7105            0 :         v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
    7106              :                                       false, GSI_CONTINUE_LINKING);
    7107          738 :       t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
    7108              :     }
    7109              :   else
    7110              :     {
    7111         9462 :       if (fd->collapse > 1 && !broken_loop)
    7112              :         t = n2var;
    7113              :       else
    7114         7038 :         t = fold_convert (type, unshare_expr (n2));
    7115         9462 :       t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    7116              :                                     false, GSI_CONTINUE_LINKING);
    7117         9462 :       tree v = fd->loop.v;
    7118         9462 :       if (DECL_P (v) && TREE_ADDRESSABLE (v))
    7119            0 :         v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
    7120              :                                       false, GSI_CONTINUE_LINKING);
    7121         9462 :       t = build2 (fd->loop.cond_code, boolean_type_node, v, t);
    7122              :     }
    7123        10428 :   cond_stmt = gimple_build_cond_empty (t);
    7124        10428 :   gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
    7125        10428 :   if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p,
    7126              :                  NULL, NULL)
    7127        10428 :       || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p,
    7128              :                     NULL, NULL))
    7129              :     {
    7130            0 :       gsi = gsi_for_stmt (cond_stmt);
    7131            0 :       gimple_regimplify_operands (cond_stmt, &gsi);
    7132              :     }
    7133              : 
    7134              :   /* Add 'V -= STEP * (SIMT_VF - 1)' after the loop.  */
    7135        10428 :   if (is_simt
    7136        10428 :       && (fd->collapse == 1 || gimple_omp_for_combined_into_p (fd->for_stmt)))
    7137              :     {
    7138            0 :       gsi = gsi_start_bb (l2_bb);
    7139            0 :       step = fold_build2 (MINUS_EXPR, TREE_TYPE (step), orig_step,
    7140              :                           unshare_expr (step));
    7141            0 :       if (POINTER_TYPE_P (type))
    7142            0 :         t = fold_build_pointer_plus (fd->loop.v, step);
    7143              :       else
    7144            0 :         t = fold_build2 (PLUS_EXPR, type, fd->loop.v, step);
    7145            0 :       expand_omp_build_assign (&gsi, fd->loop.v, t);
    7146              :     }
    7147              : 
    7148            0 :   if (is_simt && fd->collapse > 1 && !broken_loop
    7149        10428 :       && omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    7150              :                           OMP_CLAUSE_LASTPRIVATE))
    7151              :     {
    7152              :       /* After the last iteration, ensure all lanes get the same final IV
    7153              :          value.  The gimplifier makes simd loop variables lastprivate, unless
    7154              :          they are private.  */
    7155            0 :       gsi = gsi_start_bb (l2_bb);
    7156            0 :       for (int k = 0; k < fd->collapse; k++)
    7157              :         {
    7158            0 :           struct omp_for_data_loop *l = &fd->loops[k];
    7159            0 :           tree itype = TREE_TYPE (l->v);
    7160            0 :           gcc_assert (!POINTER_TYPE_P (itype) && !l->m1 && !l->m2);
    7161              : 
    7162            0 :           tree t = build_int_cst (itype, (l->cond_code == LT_EXPR ? -1 : 1));
    7163            0 :           t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, l->step), t);
    7164            0 :           t = fold_build2 (PLUS_EXPR, itype, t, fold_convert (itype, l->n2));
    7165            0 :           t = fold_build2 (MINUS_EXPR, itype, t, fold_convert (itype, l->n1));
    7166            0 :           if (TYPE_UNSIGNED (itype) && l->cond_code == GT_EXPR)
    7167            0 :             t = fold_build2 (TRUNC_DIV_EXPR, itype,
    7168              :                              fold_build1 (NEGATE_EXPR, itype, t),
    7169              :                              fold_build1 (NEGATE_EXPR, itype,
    7170              :                                           fold_convert (itype, l->step)));
    7171              :           else
    7172            0 :             t = fold_build2 (TRUNC_DIV_EXPR, itype, t,
    7173              :                              fold_convert (itype, l->step));
    7174              : 
    7175            0 :           tree niter = t;
    7176              : 
    7177            0 :           t = fold_build2 (MULT_EXPR, itype, niter,
    7178              :                            fold_convert (itype, l->step));
    7179            0 :           t = fold_build2 (PLUS_EXPR, itype, fold_convert (itype, l->n1), t);
    7180              : 
    7181            0 :           tree final_iv = t;
    7182            0 :           expand_omp_build_assign (&gsi, l->v, final_iv);
    7183              :         }
    7184              :     }
    7185              : 
    7186              :   /* Remove GIMPLE_OMP_RETURN.  */
    7187        10428 :   gsi = gsi_last_nondebug_bb (exit_bb);
    7188        10428 :   gsi_remove (&gsi, true);
    7189              : 
    7190              :   /* Connect the new blocks.  */
    7191        10428 :   remove_edge (FALLTHRU_EDGE (entry_bb));
    7192              : 
    7193        10428 :   if (!broken_loop)
    7194              :     {
    7195         9356 :       remove_edge (BRANCH_EDGE (entry_bb));
    7196         9356 :       make_edge (entry_bb, l1_bb, EDGE_FALLTHRU);
    7197              : 
    7198         9356 :       e = BRANCH_EDGE (l1_bb);
    7199         9356 :       ne = FALLTHRU_EDGE (l1_bb);
    7200         9356 :       e->flags = EDGE_TRUE_VALUE;
    7201              :     }
    7202              :   else
    7203              :     {
    7204         1072 :       single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
    7205              : 
    7206         1072 :       ne = single_succ_edge (l1_bb);
    7207         1072 :       e = make_edge (l1_bb, l0_bb, EDGE_TRUE_VALUE);
    7208              : 
    7209              :     }
    7210        10428 :   ne->flags = EDGE_FALSE_VALUE;
    7211        10428 :   e->probability = profile_probability::guessed_always ().apply_scale (7, 8);
    7212        10428 :   ne->probability = e->probability.invert ();
    7213              : 
    7214        10428 :   set_immediate_dominator (CDI_DOMINATORS, l1_bb, entry_bb);
    7215        10428 :   set_immediate_dominator (CDI_DOMINATORS, l0_bb, l1_bb);
    7216              : 
    7217        10428 :   if (simt_maxlane)
    7218              :     {
    7219            0 :       cond_stmt = gimple_build_cond (LT_EXPR, simt_lane, simt_maxlane,
    7220              :                                      NULL_TREE, NULL_TREE);
    7221            0 :       gsi = gsi_last_bb (entry_bb);
    7222            0 :       gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
    7223            0 :       make_edge (entry_bb, l2_bb, EDGE_FALSE_VALUE);
    7224            0 :       FALLTHRU_EDGE (entry_bb)->flags = EDGE_TRUE_VALUE;
    7225            0 :       FALLTHRU_EDGE (entry_bb)->probability
    7226            0 :          = profile_probability::guessed_always ().apply_scale (7, 8);
    7227            0 :       BRANCH_EDGE (entry_bb)->probability
    7228            0 :          = FALLTHRU_EDGE (entry_bb)->probability.invert ();
    7229            0 :       if (l2_dom_bb == l1_bb)
    7230            0 :         l2_dom_bb = entry_bb;
    7231              :     }
    7232        10428 :   set_immediate_dominator (CDI_DOMINATORS, l2_bb, l2_dom_bb);
    7233              : 
    7234        10428 :   if (!broken_loop && fd->collapse > 1)
    7235              :     {
    7236         3214 :       basic_block last_bb = l1_bb;
    7237         3214 :       basic_block init_bb = NULL;
    7238         8180 :       for (i = fd->collapse - 2; i >= 0; i--)
    7239              :         {
    7240         4966 :           tree nextn2v = NULL_TREE;
    7241         4966 :           if (EDGE_SUCC (last_bb, 0)->flags & EDGE_FALSE_VALUE)
    7242              :             e = EDGE_SUCC (last_bb, 0);
    7243              :           else
    7244         3214 :             e = EDGE_SUCC (last_bb, 1);
    7245         4966 :           basic_block bb = split_edge (e);
    7246         4966 :           if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i].v)))
    7247          559 :             t = fold_build_pointer_plus (fd->loops[i].v, fd->loops[i].step);
    7248              :           else
    7249              :             {
    7250         4407 :               t = fold_convert (TREE_TYPE (fd->loops[i].v),
    7251              :                                 fd->loops[i].step);
    7252         4407 :               t = fold_build2 (PLUS_EXPR, TREE_TYPE (fd->loops[i].v),
    7253              :                                fd->loops[i].v, t);
    7254              :             }
    7255         4966 :           gsi = gsi_after_labels (bb);
    7256         4966 :           expand_omp_build_assign (&gsi, fd->loops[i].v, t);
    7257              : 
    7258         4966 :           basic_block early_exit = NULL;
    7259         4966 :           if (is_simt && i + 2 == fd->collapse && n2var)
    7260         4966 :             early_exit = bb;
    7261              : 
    7262         4966 :           bb = split_block (bb, last_nondebug_stmt (bb))->dest;
    7263              : 
    7264              :           /* Composite case: leave when the share ends in this row.  Test N2VAR
    7265              :              rather than V so that each lane leaves with its own V, which the
    7266              :              lastprivate copy-out needs.  */
    7267         4966 :           if (early_exit)
    7268              :             {
    7269            0 :               gsi = gsi_last_bb (early_exit);
    7270            0 :               gcond *c = gimple_build_cond (LT_EXPR, n2var, n2, NULL_TREE,
    7271              :                                             NULL_TREE);
    7272            0 :               gsi_insert_after (&gsi, c, GSI_NEW_STMT);
    7273            0 :               edge et = single_succ_edge (early_exit);
    7274            0 :               et->flags = EDGE_TRUE_VALUE;
    7275            0 :               et->probability
    7276            0 :                 = profile_probability::guessed_always ().apply_scale (7, 8);
    7277            0 :               edge ef = make_edge (early_exit, l2_bb, EDGE_FALSE_VALUE);
    7278            0 :               ef->probability = et->probability.invert ();
    7279            0 :               set_immediate_dominator (
    7280              :                 CDI_DOMINATORS, l2_bb,
    7281              :                 nearest_common_dominator (
    7282              :                   CDI_DOMINATORS,
    7283              :                   get_immediate_dominator (CDI_DOMINATORS, l2_bb), early_exit));
    7284              :             }
    7285              : 
    7286         4966 :           gsi = gsi_start_bb (bb);
    7287         4966 :           tree itype = TREE_TYPE (fd->loops[i].v);
    7288         4966 :           if (fd->loops[i].m2)
    7289            0 :             t = nextn2v = create_tmp_var (itype);
    7290              :           else
    7291         4966 :             t = fold_convert (itype, unshare_expr (fd->loops[i].n2));
    7292         4966 :           t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    7293              :                                         false, GSI_CONTINUE_LINKING);
    7294         4966 :           tree v = fd->loops[i].v;
    7295         4966 :           if (DECL_P (v) && TREE_ADDRESSABLE (v))
    7296            0 :             v = force_gimple_operand_gsi (&gsi, v, true, NULL_TREE,
    7297              :                                           false, GSI_CONTINUE_LINKING);
    7298         4966 :           t = build2 (fd->loops[i].cond_code, boolean_type_node, v, t);
    7299         4966 :           cond_stmt = gimple_build_cond_empty (t);
    7300         4966 :           gsi_insert_after (&gsi, cond_stmt, GSI_CONTINUE_LINKING);
    7301         4966 :           if (walk_tree (gimple_cond_lhs_ptr (cond_stmt),
    7302              :                          expand_omp_regimplify_p, NULL, NULL)
    7303         4966 :               || walk_tree (gimple_cond_rhs_ptr (cond_stmt),
    7304              :                             expand_omp_regimplify_p, NULL, NULL))
    7305              :             {
    7306            6 :               gsi = gsi_for_stmt (cond_stmt);
    7307            6 :               gimple_regimplify_operands (cond_stmt, &gsi);
    7308              :             }
    7309         4966 :           ne = single_succ_edge (bb);
    7310         4966 :           ne->flags = EDGE_FALSE_VALUE;
    7311              : 
    7312         4966 :           init_bb = create_empty_bb (bb);
    7313         4966 :           set_immediate_dominator (CDI_DOMINATORS, init_bb, bb);
    7314         4966 :           add_bb_to_loop (init_bb, bb->loop_father);
    7315         4966 :           e = make_edge (bb, init_bb, EDGE_TRUE_VALUE);
    7316         4966 :           e->probability
    7317         4966 :             = profile_probability::guessed_always ().apply_scale (7, 8);
    7318         4966 :           ne->probability = e->probability.invert ();
    7319              : 
    7320         4966 :           gsi = gsi_after_labels (init_bb);
    7321         4966 :           if (is_simt && i + 2 == fd->collapse && n2var)
    7322              :             /* Composite case: restart V from the end of the previous row.  */
    7323            0 :             expand_omp_build_assign (&gsi, fd->loop.v, n2var);
    7324         4966 :           if (fd->loops[i + 1].m1)
    7325              :             {
    7326          248 :               tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
    7327              :                                       fd->loops[i + 1
    7328              :                                                 - fd->loops[i + 1].outer].v);
    7329          248 :               if (POINTER_TYPE_P (TREE_TYPE (t2)))
    7330            6 :                 t = fold_build_pointer_plus (t2, fd->loops[i + 1].n1);
    7331              :               else
    7332              :                 {
    7333          242 :                   t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
    7334              :                                     fd->loops[i + 1].n1);
    7335          242 :                   tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m1);
    7336          242 :                   t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
    7337          242 :                   t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
    7338              :                 }
    7339              :             }
    7340              :           else
    7341         4718 :             t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
    7342              :                               fd->loops[i + 1].n1);
    7343         4966 :           expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t);
    7344              : 
    7345         4966 :           if (is_simt && i + 2 == fd->collapse && !n2var)
    7346              :             {
    7347              :               /* Non-composite case: add the step scaled by the SIMT lane to the
    7348              :                  inner IVs.  */
    7349            0 :               itype = TREE_TYPE (fd->loops[i + 1].v);
    7350            0 :               if (POINTER_TYPE_P (itype))
    7351            0 :                 itype = signed_type_for (itype);
    7352            0 :               tree ilane = fold_convert (itype, simt_lane);
    7353            0 :               t = fold_build2 (MULT_EXPR, itype, ilane,
    7354              :                                fold_convert (itype, fd->loops[i + 1].step));
    7355            0 :               if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i + 1].v)))
    7356            0 :                 t = fold_build_pointer_plus (fd->loops[i + 1].v, t);
    7357              :               else
    7358            0 :                 t = fold_build2 (PLUS_EXPR, itype, fd->loops[i + 1].v, t);
    7359            0 :               expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t);
    7360              :             }
    7361              : 
    7362         4966 :           if (fd->loops[i + 1].m2)
    7363              :             {
    7364          241 :               if (i + 2 == fd->collapse && (n2var || altv))
    7365              :                 {
    7366           38 :                   gcc_assert (n2v == NULL_TREE);
    7367           38 :                   n2v = create_tmp_var (TREE_TYPE (fd->loops[i + 1].v));
    7368              :                 }
    7369          241 :               tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
    7370              :                                       fd->loops[i + 1
    7371              :                                                 - fd->loops[i + 1].outer].v);
    7372          241 :               if (POINTER_TYPE_P (TREE_TYPE (t2)))
    7373            6 :                 t = fold_build_pointer_plus (t2, fd->loops[i + 1].n2);
    7374              :               else
    7375              :                 {
    7376          235 :                   t = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
    7377              :                                     fd->loops[i + 1].n2);
    7378          235 :                   tree t3 = fold_convert (TREE_TYPE (t), fd->loops[i + 1].m2);
    7379          235 :                   t2 = fold_build2 (MULT_EXPR, TREE_TYPE (t), t2, t3);
    7380          235 :                   t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t, t2);
    7381              :                 }
    7382          241 :               expand_omp_build_assign (&gsi, n2v, t);
    7383              :             }
    7384         4966 :           if (i + 2 == fd->collapse && n2var)
    7385              :             {
    7386              :               /* For composite simd, n2 is the first iteration the current
    7387              :                  task shouldn't already handle, so we effectively want to use
    7388              :                  for (V3 = N31; V < N2 && V3 < N32; V++, V3 += STEP3)
    7389              :                  as the vectorized loop.  Except the vectorizer will not
    7390              :                  vectorize that, so instead compute N2VAR as
    7391              :                  N2VAR = V + MIN (N2 - V, COUNTS3) and use
    7392              :                  for (V3 = N31; V < N2VAR; V++, V3 += STEP3)
    7393              :                  as the loop to vectorize.  */
    7394         2424 :               tree t2 = fold_build2 (MINUS_EXPR, type, n2, fd->loop.v);
    7395         2424 :               if (fd->loops[i + 1].m1 || fd->loops[i + 1].m2)
    7396              :                 {
    7397           99 :                   tree itype = TREE_TYPE (fd->loops[i].v);
    7398           99 :                   if (POINTER_TYPE_P (itype))
    7399            4 :                     itype = signed_type_for (itype);
    7400           99 :                   t = build_int_cst (itype, (fd->loops[i + 1].cond_code
    7401           99 :                                              == LT_EXPR ? -1 : 1));
    7402           99 :                   t = fold_build2 (PLUS_EXPR, itype,
    7403              :                                    fold_convert (itype,
    7404              :                                                  fd->loops[i + 1].step), t);
    7405           99 :                   if (fd->loops[i + 1].m2 == NULL_TREE)
    7406           73 :                     t = fold_build2 (PLUS_EXPR, itype, t,
    7407              :                                      fold_convert (itype,
    7408              :                                                    fd->loops[i + 1].n2));
    7409           26 :                   else if (POINTER_TYPE_P (TREE_TYPE (n2v)))
    7410              :                     {
    7411            4 :                       t = fold_build_pointer_plus (n2v, t);
    7412            4 :                       t = fold_convert (itype, t);
    7413              :                     }
    7414              :                   else
    7415           22 :                     t = fold_build2 (PLUS_EXPR, itype, t, n2v);
    7416           99 :                   t = fold_build2 (MINUS_EXPR, itype, t,
    7417              :                                    fold_convert (itype, fd->loops[i + 1].v));
    7418           99 :                   tree step = fold_convert (itype, fd->loops[i + 1].step);
    7419           99 :                   if (TYPE_UNSIGNED (itype)
    7420           99 :                       && fd->loops[i + 1].cond_code == GT_EXPR)
    7421            0 :                     t = fold_build2 (TRUNC_DIV_EXPR, itype,
    7422              :                                      fold_build1 (NEGATE_EXPR, itype, t),
    7423              :                                      fold_build1 (NEGATE_EXPR, itype, step));
    7424              :                   else
    7425           99 :                     t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    7426           99 :                   t = fold_convert (type, t);
    7427           99 :                 }
    7428              :               else
    7429         2325 :                 t = counts[i + 1];
    7430         2424 :               expand_omp_build_assign (&gsi, min_arg1, t2);
    7431         2424 :               expand_omp_build_assign (&gsi, min_arg2, t);
    7432         2424 :               e = split_block (init_bb, last_nondebug_stmt (init_bb));
    7433         2424 :               gsi = gsi_after_labels (e->dest);
    7434         2424 :               init_bb = e->dest;
    7435         2424 :               redirect_edge_succ (find_edge (entry_bb, l1_bb), init_bb);
    7436         2424 :               set_immediate_dominator (CDI_DOMINATORS, init_bb, entry_bb);
    7437         2424 :               set_immediate_dominator (CDI_DOMINATORS, l1_bb, init_bb);
    7438         2424 :               t = fold_build2 (MIN_EXPR, type, min_arg1, min_arg2);
    7439         2424 :               t = fold_build2 (PLUS_EXPR, type, fd->loop.v, t);
    7440         2424 :               expand_omp_build_assign (&gsi, n2var, t);
    7441         2424 :               if (is_simt)
    7442              :                 {
    7443              :                   /* Join block: add lane offset to both the linear and the
    7444              :                      inner IVs.  */
    7445            0 :                   tree itype = TREE_TYPE (fd->loops[i + 1].v);
    7446            0 :                   if (POINTER_TYPE_P (itype))
    7447            0 :                     itype = signed_type_for (itype);
    7448            0 :                   tree ilane = fold_convert (itype, simt_lane);
    7449            0 :                   t = fold_build2 (MULT_EXPR, itype, ilane,
    7450              :                                    fold_convert (itype, fd->loops[i + 1].step));
    7451            0 :                   if (POINTER_TYPE_P (TREE_TYPE (fd->loops[i + 1].v)))
    7452            0 :                     t = fold_build_pointer_plus (fd->loops[i + 1].v, t);
    7453              :                   else
    7454            0 :                     t = fold_build2 (PLUS_EXPR, itype, fd->loops[i + 1].v, t);
    7455            0 :                   expand_omp_build_assign (&gsi, fd->loops[i + 1].v, t);
    7456            0 :                   tree lane = fold_convert (type, simt_lane);
    7457            0 :                   t = fold_build2 (MULT_EXPR, type, lane,
    7458              :                                    fold_convert (type, orig_step));
    7459            0 :                   t = fold_build2 (PLUS_EXPR, type, fd->loop.v, t);
    7460            0 :                   expand_omp_build_assign (&gsi, fd->loop.v, t);
    7461              :                 }
    7462              :             }
    7463         4966 :           if (i + 2 == fd->collapse && altv)
    7464              :             {
    7465              :               /* The vectorizer currently punts on loops with non-constant
    7466              :                  steps for the main IV (can't compute number of iterations
    7467              :                  and gives up because of that).  As for OpenMP loops it is
    7468              :                  always possible to compute the number of iterations upfront,
    7469              :                  use an alternate IV as the loop iterator.  */
    7470           52 :               expand_omp_build_assign (&gsi, altv,
    7471           52 :                                        build_zero_cst (TREE_TYPE (altv)));
    7472           52 :               tree itype = TREE_TYPE (fd->loops[i + 1].v);
    7473           52 :               if (POINTER_TYPE_P (itype))
    7474            0 :                 itype = signed_type_for (itype);
    7475           52 :               t = build_int_cst (itype, (fd->loops[i + 1].cond_code == LT_EXPR
    7476           56 :                                          ? -1 : 1));
    7477           52 :               t = fold_build2 (PLUS_EXPR, itype,
    7478              :                                fold_convert (itype, fd->loops[i + 1].step), t);
    7479           52 :               t = fold_build2 (PLUS_EXPR, itype, t,
    7480              :                                fold_convert (itype,
    7481              :                                              fd->loops[i + 1].m2
    7482              :                                              ? n2v : fd->loops[i + 1].n2));
    7483           52 :               t = fold_build2 (MINUS_EXPR, itype, t,
    7484              :                                fold_convert (itype, fd->loops[i + 1].v));
    7485           52 :               tree step = fold_convert (itype, fd->loops[i + 1].step);
    7486           52 :               if (TYPE_UNSIGNED (itype)
    7487           52 :                   && fd->loops[i + 1].cond_code == GT_EXPR)
    7488            0 :                 t = fold_build2 (TRUNC_DIV_EXPR, itype,
    7489              :                                  fold_build1 (NEGATE_EXPR, itype, t),
    7490              :                                  fold_build1 (NEGATE_EXPR, itype, step));
    7491              :               else
    7492           52 :                 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
    7493           52 :               t = fold_convert (TREE_TYPE (altv), t);
    7494           52 :               expand_omp_build_assign (&gsi, altn2, t);
    7495           52 :               tree t2 = fold_convert (TREE_TYPE (fd->loops[i + 1].v),
    7496              :                                       fd->loops[i + 1].m2
    7497              :                                       ? n2v : fd->loops[i + 1].n2);
    7498           52 :               t2 = fold_build2 (fd->loops[i + 1].cond_code, boolean_type_node,
    7499              :                                 fd->loops[i + 1].v, t2);
    7500           52 :               t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
    7501              :                                              true, GSI_SAME_STMT);
    7502           52 :               gassign *g
    7503           52 :                 = gimple_build_assign (altn2, COND_EXPR, t2, altn2,
    7504           52 :                                        build_zero_cst (TREE_TYPE (altv)));
    7505           52 :               gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    7506              :             }
    7507         4966 :           n2v = nextn2v;
    7508              : 
    7509         4966 :           make_edge (init_bb, last_bb, EDGE_FALLTHRU);
    7510         4966 :           if (!gimple_omp_for_combined_into_p (fd->for_stmt))
    7511              :             {
    7512         1123 :               e = find_edge (entry_bb, last_bb);
    7513         1123 :               redirect_edge_succ (e, bb);
    7514         1123 :               set_immediate_dominator (CDI_DOMINATORS, bb, entry_bb);
    7515         1123 :               set_immediate_dominator (CDI_DOMINATORS, last_bb, init_bb);
    7516              :             }
    7517              : 
    7518         4966 :           last_bb = bb;
    7519              :         }
    7520              :     }
    7521         9356 :   if (!broken_loop)
    7522              :     {
    7523         9356 :       class loop *loop = alloc_loop ();
    7524         9356 :       loop->header = l1_bb;
    7525         9356 :       loop->latch = cont_bb;
    7526         9356 :       add_loop (loop, l1_bb->loop_father);
    7527         9356 :       loop->safelen = safelen_int;
    7528         9356 :       if (simduid)
    7529              :         {
    7530         4069 :           loop->simduid = OMP_CLAUSE__SIMDUID__DECL (simduid);
    7531         4069 :           cfun->has_simduid_loops = true;
    7532              :         }
    7533              :       /* If not -fno-tree-loop-vectorize, hint that we want to vectorize
    7534              :          the loop.  */
    7535         9356 :       if ((flag_tree_loop_vectorize
    7536         3372 :            || !OPTION_SET_P (flag_tree_loop_vectorize))
    7537         9356 :           && flag_tree_loop_optimize
    7538         9355 :           && loop->safelen > 1)
    7539              :         {
    7540         7551 :           loop->force_vectorize = true;
    7541         7551 :           if (simdlen && tree_fits_uhwi_p (OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
    7542              :             {
    7543           69 :               unsigned HOST_WIDE_INT v
    7544           69 :                 = tree_to_uhwi (OMP_CLAUSE_SIMDLEN_EXPR (simdlen));
    7545           69 :               if (v < INT_MAX && v <= (unsigned HOST_WIDE_INT) loop->safelen)
    7546           69 :                 loop->simdlen = v;
    7547              :             }
    7548         7551 :           cfun->has_force_vectorize_loops = true;
    7549         7551 :         }
    7550         1805 :       else if (dont_vectorize)
    7551          234 :         loop->dont_vectorize = true;
    7552              :     }
    7553         1072 :   else if (simduid)
    7554          530 :     cfun->has_simduid_loops = true;
    7555        10428 : }
    7556              : 
    7557              : /* Taskloop construct is represented after gimplification with
    7558              :    two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
    7559              :    in between them.  This routine expands the outer GIMPLE_OMP_FOR,
    7560              :    which should just compute all the needed loop temporaries
    7561              :    for GIMPLE_OMP_TASK.  */
    7562              : 
    7563              : static void
    7564         1337 : expand_omp_taskloop_for_outer (struct omp_region *region,
    7565              :                                struct omp_for_data *fd,
    7566              :                                gimple *inner_stmt)
    7567              : {
    7568         1337 :   tree type, bias = NULL_TREE;
    7569         1337 :   basic_block entry_bb, cont_bb, exit_bb;
    7570         1337 :   gimple_stmt_iterator gsi;
    7571         1337 :   gassign *assign_stmt;
    7572         1337 :   tree *counts = NULL;
    7573         1337 :   int i;
    7574              : 
    7575         1337 :   gcc_assert (inner_stmt);
    7576         1337 :   gcc_assert (region->cont);
    7577         1337 :   gcc_assert (gimple_code (inner_stmt) == GIMPLE_OMP_TASK
    7578              :               && gimple_omp_task_taskloop_p (inner_stmt));
    7579         1337 :   type = TREE_TYPE (fd->loop.v);
    7580              : 
    7581              :   /* See if we need to bias by LLONG_MIN.  */
    7582         1337 :   if (fd->iter_type == long_long_unsigned_type_node
    7583           42 :       && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
    7584         1365 :       && !TYPE_UNSIGNED (type))
    7585              :     {
    7586            0 :       tree n1, n2;
    7587              : 
    7588            0 :       if (fd->loop.cond_code == LT_EXPR)
    7589              :         {
    7590            0 :           n1 = fd->loop.n1;
    7591            0 :           n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
    7592              :         }
    7593              :       else
    7594              :         {
    7595            0 :           n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
    7596            0 :           n2 = fd->loop.n1;
    7597              :         }
    7598            0 :       if (TREE_CODE (n1) != INTEGER_CST
    7599            0 :           || TREE_CODE (n2) != INTEGER_CST
    7600            0 :           || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
    7601            0 :         bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
    7602              :     }
    7603              : 
    7604         1337 :   entry_bb = region->entry;
    7605         1337 :   cont_bb = region->cont;
    7606         1337 :   gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
    7607         1337 :   gcc_assert (BRANCH_EDGE (entry_bb)->dest == FALLTHRU_EDGE (cont_bb)->dest);
    7608         1337 :   exit_bb = region->exit;
    7609              : 
    7610         1337 :   gsi = gsi_last_nondebug_bb (entry_bb);
    7611         1337 :   gimple *for_stmt = gsi_stmt (gsi);
    7612         1337 :   gcc_assert (gimple_code (for_stmt) == GIMPLE_OMP_FOR);
    7613         1337 :   if (fd->collapse > 1)
    7614              :     {
    7615          181 :       int first_zero_iter = -1, dummy = -1;
    7616          181 :       basic_block zero_iter_bb = NULL, dummy_bb = NULL, l2_dom_bb = NULL;
    7617              : 
    7618          181 :       counts = XALLOCAVEC (tree, fd->collapse);
    7619          181 :       expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
    7620              :                                   zero_iter_bb, first_zero_iter,
    7621              :                                   dummy_bb, dummy, l2_dom_bb);
    7622              : 
    7623          181 :       if (zero_iter_bb)
    7624              :         {
    7625              :           /* Some counts[i] vars might be uninitialized if
    7626              :              some loop has zero iterations.  But the body shouldn't
    7627              :              be executed in that case, so just avoid uninit warnings.  */
    7628          336 :           for (i = first_zero_iter; i < fd->collapse; i++)
    7629          239 :             if (SSA_VAR_P (counts[i]))
    7630          226 :               suppress_warning (counts[i], OPT_Wuninitialized);
    7631           97 :           gsi_prev (&gsi);
    7632           97 :           edge e = split_block (entry_bb, gsi_stmt (gsi));
    7633           97 :           entry_bb = e->dest;
    7634           97 :           make_edge (zero_iter_bb, entry_bb, EDGE_FALLTHRU);
    7635           97 :           gsi = gsi_last_bb (entry_bb);
    7636           97 :           set_immediate_dominator (CDI_DOMINATORS, entry_bb,
    7637              :                                    get_immediate_dominator (CDI_DOMINATORS,
    7638              :                                                             zero_iter_bb));
    7639              :         }
    7640              :     }
    7641              : 
    7642         1337 :   tree t0, t1;
    7643         1337 :   t1 = fd->loop.n2;
    7644         1337 :   t0 = fd->loop.n1;
    7645         2660 :   if (POINTER_TYPE_P (TREE_TYPE (t0))
    7646         1337 :       && TYPE_PRECISION (TREE_TYPE (t0))
    7647           14 :          != TYPE_PRECISION (fd->iter_type))
    7648              :     {
    7649              :       /* Avoid casting pointers to integer of a different size.  */
    7650            0 :       tree itype = signed_type_for (type);
    7651            0 :       t1 = fold_convert (fd->iter_type, fold_convert (itype, t1));
    7652            0 :       t0 = fold_convert (fd->iter_type, fold_convert (itype, t0));
    7653              :     }
    7654              :   else
    7655              :     {
    7656         1337 :       t1 = fold_convert (fd->iter_type, t1);
    7657         1337 :       t0 = fold_convert (fd->iter_type, t0);
    7658              :     }
    7659         1337 :   if (bias)
    7660              :     {
    7661            0 :       t1 = fold_build2 (PLUS_EXPR, fd->iter_type, t1, bias);
    7662            0 :       t0 = fold_build2 (PLUS_EXPR, fd->iter_type, t0, bias);
    7663              :     }
    7664              : 
    7665         1337 :   tree innerc = omp_find_clause (gimple_omp_task_clauses (inner_stmt),
    7666              :                                  OMP_CLAUSE__LOOPTEMP_);
    7667         1337 :   gcc_assert (innerc);
    7668         1337 :   tree startvar = OMP_CLAUSE_DECL (innerc);
    7669         1337 :   innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
    7670         1337 :   gcc_assert (innerc);
    7671         1337 :   tree endvar = OMP_CLAUSE_DECL (innerc);
    7672         1337 :   if (fd->collapse > 1 && TREE_CODE (fd->loop.n2) != INTEGER_CST)
    7673              :     {
    7674          101 :       innerc = find_lastprivate_looptemp (fd, innerc);
    7675          101 :       if (innerc)
    7676              :         {
    7677              :           /* If needed (inner taskloop has lastprivate clause), propagate
    7678              :              down the total number of iterations.  */
    7679           31 :           tree t = force_gimple_operand_gsi (&gsi, fd->loop.n2, false,
    7680              :                                              NULL_TREE, false,
    7681              :                                              GSI_CONTINUE_LINKING);
    7682           31 :           assign_stmt = gimple_build_assign (OMP_CLAUSE_DECL (innerc), t);
    7683           31 :           gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    7684              :         }
    7685              :     }
    7686              : 
    7687         1337 :   t0 = force_gimple_operand_gsi (&gsi, t0, false, NULL_TREE, false,
    7688              :                                  GSI_CONTINUE_LINKING);
    7689         1337 :   assign_stmt = gimple_build_assign (startvar, t0);
    7690         1337 :   gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    7691              : 
    7692         1337 :   t1 = force_gimple_operand_gsi (&gsi, t1, false, NULL_TREE, false,
    7693              :                                  GSI_CONTINUE_LINKING);
    7694         1337 :   assign_stmt = gimple_build_assign (endvar, t1);
    7695         1337 :   gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    7696         1337 :   if (fd->collapse > 1)
    7697          181 :     expand_omp_for_init_vars (fd, &gsi, counts, NULL, inner_stmt, startvar);
    7698              : 
    7699              :   /* Remove the GIMPLE_OMP_FOR statement.  */
    7700         1337 :   gsi = gsi_for_stmt (for_stmt);
    7701         1337 :   gsi_remove (&gsi, true);
    7702              : 
    7703         1337 :   gsi = gsi_last_nondebug_bb (cont_bb);
    7704         1337 :   gsi_remove (&gsi, true);
    7705              : 
    7706         1337 :   gsi = gsi_last_nondebug_bb (exit_bb);
    7707         1337 :   gsi_remove (&gsi, true);
    7708              : 
    7709         1337 :   FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
    7710         1337 :   remove_edge (BRANCH_EDGE (entry_bb));
    7711         1337 :   FALLTHRU_EDGE (cont_bb)->probability = profile_probability::always ();
    7712         1337 :   remove_edge (BRANCH_EDGE (cont_bb));
    7713         1337 :   set_immediate_dominator (CDI_DOMINATORS, exit_bb, cont_bb);
    7714         1337 :   set_immediate_dominator (CDI_DOMINATORS, region->entry,
    7715              :                            recompute_dominator (CDI_DOMINATORS, region->entry));
    7716         1337 : }
    7717              : 
    7718              : /* Taskloop construct is represented after gimplification with
    7719              :    two GIMPLE_OMP_FOR constructs with GIMPLE_OMP_TASK sandwiched
    7720              :    in between them.  This routine expands the inner GIMPLE_OMP_FOR.
    7721              :    GOMP_taskloop{,_ull} function arranges for each task to be given just
    7722              :    a single range of iterations.  */
    7723              : 
    7724              : static void
    7725         1337 : expand_omp_taskloop_for_inner (struct omp_region *region,
    7726              :                                struct omp_for_data *fd,
    7727              :                                gimple *inner_stmt)
    7728              : {
    7729         1337 :   tree e, t, type, itype, vmain, vback, bias = NULL_TREE;
    7730         1337 :   basic_block entry_bb, exit_bb, body_bb, cont_bb, collapse_bb = NULL;
    7731         1337 :   basic_block fin_bb;
    7732         1337 :   gimple_stmt_iterator gsi;
    7733         1337 :   edge ep;
    7734         1337 :   bool broken_loop = region->cont == NULL;
    7735         1337 :   tree *counts = NULL;
    7736         1337 :   tree n1, n2, step;
    7737              : 
    7738         1337 :   itype = type = TREE_TYPE (fd->loop.v);
    7739         1337 :   if (POINTER_TYPE_P (type))
    7740           14 :     itype = signed_type_for (type);
    7741              : 
    7742              :   /* See if we need to bias by LLONG_MIN.  */
    7743         1337 :   if (fd->iter_type == long_long_unsigned_type_node
    7744           42 :       && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)
    7745         1365 :       && !TYPE_UNSIGNED (type))
    7746              :     {
    7747            0 :       tree n1, n2;
    7748              : 
    7749            0 :       if (fd->loop.cond_code == LT_EXPR)
    7750              :         {
    7751            0 :           n1 = fd->loop.n1;
    7752            0 :           n2 = fold_build2 (PLUS_EXPR, type, fd->loop.n2, fd->loop.step);
    7753              :         }
    7754              :       else
    7755              :         {
    7756            0 :           n1 = fold_build2 (MINUS_EXPR, type, fd->loop.n2, fd->loop.step);
    7757            0 :           n2 = fd->loop.n1;
    7758              :         }
    7759            0 :       if (TREE_CODE (n1) != INTEGER_CST
    7760            0 :           || TREE_CODE (n2) != INTEGER_CST
    7761            0 :           || ((tree_int_cst_sgn (n1) < 0) ^ (tree_int_cst_sgn (n2) < 0)))
    7762            0 :         bias = fold_convert (fd->iter_type, TYPE_MIN_VALUE (type));
    7763              :     }
    7764              : 
    7765         1337 :   entry_bb = region->entry;
    7766         1337 :   cont_bb = region->cont;
    7767         1337 :   gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
    7768         1337 :   fin_bb = BRANCH_EDGE (entry_bb)->dest;
    7769         1337 :   gcc_assert (broken_loop
    7770              :               || (fin_bb == FALLTHRU_EDGE (cont_bb)->dest));
    7771         1337 :   body_bb = FALLTHRU_EDGE (entry_bb)->dest;
    7772         1337 :   if (!broken_loop)
    7773              :     {
    7774         1321 :       gcc_assert (BRANCH_EDGE (cont_bb)->dest == body_bb);
    7775         1321 :       gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
    7776              :     }
    7777         1337 :   exit_bb = region->exit;
    7778              : 
    7779              :   /* Iteration space partitioning goes in ENTRY_BB.  */
    7780         1337 :   gsi = gsi_last_nondebug_bb (entry_bb);
    7781         1337 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_FOR);
    7782              : 
    7783         1337 :   if (fd->collapse > 1)
    7784              :     {
    7785          181 :       int first_zero_iter = -1, dummy = -1;
    7786          181 :       basic_block l2_dom_bb = NULL, dummy_bb = NULL;
    7787              : 
    7788          181 :       counts = XALLOCAVEC (tree, fd->collapse);
    7789          181 :       expand_omp_for_init_counts (fd, &gsi, entry_bb, counts,
    7790              :                                   fin_bb, first_zero_iter,
    7791              :                                   dummy_bb, dummy, l2_dom_bb);
    7792          181 :       t = NULL_TREE;
    7793              :     }
    7794              :   else
    7795         1337 :     t = integer_one_node;
    7796              : 
    7797         1337 :   step = fd->loop.step;
    7798         1337 :   tree innerc = omp_find_clause (gimple_omp_for_clauses (fd->for_stmt),
    7799              :                                  OMP_CLAUSE__LOOPTEMP_);
    7800         1337 :   gcc_assert (innerc);
    7801         1337 :   n1 = OMP_CLAUSE_DECL (innerc);
    7802         1337 :   innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc), OMP_CLAUSE__LOOPTEMP_);
    7803         1337 :   gcc_assert (innerc);
    7804         1337 :   n2 = OMP_CLAUSE_DECL (innerc);
    7805         1337 :   if (bias)
    7806              :     {
    7807            0 :       n1 = fold_build2 (PLUS_EXPR, fd->iter_type, n1, bias);
    7808            0 :       n2 = fold_build2 (PLUS_EXPR, fd->iter_type, n2, bias);
    7809              :     }
    7810         1337 :   n1 = force_gimple_operand_gsi (&gsi, fold_convert (type, n1),
    7811              :                                  true, NULL_TREE, true, GSI_SAME_STMT);
    7812         1337 :   n2 = force_gimple_operand_gsi (&gsi, fold_convert (itype, n2),
    7813              :                                  true, NULL_TREE, true, GSI_SAME_STMT);
    7814         1337 :   step = force_gimple_operand_gsi (&gsi, fold_convert (itype, step),
    7815              :                                    true, NULL_TREE, true, GSI_SAME_STMT);
    7816              : 
    7817         1337 :   tree startvar = fd->loop.v;
    7818         1337 :   tree endvar = NULL_TREE;
    7819              : 
    7820         1337 :   if (gimple_omp_for_combined_p (fd->for_stmt))
    7821              :     {
    7822          620 :       tree clauses = gimple_omp_for_clauses (inner_stmt);
    7823          620 :       tree innerc = omp_find_clause (clauses, OMP_CLAUSE__LOOPTEMP_);
    7824          620 :       gcc_assert (innerc);
    7825          620 :       startvar = OMP_CLAUSE_DECL (innerc);
    7826          620 :       innerc = omp_find_clause (OMP_CLAUSE_CHAIN (innerc),
    7827              :                                 OMP_CLAUSE__LOOPTEMP_);
    7828          620 :       gcc_assert (innerc);
    7829          620 :       endvar = OMP_CLAUSE_DECL (innerc);
    7830              :     }
    7831         1337 :   t = fold_convert (TREE_TYPE (startvar), n1);
    7832         1337 :   t = force_gimple_operand_gsi (&gsi, t,
    7833         1337 :                                 DECL_P (startvar)
    7834         1337 :                                 && TREE_ADDRESSABLE (startvar),
    7835              :                                 NULL_TREE, false, GSI_CONTINUE_LINKING);
    7836         1337 :   gimple *assign_stmt = gimple_build_assign (startvar, t);
    7837         1337 :   gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    7838              : 
    7839         1337 :   t = fold_convert (TREE_TYPE (startvar), n2);
    7840         1337 :   e = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
    7841              :                                 false, GSI_CONTINUE_LINKING);
    7842         1337 :   if (endvar)
    7843              :     {
    7844          620 :       assign_stmt = gimple_build_assign (endvar, e);
    7845          620 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    7846          620 :       if (useless_type_conversion_p (TREE_TYPE (fd->loop.v), TREE_TYPE (e)))
    7847          532 :         assign_stmt = gimple_build_assign (fd->loop.v, e);
    7848              :       else
    7849           88 :         assign_stmt = gimple_build_assign (fd->loop.v, NOP_EXPR, e);
    7850          620 :       gsi_insert_after (&gsi, assign_stmt, GSI_CONTINUE_LINKING);
    7851              :     }
    7852              : 
    7853         1337 :   tree *nonrect_bounds = NULL;
    7854         1337 :   if (fd->collapse > 1)
    7855              :     {
    7856          181 :       if (fd->non_rect)
    7857              :         {
    7858           19 :           nonrect_bounds = XALLOCAVEC (tree, fd->last_nonrect + 1);
    7859           19 :           memset (nonrect_bounds, 0, sizeof (tree) * (fd->last_nonrect + 1));
    7860              :         }
    7861          181 :       gcc_assert (gsi_bb (gsi) == entry_bb);
    7862          181 :       expand_omp_for_init_vars (fd, &gsi, counts, nonrect_bounds, inner_stmt,
    7863              :                                 startvar);
    7864          181 :       entry_bb = gsi_bb (gsi);
    7865              :     }
    7866              : 
    7867         1337 :   if (!broken_loop)
    7868              :     {
    7869              :       /* The code controlling the sequential loop replaces the
    7870              :          GIMPLE_OMP_CONTINUE.  */
    7871         1321 :       gsi = gsi_last_nondebug_bb (cont_bb);
    7872         1321 :       gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
    7873         1321 :       gcc_assert (gimple_code (cont_stmt) == GIMPLE_OMP_CONTINUE);
    7874         1321 :       vmain = gimple_omp_continue_control_use (cont_stmt);
    7875         1321 :       vback = gimple_omp_continue_control_def (cont_stmt);
    7876              : 
    7877         1321 :       if (!gimple_omp_for_combined_p (fd->for_stmt))
    7878              :         {
    7879          701 :           if (POINTER_TYPE_P (type))
    7880            8 :             t = fold_build_pointer_plus (vmain, step);
    7881              :           else
    7882          693 :             t = fold_build2 (PLUS_EXPR, type, vmain, step);
    7883          701 :           t = force_gimple_operand_gsi (&gsi, t,
    7884          701 :                                         DECL_P (vback)
    7885          701 :                                         && TREE_ADDRESSABLE (vback),
    7886              :                                         NULL_TREE, true, GSI_SAME_STMT);
    7887          701 :           assign_stmt = gimple_build_assign (vback, t);
    7888          701 :           gsi_insert_before (&gsi, assign_stmt, GSI_SAME_STMT);
    7889              : 
    7890          701 :           t = build2 (fd->loop.cond_code, boolean_type_node,
    7891          701 :                       DECL_P (vback) && TREE_ADDRESSABLE (vback)
    7892              :                       ? t : vback, e);
    7893          701 :           gsi_insert_before (&gsi, gimple_build_cond_empty (t), GSI_SAME_STMT);
    7894              :         }
    7895              : 
    7896              :       /* Remove the GIMPLE_OMP_CONTINUE statement.  */
    7897         1321 :       gsi_remove (&gsi, true);
    7898              : 
    7899         1321 :       if (fd->collapse > 1 && !gimple_omp_for_combined_p (fd->for_stmt))
    7900           81 :         collapse_bb = extract_omp_for_update_vars (fd, nonrect_bounds,
    7901              :                                                    cont_bb, body_bb);
    7902              :     }
    7903              : 
    7904              :   /* Remove the GIMPLE_OMP_FOR statement.  */
    7905         1337 :   gsi = gsi_for_stmt (fd->for_stmt);
    7906         1337 :   gsi_remove (&gsi, true);
    7907              : 
    7908              :   /* Remove the GIMPLE_OMP_RETURN statement.  */
    7909         1337 :   gsi = gsi_last_nondebug_bb (exit_bb);
    7910         1337 :   gsi_remove (&gsi, true);
    7911              : 
    7912         1337 :   FALLTHRU_EDGE (entry_bb)->probability = profile_probability::always ();
    7913         1337 :   if (!broken_loop)
    7914         1321 :     remove_edge (BRANCH_EDGE (entry_bb));
    7915              :   else
    7916              :     {
    7917           16 :       remove_edge_and_dominated_blocks (BRANCH_EDGE (entry_bb));
    7918           16 :       region->outer->cont = NULL;
    7919              :     }
    7920              : 
    7921              :   /* Connect all the blocks.  */
    7922         1337 :   if (!broken_loop)
    7923              :     {
    7924         1321 :       ep = find_edge (cont_bb, body_bb);
    7925         1321 :       if (gimple_omp_for_combined_p (fd->for_stmt))
    7926              :         {
    7927          620 :           remove_edge (ep);
    7928          620 :           ep = NULL;
    7929              :         }
    7930          701 :       else if (fd->collapse > 1)
    7931              :         {
    7932           81 :           remove_edge (ep);
    7933           81 :           ep = make_edge (cont_bb, collapse_bb, EDGE_TRUE_VALUE);
    7934              :         }
    7935              :       else
    7936          620 :         ep->flags = EDGE_TRUE_VALUE;
    7937         1941 :       find_edge (cont_bb, fin_bb)->flags
    7938         1402 :         = ep ? EDGE_FALSE_VALUE : EDGE_FALLTHRU;
    7939              :     }
    7940              : 
    7941         1337 :   set_immediate_dominator (CDI_DOMINATORS, body_bb,
    7942              :                            recompute_dominator (CDI_DOMINATORS, body_bb));
    7943         1337 :   if (!broken_loop)
    7944         1321 :     set_immediate_dominator (CDI_DOMINATORS, fin_bb,
    7945              :                              recompute_dominator (CDI_DOMINATORS, fin_bb));
    7946              : 
    7947         1321 :   if (!broken_loop && !gimple_omp_for_combined_p (fd->for_stmt))
    7948              :     {
    7949          701 :       class loop *loop = alloc_loop ();
    7950          701 :       loop->header = body_bb;
    7951          701 :       if (collapse_bb == NULL)
    7952          620 :         loop->latch = cont_bb;
    7953          701 :       add_loop (loop, body_bb->loop_father);
    7954              :     }
    7955         1337 : }
    7956              : 
    7957              : /* A subroutine of expand_omp_for.  Generate code for an OpenACC
    7958              :    partitioned loop.  The lowering here is abstracted, in that the
    7959              :    loop parameters are passed through internal functions, which are
    7960              :    further lowered by oacc_device_lower, once we get to the target
    7961              :    compiler.  The loop is of the form:
    7962              : 
    7963              :    for (V = B; V LTGT E; V += S) {BODY}
    7964              : 
    7965              :    where LTGT is < or >.  We may have a specified chunking size, CHUNKING
    7966              :    (constant 0 for no chunking) and we will have a GWV partitioning
    7967              :    mask, specifying dimensions over which the loop is to be
    7968              :    partitioned (see note below).  We generate code that looks like
    7969              :    (this ignores tiling):
    7970              : 
    7971              :    <entry_bb> [incoming FALL->body, BRANCH->exit]
    7972              :      typedef signedintify (typeof (V)) T;  // underlying signed integral type
    7973              :      T range = E - B;
    7974              :      T chunk_no = 0;
    7975              :      T DIR = LTGT == '<' ? +1 : -1;
    7976              :      T chunk_max = GOACC_LOOP_CHUNK (dir, range, S, CHUNK_SIZE, GWV);
    7977              :      T step = GOACC_LOOP_STEP (dir, range, S, CHUNK_SIZE, GWV);
    7978              : 
    7979              :    <head_bb> [created by splitting end of entry_bb]
    7980              :      T offset = GOACC_LOOP_OFFSET (dir, range, S, CHUNK_SIZE, GWV, chunk_no);
    7981              :      T bound = GOACC_LOOP_BOUND (dir, range, S, CHUNK_SIZE, GWV, offset);
    7982              :      if (!(offset LTGT bound)) goto bottom_bb;
    7983              : 
    7984              :    <body_bb> [incoming]
    7985              :      V = B + offset;
    7986              :      {BODY}
    7987              : 
    7988              :    <cont_bb> [incoming, may == body_bb FALL->exit_bb, BRANCH->body_bb]
    7989              :      offset += step;
    7990              :      if (offset LTGT bound) goto body_bb; [*]
    7991              : 
    7992              :    <bottom_bb> [created by splitting start of exit_bb] insert BRANCH->head_bb
    7993              :      chunk_no++;
    7994              :      if (chunk < chunk_max) goto head_bb;
    7995              : 
    7996              :    <exit_bb> [incoming]
    7997              :      V = B + ((range -/+ 1) / S +/- 1) * S [*]
    7998              : 
    7999              :    [*] Needed if V live at end of loop.  */
    8000              : 
    8001              : static void
    8002        11711 : expand_oacc_for (struct omp_region *region, struct omp_for_data *fd)
    8003              : {
    8004        11711 :   bool is_oacc_kernels_parallelized
    8005        11711 :     = (lookup_attribute ("oacc kernels parallelized",
    8006        11711 :                          DECL_ATTRIBUTES (current_function_decl)) != NULL);
    8007        11711 :   {
    8008        11711 :     bool is_oacc_kernels
    8009        11711 :       = (lookup_attribute ("oacc kernels",
    8010        11711 :                            DECL_ATTRIBUTES (current_function_decl)) != NULL);
    8011        11711 :     if (is_oacc_kernels_parallelized)
    8012          386 :       gcc_checking_assert (is_oacc_kernels);
    8013              :   }
    8014        23422 :   gcc_assert (gimple_in_ssa_p (cfun) == is_oacc_kernels_parallelized);
    8015              :   /* In the following, some of the 'gimple_in_ssa_p (cfun)' conditionals are
    8016              :      for SSA specifics, and some are for 'parloops' OpenACC
    8017              :      'kernels'-parallelized specifics.  */
    8018              : 
    8019        11711 :   tree v = fd->loop.v;
    8020        11711 :   enum tree_code cond_code = fd->loop.cond_code;
    8021        11711 :   enum tree_code plus_code = PLUS_EXPR;
    8022              : 
    8023        11711 :   tree chunk_size = integer_minus_one_node;
    8024        11711 :   tree gwv = integer_zero_node;
    8025        11711 :   tree iter_type = TREE_TYPE (v);
    8026        11711 :   tree diff_type = iter_type;
    8027        11711 :   tree plus_type = iter_type;
    8028        11711 :   struct oacc_collapse *counts = NULL;
    8029              : 
    8030        11711 :   gcc_checking_assert (gimple_omp_for_kind (fd->for_stmt)
    8031              :                        == GF_OMP_FOR_KIND_OACC_LOOP);
    8032        11711 :   gcc_assert (!gimple_omp_for_combined_into_p (fd->for_stmt));
    8033        11711 :   gcc_assert (cond_code == LT_EXPR || cond_code == GT_EXPR);
    8034              : 
    8035        11711 :   if (POINTER_TYPE_P (iter_type))
    8036              :     {
    8037           52 :       plus_code = POINTER_PLUS_EXPR;
    8038           52 :       plus_type = sizetype;
    8039              :     }
    8040        24124 :   for (int ix = fd->collapse; ix--;)
    8041              :     {
    8042        12413 :       tree diff_type2 = TREE_TYPE (fd->loops[ix].step);
    8043        12413 :       if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (diff_type2))
    8044            0 :         diff_type = diff_type2;
    8045              :     }
    8046        11711 :   if (POINTER_TYPE_P (diff_type) || TYPE_UNSIGNED (diff_type))
    8047         1002 :     diff_type = signed_type_for (diff_type);
    8048        11711 :   if (TYPE_PRECISION (diff_type) < TYPE_PRECISION (integer_type_node))
    8049           23 :     diff_type = integer_type_node;
    8050              : 
    8051        11711 :   basic_block entry_bb = region->entry; /* BB ending in OMP_FOR */
    8052        11711 :   basic_block exit_bb = region->exit; /* BB ending in OMP_RETURN */
    8053        11711 :   basic_block cont_bb = region->cont; /* BB ending in OMP_CONTINUE  */
    8054        11711 :   basic_block bottom_bb = NULL;
    8055              : 
    8056              :   /* entry_bb has two successors; the branch edge is to the exit
    8057              :      block (or to finalization blocks preceding it), fallthrough edge
    8058              :      to body.  */
    8059        11711 :   gcc_assert (EDGE_COUNT (entry_bb->succs) == 2);
    8060              : 
    8061              :   /* If cont_bb non-NULL, it has 2 successors.  The branch successor is
    8062              :      body_bb, or to a block whose only successor is the body_bb.  Its
    8063              :      fallthrough successor is the final block (same as the branch
    8064              :      successor of the entry_bb), possibly via finalization blocks.  */
    8065        11711 :   if (cont_bb)
    8066              :     {
    8067        11672 :       basic_block body_bb = FALLTHRU_EDGE (entry_bb)->dest;
    8068        11672 :       basic_block bed = BRANCH_EDGE (cont_bb)->dest;
    8069              : 
    8070        11672 :       gcc_assert (EDGE_COUNT (cont_bb->succs) == 2);
    8071        11672 :       gcc_assert (bed == body_bb || single_succ_edge (bed)->dest == body_bb);
    8072              :     }
    8073              :   else
    8074           39 :     gcc_assert (!gimple_in_ssa_p (cfun));
    8075              : 
    8076        11711 :   tree chunk_no;
    8077        11711 :   tree chunk_max = NULL_TREE;
    8078        11711 :   tree bound, offset;
    8079        11711 :   tree step = create_tmp_var (diff_type, ".step");
    8080        11711 :   bool up = cond_code == LT_EXPR;
    8081        11871 :   tree dir = build_int_cst (diff_type, up ? +1 : -1);
    8082        11711 :   bool chunking = !gimple_in_ssa_p (cfun);
    8083        11711 :   bool negating;
    8084              : 
    8085              :   /* Tiling vars.  */
    8086        11711 :   tree tile_size = NULL_TREE;
    8087        11711 :   tree element_s = NULL_TREE;
    8088        11711 :   tree e_bound = NULL_TREE, e_offset = NULL_TREE, e_step = NULL_TREE;
    8089        11711 :   basic_block elem_body_bb = NULL;
    8090        11711 :   basic_block elem_cont_bb = NULL;
    8091              : 
    8092              :   /* SSA instances.  */
    8093        11711 :   tree offset_incr = NULL_TREE;
    8094        11711 :   tree offset_init = NULL_TREE;
    8095              : 
    8096        11711 :   gimple_stmt_iterator gsi;
    8097        11711 :   gassign *ass;
    8098        11711 :   gcall *call;
    8099        11711 :   gimple *stmt;
    8100        11711 :   tree expr;
    8101        11711 :   location_t loc;
    8102        11711 :   edge split, be, fte;
    8103              : 
    8104              :   /* Split the end of entry_bb to create head_bb.  */
    8105        11711 :   split = split_block (entry_bb, last_nondebug_stmt (entry_bb));
    8106        11711 :   basic_block head_bb = split->dest;
    8107        11711 :   entry_bb = split->src;
    8108              : 
    8109              :   /* Chunk setup goes at end of entry_bb, replacing the omp_for.  */
    8110        11711 :   gsi = gsi_last_nondebug_bb (entry_bb);
    8111        11711 :   gomp_for *for_stmt = as_a <gomp_for *> (gsi_stmt (gsi));
    8112        11711 :   loc = gimple_location (for_stmt);
    8113              : 
    8114        11711 :   if (gimple_in_ssa_p (cfun))
    8115              :     {
    8116          386 :       offset_init = gimple_omp_for_index (for_stmt, 0);
    8117          386 :       gcc_assert (integer_zerop (fd->loop.n1));
    8118              :       /* The SSA parallelizer does gang parallelism.  */
    8119          386 :       gwv = build_int_cst (integer_type_node, GOMP_DIM_MASK (GOMP_DIM_GANG));
    8120              :     }
    8121              : 
    8122        11711 :   if (fd->collapse > 1 || fd->tiling)
    8123              :     {
    8124         1188 :       gcc_assert (!gimple_in_ssa_p (cfun) && up);
    8125          594 :       counts = XALLOCAVEC (struct oacc_collapse, fd->collapse);
    8126          594 :       tree total = expand_oacc_collapse_init (fd, &gsi, counts, diff_type,
    8127          594 :                                               TREE_TYPE (fd->loop.n2), loc);
    8128              : 
    8129          594 :       if (SSA_VAR_P (fd->loop.n2))
    8130              :         {
    8131          107 :           total = force_gimple_operand_gsi (&gsi, total, false, NULL_TREE,
    8132              :                                             true, GSI_SAME_STMT);
    8133          107 :           ass = gimple_build_assign (fd->loop.n2, total);
    8134          107 :           gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8135              :         }
    8136              :     }
    8137              : 
    8138        11711 :   tree b = fd->loop.n1;
    8139        11711 :   tree e = fd->loop.n2;
    8140        11711 :   tree s = fd->loop.step;
    8141              : 
    8142        11711 :   b = force_gimple_operand_gsi (&gsi, b, true, NULL_TREE, true, GSI_SAME_STMT);
    8143        11711 :   e = force_gimple_operand_gsi (&gsi, e, true, NULL_TREE, true, GSI_SAME_STMT);
    8144              : 
    8145              :   /* Convert the step, avoiding possible unsigned->signed overflow.  */
    8146        11711 :   negating = !up && TYPE_UNSIGNED (TREE_TYPE (s));
    8147           32 :   if (negating)
    8148           32 :     s = fold_build1 (NEGATE_EXPR, TREE_TYPE (s), s);
    8149        11711 :   s = fold_convert (diff_type, s);
    8150        11711 :   if (negating)
    8151           32 :     s = fold_build1 (NEGATE_EXPR, diff_type, s);
    8152        11711 :   s = force_gimple_operand_gsi (&gsi, s, true, NULL_TREE, true, GSI_SAME_STMT);
    8153              : 
    8154        11711 :   if (!chunking)
    8155          386 :     chunk_size = integer_zero_node;
    8156        11711 :   expr = fold_convert (diff_type, chunk_size);
    8157        11711 :   chunk_size = force_gimple_operand_gsi (&gsi, expr, true,
    8158              :                                          NULL_TREE, true, GSI_SAME_STMT);
    8159              : 
    8160        11711 :   if (fd->tiling)
    8161              :     {
    8162              :       /* Determine the tile size and element step,
    8163              :          modify the outer loop step size.  */
    8164          177 :       tile_size = create_tmp_var (diff_type, ".tile_size");
    8165          177 :       expr = build_int_cst (diff_type, 1);
    8166          461 :       for (int ix = 0; ix < fd->collapse; ix++)
    8167          284 :         expr = fold_build2 (MULT_EXPR, diff_type, counts[ix].tile, expr);
    8168          177 :       expr = force_gimple_operand_gsi (&gsi, expr, true,
    8169              :                                        NULL_TREE, true, GSI_SAME_STMT);
    8170          177 :       ass = gimple_build_assign (tile_size, expr);
    8171          177 :       gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8172              : 
    8173          177 :       element_s = create_tmp_var (diff_type, ".element_s");
    8174          177 :       ass = gimple_build_assign (element_s, s);
    8175          177 :       gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8176              : 
    8177          177 :       expr = fold_build2 (MULT_EXPR, diff_type, s, tile_size);
    8178          177 :       s = force_gimple_operand_gsi (&gsi, expr, true,
    8179              :                                     NULL_TREE, true, GSI_SAME_STMT);
    8180              :     }
    8181              : 
    8182              :   /* Determine the range, avoiding possible unsigned->signed overflow.  */
    8183        11711 :   negating = !up && TYPE_UNSIGNED (iter_type);
    8184        23374 :   expr = fold_build2 (MINUS_EXPR, plus_type,
    8185              :                       fold_convert (plus_type, negating ? b : e),
    8186              :                       fold_convert (plus_type, negating ? e : b));
    8187        11711 :   expr = fold_convert (diff_type, expr);
    8188        11711 :   if (negating)
    8189           48 :     expr = fold_build1 (NEGATE_EXPR, diff_type, expr);
    8190        11711 :   tree range = force_gimple_operand_gsi (&gsi, expr, true,
    8191              :                                          NULL_TREE, true, GSI_SAME_STMT);
    8192              : 
    8193        11711 :   chunk_no = build_int_cst (diff_type, 0);
    8194        11711 :   if (chunking)
    8195              :     {
    8196        11325 :       gcc_assert (!gimple_in_ssa_p (cfun));
    8197              : 
    8198        11325 :       expr = chunk_no;
    8199        11325 :       chunk_max = create_tmp_var (diff_type, ".chunk_max");
    8200        11325 :       chunk_no = create_tmp_var (diff_type, ".chunk_no");
    8201              : 
    8202        11325 :       ass = gimple_build_assign (chunk_no, expr);
    8203        11325 :       gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8204              : 
    8205        11325 :       call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
    8206              :                                          build_int_cst (integer_type_node,
    8207              :                                                         IFN_GOACC_LOOP_CHUNKS),
    8208              :                                          dir, range, s, chunk_size, gwv);
    8209        11325 :       gimple_call_set_lhs (call, chunk_max);
    8210        11325 :       gimple_set_location (call, loc);
    8211        11325 :       gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    8212              :     }
    8213              :   else
    8214              :     chunk_size = chunk_no;
    8215              : 
    8216        11711 :   call = gimple_build_call_internal (IFN_GOACC_LOOP, 6,
    8217              :                                      build_int_cst (integer_type_node,
    8218              :                                                     IFN_GOACC_LOOP_STEP),
    8219              :                                      dir, range, s, chunk_size, gwv);
    8220        11711 :   gimple_call_set_lhs (call, step);
    8221        11711 :   gimple_set_location (call, loc);
    8222        11711 :   gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    8223              : 
    8224              :   /* Remove the GIMPLE_OMP_FOR.  */
    8225        11711 :   gsi_remove (&gsi, true);
    8226              : 
    8227              :   /* Fixup edges from head_bb.  */
    8228        11711 :   be = BRANCH_EDGE (head_bb);
    8229        11711 :   fte = FALLTHRU_EDGE (head_bb);
    8230        11711 :   be->flags |= EDGE_FALSE_VALUE;
    8231        11711 :   fte->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
    8232              : 
    8233        11711 :   basic_block body_bb = fte->dest;
    8234              : 
    8235        11711 :   if (gimple_in_ssa_p (cfun))
    8236              :     {
    8237          386 :       gsi = gsi_last_nondebug_bb (cont_bb);
    8238          386 :       gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
    8239              : 
    8240          386 :       offset = gimple_omp_continue_control_use (cont_stmt);
    8241          386 :       offset_incr = gimple_omp_continue_control_def (cont_stmt);
    8242              :     }
    8243              :   else
    8244              :     {
    8245        11325 :       offset = create_tmp_var (diff_type, ".offset");
    8246        11325 :       offset_init = offset_incr = offset;
    8247              :     }
    8248        11711 :   bound = create_tmp_var (TREE_TYPE (offset), ".bound");
    8249              : 
    8250              :   /* Loop offset & bound go into head_bb.  */
    8251        11711 :   gsi = gsi_start_bb (head_bb);
    8252              : 
    8253        11711 :   call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
    8254              :                                      build_int_cst (integer_type_node,
    8255              :                                                     IFN_GOACC_LOOP_OFFSET),
    8256              :                                      dir, range, s,
    8257              :                                      chunk_size, gwv, chunk_no);
    8258        11711 :   gimple_call_set_lhs (call, offset_init);
    8259        11711 :   gimple_set_location (call, loc);
    8260        11711 :   gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
    8261              : 
    8262        11711 :   call = gimple_build_call_internal (IFN_GOACC_LOOP, 7,
    8263              :                                      build_int_cst (integer_type_node,
    8264              :                                                     IFN_GOACC_LOOP_BOUND),
    8265              :                                      dir, range, s,
    8266              :                                      chunk_size, gwv, offset_init);
    8267        11711 :   gimple_call_set_lhs (call, bound);
    8268        11711 :   gimple_set_location (call, loc);
    8269        11711 :   gsi_insert_after (&gsi, call, GSI_CONTINUE_LINKING);
    8270              : 
    8271        11711 :   expr = build2 (cond_code, boolean_type_node, offset_init, bound);
    8272        11711 :   gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
    8273              :                     GSI_CONTINUE_LINKING);
    8274              : 
    8275              :   /* V assignment goes into body_bb.  */
    8276        11711 :   if (!gimple_in_ssa_p (cfun))
    8277              :     {
    8278        11325 :       gsi = gsi_start_bb (body_bb);
    8279              : 
    8280        11325 :       expr = build2 (plus_code, iter_type, b,
    8281              :                      fold_convert (plus_type, offset));
    8282        11325 :       expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
    8283              :                                        true, GSI_SAME_STMT);
    8284        11325 :       ass = gimple_build_assign (v, expr);
    8285        11325 :       gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8286              : 
    8287        11325 :       if (fd->collapse > 1 || fd->tiling)
    8288          594 :         expand_oacc_collapse_vars (fd, false, &gsi, counts, v, diff_type);
    8289              : 
    8290        11325 :       if (fd->tiling)
    8291              :         {
    8292              :           /* Determine the range of the element loop -- usually simply
    8293              :              the tile_size, but could be smaller if the final
    8294              :              iteration of the outer loop is a partial tile.  */
    8295          177 :           tree e_range = create_tmp_var (diff_type, ".e_range");
    8296              : 
    8297          177 :           expr = build2 (MIN_EXPR, diff_type,
    8298              :                          build2 (MINUS_EXPR, diff_type, bound, offset),
    8299              :                          build2 (MULT_EXPR, diff_type, tile_size,
    8300              :                                  element_s));
    8301          177 :           expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
    8302              :                                            true, GSI_SAME_STMT);
    8303          177 :           ass = gimple_build_assign (e_range, expr);
    8304          177 :           gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8305              : 
    8306              :           /* Determine bound, offset & step of inner loop. */
    8307          177 :           e_bound = create_tmp_var (diff_type, ".e_bound");
    8308          177 :           e_offset = create_tmp_var (diff_type, ".e_offset");
    8309          177 :           e_step = create_tmp_var (diff_type, ".e_step");
    8310              : 
    8311              :           /* Mark these as element loops.  */
    8312          177 :           tree t, e_gwv = integer_minus_one_node;
    8313          177 :           tree chunk = build_int_cst (diff_type, 0); /* Never chunked.  */
    8314              : 
    8315          177 :           t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_OFFSET);
    8316          177 :           call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
    8317              :                                              element_s, chunk, e_gwv, chunk);
    8318          177 :           gimple_call_set_lhs (call, e_offset);
    8319          177 :           gimple_set_location (call, loc);
    8320          177 :           gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    8321              : 
    8322          177 :           t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_BOUND);
    8323          177 :           call = gimple_build_call_internal (IFN_GOACC_LOOP, 7, t, dir, e_range,
    8324              :                                              element_s, chunk, e_gwv, e_offset);
    8325          177 :           gimple_call_set_lhs (call, e_bound);
    8326          177 :           gimple_set_location (call, loc);
    8327          177 :           gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    8328              : 
    8329          177 :           t = build_int_cst (integer_type_node, IFN_GOACC_LOOP_STEP);
    8330          177 :           call = gimple_build_call_internal (IFN_GOACC_LOOP, 6, t, dir, e_range,
    8331              :                                              element_s, chunk, e_gwv);
    8332          177 :           gimple_call_set_lhs (call, e_step);
    8333          177 :           gimple_set_location (call, loc);
    8334          177 :           gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    8335              : 
    8336              :           /* Add test and split block.  */
    8337          177 :           expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
    8338          177 :           stmt = gimple_build_cond_empty (expr);
    8339          177 :           gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    8340          177 :           split = split_block (body_bb, stmt);
    8341          177 :           elem_body_bb = split->dest;
    8342          177 :           if (cont_bb == body_bb)
    8343          146 :             cont_bb = elem_body_bb;
    8344          177 :           body_bb = split->src;
    8345              : 
    8346          177 :           split->flags ^= EDGE_FALLTHRU | EDGE_TRUE_VALUE;
    8347              : 
    8348              :           /* Add a dummy exit for the tiled block when cont_bb is missing.  */
    8349          177 :           if (cont_bb == NULL)
    8350              :             {
    8351            5 :               edge e = make_edge (body_bb, exit_bb, EDGE_FALSE_VALUE);
    8352            5 :               e->probability = profile_probability::even ();
    8353            5 :               split->probability = profile_probability::even ();
    8354              :             }
    8355              : 
    8356              :           /* Initialize the user's loop vars.  */
    8357          177 :           gsi = gsi_start_bb (elem_body_bb);
    8358          177 :           expand_oacc_collapse_vars (fd, true, &gsi, counts, e_offset,
    8359              :                                      diff_type);
    8360              :         }
    8361              :     }
    8362              : 
    8363              :   /* Loop increment goes into cont_bb.  If this is not a loop, we
    8364              :      will have spawned threads as if it was, and each one will
    8365              :      execute one iteration.  The specification is not explicit about
    8366              :      whether such constructs are ill-formed or not, and they can
    8367              :      occur, especially when noreturn routines are involved.  */
    8368        11711 :   if (cont_bb)
    8369              :     {
    8370        11672 :       gsi = gsi_last_nondebug_bb (cont_bb);
    8371        11672 :       gomp_continue *cont_stmt = as_a <gomp_continue *> (gsi_stmt (gsi));
    8372        11672 :       loc = gimple_location (cont_stmt);
    8373              : 
    8374        11672 :       if (fd->tiling)
    8375              :         {
    8376              :           /* Insert element loop increment and test.  */
    8377          172 :           expr = build2 (PLUS_EXPR, diff_type, e_offset, e_step);
    8378          172 :           expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
    8379              :                                            true, GSI_SAME_STMT);
    8380          172 :           ass = gimple_build_assign (e_offset, expr);
    8381          172 :           gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8382          172 :           expr = build2 (cond_code, boolean_type_node, e_offset, e_bound);
    8383              : 
    8384          172 :           stmt = gimple_build_cond_empty (expr);
    8385          172 :           gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    8386          172 :           split = split_block (cont_bb, stmt);
    8387          172 :           elem_cont_bb = split->src;
    8388          172 :           cont_bb = split->dest;
    8389              : 
    8390          172 :           split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
    8391          172 :           split->probability = profile_probability::unlikely ().guessed ();
    8392          172 :           edge latch_edge
    8393          172 :             = make_edge (elem_cont_bb, elem_body_bb, EDGE_TRUE_VALUE);
    8394          172 :           latch_edge->probability = profile_probability::likely ().guessed ();
    8395              : 
    8396          172 :           edge skip_edge = make_edge (body_bb, cont_bb, EDGE_FALSE_VALUE);
    8397          172 :           skip_edge->probability = profile_probability::unlikely ().guessed ();
    8398          172 :           edge loop_entry_edge = EDGE_SUCC (body_bb, 1 - skip_edge->dest_idx);
    8399          172 :           loop_entry_edge->probability
    8400          172 :             = profile_probability::likely ().guessed ();
    8401              : 
    8402          172 :           gsi = gsi_for_stmt (cont_stmt);
    8403              :         }
    8404              : 
    8405              :       /* Increment offset.  */
    8406        11672 :       if (gimple_in_ssa_p (cfun))
    8407          386 :         expr = build2 (plus_code, iter_type, offset,
    8408              :                        fold_convert (plus_type, step));
    8409              :       else
    8410        11286 :         expr = build2 (PLUS_EXPR, diff_type, offset, step);
    8411        11672 :       expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
    8412              :                                        true, GSI_SAME_STMT);
    8413        11672 :       ass = gimple_build_assign (offset_incr, expr);
    8414        11672 :       gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8415        11672 :       expr = build2 (cond_code, boolean_type_node, offset_incr, bound);
    8416        11672 :       gsi_insert_before (&gsi, gimple_build_cond_empty (expr), GSI_SAME_STMT);
    8417              : 
    8418              :       /*  Remove the GIMPLE_OMP_CONTINUE.  */
    8419        11672 :       gsi_remove (&gsi, true);
    8420              : 
    8421              :       /* Fixup edges from cont_bb.  */
    8422        11672 :       be = BRANCH_EDGE (cont_bb);
    8423        11672 :       fte = FALLTHRU_EDGE (cont_bb);
    8424        11672 :       be->flags |= EDGE_TRUE_VALUE;
    8425        11672 :       fte->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
    8426              : 
    8427        11672 :       if (chunking)
    8428              :         {
    8429              :           /* Split the beginning of exit_bb to make bottom_bb.  We
    8430              :              need to insert a nop at the start, because splitting is
    8431              :              after a stmt, not before.  */
    8432        11286 :           gsi = gsi_start_bb (exit_bb);
    8433        11286 :           stmt = gimple_build_nop ();
    8434        11286 :           gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    8435        11286 :           split = split_block (exit_bb, stmt);
    8436        11286 :           bottom_bb = split->src;
    8437        11286 :           exit_bb = split->dest;
    8438        11286 :           gsi = gsi_last_bb (bottom_bb);
    8439              : 
    8440              :           /* Chunk increment and test goes into bottom_bb.  */
    8441        11286 :           expr = build2 (PLUS_EXPR, diff_type, chunk_no,
    8442              :                          build_int_cst (diff_type, 1));
    8443        11286 :           ass = gimple_build_assign (chunk_no, expr);
    8444        11286 :           gsi_insert_after (&gsi, ass, GSI_CONTINUE_LINKING);
    8445              : 
    8446              :           /* Chunk test at end of bottom_bb.  */
    8447        11286 :           expr = build2 (LT_EXPR, boolean_type_node, chunk_no, chunk_max);
    8448        11286 :           gsi_insert_after (&gsi, gimple_build_cond_empty (expr),
    8449              :                             GSI_CONTINUE_LINKING);
    8450              : 
    8451              :           /* Fixup edges from bottom_bb.  */
    8452        11286 :           split->flags ^= EDGE_FALLTHRU | EDGE_FALSE_VALUE;
    8453        11286 :           split->probability = profile_probability::unlikely ().guessed ();
    8454        11286 :           edge latch_edge = make_edge (bottom_bb, head_bb, EDGE_TRUE_VALUE);
    8455        11286 :           latch_edge->probability = profile_probability::likely ().guessed ();
    8456              :         }
    8457              :     }
    8458              : 
    8459        11711 :   gsi = gsi_last_nondebug_bb (exit_bb);
    8460        11711 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
    8461        11711 :   loc = gimple_location (gsi_stmt (gsi));
    8462              : 
    8463        11711 :   if (!gimple_in_ssa_p (cfun))
    8464              :     {
    8465              :       /* Insert the final value of V, in case it is live.  This is the
    8466              :          value for the only thread that survives past the join.  */
    8467        11325 :       expr = fold_build2 (MINUS_EXPR, diff_type, range, dir);
    8468        11325 :       expr = fold_build2 (PLUS_EXPR, diff_type, expr, s);
    8469        11325 :       expr = fold_build2 (TRUNC_DIV_EXPR, diff_type, expr, s);
    8470        11325 :       expr = fold_build2 (MULT_EXPR, diff_type, expr, s);
    8471        11325 :       expr = build2 (plus_code, iter_type, b, fold_convert (plus_type, expr));
    8472        11325 :       expr = force_gimple_operand_gsi (&gsi, expr, false, NULL_TREE,
    8473              :                                        true, GSI_SAME_STMT);
    8474        11325 :       ass = gimple_build_assign (v, expr);
    8475        11325 :       gsi_insert_before (&gsi, ass, GSI_SAME_STMT);
    8476              :     }
    8477              : 
    8478              :   /* Remove the OMP_RETURN.  */
    8479        11711 :   gsi_remove (&gsi, true);
    8480              : 
    8481        11711 :   if (cont_bb)
    8482              :     {
    8483              :       /* We now have one, two or three nested loops.  Update the loop
    8484              :          structures.  */
    8485        11672 :       class loop *parent = entry_bb->loop_father;
    8486        11672 :       class loop *body = body_bb->loop_father;
    8487              : 
    8488        11672 :       if (chunking)
    8489              :         {
    8490        11286 :           class loop *chunk_loop = alloc_loop ();
    8491        11286 :           chunk_loop->header = head_bb;
    8492        11286 :           chunk_loop->latch = bottom_bb;
    8493        11286 :           add_loop (chunk_loop, parent);
    8494        11286 :           parent = chunk_loop;
    8495              :         }
    8496          386 :       else if (parent != body)
    8497              :         {
    8498          386 :           gcc_assert (body->header == body_bb);
    8499          386 :           gcc_assert (body->latch == cont_bb
    8500              :                       || single_pred (body->latch) == cont_bb);
    8501              :           parent = NULL;
    8502              :         }
    8503              : 
    8504        11286 :       if (parent)
    8505              :         {
    8506        11286 :           class loop *body_loop = alloc_loop ();
    8507        11286 :           body_loop->header = body_bb;
    8508        11286 :           body_loop->latch = cont_bb;
    8509        11286 :           add_loop (body_loop, parent);
    8510              : 
    8511        11286 :           if (fd->tiling)
    8512              :             {
    8513              :               /* Insert tiling's element loop.  */
    8514          172 :               class loop *inner_loop = alloc_loop ();
    8515          172 :               inner_loop->header = elem_body_bb;
    8516          172 :               inner_loop->latch = elem_cont_bb;
    8517          172 :               add_loop (inner_loop, body_loop);
    8518              :             }
    8519              :         }
    8520              :     }
    8521        11711 : }
    8522              : 
    8523              : /* Expand the OMP loop defined by REGION.  */
    8524              : 
    8525              : static void
    8526        50193 : expand_omp_for (struct omp_region *region, gimple *inner_stmt)
    8527              : {
    8528        50193 :   struct omp_for_data fd;
    8529        50193 :   struct omp_for_data_loop *loops;
    8530              : 
    8531        50193 :   loops = XALLOCAVEC (struct omp_for_data_loop,
    8532              :                       gimple_omp_for_collapse
    8533              :                         (last_nondebug_stmt (region->entry)));
    8534        50193 :   omp_extract_for_data (as_a <gomp_for *> (last_nondebug_stmt (region->entry)),
    8535              :                         &fd, loops);
    8536        50193 :   region->sched_kind = fd.sched_kind;
    8537        50193 :   region->sched_modifiers = fd.sched_modifiers;
    8538        50193 :   region->has_lastprivate_conditional = fd.lastprivate_conditional != 0;
    8539        50193 :   if (fd.non_rect && !gimple_omp_for_combined_into_p (fd.for_stmt))
    8540              :     {
    8541         2362 :       for (int i = fd.first_nonrect; i <= fd.last_nonrect; i++)
    8542         1656 :         if ((loops[i].m1 || loops[i].m2)
    8543          737 :             && (loops[i].m1 == NULL_TREE
    8544          512 :                 || TREE_CODE (loops[i].m1) == INTEGER_CST)
    8545          618 :             && (loops[i].m2 == NULL_TREE
    8546          343 :                 || TREE_CODE (loops[i].m2) == INTEGER_CST)
    8547          602 :             && TREE_CODE (loops[i].step) == INTEGER_CST
    8548          592 :             && TREE_CODE (loops[i - loops[i].outer].step) == INTEGER_CST)
    8549              :           {
    8550          592 :             tree t;
    8551          592 :             tree itype = TREE_TYPE (loops[i].v);
    8552          592 :             if (loops[i].m1 && loops[i].m2)
    8553          108 :               t = fold_build2 (MINUS_EXPR, itype, loops[i].m2, loops[i].m1);
    8554          484 :             else if (loops[i].m1)
    8555          273 :               t = fold_build1 (NEGATE_EXPR, itype, loops[i].m1);
    8556              :             else
    8557              :               t = loops[i].m2;
    8558          592 :             t = fold_build2 (MULT_EXPR, itype, t,
    8559              :                              fold_convert (itype,
    8560              :                                            loops[i - loops[i].outer].step));
    8561          592 :             if (TYPE_UNSIGNED (itype) && loops[i].cond_code == GT_EXPR)
    8562            3 :               t = fold_build2 (TRUNC_MOD_EXPR, itype,
    8563              :                                fold_build1 (NEGATE_EXPR, itype, t),
    8564              :                                fold_build1 (NEGATE_EXPR, itype,
    8565              :                                             fold_convert (itype,
    8566              :                                                           loops[i].step)));
    8567              :             else
    8568          589 :               t = fold_build2 (TRUNC_MOD_EXPR, itype, t,
    8569              :                                fold_convert (itype, loops[i].step));
    8570          592 :             if (integer_nonzerop (t))
    8571            8 :               error_at (gimple_location (fd.for_stmt),
    8572              :                         "invalid OpenMP non-rectangular loop step; "
    8573              :                         "%<(%E - %E) * %E%> is not a multiple of loop %d "
    8574              :                         "step %qE",
    8575            8 :                         loops[i].m2 ? loops[i].m2 : integer_zero_node,
    8576            8 :                         loops[i].m1 ? loops[i].m1 : integer_zero_node,
    8577            8 :                         loops[i - loops[i].outer].step, i + 1,
    8578              :                         loops[i].step);
    8579              :           }
    8580              :     }
    8581              : 
    8582        50193 :   gcc_assert (EDGE_COUNT (region->entry->succs) == 2);
    8583        50193 :   BRANCH_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
    8584        50193 :   FALLTHRU_EDGE (region->entry)->flags &= ~EDGE_ABNORMAL;
    8585        50193 :   if (region->cont)
    8586              :     {
    8587        47906 :       gcc_assert (EDGE_COUNT (region->cont->succs) == 2);
    8588        47906 :       BRANCH_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
    8589        47906 :       FALLTHRU_EDGE (region->cont)->flags &= ~EDGE_ABNORMAL;
    8590              :     }
    8591              :   else
    8592              :     /* If there isn't a continue then this is a degenerate case where
    8593              :        the introduction of abnormal edges during lowering will prevent
    8594              :        original loops from being detected.  Fix that up.  */
    8595         2287 :     loops_state_set (LOOPS_NEED_FIXUP);
    8596              : 
    8597        50193 :   if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_SIMD)
    8598        10428 :     expand_omp_simd (region, &fd);
    8599        39765 :   else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_OACC_LOOP)
    8600              :     {
    8601        11711 :       gcc_assert (!inner_stmt && !fd.non_rect);
    8602        11711 :       expand_oacc_for (region, &fd);
    8603              :     }
    8604        28054 :   else if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_TASKLOOP)
    8605              :     {
    8606         2674 :       if (gimple_omp_for_combined_into_p (fd.for_stmt))
    8607         1337 :         expand_omp_taskloop_for_inner (region, &fd, inner_stmt);
    8608              :       else
    8609         1337 :         expand_omp_taskloop_for_outer (region, &fd, inner_stmt);
    8610              :     }
    8611        25380 :   else if (fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC
    8612        21696 :            && !fd.have_ordered)
    8613              :     {
    8614        21244 :       if (fd.chunk_size == NULL)
    8615        15363 :         expand_omp_for_static_nochunk (region, &fd, inner_stmt);
    8616              :       else
    8617         5881 :         expand_omp_for_static_chunk (region, &fd, inner_stmt);
    8618              :     }
    8619              :   else
    8620              :     {
    8621         4136 :       int fn_index, start_ix, next_ix;
    8622         4136 :       unsigned HOST_WIDE_INT sched = 0;
    8623         4136 :       tree sched_arg = NULL_TREE;
    8624              : 
    8625         4136 :       gcc_assert (gimple_omp_for_kind (fd.for_stmt)
    8626              :                   == GF_OMP_FOR_KIND_FOR && !fd.non_rect);
    8627         4136 :       if (fd.chunk_size == NULL
    8628         1665 :           && fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC)
    8629            0 :         fd.chunk_size = integer_zero_node;
    8630         4136 :       switch (fd.sched_kind)
    8631              :         {
    8632         1665 :         case OMP_CLAUSE_SCHEDULE_RUNTIME:
    8633         1665 :           if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_NONMONOTONIC) != 0
    8634           28 :               && fd.lastprivate_conditional == 0)
    8635              :             {
    8636           28 :               gcc_assert (!fd.have_ordered);
    8637              :               fn_index = 6;
    8638              :               sched = 4;
    8639              :             }
    8640         1637 :           else if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
    8641         1605 :                    && !fd.have_ordered
    8642         1561 :                    && fd.lastprivate_conditional == 0)
    8643              :             fn_index = 7;
    8644              :           else
    8645              :             {
    8646           87 :               fn_index = 3;
    8647           87 :               sched = (HOST_WIDE_INT_1U << 31);
    8648              :             }
    8649              :           break;
    8650         2019 :         case OMP_CLAUSE_SCHEDULE_DYNAMIC:
    8651         2019 :         case OMP_CLAUSE_SCHEDULE_GUIDED:
    8652         2019 :           if ((fd.sched_modifiers & OMP_CLAUSE_SCHEDULE_MONOTONIC) == 0
    8653         1963 :               && !fd.have_ordered
    8654         1800 :               && fd.lastprivate_conditional == 0)
    8655              :             {
    8656         1773 :               fn_index = 3 + fd.sched_kind;
    8657         1773 :               sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
    8658              :               break;
    8659              :             }
    8660          246 :           fn_index = fd.sched_kind;
    8661          246 :           sched = (fd.sched_kind == OMP_CLAUSE_SCHEDULE_GUIDED) + 2;
    8662          246 :           sched += (HOST_WIDE_INT_1U << 31);
    8663          246 :           break;
    8664          452 :         case OMP_CLAUSE_SCHEDULE_STATIC:
    8665          452 :           gcc_assert (fd.have_ordered);
    8666              :           fn_index = 0;
    8667              :           sched = (HOST_WIDE_INT_1U << 31) + 1;
    8668              :           break;
    8669            0 :         default:
    8670            0 :           gcc_unreachable ();
    8671              :         }
    8672         4136 :       if (!fd.ordered)
    8673         3801 :         fn_index += fd.have_ordered * 8;
    8674         4136 :       if (fd.ordered)
    8675          335 :         start_ix = ((int)BUILT_IN_GOMP_LOOP_DOACROSS_STATIC_START) + fn_index;
    8676              :       else
    8677         3801 :         start_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_START) + fn_index;
    8678         4136 :       next_ix = ((int)BUILT_IN_GOMP_LOOP_STATIC_NEXT) + fn_index;
    8679         4136 :       if (fd.have_reductemp || fd.have_pointer_condtemp)
    8680              :         {
    8681          169 :           if (fd.ordered)
    8682              :             start_ix = (int)BUILT_IN_GOMP_LOOP_DOACROSS_START;
    8683          133 :           else if (fd.have_ordered)
    8684              :             start_ix = (int)BUILT_IN_GOMP_LOOP_ORDERED_START;
    8685              :           else
    8686           95 :             start_ix = (int)BUILT_IN_GOMP_LOOP_START;
    8687          169 :           sched_arg = build_int_cstu (long_integer_type_node, sched);
    8688          169 :           if (!fd.chunk_size)
    8689           38 :             fd.chunk_size = integer_zero_node;
    8690              :         }
    8691         4136 :       if (fd.iter_type == long_long_unsigned_type_node)
    8692              :         {
    8693          778 :           start_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_START
    8694              :                         - (int)BUILT_IN_GOMP_LOOP_STATIC_START);
    8695          778 :           next_ix += ((int)BUILT_IN_GOMP_LOOP_ULL_STATIC_NEXT
    8696              :                       - (int)BUILT_IN_GOMP_LOOP_STATIC_NEXT);
    8697              :         }
    8698         4136 :       expand_omp_for_generic (region, &fd, (enum built_in_function) start_ix,
    8699              :                               (enum built_in_function) next_ix, sched_arg,
    8700              :                               inner_stmt);
    8701              :     }
    8702        50193 : }
    8703              : 
    8704              : /* Expand code for an OpenMP sections directive.  In pseudo code, we generate
    8705              : 
    8706              :         v = GOMP_sections_start (n);
    8707              :     L0:
    8708              :         switch (v)
    8709              :           {
    8710              :           case 0:
    8711              :             goto L2;
    8712              :           case 1:
    8713              :             section 1;
    8714              :             goto L1;
    8715              :           case 2:
    8716              :             ...
    8717              :           case n:
    8718              :             ...
    8719              :           default:
    8720              :             abort ();
    8721              :           }
    8722              :     L1:
    8723              :         v = GOMP_sections_next ();
    8724              :         goto L0;
    8725              :     L2:
    8726              :         reduction;
    8727              : 
    8728              :     If this is a combined parallel sections, replace the call to
    8729              :     GOMP_sections_start with call to GOMP_sections_next.  */
    8730              : 
    8731              : static void
    8732          386 : expand_omp_sections (struct omp_region *region)
    8733              : {
    8734          386 :   tree t, u, vin = NULL, vmain, vnext, l2;
    8735          386 :   unsigned len;
    8736          386 :   basic_block entry_bb, l0_bb, l1_bb, l2_bb, default_bb;
    8737          386 :   gimple_stmt_iterator si, switch_si;
    8738          386 :   gomp_sections *sections_stmt;
    8739          386 :   gimple *stmt;
    8740          386 :   gomp_continue *cont;
    8741          386 :   edge_iterator ei;
    8742          386 :   edge e;
    8743          386 :   struct omp_region *inner;
    8744          386 :   unsigned i, casei;
    8745          386 :   bool exit_reachable = region->cont != NULL;
    8746              : 
    8747          386 :   gcc_assert (region->exit != NULL);
    8748          386 :   entry_bb = region->entry;
    8749          386 :   l0_bb = single_succ (entry_bb);
    8750          386 :   l1_bb = region->cont;
    8751          386 :   l2_bb = region->exit;
    8752          735 :   if (single_pred_p (l2_bb) && single_pred (l2_bb) == l0_bb)
    8753          322 :     l2 = gimple_block_label (l2_bb);
    8754              :   else
    8755              :     {
    8756              :       /* This can happen if there are reductions.  */
    8757           64 :       len = EDGE_COUNT (l0_bb->succs);
    8758           64 :       gcc_assert (len > 0);
    8759           64 :       e = EDGE_SUCC (l0_bb, len - 1);
    8760           64 :       si = gsi_last_nondebug_bb (e->dest);
    8761           64 :       l2 = NULL_TREE;
    8762           64 :       if (gsi_end_p (si)
    8763           64 :           || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
    8764           64 :         l2 = gimple_block_label (e->dest);
    8765              :       else
    8766            0 :         FOR_EACH_EDGE (e, ei, l0_bb->succs)
    8767              :           {
    8768            0 :             si = gsi_last_nondebug_bb (e->dest);
    8769            0 :             if (gsi_end_p (si)
    8770            0 :                 || gimple_code (gsi_stmt (si)) != GIMPLE_OMP_SECTION)
    8771              :               {
    8772            0 :                 l2 = gimple_block_label (e->dest);
    8773            0 :                 break;
    8774              :               }
    8775              :           }
    8776              :     }
    8777          386 :   if (exit_reachable)
    8778          340 :     default_bb = create_empty_bb (l1_bb->prev_bb);
    8779              :   else
    8780           46 :     default_bb = create_empty_bb (l0_bb);
    8781              : 
    8782              :   /* We will build a switch() with enough cases for all the
    8783              :      GIMPLE_OMP_SECTION regions, a '0' case to handle the end of more work
    8784              :      and a default case to abort if something goes wrong.  */
    8785          386 :   len = EDGE_COUNT (l0_bb->succs);
    8786              : 
    8787              :   /* Use vec::quick_push on label_vec throughout, since we know the size
    8788              :      in advance.  */
    8789          386 :   auto_vec<tree> label_vec (len);
    8790              : 
    8791              :   /* The call to GOMP_sections_start goes in ENTRY_BB, replacing the
    8792              :      GIMPLE_OMP_SECTIONS statement.  */
    8793          386 :   si = gsi_last_nondebug_bb (entry_bb);
    8794          386 :   sections_stmt = as_a <gomp_sections *> (gsi_stmt (si));
    8795          386 :   gcc_assert (gimple_code (sections_stmt) == GIMPLE_OMP_SECTIONS);
    8796          386 :   vin = gimple_omp_sections_control (sections_stmt);
    8797          386 :   tree clauses = gimple_omp_sections_clauses (sections_stmt);
    8798          386 :   tree reductmp = omp_find_clause (clauses, OMP_CLAUSE__REDUCTEMP_);
    8799          386 :   tree condtmp = omp_find_clause (clauses, OMP_CLAUSE__CONDTEMP_);
    8800          386 :   tree cond_var = NULL_TREE;
    8801          386 :   if (reductmp || condtmp)
    8802              :     {
    8803           18 :       tree reductions = null_pointer_node, mem = null_pointer_node;
    8804           18 :       tree memv = NULL_TREE, condtemp = NULL_TREE;
    8805           18 :       gimple_stmt_iterator gsi = gsi_none ();
    8806           18 :       gimple *g = NULL;
    8807           18 :       if (reductmp)
    8808              :         {
    8809            8 :           reductions = OMP_CLAUSE_DECL (reductmp);
    8810            8 :           gcc_assert (TREE_CODE (reductions) == SSA_NAME);
    8811            8 :           g = SSA_NAME_DEF_STMT (reductions);
    8812            8 :           reductions = gimple_assign_rhs1 (g);
    8813            8 :           OMP_CLAUSE_DECL (reductmp) = reductions;
    8814            8 :           gsi = gsi_for_stmt (g);
    8815              :         }
    8816              :       else
    8817           10 :         gsi = si;
    8818           18 :       if (condtmp)
    8819              :         {
    8820           12 :           condtemp = OMP_CLAUSE_DECL (condtmp);
    8821           12 :           tree c = omp_find_clause (OMP_CLAUSE_CHAIN (condtmp),
    8822              :                                     OMP_CLAUSE__CONDTEMP_);
    8823           12 :           cond_var = OMP_CLAUSE_DECL (c);
    8824           12 :           tree type = TREE_TYPE (condtemp);
    8825           12 :           memv = create_tmp_var (type);
    8826           12 :           TREE_ADDRESSABLE (memv) = 1;
    8827           12 :           unsigned cnt = 0;
    8828           74 :           for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    8829           62 :             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
    8830           62 :                 && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
    8831           24 :               ++cnt;
    8832           12 :           unsigned HOST_WIDE_INT sz
    8833           12 :             = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))) * cnt;
    8834           12 :           expand_omp_build_assign (&gsi, memv, build_int_cst (type, sz),
    8835              :                                    false);
    8836           12 :           mem = build_fold_addr_expr (memv);
    8837              :         }
    8838           18 :       t = build_int_cst (unsigned_type_node, len - 1);
    8839           18 :       u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS2_START);
    8840           18 :       stmt = gimple_build_call (u, 3, t, reductions, mem);
    8841           18 :       gimple_call_set_lhs (stmt, vin);
    8842           18 :       gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    8843           18 :       if (condtmp)
    8844              :         {
    8845           12 :           expand_omp_build_assign (&gsi, condtemp, memv, false);
    8846           12 :           tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
    8847           12 :                            vin, build_one_cst (TREE_TYPE (cond_var)));
    8848           12 :           expand_omp_build_assign (&gsi, cond_var, t, false);
    8849              :         }
    8850           18 :       if (reductmp)
    8851              :         {
    8852            8 :           gsi_remove (&gsi, true);
    8853            8 :           release_ssa_name (gimple_assign_lhs (g));
    8854              :         }
    8855              :     }
    8856          368 :   else if (!is_combined_parallel (region))
    8857              :     {
    8858              :       /* If we are not inside a combined parallel+sections region,
    8859              :          call GOMP_sections_start.  */
    8860          252 :       t = build_int_cst (unsigned_type_node, len - 1);
    8861          252 :       u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_START);
    8862          252 :       stmt = gimple_build_call (u, 1, t);
    8863              :     }
    8864              :   else
    8865              :     {
    8866              :       /* Otherwise, call GOMP_sections_next.  */
    8867          116 :       u = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
    8868          116 :       stmt = gimple_build_call (u, 0);
    8869              :     }
    8870          386 :   if (!reductmp && !condtmp)
    8871              :     {
    8872          368 :       gimple_call_set_lhs (stmt, vin);
    8873          368 :       gsi_insert_after (&si, stmt, GSI_SAME_STMT);
    8874              :     }
    8875          386 :   gsi_remove (&si, true);
    8876              : 
    8877              :   /* The switch() statement replacing GIMPLE_OMP_SECTIONS_SWITCH goes in
    8878              :      L0_BB.  */
    8879          386 :   switch_si = gsi_last_nondebug_bb (l0_bb);
    8880          386 :   gcc_assert (gimple_code (gsi_stmt (switch_si)) == GIMPLE_OMP_SECTIONS_SWITCH);
    8881          386 :   if (exit_reachable)
    8882              :     {
    8883          340 :       cont = as_a <gomp_continue *> (last_nondebug_stmt (l1_bb));
    8884          340 :       gcc_assert (gimple_code (cont) == GIMPLE_OMP_CONTINUE);
    8885          340 :       vmain = gimple_omp_continue_control_use (cont);
    8886          340 :       vnext = gimple_omp_continue_control_def (cont);
    8887              :     }
    8888              :   else
    8889              :     {
    8890              :       vmain = vin;
    8891              :       vnext = NULL_TREE;
    8892              :     }
    8893              : 
    8894          386 :   t = build_case_label (build_int_cst (unsigned_type_node, 0), NULL, l2);
    8895          386 :   label_vec.quick_push (t);
    8896          386 :   i = 1;
    8897              : 
    8898              :   /* Convert each GIMPLE_OMP_SECTION into a CASE_LABEL_EXPR.  */
    8899          386 :   for (inner = region->inner, casei = 1;
    8900         1276 :        inner;
    8901          890 :        inner = inner->next, i++, casei++)
    8902              :     {
    8903          890 :       basic_block s_entry_bb, s_exit_bb;
    8904              : 
    8905              :       /* Skip optional reduction region.  */
    8906          890 :       if (inner->type == GIMPLE_OMP_ATOMIC_LOAD)
    8907              :         {
    8908           27 :           --i;
    8909           27 :           --casei;
    8910           27 :           continue;
    8911              :         }
    8912              : 
    8913          863 :       s_entry_bb = inner->entry;
    8914          863 :       s_exit_bb = inner->exit;
    8915              : 
    8916          863 :       t = gimple_block_label (s_entry_bb);
    8917          863 :       u = build_int_cst (unsigned_type_node, casei);
    8918          863 :       u = build_case_label (u, NULL, t);
    8919          863 :       label_vec.quick_push (u);
    8920              : 
    8921          863 :       si = gsi_last_nondebug_bb (s_entry_bb);
    8922          863 :       gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SECTION);
    8923          863 :       gcc_assert (i < len || gimple_omp_section_last_p (gsi_stmt (si)));
    8924          863 :       gsi_remove (&si, true);
    8925          863 :       single_succ_edge (s_entry_bb)->flags = EDGE_FALLTHRU;
    8926              : 
    8927          863 :       if (s_exit_bb == NULL)
    8928          122 :         continue;
    8929              : 
    8930          741 :       si = gsi_last_nondebug_bb (s_exit_bb);
    8931          741 :       gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
    8932          741 :       gsi_remove (&si, true);
    8933              : 
    8934          741 :       single_succ_edge (s_exit_bb)->flags = EDGE_FALLTHRU;
    8935              :     }
    8936              : 
    8937              :   /* Error handling code goes in DEFAULT_BB.  */
    8938          386 :   t = gimple_block_label (default_bb);
    8939          386 :   u = build_case_label (NULL, NULL, t);
    8940          386 :   make_edge (l0_bb, default_bb, 0);
    8941          386 :   add_bb_to_loop (default_bb, current_loops->tree_root);
    8942              : 
    8943          386 :   stmt = gimple_build_switch (vmain, u, label_vec);
    8944          386 :   gsi_insert_after (&switch_si, stmt, GSI_SAME_STMT);
    8945          386 :   gsi_remove (&switch_si, true);
    8946              : 
    8947          386 :   si = gsi_start_bb (default_bb);
    8948          386 :   stmt = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
    8949          386 :   gsi_insert_after (&si, stmt, GSI_CONTINUE_LINKING);
    8950              : 
    8951          386 :   if (exit_reachable)
    8952              :     {
    8953          340 :       tree bfn_decl;
    8954              : 
    8955              :       /* Code to get the next section goes in L1_BB.  */
    8956          340 :       si = gsi_last_nondebug_bb (l1_bb);
    8957          340 :       gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CONTINUE);
    8958              : 
    8959          340 :       bfn_decl = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_NEXT);
    8960          340 :       stmt = gimple_build_call (bfn_decl, 0);
    8961          340 :       gimple_call_set_lhs (stmt, vnext);
    8962          340 :       gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    8963          340 :       if (cond_var)
    8964              :         {
    8965           12 :           tree t = build2 (PLUS_EXPR, TREE_TYPE (cond_var),
    8966           12 :                            vnext, build_one_cst (TREE_TYPE (cond_var)));
    8967           12 :           expand_omp_build_assign (&si, cond_var, t, false);
    8968              :         }
    8969          340 :       gsi_remove (&si, true);
    8970              : 
    8971          340 :       single_succ_edge (l1_bb)->flags = EDGE_FALLTHRU;
    8972              :     }
    8973              : 
    8974              :   /* Cleanup function replaces GIMPLE_OMP_RETURN in EXIT_BB.  */
    8975          386 :   si = gsi_last_nondebug_bb (l2_bb);
    8976          386 :   if (gimple_omp_return_nowait_p (gsi_stmt (si)))
    8977          255 :     t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_NOWAIT);
    8978          131 :   else if (gimple_omp_return_lhs (gsi_stmt (si)))
    8979            0 :     t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END_CANCEL);
    8980              :   else
    8981          131 :     t = builtin_decl_explicit (BUILT_IN_GOMP_SECTIONS_END);
    8982          386 :   stmt = gimple_build_call (t, 0);
    8983          386 :   if (gimple_omp_return_lhs (gsi_stmt (si)))
    8984            0 :     gimple_call_set_lhs (stmt, gimple_omp_return_lhs (gsi_stmt (si)));
    8985          386 :   gsi_insert_after (&si, stmt, GSI_SAME_STMT);
    8986          386 :   gsi_remove (&si, true);
    8987              : 
    8988          386 :   set_immediate_dominator (CDI_DOMINATORS, default_bb, l0_bb);
    8989          386 : }
    8990              : 
    8991              : /* Expand code for an OpenMP single or scope directive.  We've already expanded
    8992              :    much of the code, here we simply place the GOMP_barrier call.  */
    8993              : 
    8994              : static void
    8995         1307 : expand_omp_single (struct omp_region *region)
    8996              : {
    8997         1307 :   basic_block entry_bb, exit_bb;
    8998         1307 :   gimple_stmt_iterator si;
    8999              : 
    9000         1307 :   entry_bb = region->entry;
    9001         1307 :   exit_bb = region->exit;
    9002              : 
    9003         1307 :   si = gsi_last_nondebug_bb (entry_bb);
    9004         1307 :   enum gimple_code code = gimple_code (gsi_stmt (si));
    9005         1307 :   gcc_assert (code == GIMPLE_OMP_SINGLE || code == GIMPLE_OMP_SCOPE);
    9006         1307 :   gsi_remove (&si, true);
    9007         1307 :   single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
    9008              : 
    9009         1307 :   if (exit_bb == NULL)
    9010              :     {
    9011            8 :       gcc_assert (code == GIMPLE_OMP_SCOPE);
    9012            8 :       return;
    9013              :     }
    9014              : 
    9015         1299 :   si = gsi_last_nondebug_bb (exit_bb);
    9016         1299 :   if (!gimple_omp_return_nowait_p (gsi_stmt (si)))
    9017              :     {
    9018         1009 :       tree t = gimple_omp_return_lhs (gsi_stmt (si));
    9019         1009 :       gsi_insert_after (&si,
    9020              :                         omp_build_barrier (t, GOMP_BARRIER_IMPLICIT_WORKSHARE),
    9021              :                         GSI_SAME_STMT);
    9022              :     }
    9023         1299 :   gsi_remove (&si, true);
    9024         1299 :   single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
    9025              : }
    9026              : 
    9027              : /* Generic expansion for OpenMP synchronization directives: master,
    9028              :    ordered and critical.  All we need to do here is remove the entry
    9029              :    and exit markers for REGION.  */
    9030              : 
    9031              : static void
    9032        11531 : expand_omp_synch (struct omp_region *region)
    9033              : {
    9034        11531 :   basic_block entry_bb, exit_bb;
    9035        11531 :   gimple_stmt_iterator si;
    9036              : 
    9037        11531 :   entry_bb = region->entry;
    9038        11531 :   exit_bb = region->exit;
    9039              : 
    9040        11531 :   si = gsi_last_nondebug_bb (entry_bb);
    9041        11531 :   gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_SINGLE
    9042              :               || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASTER
    9043              :               || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_MASKED
    9044              :               || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TASKGROUP
    9045              :               || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ORDERED
    9046              :               || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_CRITICAL
    9047              :               || gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS);
    9048        11531 :   if (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_TEAMS
    9049        11531 :       && gimple_omp_teams_host (as_a <gomp_teams *> (gsi_stmt (si))))
    9050              :     {
    9051         2578 :       expand_omp_taskreg (region);
    9052         2578 :       return;
    9053              :     }
    9054         8953 :   gsi_remove (&si, true);
    9055         8953 :   single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
    9056              : 
    9057         8953 :   if (exit_bb)
    9058              :     {
    9059         8395 :       si = gsi_last_nondebug_bb (exit_bb);
    9060         8395 :       gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_RETURN);
    9061         8395 :       gsi_remove (&si, true);
    9062         8395 :       single_succ_edge (exit_bb)->flags = EDGE_FALLTHRU;
    9063              :     }
    9064              : }
    9065              : 
    9066              : /* Translate enum omp_memory_order to enum memmodel for the embedded
    9067              :    fail clause in there.  */
    9068              : 
    9069              : static enum memmodel
    9070         3168 : omp_memory_order_to_fail_memmodel (enum omp_memory_order mo)
    9071              : {
    9072         3168 :   switch (mo & OMP_FAIL_MEMORY_ORDER_MASK)
    9073              :     {
    9074         2897 :     case OMP_FAIL_MEMORY_ORDER_UNSPECIFIED:
    9075         2897 :       switch (mo & OMP_MEMORY_ORDER_MASK)
    9076              :         {
    9077              :         case OMP_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
    9078              :         case OMP_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
    9079              :         case OMP_MEMORY_ORDER_RELEASE: return MEMMODEL_RELAXED;
    9080              :         case OMP_MEMORY_ORDER_ACQ_REL: return MEMMODEL_ACQUIRE;
    9081              :         case OMP_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
    9082            0 :         default: break;
    9083              :         }
    9084            0 :       gcc_unreachable ();
    9085              :     case OMP_FAIL_MEMORY_ORDER_RELAXED: return MEMMODEL_RELAXED;
    9086              :     case OMP_FAIL_MEMORY_ORDER_ACQUIRE: return MEMMODEL_ACQUIRE;
    9087              :     case OMP_FAIL_MEMORY_ORDER_SEQ_CST: return MEMMODEL_SEQ_CST;
    9088            0 :     default: gcc_unreachable ();
    9089              :     }
    9090              : }
    9091              : 
    9092              : /* Translate enum omp_memory_order to enum memmodel.  The two enums
    9093              :    are using different numbers so that OMP_MEMORY_ORDER_UNSPECIFIED
    9094              :    is 0 and omp_memory_order has the fail mode encoded in it too.  */
    9095              : 
    9096              : static enum memmodel
    9097        10609 : omp_memory_order_to_memmodel (enum omp_memory_order mo)
    9098              : {
    9099        10609 :   enum memmodel ret, fail_ret;
    9100        10609 :   switch (mo & OMP_MEMORY_ORDER_MASK)
    9101              :     {
    9102              :     case OMP_MEMORY_ORDER_RELAXED: ret = MEMMODEL_RELAXED; break;
    9103              :     case OMP_MEMORY_ORDER_ACQUIRE: ret = MEMMODEL_ACQUIRE; break;
    9104              :     case OMP_MEMORY_ORDER_RELEASE: ret = MEMMODEL_RELEASE; break;
    9105              :     case OMP_MEMORY_ORDER_ACQ_REL: ret = MEMMODEL_ACQ_REL; break;
    9106              :     case OMP_MEMORY_ORDER_SEQ_CST: ret = MEMMODEL_SEQ_CST; break;
    9107            0 :     default: gcc_unreachable ();
    9108              :     }
    9109              :   /* If we drop the -Winvalid-memory-model warning for C++17 P0418R2,
    9110              :      we can just return ret here unconditionally.  Otherwise, work around
    9111              :      it here and make sure fail memmodel is not stronger.  */
    9112        10609 :   if ((mo & OMP_FAIL_MEMORY_ORDER_MASK) == OMP_FAIL_MEMORY_ORDER_UNSPECIFIED)
    9113              :     return ret;
    9114          142 :   fail_ret = omp_memory_order_to_fail_memmodel (mo);
    9115          142 :   if (fail_ret > ret)
    9116           13 :     return fail_ret;
    9117              :   return ret;
    9118              : }
    9119              : 
    9120              : /* A subroutine of expand_omp_atomic.  Attempt to implement the atomic
    9121              :    operation as a normal volatile load.  */
    9122              : 
    9123              : static bool
    9124         1199 : expand_omp_atomic_load (basic_block load_bb, tree addr,
    9125              :                         tree loaded_val, int index)
    9126              : {
    9127         1199 :   enum built_in_function tmpbase;
    9128         1199 :   gimple_stmt_iterator gsi;
    9129         1199 :   basic_block store_bb;
    9130         1199 :   location_t loc;
    9131         1199 :   gimple *stmt;
    9132         1199 :   tree decl, type, itype;
    9133              : 
    9134         1199 :   gsi = gsi_last_nondebug_bb (load_bb);
    9135         1199 :   stmt = gsi_stmt (gsi);
    9136         1199 :   gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
    9137         1199 :   loc = gimple_location (stmt);
    9138              : 
    9139              :   /* ??? If the target does not implement atomic_load_optab[mode], and mode
    9140              :      is smaller than word size, then expand_atomic_load assumes that the load
    9141              :      is atomic.  We could avoid the builtin entirely in this case.  */
    9142              : 
    9143         1199 :   tmpbase = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
    9144         1199 :   decl = builtin_decl_explicit (tmpbase);
    9145         1199 :   if (decl == NULL_TREE)
    9146              :     return false;
    9147              : 
    9148         1199 :   type = TREE_TYPE (loaded_val);
    9149         1199 :   itype = TREE_TYPE (TREE_TYPE (decl));
    9150              : 
    9151         1199 :   enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
    9152         1199 :   tree mo = build_int_cst (integer_type_node,
    9153         1199 :                            omp_memory_order_to_memmodel (omo));
    9154         1199 :   gcall *call = gimple_build_call (decl, 2, addr, mo);
    9155         1199 :   gimple_set_location (call, loc);
    9156         2398 :   gimple_set_vuse (call, gimple_vuse (stmt));
    9157         1199 :   gimple *repl;
    9158         1199 :   if (!useless_type_conversion_p (type, itype))
    9159              :     {
    9160         1197 :       tree lhs = make_ssa_name (itype);
    9161         1197 :       gimple_call_set_lhs (call, lhs);
    9162         1197 :       gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    9163         1197 :       repl = gimple_build_assign (loaded_val,
    9164              :                                   build1 (VIEW_CONVERT_EXPR, type, lhs));
    9165         1197 :       gimple_set_location (repl, loc);
    9166              :     }
    9167              :   else
    9168              :     {
    9169            2 :       gimple_call_set_lhs (call, loaded_val);
    9170              :       repl = call;
    9171              :     }
    9172         1199 :   gsi_replace (&gsi, repl, true);
    9173              : 
    9174         1199 :   store_bb = single_succ (load_bb);
    9175         1199 :   gsi = gsi_last_nondebug_bb (store_bb);
    9176         1199 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
    9177         1199 :   gsi_remove (&gsi, true);
    9178              : 
    9179         1199 :   return true;
    9180              : }
    9181              : 
    9182              : /* A subroutine of expand_omp_atomic.  Attempt to implement the atomic
    9183              :    operation as a normal volatile store.  */
    9184              : 
    9185              : static bool
    9186          880 : expand_omp_atomic_store (basic_block load_bb, tree addr,
    9187              :                          tree loaded_val, tree stored_val, int index)
    9188              : {
    9189          880 :   enum built_in_function tmpbase;
    9190          880 :   gimple_stmt_iterator gsi;
    9191          880 :   basic_block store_bb = single_succ (load_bb);
    9192          880 :   location_t loc;
    9193          880 :   gimple *stmt;
    9194          880 :   tree decl, type, itype;
    9195          880 :   machine_mode imode;
    9196          880 :   bool exchange;
    9197              : 
    9198          880 :   gsi = gsi_last_nondebug_bb (load_bb);
    9199          880 :   stmt = gsi_stmt (gsi);
    9200          880 :   gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_LOAD);
    9201              : 
    9202              :   /* If the load value is needed, then this isn't a store but an exchange.  */
    9203          880 :   exchange = gimple_omp_atomic_need_value_p (stmt);
    9204              : 
    9205          880 :   gsi = gsi_last_nondebug_bb (store_bb);
    9206          880 :   stmt = gsi_stmt (gsi);
    9207          880 :   gcc_assert (gimple_code (stmt) == GIMPLE_OMP_ATOMIC_STORE);
    9208          880 :   loc = gimple_location (stmt);
    9209              : 
    9210              :   /* ??? If the target does not implement atomic_store_optab[mode], and mode
    9211              :      is smaller than word size, then expand_atomic_store assumes that the store
    9212              :      is atomic.  We could avoid the builtin entirely in this case.  */
    9213              : 
    9214          880 :   tmpbase = (exchange ? BUILT_IN_ATOMIC_EXCHANGE_N : BUILT_IN_ATOMIC_STORE_N);
    9215          880 :   tmpbase = (enum built_in_function) ((int) tmpbase + index + 1);
    9216          880 :   decl = builtin_decl_explicit (tmpbase);
    9217          880 :   if (decl == NULL_TREE)
    9218              :     return false;
    9219              : 
    9220          880 :   type = TREE_TYPE (stored_val);
    9221              : 
    9222              :   /* Dig out the type of the function's second argument.  */
    9223          880 :   itype = TREE_TYPE (decl);
    9224          880 :   itype = TYPE_ARG_TYPES (itype);
    9225          880 :   itype = TREE_CHAIN (itype);
    9226          880 :   itype = TREE_VALUE (itype);
    9227          880 :   imode = TYPE_MODE (itype);
    9228              : 
    9229          880 :   if (exchange && !can_atomic_exchange_p (imode, true))
    9230              :     return false;
    9231              : 
    9232          880 :   if (!useless_type_conversion_p (itype, type))
    9233          880 :     stored_val = fold_build1_loc (loc, VIEW_CONVERT_EXPR, itype, stored_val);
    9234          880 :   enum omp_memory_order omo = gimple_omp_atomic_memory_order (stmt);
    9235          880 :   tree mo = build_int_cst (integer_type_node,
    9236          880 :                            omp_memory_order_to_memmodel (omo));
    9237          880 :   stored_val = force_gimple_operand_gsi (&gsi, stored_val, true, NULL_TREE,
    9238              :                                          true, GSI_SAME_STMT);
    9239          880 :   gcall *call = gimple_build_call (decl, 3, addr, stored_val, mo);
    9240          880 :   gimple_set_location (call, loc);
    9241         1760 :   gimple_set_vuse (call, gimple_vuse (stmt));
    9242         1760 :   gimple_set_vdef (call, gimple_vdef (stmt));
    9243              : 
    9244          880 :   gimple *repl = call;
    9245          880 :   if (exchange)
    9246              :     {
    9247           88 :       if (!useless_type_conversion_p (type, itype))
    9248              :         {
    9249           88 :           tree lhs = make_ssa_name (itype);
    9250           88 :           gimple_call_set_lhs (call, lhs);
    9251           88 :           gsi_insert_before (&gsi, call, GSI_SAME_STMT);
    9252           88 :           repl = gimple_build_assign (loaded_val,
    9253              :                                       build1 (VIEW_CONVERT_EXPR, type, lhs));
    9254           88 :           gimple_set_location  (repl, loc);
    9255              :         }
    9256              :       else
    9257            0 :         gimple_call_set_lhs (call, loaded_val);
    9258              :     }
    9259          880 :   gsi_replace (&gsi, repl, true);
    9260              : 
    9261              :   /* Remove the GIMPLE_OMP_ATOMIC_LOAD that we verified above.  */
    9262          880 :   gsi = gsi_last_nondebug_bb (load_bb);
    9263          880 :   gsi_remove (&gsi, true);
    9264              : 
    9265          880 :   return true;
    9266              : }
    9267              : 
    9268              : /* A subroutine of expand_omp_atomic.  Attempt to implement the atomic
    9269              :    operation as a __atomic_fetch_op builtin.  INDEX is log2 of the
    9270              :    size of the data type, and thus usable to find the index of the builtin
    9271              :    decl.  Returns false if the expression is not of the proper form.  */
    9272              : 
    9273              : static bool
    9274         6170 : expand_omp_atomic_fetch_op (basic_block load_bb,
    9275              :                             tree addr, tree loaded_val,
    9276              :                             tree stored_val, int index)
    9277              : {
    9278         6170 :   enum built_in_function oldbase, newbase, tmpbase;
    9279         6170 :   tree decl, itype, call;
    9280         6170 :   tree lhs, rhs;
    9281         6170 :   basic_block store_bb = single_succ (load_bb);
    9282         6170 :   gimple_stmt_iterator gsi;
    9283         6170 :   gimple *stmt;
    9284         6170 :   location_t loc;
    9285         6170 :   enum tree_code code;
    9286         6170 :   bool need_old, need_new;
    9287         6170 :   machine_mode imode;
    9288              : 
    9289              :   /* We expect to find the following sequences:
    9290              : 
    9291              :    load_bb:
    9292              :        GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
    9293              : 
    9294              :    store_bb:
    9295              :        val = tmp OP something; (or: something OP tmp)
    9296              :        GIMPLE_OMP_STORE (val)
    9297              : 
    9298              :   ???FIXME: Allow a more flexible sequence.
    9299              :   Perhaps use data flow to pick the statements.
    9300              : 
    9301              :   */
    9302              : 
    9303         6170 :   gsi = gsi_after_labels (store_bb);
    9304         6170 :   stmt = gsi_stmt (gsi);
    9305         6170 :   if (is_gimple_debug (stmt))
    9306              :     {
    9307            0 :       gsi_next_nondebug (&gsi);
    9308            0 :       if (gsi_end_p (gsi))
    9309              :         return false;
    9310         6170 :       stmt = gsi_stmt (gsi);
    9311              :     }
    9312         6170 :   loc = gimple_location (stmt);
    9313         6170 :   if (!is_gimple_assign (stmt))
    9314              :     return false;
    9315         6170 :   gsi_next_nondebug (&gsi);
    9316         6170 :   if (gimple_code (gsi_stmt (gsi)) != GIMPLE_OMP_ATOMIC_STORE)
    9317              :     return false;
    9318         5504 :   need_new = gimple_omp_atomic_need_value_p (gsi_stmt (gsi));
    9319         5504 :   need_old = gimple_omp_atomic_need_value_p (last_nondebug_stmt (load_bb));
    9320         5504 :   enum omp_memory_order omo
    9321         5504 :     = gimple_omp_atomic_memory_order (last_nondebug_stmt (load_bb));
    9322         5504 :   enum memmodel mo = omp_memory_order_to_memmodel (omo);
    9323         5504 :   gcc_checking_assert (!need_old || !need_new);
    9324              : 
    9325         5504 :   if (!operand_equal_p (gimple_assign_lhs (stmt), stored_val, 0))
    9326              :     return false;
    9327              : 
    9328              :   /* Check for one of the supported fetch-op operations.  */
    9329         5504 :   code = gimple_assign_rhs_code (stmt);
    9330         5504 :   switch (code)
    9331              :     {
    9332              :     case PLUS_EXPR:
    9333              :     case POINTER_PLUS_EXPR:
    9334              :       oldbase = BUILT_IN_ATOMIC_FETCH_ADD_N;
    9335              :       newbase = BUILT_IN_ATOMIC_ADD_FETCH_N;
    9336              :       break;
    9337           79 :     case MINUS_EXPR:
    9338           79 :       oldbase = BUILT_IN_ATOMIC_FETCH_SUB_N;
    9339           79 :       newbase = BUILT_IN_ATOMIC_SUB_FETCH_N;
    9340           79 :       break;
    9341          136 :     case BIT_AND_EXPR:
    9342          136 :       oldbase = BUILT_IN_ATOMIC_FETCH_AND_N;
    9343          136 :       newbase = BUILT_IN_ATOMIC_AND_FETCH_N;
    9344          136 :       break;
    9345          251 :     case BIT_IOR_EXPR:
    9346          251 :       oldbase = BUILT_IN_ATOMIC_FETCH_OR_N;
    9347          251 :       newbase = BUILT_IN_ATOMIC_OR_FETCH_N;
    9348          251 :       break;
    9349          135 :     case BIT_XOR_EXPR:
    9350          135 :       oldbase = BUILT_IN_ATOMIC_FETCH_XOR_N;
    9351          135 :       newbase = BUILT_IN_ATOMIC_XOR_FETCH_N;
    9352          135 :       break;
    9353              :     default:
    9354              :       return false;
    9355              :     }
    9356              : 
    9357              :   /* Make sure the expression is of the proper form.  */
    9358         4794 :   if (operand_equal_p (gimple_assign_rhs1 (stmt), loaded_val, 0))
    9359         4614 :     rhs = gimple_assign_rhs2 (stmt);
    9360          180 :   else if (commutative_tree_code (gimple_assign_rhs_code (stmt))
    9361          180 :            && operand_equal_p (gimple_assign_rhs2 (stmt), loaded_val, 0))
    9362          137 :     rhs = gimple_assign_rhs1 (stmt);
    9363              :   else
    9364              :     return false;
    9365              : 
    9366         9502 :   tmpbase = ((enum built_in_function)
    9367         4751 :              ((need_new ? newbase : oldbase) + index + 1));
    9368         4751 :   decl = builtin_decl_explicit (tmpbase);
    9369         4751 :   if (decl == NULL_TREE)
    9370              :     return false;
    9371         4751 :   itype = TREE_TYPE (TREE_TYPE (decl));
    9372         4751 :   imode = TYPE_MODE (itype);
    9373              : 
    9374              :   /* We could test all of the various optabs involved, but the fact of the
    9375              :      matter is that (with the exception of i486 vs i586 and xadd) all targets
    9376              :      that support any atomic operation optab also implements compare-and-swap.
    9377              :      Let optabs.cc take care of expanding any compare-and-swap loop.  */
    9378         4751 :   if (!can_compare_and_swap_p (imode, true) || !can_atomic_load_p (imode))
    9379              :     return false;
    9380              : 
    9381         4747 :   gsi = gsi_last_nondebug_bb (load_bb);
    9382         4747 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_LOAD);
    9383              : 
    9384              :   /* OpenMP does not imply any barrier-like semantics on its atomic ops.
    9385              :      It only requires that the operation happen atomically.  Thus we can
    9386              :      use the RELAXED memory model.  */
    9387         4747 :   call = build_call_expr_loc (loc, decl, 3, addr,
    9388              :                               fold_convert_loc (loc, itype, rhs),
    9389         4747 :                               build_int_cst (integer_type_node, mo));
    9390              : 
    9391         4747 :   if (need_old || need_new)
    9392              :     {
    9393          552 :       lhs = need_old ? loaded_val : stored_val;
    9394          552 :       call = fold_convert_loc (loc, TREE_TYPE (lhs), call);
    9395          552 :       call = build2_loc (loc, MODIFY_EXPR, void_type_node, lhs, call);
    9396              :     }
    9397              :   else
    9398         4195 :     call = fold_convert_loc (loc, void_type_node, call);
    9399         4747 :   force_gimple_operand_gsi (&gsi, call, true, NULL_TREE, true, GSI_SAME_STMT);
    9400         4747 :   gsi_remove (&gsi, true);
    9401              : 
    9402         4747 :   gsi = gsi_last_nondebug_bb (store_bb);
    9403         4747 :   gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_ATOMIC_STORE);
    9404         4747 :   gsi_remove (&gsi, true);
    9405         4747 :   gsi = gsi_last_nondebug_bb (store_bb);
    9406         4747 :   stmt = gsi_stmt (gsi);
    9407         4747 :   gsi_remove (&gsi, true);
    9408              : 
    9409         4747 :   if (gimple_in_ssa_p (cfun))
    9410           29 :     release_defs (stmt);
    9411              : 
    9412              :   return true;
    9413              : }
    9414              : 
    9415              : /* A subroutine of expand_omp_atomic.  Attempt to implement the atomic
    9416              :    compare and exchange as an ATOMIC_COMPARE_EXCHANGE internal function.
    9417              :    Returns false if the expression is not of the proper form.  */
    9418              : 
    9419              : static bool
    9420         2370 : expand_omp_atomic_cas (basic_block load_bb, tree addr,
    9421              :                        tree loaded_val, tree stored_val, int index)
    9422              : {
    9423              :   /* We expect to find the following sequences:
    9424              : 
    9425              :    load_bb:
    9426              :        GIMPLE_OMP_ATOMIC_LOAD (tmp, mem)
    9427              : 
    9428              :    store_bb:
    9429              :        val = tmp == e ? d : tmp;
    9430              :        GIMPLE_OMP_ATOMIC_STORE (val)
    9431              : 
    9432              :      or in store_bb instead:
    9433              :        tmp2 = tmp == e;
    9434              :        val = tmp2 ? d : tmp;
    9435              :        GIMPLE_OMP_ATOMIC_STORE (val)
    9436              : 
    9437              :      or:
    9438              :        tmp3 = VIEW_CONVERT_EXPR<integral_type>(tmp);
    9439              :        val = e == tmp3 ? d : tmp;
    9440              :        GIMPLE_OMP_ATOMIC_STORE (val)
    9441              : 
    9442              :      etc.  */
    9443              : 
    9444              : 
    9445         2370 :   basic_block store_bb = single_succ (load_bb);
    9446         2370 :   gimple_stmt_iterator gsi = gsi_last_nondebug_bb (store_bb);
    9447         2370 :   gimple *store_stmt = gsi_stmt (gsi);
    9448         2370 :   if (!store_stmt || gimple_code (store_stmt) != GIMPLE_OMP_ATOMIC_STORE)
    9449              :     return false;
    9450         2370 :   gsi_prev_nondebug (&gsi);
    9451         2370 :   if (gsi_end_p (gsi))
    9452              :     return false;
    9453         2364 :   gimple *condexpr_stmt = gsi_stmt (gsi);
    9454         2364 :   if (!is_gimple_assign (condexpr_stmt)
    9455         2364 :       || gimple_assign_rhs_code (condexpr_stmt) != COND_EXPR)
    9456              :     return false;
    9457          409 :   if (!operand_equal_p (gimple_assign_lhs (condexpr_stmt), stored_val, 0))
    9458              :     return false;
    9459          409 :   gassign *cond_stmt = NULL;
    9460          409 :   gassign *vce_stmt = NULL;
    9461          409 :   gsi_prev_nondebug (&gsi);
    9462          409 :   if (!gsi_end_p (gsi))
    9463              :     {
    9464          409 :       cond_stmt = dyn_cast<gassign *> (gsi_stmt (gsi));
    9465          409 :       if (!cond_stmt)
    9466              :         return false;
    9467          409 :       if (gimple_assign_rhs_code (cond_stmt) == EQ_EXPR)
    9468              :         {
    9469          324 :           gsi_prev_nondebug (&gsi);
    9470          324 :           if (!gsi_end_p (gsi))
    9471              :             {
    9472           89 :               vce_stmt = dyn_cast<gassign *> (gsi_stmt (gsi));
    9473           74 :               if (!vce_stmt
    9474          143 :                   || gimple_assign_rhs_code (vce_stmt) != VIEW_CONVERT_EXPR)
    9475              :                 return false;
    9476              :             }
    9477              :         }
    9478           85 :       else if (gimple_assign_rhs_code (cond_stmt) == VIEW_CONVERT_EXPR)
    9479              :         std::swap (vce_stmt, cond_stmt);
    9480              :       else
    9481              :         return false;
    9482           69 :       if (vce_stmt)
    9483              :         {
    9484           69 :           tree vce_rhs = gimple_assign_rhs1 (vce_stmt);
    9485           69 :           if (TREE_CODE (vce_rhs) != VIEW_CONVERT_EXPR
    9486           69 :               || !operand_equal_p (TREE_OPERAND (vce_rhs, 0), loaded_val))
    9487              :             return false;
    9488          114 :           if (!INTEGRAL_TYPE_P (TREE_TYPE (vce_rhs))
    9489           57 :               || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (loaded_val))
    9490          114 :               || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vce_rhs)),
    9491           57 :                                       TYPE_SIZE (TREE_TYPE (loaded_val))))
    9492              :             return false;
    9493           57 :           gsi_prev_nondebug (&gsi);
    9494           57 :           if (!gsi_end_p (gsi))
    9495              :             return false;
    9496              :         }
    9497              :     }
    9498          292 :   tree cond = gimple_assign_rhs1 (condexpr_stmt);
    9499          292 :   tree cond_op1, cond_op2;
    9500          292 :   if (cond_stmt)
    9501              :     {
    9502              :       /* We should now always get a separate cond_stmt.  */
    9503          292 :       if (!operand_equal_p (cond, gimple_assign_lhs (cond_stmt)))
    9504              :         return false;
    9505          292 :       cond_op1 = gimple_assign_rhs1 (cond_stmt);
    9506          292 :       cond_op2 = gimple_assign_rhs2 (cond_stmt);
    9507              :     }
    9508            0 :   else if (TREE_CODE (cond) != EQ_EXPR && TREE_CODE (cond) != NE_EXPR)
    9509              :     return false;
    9510              :   else
    9511              :     {
    9512            0 :       cond_op1 = TREE_OPERAND (cond, 0);
    9513            0 :       cond_op2 = TREE_OPERAND (cond, 1);
    9514              :     }
    9515          292 :   tree d;
    9516          292 :   if (TREE_CODE (cond) == NE_EXPR)
    9517              :     {
    9518            0 :       if (!operand_equal_p (gimple_assign_rhs2 (condexpr_stmt), loaded_val))
    9519              :         return false;
    9520            0 :       d = gimple_assign_rhs3 (condexpr_stmt);
    9521              :     }
    9522          292 :   else if (!operand_equal_p (gimple_assign_rhs3 (condexpr_stmt), loaded_val))
    9523              :     return false;
    9524              :   else
    9525          292 :     d = gimple_assign_rhs2 (condexpr_stmt);
    9526          292 :   tree e = vce_stmt ? gimple_assign_lhs (vce_stmt) : loaded_val;
    9527          292 :   if (operand_equal_p (e, cond_op1))
    9528              :     e = cond_op2;
    9529           21 :   else if (operand_equal_p (e, cond_op2))
    9530              :     e = cond_op1;
    9531              :   else
    9532              :     return false;
    9533              : 
    9534          292 :   location_t loc = gimple_location (store_stmt);
    9535          292 :   gimple *load_stmt = last_nondebug_stmt (load_bb);
    9536          292 :   bool need_new = gimple_omp_atomic_need_value_p (store_stmt);
    9537          292 :   bool need_old = gimple_omp_atomic_need_value_p (load_stmt);
    9538          292 :   bool weak = gimple_omp_atomic_weak_p (load_stmt);
    9539          292 :   enum omp_memory_order omo = gimple_omp_atomic_memory_order (load_stmt);
    9540          292 :   tree mo = build_int_cst (integer_type_node, omp_memory_order_to_memmodel (omo));
    9541          292 :   tree fmo = build_int_cst (integer_type_node, omp_memory_order_to_fail_memmodel (omo));
    9542          292 :   gcc_checking_assert (!need_old || !need_new);
    9543              : 
    9544          292 :   enum built_in_function fncode
    9545              :     = (enum built_in_function) ((int) BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
    9546          292 :                                 + index + 1);
    9547          292 :   tree cmpxchg = builtin_decl_explicit (fncode);
    9548          292 :   if (cmpxchg == NULL_TREE)
    9549              :     return false;
    9550          292 :   tree itype = TREE_TYPE (TREE_TYPE (cmpxchg));
    9551              : 
    9552          292 :   if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
    9553          292 :       || !can_atomic_load_p (TYPE_MODE (itype)))
    9554              :     return false;
    9555              : 
    9556          291 :   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
    9557          291 :   if (SCALAR_FLOAT_TYPE_P (type) && !vce_stmt)
    9558              :     return false;
    9559              : 
    9560          290 :   gsi = gsi_for_stmt (store_stmt);
    9561          290 :   if (!useless_type_conversion_p (itype, TREE_TYPE (e)))
    9562              :     {
    9563          233 :       tree ne = create_tmp_reg (itype);
    9564          233 :       gimple *g = gimple_build_assign (ne, NOP_EXPR, e);
    9565          233 :       gimple_set_location (g, loc);
    9566          233 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9567          233 :       e = ne;
    9568              :     }
    9569          290 :   if (!useless_type_conversion_p (itype, TREE_TYPE (d)))
    9570              :     {
    9571          290 :       tree nd = create_tmp_reg (itype);
    9572          290 :       enum tree_code code;
    9573          290 :       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (d)))
    9574              :         {
    9575           57 :           code = VIEW_CONVERT_EXPR;
    9576           57 :           d = build1 (VIEW_CONVERT_EXPR, itype, d);
    9577              :         }
    9578              :       else
    9579              :         code = NOP_EXPR;
    9580          290 :       gimple *g = gimple_build_assign (nd, code, d);
    9581          290 :       gimple_set_location (g, loc);
    9582          290 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9583          290 :       d = nd;
    9584              :     }
    9585              : 
    9586          290 :   tree ctype = build_complex_type (itype);
    9587          290 :   int flag = int_size_in_bytes (itype) + (weak ? 256 : 0);
    9588          290 :   gimple *g
    9589          290 :     = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6, addr, e, d,
    9590          290 :                                   build_int_cst (integer_type_node, flag),
    9591              :                                   mo, fmo);
    9592          290 :   tree cres = create_tmp_reg (ctype);
    9593          290 :   gimple_call_set_lhs (g, cres);
    9594          290 :   gimple_set_location (g, loc);
    9595          290 :   gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9596              : 
    9597          290 :   if (cond_stmt || need_old || need_new)
    9598              :     {
    9599          290 :       tree im = create_tmp_reg (itype);
    9600          290 :       g = gimple_build_assign (im, IMAGPART_EXPR,
    9601              :                                build1 (IMAGPART_EXPR, itype, cres));
    9602          290 :       gimple_set_location (g, loc);
    9603          290 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9604              : 
    9605          290 :       tree re = NULL_TREE;
    9606          290 :       if (need_old || need_new)
    9607              :         {
    9608          199 :           re = create_tmp_reg (itype);
    9609          199 :           g = gimple_build_assign (re, REALPART_EXPR,
    9610              :                                    build1 (REALPART_EXPR, itype, cres));
    9611          199 :           gimple_set_location (g, loc);
    9612          199 :           gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9613              :         }
    9614              : 
    9615          290 :       if (cond_stmt)
    9616              :         {
    9617          290 :           g = gimple_build_assign (cond, NOP_EXPR, im);
    9618          290 :           gimple_set_location (g, loc);
    9619          290 :           gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9620              :         }
    9621              : 
    9622          290 :       if (need_new)
    9623              :         {
    9624           44 :           gassign *a = gimple_build_assign (create_tmp_reg (itype), COND_EXPR,
    9625              :                                             cond_stmt
    9626              :                                             ? cond
    9627            0 :                                             : build2 (NE_EXPR,
    9628              :                                                       boolean_type_node,
    9629              :                                                       im,
    9630              :                                                       build_zero_cst (itype)),
    9631              :                                             d, re);
    9632           44 :           gimple_set_location (a, loc);
    9633           44 :           gsi_insert_before (&gsi, a, GSI_SAME_STMT);
    9634           44 :           re = gimple_assign_lhs (a);
    9635              :         }
    9636              : 
    9637          290 :       if (need_old || need_new)
    9638              :         {
    9639          199 :           tree v = need_old ? loaded_val : stored_val;
    9640          199 :           enum tree_code code;
    9641          199 :           if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (v)))
    9642              :             {
    9643           38 :               code = VIEW_CONVERT_EXPR;
    9644           38 :               re = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (v), re);
    9645              :             }
    9646          161 :           else if (!useless_type_conversion_p (TREE_TYPE (v), itype))
    9647              :             code = NOP_EXPR;
    9648              :           else
    9649            0 :             code = TREE_CODE (re);
    9650          199 :           g = gimple_build_assign (v, code, re);
    9651          199 :           gimple_set_location (g, loc);
    9652          199 :           gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    9653              :         }
    9654              :     }
    9655              : 
    9656          290 :   gsi_remove (&gsi, true);
    9657          290 :   gsi = gsi_for_stmt (load_stmt);
    9658          290 :   gsi_remove (&gsi, true);
    9659          290 :   gsi = gsi_for_stmt (condexpr_stmt);
    9660          290 :   gsi_remove (&gsi, true);
    9661          290 :   if (cond_stmt)
    9662              :     {
    9663          290 :       gsi = gsi_for_stmt (cond_stmt);
    9664          290 :       gsi_remove (&gsi, true);
    9665              :     }
    9666          290 :   if (vce_stmt)
    9667              :     {
    9668           57 :       gsi = gsi_for_stmt (vce_stmt);
    9669           57 :       gsi_remove (&gsi, true);
    9670              :     }
    9671              : 
    9672              :   return true;
    9673              : }
    9674              : 
    9675              : /* A subroutine of expand_omp_atomic.  Implement the atomic operation as:
    9676              : 
    9677              :       oldval = *addr;
    9678              :       repeat:
    9679              :         newval = rhs;    // with oldval replacing *addr in rhs
    9680              :         oldval = __sync_val_compare_and_swap (addr, oldval, newval);
    9681              :         if (oldval != newval)
    9682              :           goto repeat;
    9683              : 
    9684              :    INDEX is log2 of the size of the data type, and thus usable to find the
    9685              :    index of the builtin decl.  */
    9686              : 
    9687              : static bool
    9688         2817 : expand_omp_atomic_pipeline (basic_block load_bb, basic_block store_bb,
    9689              :                             tree addr, tree loaded_val, tree stored_val,
    9690              :                             int index)
    9691              : {
    9692         2817 :   tree loadedi, storedi, initial, new_storedi, old_vali;
    9693         2817 :   tree type, itype, cmpxchg, iaddr, atype;
    9694         2817 :   gimple_stmt_iterator si;
    9695         2817 :   basic_block loop_header = single_succ (load_bb);
    9696         2817 :   gimple *phi, *stmt;
    9697         2817 :   edge e;
    9698         2817 :   enum built_in_function fncode;
    9699              : 
    9700         2817 :   fncode = (enum built_in_function)((int)BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
    9701         2817 :                                     + index + 1);
    9702         2817 :   cmpxchg = builtin_decl_explicit (fncode);
    9703         2817 :   if (cmpxchg == NULL_TREE)
    9704              :     return false;
    9705         2817 :   type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
    9706         2817 :   atype = type;
    9707         2817 :   itype = TREE_TYPE (TREE_TYPE (cmpxchg));
    9708              : 
    9709         2817 :   if (!can_compare_and_swap_p (TYPE_MODE (itype), true)
    9710         2817 :       || !can_atomic_load_p (TYPE_MODE (itype)))
    9711              :     return false;
    9712              : 
    9713              :   /* Load the initial value, replacing the GIMPLE_OMP_ATOMIC_LOAD.  */
    9714         2734 :   si = gsi_last_nondebug_bb (load_bb);
    9715         2734 :   gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
    9716         2734 :   location_t loc = gimple_location (gsi_stmt (si));
    9717         2734 :   enum omp_memory_order omo = gimple_omp_atomic_memory_order (gsi_stmt (si));
    9718         2734 :   tree mo = build_int_cst (integer_type_node, omp_memory_order_to_memmodel (omo));
    9719         2734 :   tree fmo = build_int_cst (integer_type_node, omp_memory_order_to_fail_memmodel (omo));
    9720              : 
    9721              :   /* For floating-point values, we'll need to view-convert them to integers
    9722              :      so that we can perform the atomic compare and swap.  Simplify the
    9723              :      following code by always setting up the "i"ntegral variables.  */
    9724         2734 :   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
    9725              :     {
    9726          957 :       tree iaddr_val;
    9727              : 
    9728          957 :       iaddr = create_tmp_reg (build_pointer_type_for_mode (itype, ptr_mode,
    9729              :                                                            true));
    9730          957 :       atype = itype;
    9731          957 :       iaddr_val
    9732          957 :         = force_gimple_operand_gsi (&si,
    9733          957 :                                     fold_convert (TREE_TYPE (iaddr), addr),
    9734              :                                     false, NULL_TREE, true, GSI_SAME_STMT);
    9735          957 :       stmt = gimple_build_assign (iaddr, iaddr_val);
    9736          957 :       gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    9737          957 :       loadedi = create_tmp_var (itype);
    9738          957 :       if (gimple_in_ssa_p (cfun))
    9739           15 :         loadedi = make_ssa_name (loadedi);
    9740              :     }
    9741              :   else
    9742              :     {
    9743              :       iaddr = addr;
    9744              :       loadedi = loaded_val;
    9745              :     }
    9746              : 
    9747         2734 :   fncode = (enum built_in_function) (BUILT_IN_ATOMIC_LOAD_N + index + 1);
    9748         2734 :   tree loaddecl = builtin_decl_explicit (fncode);
    9749         2734 :   if (loaddecl)
    9750         2734 :     initial
    9751         2734 :       = fold_convert (atype,
    9752              :                       build_call_expr (loaddecl, 2, iaddr,
    9753              :                                        build_int_cst (integer_type_node,
    9754              :                                                       MEMMODEL_RELAXED)));
    9755              :   else
    9756              :     {
    9757            0 :       tree off
    9758            0 :         = build_int_cst (build_pointer_type_for_mode (atype, ptr_mode,
    9759              :                                                       true), 0);
    9760            0 :       initial = build2 (MEM_REF, atype, iaddr, off);
    9761              :     }
    9762              : 
    9763         2734 :   initial
    9764         2734 :     = force_gimple_operand_gsi (&si, initial, true, NULL_TREE, true,
    9765              :                                 GSI_SAME_STMT);
    9766              : 
    9767              :   /* Move the value to the LOADEDI temporary.  */
    9768         2734 :   if (gimple_in_ssa_p (cfun))
    9769              :     {
    9770           38 :       gcc_assert (gimple_seq_empty_p (phi_nodes (loop_header)));
    9771           38 :       phi = create_phi_node (loadedi, loop_header);
    9772           38 :       SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (load_bb)),
    9773              :                initial);
    9774              :     }
    9775              :   else
    9776         2696 :     gsi_insert_before (&si,
    9777         2696 :                        gimple_build_assign (loadedi, initial),
    9778              :                        GSI_SAME_STMT);
    9779         2734 :   if (loadedi != loaded_val)
    9780              :     {
    9781          957 :       gimple_stmt_iterator gsi2;
    9782          957 :       tree x;
    9783              : 
    9784          957 :       x = build1 (VIEW_CONVERT_EXPR, type, loadedi);
    9785          957 :       gsi2 = gsi_start_bb (loop_header);
    9786          957 :       if (gimple_in_ssa_p (cfun))
    9787              :         {
    9788           15 :           gassign *stmt;
    9789           15 :           x = force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
    9790              :                                         true, GSI_SAME_STMT);
    9791           15 :           stmt = gimple_build_assign (loaded_val, x);
    9792           15 :           gsi_insert_before (&gsi2, stmt, GSI_SAME_STMT);
    9793              :         }
    9794              :       else
    9795              :         {
    9796          942 :           x = build2 (MODIFY_EXPR, TREE_TYPE (loaded_val), loaded_val, x);
    9797          942 :           force_gimple_operand_gsi (&gsi2, x, true, NULL_TREE,
    9798              :                                     true, GSI_SAME_STMT);
    9799              :         }
    9800              :     }
    9801         2734 :   gsi_remove (&si, true);
    9802              : 
    9803         2734 :   si = gsi_last_nondebug_bb (store_bb);
    9804         2734 :   gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
    9805              : 
    9806         2734 :   if (iaddr == addr)
    9807              :     storedi = stored_val;
    9808              :   else
    9809          957 :     storedi
    9810          957 :       = force_gimple_operand_gsi (&si,
    9811              :                                   build1 (VIEW_CONVERT_EXPR, itype,
    9812              :                                           stored_val), true, NULL_TREE, true,
    9813              :                                   GSI_SAME_STMT);
    9814              : 
    9815              :   /* Build the compare&swap statement.  */
    9816         2734 :   tree ctype = build_complex_type (itype);
    9817         2734 :   int flag = int_size_in_bytes (itype);
    9818         2734 :   new_storedi = build_call_expr_internal_loc (loc, IFN_ATOMIC_COMPARE_EXCHANGE,
    9819              :                                               ctype, 6, iaddr, loadedi,
    9820              :                                               storedi,
    9821              :                                               build_int_cst (integer_type_node,
    9822         2734 :                                                              flag),
    9823              :                                               mo, fmo);
    9824         2734 :   new_storedi = build1 (REALPART_EXPR, itype, new_storedi);
    9825         2734 :   new_storedi = force_gimple_operand_gsi (&si,
    9826         2734 :                                           fold_convert (TREE_TYPE (loadedi),
    9827              :                                                         new_storedi),
    9828              :                                           true, NULL_TREE,
    9829              :                                           true, GSI_SAME_STMT);
    9830              : 
    9831         2734 :   if (gimple_in_ssa_p (cfun))
    9832              :     old_vali = loadedi;
    9833              :   else
    9834              :     {
    9835         2696 :       old_vali = create_tmp_var (TREE_TYPE (loadedi));
    9836         2696 :       stmt = gimple_build_assign (old_vali, loadedi);
    9837         2696 :       gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    9838              : 
    9839         2696 :       stmt = gimple_build_assign (loadedi, new_storedi);
    9840         2696 :       gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    9841              :     }
    9842              : 
    9843              :   /* Note that we always perform the comparison as an integer, even for
    9844              :      floating point.  This allows the atomic operation to properly
    9845              :      succeed even with NaNs and -0.0.  */
    9846         2734 :   tree ne = build2 (NE_EXPR, boolean_type_node, new_storedi, old_vali);
    9847         2734 :   stmt = gimple_build_cond_empty (ne);
    9848         2734 :   gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    9849              : 
    9850              :   /* Update cfg.  */
    9851         2734 :   e = single_succ_edge (store_bb);
    9852         2734 :   e->flags &= ~EDGE_FALLTHRU;
    9853         2734 :   e->flags |= EDGE_FALSE_VALUE;
    9854              :   /* Expect no looping.  */
    9855         2734 :   e->probability = profile_probability::guessed_always ();
    9856              : 
    9857         2734 :   e = make_edge (store_bb, loop_header, EDGE_TRUE_VALUE);
    9858         2734 :   e->probability = profile_probability::guessed_never ();
    9859              : 
    9860              :   /* Copy the new value to loadedi (we already did that before the condition
    9861              :      if we are not in SSA).  */
    9862         2734 :   if (gimple_in_ssa_p (cfun))
    9863              :     {
    9864           38 :       phi = gimple_seq_first_stmt (phi_nodes (loop_header));
    9865           38 :       SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), new_storedi);
    9866              :     }
    9867              : 
    9868              :   /* Remove GIMPLE_OMP_ATOMIC_STORE.  */
    9869         2734 :   stmt = gsi_stmt (si);
    9870         2734 :   gsi_remove (&si, true);
    9871         2734 :   if (gimple_in_ssa_p (cfun))
    9872           38 :     release_defs (stmt);
    9873              : 
    9874         2734 :   class loop *loop = alloc_loop ();
    9875         2734 :   loop->header = loop_header;
    9876         2734 :   loop->latch = store_bb;
    9877         2734 :   add_loop (loop, loop_header->loop_father);
    9878              : 
    9879         2734 :   return true;
    9880              : }
    9881              : 
    9882              : /* A subroutine of expand_omp_atomic.  Implement the atomic operation as:
    9883              : 
    9884              :                                   GOMP_atomic_start ();
    9885              :                                   *addr = rhs;
    9886              :                                   GOMP_atomic_end ();
    9887              : 
    9888              :    The result is not globally atomic, but works so long as all parallel
    9889              :    references are within #pragma omp atomic directives.  According to
    9890              :    responses received from omp@openmp.org, appears to be within spec.
    9891              :    Which makes sense, since that's how several other compilers handle
    9892              :    this situation as well.
    9893              :    LOADED_VAL and ADDR are the operands of GIMPLE_OMP_ATOMIC_LOAD we're
    9894              :    expanding.  STORED_VAL is the operand of the matching
    9895              :    GIMPLE_OMP_ATOMIC_STORE.
    9896              : 
    9897              :    We replace
    9898              :    GIMPLE_OMP_ATOMIC_LOAD (loaded_val, addr) with
    9899              :    loaded_val = *addr;
    9900              : 
    9901              :    and replace
    9902              :    GIMPLE_OMP_ATOMIC_STORE (stored_val)  with
    9903              :    *addr = stored_val;
    9904              : */
    9905              : 
    9906              : static bool
    9907          309 : expand_omp_atomic_mutex (basic_block load_bb, basic_block store_bb,
    9908              :                          tree addr, tree loaded_val, tree stored_val)
    9909              : {
    9910          309 :   gimple_stmt_iterator si;
    9911          309 :   gassign *stmt;
    9912          309 :   tree t;
    9913              : 
    9914          309 :   si = gsi_last_nondebug_bb (load_bb);
    9915          309 :   gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_LOAD);
    9916              : 
    9917          309 :   t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_START);
    9918          309 :   t = build_call_expr (t, 0);
    9919          309 :   force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
    9920              : 
    9921          309 :   tree mem = build_simple_mem_ref (addr);
    9922          309 :   TREE_TYPE (mem) = TREE_TYPE (loaded_val);
    9923          309 :   TREE_OPERAND (mem, 1)
    9924          309 :     = fold_convert (build_pointer_type_for_mode (TREE_TYPE (mem), ptr_mode,
    9925              :                                                  true),
    9926              :                     TREE_OPERAND (mem, 1));
    9927          309 :   stmt = gimple_build_assign (loaded_val, mem);
    9928          309 :   gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    9929          309 :   gsi_remove (&si, true);
    9930              : 
    9931          309 :   si = gsi_last_nondebug_bb (store_bb);
    9932          309 :   gcc_assert (gimple_code (gsi_stmt (si)) == GIMPLE_OMP_ATOMIC_STORE);
    9933              : 
    9934          309 :   stmt = gimple_build_assign (unshare_expr (mem), stored_val);
    9935          618 :   gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (si)));
    9936          618 :   gimple_set_vdef (stmt, gimple_vdef (gsi_stmt (si)));
    9937          309 :   gsi_insert_before (&si, stmt, GSI_SAME_STMT);
    9938              : 
    9939          309 :   t = builtin_decl_explicit (BUILT_IN_GOMP_ATOMIC_END);
    9940          309 :   t = build_call_expr (t, 0);
    9941          309 :   force_gimple_operand_gsi (&si, t, true, NULL_TREE, true, GSI_SAME_STMT);
    9942          309 :   gsi_remove (&si, true);
    9943          309 :   return true;
    9944              : }
    9945              : 
    9946              : /* Expand an GIMPLE_OMP_ATOMIC statement.  We try to expand
    9947              :    using expand_omp_atomic_fetch_op.  If it failed, we try to
    9948              :    call expand_omp_atomic_pipeline, and if it fails too, the
    9949              :    ultimate fallback is wrapping the operation in a mutex
    9950              :    (expand_omp_atomic_mutex).  REGION is the atomic region built
    9951              :    by build_omp_regions_1().  */
    9952              : 
    9953              : static void
    9954        10159 : expand_omp_atomic (struct omp_region *region)
    9955              : {
    9956        10159 :   basic_block load_bb = region->entry, store_bb = region->exit;
    9957        10159 :   gomp_atomic_load *load
    9958        10159 :     = as_a <gomp_atomic_load *> (last_nondebug_stmt (load_bb));
    9959        10159 :   gomp_atomic_store *store
    9960        10159 :     = as_a <gomp_atomic_store *> (last_nondebug_stmt (store_bb));
    9961        10159 :   tree loaded_val = gimple_omp_atomic_load_lhs (load);
    9962        10159 :   tree addr = gimple_omp_atomic_load_rhs (load);
    9963        10159 :   tree stored_val = gimple_omp_atomic_store_val (store);
    9964        10159 :   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (loaded_val));
    9965        10159 :   HOST_WIDE_INT index;
    9966              : 
    9967              :   /* Make sure the type is one of the supported sizes.  */
    9968        10159 :   index = tree_to_uhwi (TYPE_SIZE_UNIT (type));
    9969        10159 :   index = exact_log2 (index);
    9970        10125 :   if (index >= 0 && index <= 4)
    9971              :     {
    9972        10125 :       unsigned int align = TYPE_ALIGN_UNIT (type);
    9973              : 
    9974              :       /* __sync builtins require strict data alignment.  */
    9975        20250 :       if (exact_log2 (align) >= index)
    9976              :         {
    9977              :           /* Atomic load.  */
    9978         9933 :           scalar_mode smode;
    9979         9933 :           if (loaded_val == stored_val
    9980         1203 :               && (is_int_mode (TYPE_MODE (type), &smode)
    9981         1353 :                   || is_float_mode (TYPE_MODE (type), &smode))
    9982         1203 :               && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
    9983        11132 :               && expand_omp_atomic_load (load_bb, addr, loaded_val, index))
    9984        10159 :             return;
    9985              : 
    9986              :           /* Atomic store.  */
    9987         8734 :           if ((is_int_mode (TYPE_MODE (type), &smode)
    9988         9891 :                || is_float_mode (TYPE_MODE (type), &smode))
    9989         8734 :               && GET_MODE_BITSIZE (smode) <= BITS_PER_WORD
    9990         8651 :               && store_bb == single_succ (load_bb)
    9991         7956 :               && first_stmt (store_bb) == store
    9992          880 :               && expand_omp_atomic_store (load_bb, addr, loaded_val,
    9993              :                                           stored_val, index))
    9994              :             return;
    9995              : 
    9996              :           /* When possible, use specialized atomic update functions.  */
    9997         1097 :           if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
    9998         6765 :               && store_bb == single_succ (load_bb)
    9999        14024 :               && expand_omp_atomic_fetch_op (load_bb, addr,
   10000              :                                              loaded_val, stored_val, index))
   10001              :             return;
   10002              : 
   10003              :           /* When possible, use ATOMIC_COMPARE_EXCHANGE ifn without a loop.  */
   10004         3107 :           if (store_bb == single_succ (load_bb)
   10005         2408 :               && !gimple_in_ssa_p (cfun)
   10006         5477 :               && expand_omp_atomic_cas (load_bb, addr, loaded_val, stored_val,
   10007              :                                         index))
   10008              :             return;
   10009              : 
   10010              :           /* If we don't have specialized __sync builtins, try and implement
   10011              :              as a compare and swap loop.  */
   10012         2817 :           if (expand_omp_atomic_pipeline (load_bb, store_bb, addr,
   10013              :                                           loaded_val, stored_val, index))
   10014              :             return;
   10015              :         }
   10016              :     }
   10017              : 
   10018              :   /* The ultimate fallback is wrapping the operation in a mutex.  */
   10019          309 :   expand_omp_atomic_mutex (load_bb, store_bb, addr, loaded_val, stored_val);
   10020              : }
   10021              : 
   10022              : /* Mark the loops inside the kernels region starting at REGION_ENTRY and ending
   10023              :    at REGION_EXIT.  */
   10024              : 
   10025              : static void
   10026         1651 : mark_loops_in_oacc_kernels_region (basic_block region_entry,
   10027              :                                    basic_block region_exit)
   10028              : {
   10029         1651 :   class loop *outer = region_entry->loop_father;
   10030         1651 :   gcc_assert (region_exit == NULL || outer == region_exit->loop_father);
   10031              : 
   10032              :   /* Don't parallelize the kernels region if it contains more than one outer
   10033              :      loop.  */
   10034         1651 :   unsigned int nr_outer_loops = 0;
   10035         1651 :   class loop *single_outer = NULL;
   10036        33062 :   for (class loop *loop = outer->inner; loop != NULL; loop = loop->next)
   10037              :     {
   10038        31411 :       gcc_assert (loop_outer (loop) == outer);
   10039              : 
   10040        31411 :       if (!dominated_by_p (CDI_DOMINATORS, loop->header, region_entry))
   10041        23379 :         continue;
   10042              : 
   10043        14588 :       if (region_exit != NULL
   10044         8032 :           && dominated_by_p (CDI_DOMINATORS, loop->header, region_exit))
   10045         6556 :         continue;
   10046              : 
   10047         1476 :       nr_outer_loops++;
   10048         1476 :       single_outer = loop;
   10049              :     }
   10050         1651 :   if (nr_outer_loops != 1)
   10051              :     return;
   10052              : 
   10053         1336 :   for (class loop *loop = single_outer->inner;
   10054         2293 :        loop != NULL;
   10055          957 :        loop = loop->inner)
   10056         1045 :     if (loop->next)
   10057              :       return;
   10058              : 
   10059              :   /* Mark the loops in the region.  */
   10060         3269 :   for (class loop *loop = single_outer; loop != NULL; loop = loop->inner)
   10061         2021 :     loop->in_oacc_kernels_region = true;
   10062              : }
   10063              : 
   10064              : /* Build target argument identifier from the DEVICE identifier, value
   10065              :    identifier ID and whether the element also has a SUBSEQUENT_PARAM.  */
   10066              : 
   10067              : static tree
   10068        25706 : get_target_argument_identifier_1 (int device, bool subseqent_param, int id)
   10069              : {
   10070        25706 :   tree t = build_int_cst (integer_type_node, device);
   10071        25706 :   if (subseqent_param)
   10072          772 :     t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
   10073              :                      build_int_cst (integer_type_node,
   10074              :                                     GOMP_TARGET_ARG_SUBSEQUENT_PARAM));
   10075        25706 :   t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
   10076              :                    build_int_cst (integer_type_node, id));
   10077        25706 :   return t;
   10078              : }
   10079              : 
   10080              : /* Like above but return it in type that can be directly stored as an element
   10081              :    of the argument array.  */
   10082              : 
   10083              : static tree
   10084          772 : get_target_argument_identifier (int device, bool subseqent_param, int id)
   10085              : {
   10086          772 :   tree t = get_target_argument_identifier_1 (device, subseqent_param, id);
   10087          772 :   return fold_convert (ptr_type_node, t);
   10088              : }
   10089              : 
   10090              : /* Return a target argument consisting of DEVICE identifier, value identifier
   10091              :    ID, and the actual VALUE.  */
   10092              : 
   10093              : static tree
   10094        24934 : get_target_argument_value (gimple_stmt_iterator *gsi, int device, int id,
   10095              :                            tree value)
   10096              : {
   10097        24934 :   tree t = fold_build2 (LSHIFT_EXPR, integer_type_node,
   10098              :                         fold_convert (integer_type_node, value),
   10099              :                         build_int_cst (unsigned_type_node,
   10100              :                                        GOMP_TARGET_ARG_VALUE_SHIFT));
   10101        24934 :   t = fold_build2 (BIT_IOR_EXPR, integer_type_node, t,
   10102              :                    get_target_argument_identifier_1 (device, false, id));
   10103        24934 :   t = fold_convert (ptr_type_node, t);
   10104        24934 :   return force_gimple_operand_gsi (gsi, t, true, NULL, true, GSI_SAME_STMT);
   10105              : }
   10106              : 
   10107              : /* If VALUE is an integer constant greater than -2^15 and smaller than 2^15,
   10108              :    push one argument to ARGS with both the DEVICE, ID and VALUE embedded in it,
   10109              :    otherwise push an identifier (with DEVICE and ID) and the VALUE in two
   10110              :    arguments.  */
   10111              : 
   10112              : static void
   10113        25706 : push_target_argument_according_to_value (gimple_stmt_iterator *gsi, int device,
   10114              :                                          int id, tree value, vec <tree> *args)
   10115              : {
   10116        25706 :   if (tree_fits_shwi_p (value)
   10117        24934 :       && tree_to_shwi (value) > -(1 << 15)
   10118        24934 :       && tree_to_shwi (value) < (1 << 15))
   10119        24934 :     args->quick_push (get_target_argument_value (gsi, device, id, value));
   10120              :   else
   10121              :     {
   10122          772 :       args->quick_push (get_target_argument_identifier (device, true, id));
   10123          772 :       value = fold_convert (ptr_type_node, value);
   10124          772 :       value = force_gimple_operand_gsi (gsi, value, true, NULL, true,
   10125              :                                         GSI_SAME_STMT);
   10126          772 :       args->quick_push (value);
   10127              :     }
   10128        25706 : }
   10129              : 
   10130              : /* Create an array of arguments that is then passed to GOMP_target.  */
   10131              : 
   10132              : static tree
   10133        12853 : get_target_arguments (gimple_stmt_iterator *gsi, gomp_target *tgt_stmt)
   10134              : {
   10135        12853 :   auto_vec <tree, 6> args;
   10136        12853 :   tree clauses = gimple_omp_target_clauses (tgt_stmt);
   10137        12853 :   tree num_teams = omp_find_clause (clauses, OMP_CLAUSE_NUM_TEAMS);
   10138        12853 :   tree thread_limit = omp_find_clause (clauses, OMP_CLAUSE_THREAD_LIMIT);
   10139        12853 :   tree message = omp_find_clause (clauses, OMP_CLAUSE_MESSAGE);
   10140        12853 :   if (message)
   10141              :     /* FIXME: error termination checking also requires to check
   10142              :        OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR not only UPPER, i.e. it
   10143              :        needs to be passed to the runtime! Note that both target and
   10144              :        a nested teams can have a 'message' clause - and both take a
   10145              :        thread_limit clause.  */
   10146            1 :     sorry_at (OMP_CLAUSE_LOCATION (message), "%<message%> clause");
   10147        12853 :   if (num_teams)
   10148              :     {
   10149        12853 :       tree expr = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (num_teams);
   10150              :       /* FIXME: Update omp-low.cc's lower_omp_teams when implementing.  */
   10151        12853 :       if (TREE_CODE (expr) == TREE_LIST
   10152            5 :           && OMP_CLAUSE_NUM_TEAMS_DIMS (num_teams)
   10153        12858 :           && TREE_CHAIN (expr))
   10154              :         {
   10155            3 :           sorry_at (OMP_CLAUSE_LOCATION (num_teams),
   10156              :                     "%<num_teams%> clause with %<dims%> modifier");
   10157            3 :           num_teams = TREE_VALUE (expr);
   10158              :         }
   10159        12850 :       else if (TREE_CODE (expr) == TREE_LIST)
   10160            2 :         num_teams = (TREE_CHAIN (expr)
   10161            2 :                      ? TREE_VALUE (TREE_CHAIN (expr)) : TREE_VALUE (expr));
   10162              :       else
   10163              :         num_teams = expr;
   10164              :     }
   10165              :   else
   10166            0 :     num_teams = integer_minus_one_node;
   10167        12853 :   push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
   10168              :                                            GOMP_TARGET_ARG_NUM_TEAMS,
   10169              :                                            num_teams, &args);
   10170        12853 :   if (thread_limit)
   10171              :     {
   10172        12817 :       tree expr = OMP_CLAUSE_THREAD_LIMIT_EXPR (thread_limit);
   10173        12817 :       if (TREE_CODE (expr) == TREE_LIST)
   10174              :         {
   10175           75 :           if (OMP_CLAUSE_THREAD_LIMIT_DIMS (thread_limit)
   10176           75 :               && (TREE_CHAIN (expr)
   10177            1 :                   || OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit)))
   10178            4 :             sorry_at (OMP_CLAUSE_LOCATION (thread_limit),
   10179              :                       "%<thread_limit%> clause with %<dims%> modifier");
   10180           71 :           else if (OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit))
   10181            1 :             sorry_at (OMP_CLAUSE_LOCATION (thread_limit),
   10182              :                       "%<thread_limit%> clause with %<strict%> modifier");
   10183           75 :           expr = TREE_VALUE (expr);
   10184              :         }
   10185        12742 :       else if (OMP_CLAUSE_THREAD_LIMIT_STRICT (thread_limit))
   10186            0 :         sorry_at (OMP_CLAUSE_LOCATION (thread_limit),
   10187              :                   "%<thread_limit%> clause with %<strict%> modifier");
   10188              :       thread_limit = expr;
   10189              :     }
   10190              :   else
   10191           36 :     thread_limit = integer_minus_one_node;
   10192        12853 :   push_target_argument_according_to_value (gsi, GOMP_TARGET_ARG_DEVICE_ALL,
   10193              :                                            GOMP_TARGET_ARG_THREAD_LIMIT,
   10194              :                                            thread_limit, &args);
   10195              : 
   10196              :   /* Produce more, perhaps device specific, arguments here.  */
   10197              : 
   10198        25706 :   tree argarray = create_tmp_var (build_array_type_nelts (ptr_type_node,
   10199        12853 :                                                           args.length () + 1),
   10200              :                                   ".omp_target_args");
   10201        39331 :   for (unsigned i = 0; i < args.length (); i++)
   10202              :     {
   10203        26478 :       tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
   10204        26478 :                          build_int_cst (integer_type_node, i),
   10205              :                          NULL_TREE, NULL_TREE);
   10206        26478 :       gsi_insert_before (gsi, gimple_build_assign (ref, args[i]),
   10207              :                          GSI_SAME_STMT);
   10208              :     }
   10209        25706 :   tree ref = build4 (ARRAY_REF, ptr_type_node, argarray,
   10210        12853 :                      build_int_cst (integer_type_node, args.length ()),
   10211              :                      NULL_TREE, NULL_TREE);
   10212        12853 :   gsi_insert_before (gsi, gimple_build_assign (ref, null_pointer_node),
   10213              :                      GSI_SAME_STMT);
   10214        12853 :   TREE_ADDRESSABLE (argarray) = 1;
   10215        12853 :   return build_fold_addr_expr (argarray);
   10216        12853 : }
   10217              : 
   10218              : /* Expand the GIMPLE_OMP_TARGET starting at REGION.  */
   10219              : 
   10220              : static void
   10221        38625 : expand_omp_target (struct omp_region *region)
   10222              : {
   10223        38625 :   basic_block entry_bb, exit_bb, new_bb;
   10224        38625 :   struct function *child_cfun;
   10225        38625 :   tree child_fn, child_fn2, block, t, c;
   10226        38625 :   gimple_stmt_iterator gsi;
   10227        38625 :   gomp_target *entry_stmt;
   10228        38625 :   gimple *stmt;
   10229        38625 :   edge e;
   10230        38625 :   bool offloaded;
   10231        38625 :   int target_kind;
   10232              : 
   10233        38625 :   entry_stmt = as_a <gomp_target *> (last_nondebug_stmt (region->entry));
   10234        38625 :   target_kind = gimple_omp_target_kind (entry_stmt);
   10235        38625 :   new_bb = region->entry;
   10236              : 
   10237        38625 :   offloaded = is_gimple_omp_offloaded (entry_stmt);
   10238        38625 :   switch (target_kind)
   10239              :     {
   10240        38625 :     case GF_OMP_TARGET_KIND_REGION:
   10241        38625 :     case GF_OMP_TARGET_KIND_UPDATE:
   10242        38625 :     case GF_OMP_TARGET_KIND_ENTER_DATA:
   10243        38625 :     case GF_OMP_TARGET_KIND_EXIT_DATA:
   10244        38625 :     case GF_OMP_TARGET_KIND_OACC_PARALLEL:
   10245        38625 :     case GF_OMP_TARGET_KIND_OACC_KERNELS:
   10246        38625 :     case GF_OMP_TARGET_KIND_OACC_SERIAL:
   10247        38625 :     case GF_OMP_TARGET_KIND_OACC_UPDATE:
   10248        38625 :     case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
   10249        38625 :     case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
   10250        38625 :     case GF_OMP_TARGET_KIND_OACC_DECLARE:
   10251        38625 :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
   10252        38625 :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
   10253        38625 :     case GF_OMP_TARGET_KIND_DATA:
   10254        38625 :     case GF_OMP_TARGET_KIND_OACC_DATA:
   10255        38625 :     case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
   10256        38625 :     case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
   10257        38625 :       break;
   10258            0 :     default:
   10259            0 :       gcc_unreachable ();
   10260              :     }
   10261              : 
   10262        38625 :   tree clauses = gimple_omp_target_clauses (entry_stmt);
   10263              : 
   10264        38625 :   bool is_ancestor = false;
   10265        38625 :   bool is_host_only = false;
   10266        38625 :   child_fn = child_fn2 = NULL_TREE;
   10267        38625 :   child_cfun = NULL;
   10268        38625 :   if (offloaded)
   10269              :     {
   10270        22248 :       c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE);
   10271        22248 :       if (ENABLE_OFFLOADING && c)
   10272              :         is_ancestor = OMP_CLAUSE_DEVICE_ANCESTOR (c);
   10273        22248 :       c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE_TYPE);
   10274        22248 :       if (c && OMP_CLAUSE_DEVICE_TYPE_KIND (c) == OMP_CLAUSE_DEVICE_TYPE_HOST)
   10275              :         is_host_only = true;
   10276        22248 :       child_fn = gimple_omp_target_child_fn (entry_stmt);
   10277        22248 :       child_cfun = DECL_STRUCT_FUNCTION (child_fn);
   10278              :     }
   10279              : 
   10280              :   /* Supported by expand_omp_taskreg, but not here.  */
   10281        22248 :   if (child_cfun != NULL)
   10282        22248 :     gcc_checking_assert (!child_cfun->cfg);
   10283        38625 :   gcc_checking_assert (!gimple_in_ssa_p (cfun));
   10284              : 
   10285        38625 :   entry_bb = region->entry;
   10286        38625 :   exit_bb = region->exit;
   10287              : 
   10288        38625 :   if (target_kind == GF_OMP_TARGET_KIND_OACC_KERNELS)
   10289         1651 :     mark_loops_in_oacc_kernels_region (region->entry, region->exit);
   10290              : 
   10291              :   /* Going on, all OpenACC compute constructs are mapped to
   10292              :      'BUILT_IN_GOACC_PARALLEL', and get their compute regions outlined.
   10293              :      To distinguish between them, we attach attributes.  */
   10294        38625 :   switch (target_kind)
   10295              :     {
   10296         6825 :     case GF_OMP_TARGET_KIND_OACC_PARALLEL:
   10297         6825 :       DECL_ATTRIBUTES (child_fn)
   10298         6825 :         = tree_cons (get_identifier ("oacc parallel"),
   10299         6825 :                      NULL_TREE, DECL_ATTRIBUTES (child_fn));
   10300         6825 :       break;
   10301         1651 :     case GF_OMP_TARGET_KIND_OACC_KERNELS:
   10302         1651 :       DECL_ATTRIBUTES (child_fn)
   10303         1651 :         = tree_cons (get_identifier ("oacc kernels"),
   10304         1651 :                      NULL_TREE, DECL_ATTRIBUTES (child_fn));
   10305         1651 :       break;
   10306          756 :     case GF_OMP_TARGET_KIND_OACC_SERIAL:
   10307          756 :       DECL_ATTRIBUTES (child_fn)
   10308          756 :         = tree_cons (get_identifier ("oacc serial"),
   10309          756 :                      NULL_TREE, DECL_ATTRIBUTES (child_fn));
   10310          756 :       break;
   10311           54 :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
   10312           54 :       DECL_ATTRIBUTES (child_fn)
   10313           54 :         = tree_cons (get_identifier ("oacc parallel_kernels_parallelized"),
   10314           54 :                      NULL_TREE, DECL_ATTRIBUTES (child_fn));
   10315           54 :       break;
   10316          109 :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
   10317          109 :       DECL_ATTRIBUTES (child_fn)
   10318          109 :         = tree_cons (get_identifier ("oacc parallel_kernels_gang_single"),
   10319          109 :                      NULL_TREE, DECL_ATTRIBUTES (child_fn));
   10320          109 :       break;
   10321        29230 :     default:
   10322              :       /* Make sure we don't miss any.  */
   10323        29230 :       gcc_checking_assert (!(is_gimple_omp_oacc (entry_stmt)
   10324              :                              && is_gimple_omp_offloaded (entry_stmt)));
   10325              :       break;
   10326              :     }
   10327              : 
   10328        38625 :   if (offloaded)
   10329              :     {
   10330        22248 :       unsigned srcidx, dstidx, num;
   10331              : 
   10332              :       /* If the offloading region needs data sent from the parent
   10333              :          function, then the very first statement (except possible
   10334              :          tree profile counter updates) of the offloading body
   10335              :          is a copy assignment .OMP_DATA_I = &.OMP_DATA_O.  Since
   10336              :          &.OMP_DATA_O is passed as an argument to the child function,
   10337              :          we need to replace it with the argument as seen by the child
   10338              :          function.
   10339              : 
   10340              :          In most cases, this will end up being the identity assignment
   10341              :          .OMP_DATA_I = .OMP_DATA_I.  However, if the offloading body had
   10342              :          a function call that has been inlined, the original PARM_DECL
   10343              :          .OMP_DATA_I may have been converted into a different local
   10344              :          variable.  In which case, we need to keep the assignment.  */
   10345        22248 :       tree data_arg = gimple_omp_target_data_arg (entry_stmt);
   10346        22248 :       if (data_arg)
   10347              :         {
   10348        17392 :           basic_block entry_succ_bb = single_succ (entry_bb);
   10349        17392 :           gimple_stmt_iterator gsi;
   10350        17392 :           tree arg;
   10351        17392 :           gassign *tgtcopy_stmt = NULL;
   10352        17392 :           tree sender = TREE_VEC_ELT (data_arg, 0);
   10353              : 
   10354        34784 :           for (gsi = gsi_start_bb (entry_succ_bb); ; gsi_next (&gsi))
   10355              :             {
   10356        17392 :               gcc_assert (!gsi_end_p (gsi));
   10357        17392 :               gassign *stmt = dyn_cast<gassign *> (gsi_stmt (gsi));
   10358        17392 :               if (!stmt)
   10359            0 :                 continue;
   10360              : 
   10361        17392 :               if (gimple_num_ops (stmt) == 2)
   10362              :                 {
   10363        17392 :                   tree arg = gimple_assign_rhs1 (stmt);
   10364              : 
   10365              :                   /* We're ignoring the subcode because we're
   10366              :                      effectively doing a STRIP_NOPS.  */
   10367              : 
   10368        17392 :                   if ((TREE_CODE (arg) == ADDR_EXPR
   10369        17284 :                        && TREE_OPERAND (arg, 0) == sender)
   10370        17392 :                       || arg == sender)
   10371              :                     {
   10372        17392 :                       tgtcopy_stmt = stmt;
   10373        17392 :                       break;
   10374              :                     }
   10375              :                 }
   10376            0 :             }
   10377              : 
   10378        17392 :           gcc_assert (tgtcopy_stmt != NULL);
   10379        17392 :           arg = DECL_ARGUMENTS (child_fn);
   10380              : 
   10381        17392 :           gcc_assert (gimple_assign_lhs (tgtcopy_stmt) == arg);
   10382        17392 :           gsi_remove (&gsi, true);
   10383              :         }
   10384              : 
   10385              :       /* Declare local variables needed in CHILD_CFUN.  */
   10386        22248 :       block = DECL_INITIAL (child_fn);
   10387        22248 :       BLOCK_VARS (block) = vec2chain (child_cfun->local_decls);
   10388              :       /* The gimplifier could record temporaries in the offloading block
   10389              :          rather than in containing function's local_decls chain,
   10390              :          which would mean cgraph missed finalizing them.  Do it now.  */
   10391       162669 :       for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
   10392       140421 :         if (VAR_P (t) && TREE_STATIC (t) && !DECL_EXTERNAL (t))
   10393            0 :           varpool_node::finalize_decl (t);
   10394        22248 :       DECL_SAVED_TREE (child_fn) = NULL;
   10395              :       /* We'll create a CFG for child_fn, so no gimple body is needed.  */
   10396        22248 :       gimple_set_body (child_fn, NULL);
   10397        22248 :       TREE_USED (block) = 1;
   10398              : 
   10399              :       /* Reset DECL_CONTEXT on function arguments.  */
   10400        44496 :       for (t = DECL_ARGUMENTS (child_fn); t; t = DECL_CHAIN (t))
   10401        22248 :         DECL_CONTEXT (t) = child_fn;
   10402              : 
   10403              :       /* Split ENTRY_BB at GIMPLE_*,
   10404              :          so that it can be moved to the child function.  */
   10405        22248 :       gsi = gsi_last_nondebug_bb (entry_bb);
   10406        22248 :       stmt = gsi_stmt (gsi);
   10407        22248 :       gcc_assert (stmt
   10408              :                   && gimple_code (stmt) == gimple_code (entry_stmt));
   10409        22248 :       e = split_block (entry_bb, stmt);
   10410        22248 :       gsi_remove (&gsi, true);
   10411        22248 :       entry_bb = e->dest;
   10412        22248 :       single_succ_edge (entry_bb)->flags = EDGE_FALLTHRU;
   10413              : 
   10414              :       /* Convert GIMPLE_OMP_RETURN into a RETURN_EXPR.  */
   10415        22248 :       if (exit_bb)
   10416              :         {
   10417        22172 :           gsi = gsi_last_nondebug_bb (exit_bb);
   10418        22172 :           gcc_assert (!gsi_end_p (gsi)
   10419              :                       && gimple_code (gsi_stmt (gsi)) == GIMPLE_OMP_RETURN);
   10420        22172 :           stmt = gimple_build_return (NULL);
   10421        22172 :           gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
   10422        22172 :           gsi_remove (&gsi, true);
   10423              :         }
   10424              : 
   10425              :       /* Move the offloading region into CHILD_CFUN.  */
   10426              : 
   10427        22248 :       block = gimple_block (entry_stmt);
   10428              : 
   10429        22248 :       new_bb = move_sese_region_to_fn (child_cfun, entry_bb, exit_bb, block);
   10430        22248 :       if (exit_bb)
   10431        22172 :         single_succ_edge (new_bb)->flags = EDGE_FALLTHRU;
   10432              :       /* When the OMP expansion process cannot guarantee an up-to-date
   10433              :          loop tree arrange for the child function to fixup loops.  */
   10434        22248 :       if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
   10435        22248 :         child_cfun->x_current_loops->state |= LOOPS_NEED_FIXUP;
   10436              : 
   10437              :       /* Remove non-local VAR_DECLs from child_cfun->local_decls list.  */
   10438        22248 :       num = vec_safe_length (child_cfun->local_decls);
   10439       679674 :       for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
   10440              :         {
   10441       657426 :           t = (*child_cfun->local_decls)[srcidx];
   10442       657426 :           if (DECL_CONTEXT (t) == cfun->decl)
   10443       140421 :             continue;
   10444       517005 :           if (srcidx != dstidx)
   10445       502013 :             (*child_cfun->local_decls)[dstidx] = t;
   10446       517005 :           dstidx++;
   10447              :         }
   10448        22248 :       if (dstidx != num)
   10449        18877 :         vec_safe_truncate (child_cfun->local_decls, dstidx);
   10450              : 
   10451              :       /* Inform the callgraph about the new function.  */
   10452        22248 :       child_cfun->curr_properties = cfun->curr_properties;
   10453        22248 :       child_cfun->has_simduid_loops |= cfun->has_simduid_loops;
   10454        22248 :       child_cfun->has_force_vectorize_loops |= cfun->has_force_vectorize_loops;
   10455        22248 :       cgraph_node *node = cgraph_node::get_create (child_fn);
   10456        22248 :       node->parallelized_function = 1;
   10457        44496 :       node->has_omp_variant_constructs
   10458        22248 :         |= cgraph_node::get (cfun->decl)->has_omp_variant_constructs;
   10459        22248 :       cgraph_node::add_new_function (child_fn, true);
   10460              : 
   10461              :       /* Add the new function to the offload table.  */
   10462        22248 :       if (ENABLE_OFFLOADING)
   10463              :         {
   10464              :           if (in_lto_p)
   10465              :             DECL_PRESERVE_P (child_fn) = 1;
   10466              :           if (!is_ancestor && !is_host_only)
   10467              :             vec_safe_push (offload_funcs, child_fn);
   10468              :         }
   10469              : 
   10470        22248 :       bool need_asm = DECL_ASSEMBLER_NAME_SET_P (current_function_decl)
   10471        22248 :                       && !DECL_ASSEMBLER_NAME_SET_P (child_fn);
   10472              : 
   10473              :       /* Fix the callgraph edges for child_cfun.  Those for cfun will be
   10474              :          fixed in a following pass.  */
   10475        22248 :       push_cfun (child_cfun);
   10476        22248 :       if (need_asm)
   10477        22242 :         assign_assembler_name_if_needed (child_fn);
   10478        22248 :       cgraph_edge::rebuild_edges ();
   10479              : 
   10480              :       /* Some EH regions might become dead, see PR34608.  If
   10481              :          pass_cleanup_cfg isn't the first pass to happen with the
   10482              :          new child, these dead EH edges might cause problems.
   10483              :          Clean them up now.  */
   10484        22248 :       if (flag_exceptions)
   10485              :         {
   10486         8792 :           basic_block bb;
   10487         8792 :           bool changed = false;
   10488              : 
   10489       126404 :           FOR_EACH_BB_FN (bb, cfun)
   10490       117612 :             changed |= gimple_purge_dead_eh_edges (bb);
   10491         8792 :           if (changed)
   10492            0 :             cleanup_tree_cfg ();
   10493              :         }
   10494        22248 :       if (flag_checking && !loops_state_satisfies_p (LOOPS_NEED_FIXUP))
   10495            0 :         verify_loop_structure ();
   10496        22248 :       pop_cfun ();
   10497              : 
   10498        22248 :       if (dump_file && !gimple_in_ssa_p (cfun))
   10499              :         {
   10500           37 :           omp_any_child_fn_dumped = true;
   10501           37 :           dump_function_header (dump_file, child_fn, dump_flags);
   10502           37 :           dump_function_to_file (child_fn, dump_file, dump_flags);
   10503              :         }
   10504              : 
   10505        22248 :       adjust_context_and_scope (region, gimple_block (entry_stmt), child_fn);
   10506              : 
   10507              :       /* Handle the case that an inner ancestor:1 target is called by an outer
   10508              :          target region. */
   10509        22248 :       if (is_ancestor)
   10510              :         {
   10511              :           cgraph_node *fn2_node;
   10512              :           child_fn2 = build_decl (DECL_SOURCE_LOCATION (child_fn),
   10513              :                                   FUNCTION_DECL,
   10514              :                                   clone_function_name (child_fn, "nohost"),
   10515              :                                   TREE_TYPE (child_fn));
   10516              :           if (in_lto_p)
   10517              :             DECL_PRESERVE_P (child_fn2) = 1;
   10518              :           TREE_STATIC (child_fn2) = 1;
   10519              :           DECL_ARTIFICIAL (child_fn2) = 1;
   10520              :           DECL_IGNORED_P (child_fn2) = 0;
   10521              :           TREE_PUBLIC (child_fn2) = 0;
   10522              :           DECL_UNINLINABLE (child_fn2) = 1;
   10523              :           DECL_EXTERNAL (child_fn2) = 0;
   10524              :           DECL_CONTEXT (child_fn2) = DECL_CONTEXT (child_fn);
   10525              :           DECL_INITIAL (child_fn2) = make_node (BLOCK);
   10526              :           BLOCK_SUPERCONTEXT (DECL_INITIAL (child_fn2)) = child_fn2;
   10527              :           DECL_ATTRIBUTES (child_fn)
   10528              :             = remove_attribute ("omp target entrypoint",
   10529              :                                 DECL_ATTRIBUTES (child_fn));
   10530              :           DECL_ATTRIBUTES (child_fn2)
   10531              :             = tree_cons (get_identifier ("omp target device_ancestor_nohost"),
   10532              :                          NULL_TREE, copy_list (DECL_ATTRIBUTES (child_fn)));
   10533              :           DECL_ATTRIBUTES (child_fn)
   10534              :             = tree_cons (get_identifier ("omp target device_ancestor_host"),
   10535              :                          NULL_TREE, DECL_ATTRIBUTES (child_fn));
   10536              :           DECL_FUNCTION_SPECIFIC_OPTIMIZATION (child_fn2)
   10537              :             = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
   10538              :           DECL_FUNCTION_SPECIFIC_TARGET (child_fn2)
   10539              :             = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
   10540              :           DECL_FUNCTION_VERSIONED (child_fn2)
   10541              :             = DECL_FUNCTION_VERSIONED (current_function_decl);
   10542              : 
   10543              :           fn2_node = cgraph_node::get_create (child_fn2);
   10544              :           fn2_node->offloadable = 1;
   10545              :           fn2_node->force_output = 1;
   10546              :           node->offloadable = 0;
   10547              : 
   10548              :           /* Enable pass_omp_device_lower pass.  */
   10549              :           fn2_node = cgraph_node::get (DECL_CONTEXT (child_fn));
   10550              :           fn2_node->has_omp_variant_constructs = 1;
   10551              : 
   10552              :           t = build_decl (DECL_SOURCE_LOCATION (child_fn),
   10553              :                           RESULT_DECL, NULL_TREE, void_type_node);
   10554              :           DECL_ARTIFICIAL (t) = 1;
   10555              :           DECL_IGNORED_P (t) = 1;
   10556              :           DECL_CONTEXT (t) = child_fn2;
   10557              :           DECL_RESULT (child_fn2) = t;
   10558              :           DECL_SAVED_TREE (child_fn2) = build1 (RETURN_EXPR,
   10559              :                                                 void_type_node, NULL);
   10560              :           tree tmp = DECL_ARGUMENTS (child_fn);
   10561              :           t = build_decl (DECL_SOURCE_LOCATION (child_fn), PARM_DECL,
   10562              :                           DECL_NAME (tmp), TREE_TYPE (tmp));
   10563              :           DECL_ARTIFICIAL (t) = 1;
   10564              :           DECL_NAMELESS (t) = 1;
   10565              :           DECL_ARG_TYPE (t) = ptr_type_node;
   10566              :           DECL_CONTEXT (t) = current_function_decl;
   10567              :           TREE_USED (t) = 1;
   10568              :           TREE_READONLY (t) = 1;
   10569              :           DECL_ARGUMENTS (child_fn2) = t;
   10570              :           gcc_assert (TREE_CHAIN (tmp) == NULL_TREE);
   10571              : 
   10572              :           gimplify_function_tree (child_fn2);
   10573              :           cgraph_node::add_new_function (child_fn2, true);
   10574              : 
   10575              :           vec_safe_push (offload_funcs, child_fn2);
   10576              :           if (dump_file && !gimple_in_ssa_p (cfun))
   10577              :             {
   10578              :               dump_function_header (dump_file, child_fn2, dump_flags);
   10579              :               dump_function_to_file (child_fn2, dump_file, dump_flags);
   10580              :             }
   10581              :         }
   10582              :     }
   10583              : 
   10584              :   /* Emit a library call to launch the offloading region, or do data
   10585              :      transfers.  */
   10586        38625 :   tree t1, t2, t3, t4, depend;
   10587        38625 :   enum built_in_function start_ix;
   10588        38625 :   unsigned int flags_i = 0;
   10589              : 
   10590        38625 :   switch (gimple_omp_target_kind (entry_stmt))
   10591              :     {
   10592              :     case GF_OMP_TARGET_KIND_REGION:
   10593              :       start_ix = BUILT_IN_GOMP_TARGET;
   10594              :       break;
   10595              :     case GF_OMP_TARGET_KIND_DATA:
   10596              :       start_ix = BUILT_IN_GOMP_TARGET_DATA;
   10597              :       break;
   10598              :     case GF_OMP_TARGET_KIND_UPDATE:
   10599              :       start_ix = BUILT_IN_GOMP_TARGET_UPDATE;
   10600              :       break;
   10601              :     case GF_OMP_TARGET_KIND_ENTER_DATA:
   10602              :       start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
   10603              :       break;
   10604              :     case GF_OMP_TARGET_KIND_EXIT_DATA:
   10605              :       start_ix = BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA;
   10606              :       flags_i |= GOMP_TARGET_FLAG_EXIT_DATA;
   10607              :       break;
   10608              :     case GF_OMP_TARGET_KIND_OACC_PARALLEL:
   10609              :     case GF_OMP_TARGET_KIND_OACC_KERNELS:
   10610              :     case GF_OMP_TARGET_KIND_OACC_SERIAL:
   10611              :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
   10612              :     case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
   10613              :       start_ix = BUILT_IN_GOACC_PARALLEL;
   10614              :       break;
   10615              :     case GF_OMP_TARGET_KIND_OACC_DATA:
   10616              :     case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
   10617              :     case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
   10618              :       start_ix = BUILT_IN_GOACC_DATA_START;
   10619              :       break;
   10620              :     case GF_OMP_TARGET_KIND_OACC_UPDATE:
   10621              :       start_ix = BUILT_IN_GOACC_UPDATE;
   10622              :       break;
   10623              :     case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
   10624              :       start_ix = BUILT_IN_GOACC_ENTER_DATA;
   10625              :       break;
   10626              :     case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
   10627              :       start_ix = BUILT_IN_GOACC_EXIT_DATA;
   10628              :       break;
   10629              :     case GF_OMP_TARGET_KIND_OACC_DECLARE:
   10630              :       start_ix = BUILT_IN_GOACC_DECLARE;
   10631              :       break;
   10632            0 :     default:
   10633            0 :       gcc_unreachable ();
   10634              :     }
   10635              : 
   10636        38625 :   tree device = NULL_TREE;
   10637        38625 :   location_t device_loc = UNKNOWN_LOCATION;
   10638        38625 :   tree goacc_flags = NULL_TREE;
   10639        38625 :   bool need_device_adjustment = false;
   10640        38625 :   gimple_stmt_iterator adj_gsi;
   10641        38625 :   if (is_gimple_omp_oacc (entry_stmt))
   10642              :     {
   10643              :       /* By default, no GOACC_FLAGs are set.  */
   10644        14785 :       goacc_flags = integer_zero_node;
   10645              :     }
   10646              :   else
   10647              :     {
   10648        23840 :       if (is_host_only)
   10649            6 :         device = build_int_cst (integer_type_node,
   10650              :                                 GOMP_DEVICE_HOST_FALLBACK - 1);
   10651        23834 :       else if ((c = omp_find_clause (clauses, OMP_CLAUSE_DEVICE)) != NULL_TREE)
   10652              :         {
   10653          932 :           device = OMP_CLAUSE_DEVICE_ID (c);
   10654              :           /* Ensure 'device' is of the correct type.  */
   10655          932 :           device = fold_convert_loc (device_loc, integer_type_node, device);
   10656          932 :           if (TREE_CODE (device) == INTEGER_CST)
   10657              :             {
   10658           54 :               if (wi::to_wide (device) == GOMP_DEVICE_ICV)
   10659            2 :                 device = build_int_cst (integer_type_node,
   10660              :                                         GOMP_DEVICE_HOST_FALLBACK);
   10661           52 :               else if (wi::to_wide (device) == GOMP_DEVICE_HOST_FALLBACK)
   10662            0 :                 device = build_int_cst (integer_type_node,
   10663              :                                         GOMP_DEVICE_HOST_FALLBACK - 1);
   10664              :             }
   10665              :           else
   10666              :             need_device_adjustment = true;
   10667          932 :           device_loc = OMP_CLAUSE_LOCATION (c);
   10668          932 :           if (OMP_CLAUSE_DEVICE_ANCESTOR (c))
   10669           41 :             device = build_int_cst (integer_type_node,
   10670              :                                     GOMP_DEVICE_HOST_FALLBACK);
   10671              :         }
   10672              :       else
   10673              :         {
   10674              :           /* By default, the value of DEVICE is GOMP_DEVICE_ICV (let runtime
   10675              :              library choose).  */
   10676        22902 :           device = build_int_cst (integer_type_node, GOMP_DEVICE_ICV);
   10677        22902 :           device_loc = gimple_location (entry_stmt);
   10678              :         }
   10679              : 
   10680        23840 :       c = omp_find_clause (clauses, OMP_CLAUSE_NOWAIT);
   10681              :       /* FIXME: in_reduction(...) nowait is unimplemented yet, pretend
   10682              :          nowait doesn't appear.  */
   10683        23840 :       if (c && omp_find_clause (clauses, OMP_CLAUSE_IN_REDUCTION))
   10684              :         c = NULL;
   10685        23576 :       if (c)
   10686          140 :         flags_i |= GOMP_TARGET_FLAG_NOWAIT;
   10687              :     }
   10688              : 
   10689              :   /* By default, there is no conditional.  */
   10690        38625 :   tree cond = NULL_TREE;
   10691        38625 :   c = omp_find_clause (clauses, OMP_CLAUSE_IF);
   10692        38625 :   if (c)
   10693         1741 :     cond = OMP_CLAUSE_IF_EXPR (c);
   10694              :   /* If we found the clause 'if (cond)', build:
   10695              :      OpenACC: goacc_flags = (cond ? goacc_flags
   10696              :                                   : goacc_flags | GOACC_FLAG_HOST_FALLBACK)
   10697              :      OpenMP: device = (cond ? device : GOMP_DEVICE_HOST_FALLBACK) */
   10698         1741 :   if (cond)
   10699              :     {
   10700         1741 :       tree *tp;
   10701         1741 :       if (is_gimple_omp_oacc (entry_stmt))
   10702              :         tp = &goacc_flags;
   10703              :       else
   10704          666 :         tp = &device;
   10705              : 
   10706         1741 :       cond = gimple_boolify (cond);
   10707              : 
   10708         1741 :       basic_block cond_bb, then_bb, else_bb;
   10709         1741 :       edge e;
   10710         1741 :       tree tmp_var = create_tmp_var (TREE_TYPE (*tp));
   10711         1741 :       if (offloaded)
   10712          806 :         e = split_block_after_labels (new_bb);
   10713              :       else
   10714              :         {
   10715          935 :           gsi = gsi_last_nondebug_bb (new_bb);
   10716          935 :           gsi_prev (&gsi);
   10717          935 :           e = split_block (new_bb, gsi_stmt (gsi));
   10718              :         }
   10719         1741 :       cond_bb = e->src;
   10720         1741 :       new_bb = e->dest;
   10721         1741 :       remove_edge (e);
   10722              : 
   10723         1741 :       then_bb = create_empty_bb (cond_bb);
   10724         1741 :       else_bb = create_empty_bb (then_bb);
   10725         1741 :       set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
   10726         1741 :       set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
   10727              : 
   10728         1741 :       stmt = gimple_build_cond_empty (cond);
   10729         1741 :       gsi = gsi_last_bb (cond_bb);
   10730         1741 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
   10731              : 
   10732         1741 :       gsi = gsi_start_bb (then_bb);
   10733         1741 :       stmt = gimple_build_assign (tmp_var, *tp);
   10734         1741 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
   10735         1741 :       adj_gsi = gsi;
   10736              : 
   10737         1741 :       gsi = gsi_start_bb (else_bb);
   10738         1741 :       if (is_gimple_omp_oacc (entry_stmt))
   10739         1075 :         stmt = gimple_build_assign (tmp_var,
   10740              :                                     BIT_IOR_EXPR,
   10741              :                                     *tp,
   10742              :                                     build_int_cst (integer_type_node,
   10743              :                                                    GOACC_FLAG_HOST_FALLBACK));
   10744              :       else
   10745          666 :         stmt = gimple_build_assign (tmp_var,
   10746              :                                     build_int_cst (integer_type_node,
   10747              :                                                    GOMP_DEVICE_HOST_FALLBACK));
   10748         1741 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
   10749              : 
   10750         1741 :       make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
   10751         1741 :       make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
   10752         1741 :       add_bb_to_loop (then_bb, cond_bb->loop_father);
   10753         1741 :       add_bb_to_loop (else_bb, cond_bb->loop_father);
   10754         1741 :       make_edge (then_bb, new_bb, EDGE_FALLTHRU);
   10755         1741 :       make_edge (else_bb, new_bb, EDGE_FALLTHRU);
   10756              : 
   10757         1741 :       *tp = tmp_var;
   10758              : 
   10759         1741 :       gsi = gsi_last_nondebug_bb (new_bb);
   10760              :     }
   10761              :   else
   10762              :     {
   10763        36884 :       gsi = gsi_last_nondebug_bb (new_bb);
   10764              : 
   10765        36884 :       if (device != NULL_TREE)
   10766        23174 :         device = force_gimple_operand_gsi (&gsi, device, true, NULL_TREE,
   10767              :                                            true, GSI_SAME_STMT);
   10768        36884 :       if (need_device_adjustment)
   10769              :         {
   10770          404 :           tree tmp_var = create_tmp_var (TREE_TYPE (device));
   10771          404 :           gassign *stmt = gimple_build_assign (tmp_var, device);
   10772          404 :           gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
   10773          404 :           adj_gsi = gsi_for_stmt (stmt);
   10774          404 :           device = tmp_var;
   10775              :         }
   10776              :     }
   10777              : 
   10778        38625 :   if ((c = omp_find_clause (clauses, OMP_CLAUSE_SELF)) != NULL_TREE)
   10779              :     {
   10780          324 :       gcc_assert ((is_gimple_omp_oacc (entry_stmt) && offloaded)
   10781              :                   || (gimple_omp_target_kind (entry_stmt)
   10782              :                       == GF_OMP_TARGET_KIND_OACC_DATA_KERNELS));
   10783              : 
   10784           87 :       edge e;
   10785           87 :       if (offloaded)
   10786          237 :         e = split_block_after_labels (new_bb);
   10787              :       else
   10788              :         {
   10789           87 :           gsi = gsi_last_nondebug_bb (new_bb);
   10790           87 :           gsi_prev (&gsi);
   10791           87 :           e = split_block (new_bb, gsi_stmt (gsi));
   10792              :         }
   10793          324 :       basic_block cond_bb = e->src;
   10794          324 :       new_bb = e->dest;
   10795          324 :       remove_edge (e);
   10796              : 
   10797          324 :       basic_block then_bb = create_empty_bb (cond_bb);
   10798          324 :       basic_block else_bb = create_empty_bb (then_bb);
   10799          324 :       set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
   10800          324 :       set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
   10801              : 
   10802          324 :       tree self_cond = gimple_boolify (OMP_CLAUSE_SELF_EXPR (c));
   10803          324 :       stmt = gimple_build_cond_empty (self_cond);
   10804          324 :       gsi = gsi_last_bb (cond_bb);
   10805          324 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
   10806              : 
   10807          324 :       tree tmp_var = create_tmp_var (TREE_TYPE (goacc_flags));
   10808          324 :       stmt = gimple_build_assign (tmp_var, BIT_IOR_EXPR, goacc_flags,
   10809              :                                   build_int_cst (integer_type_node,
   10810              :                                                  GOACC_FLAG_LOCAL_DEVICE));
   10811          324 :       gsi = gsi_start_bb (then_bb);
   10812          324 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
   10813              : 
   10814          324 :       gsi = gsi_start_bb (else_bb);
   10815          324 :       stmt = gimple_build_assign (tmp_var, goacc_flags);
   10816          324 :       gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
   10817              : 
   10818          324 :       make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
   10819          324 :       make_edge (cond_bb, else_bb, EDGE_FALSE_VALUE);
   10820          324 :       add_bb_to_loop (then_bb, cond_bb->loop_father);
   10821          324 :       add_bb_to_loop (else_bb, cond_bb->loop_father);
   10822          324 :       make_edge (then_bb, new_bb, EDGE_FALLTHRU);
   10823          324 :       make_edge (else_bb, new_bb, EDGE_FALLTHRU);
   10824              : 
   10825          324 :       goacc_flags = tmp_var;
   10826          324 :       gsi = gsi_last_nondebug_bb (new_bb);
   10827              :     }
   10828              : 
   10829        38625 :   if (need_device_adjustment)
   10830              :     {
   10831          878 :       tree uns = fold_convert (unsigned_type_node, device);
   10832          878 :       uns = force_gimple_operand_gsi (&adj_gsi, uns, true, NULL_TREE,
   10833              :                                       false, GSI_CONTINUE_LINKING);
   10834          878 :       edge e = split_block (gsi_bb (adj_gsi), gsi_stmt (adj_gsi));
   10835          878 :       basic_block cond_bb = e->src;
   10836          878 :       basic_block else_bb = e->dest;
   10837          878 :       if (gsi_bb (adj_gsi) == new_bb)
   10838              :         {
   10839          404 :           new_bb = else_bb;
   10840          404 :           gsi = gsi_last_nondebug_bb (new_bb);
   10841              :         }
   10842              : 
   10843          878 :       basic_block then_bb = create_empty_bb (cond_bb);
   10844          878 :       set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
   10845              : 
   10846          878 :       cond = build2 (GT_EXPR, boolean_type_node, uns,
   10847              :                      build_int_cst (unsigned_type_node,
   10848              :                                     GOMP_DEVICE_HOST_FALLBACK - 1));
   10849          878 :       stmt = gimple_build_cond_empty (cond);
   10850          878 :       adj_gsi = gsi_last_bb (cond_bb);
   10851          878 :       gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
   10852              : 
   10853          878 :       adj_gsi = gsi_start_bb (then_bb);
   10854          878 :       tree add = build2 (PLUS_EXPR, integer_type_node, device,
   10855              :                          build_int_cst (integer_type_node, -1));
   10856          878 :       stmt = gimple_build_assign (device, add);
   10857          878 :       gsi_insert_after (&adj_gsi, stmt, GSI_CONTINUE_LINKING);
   10858              : 
   10859          878 :       make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
   10860          878 :       e->flags = EDGE_FALSE_VALUE;
   10861          878 :       add_bb_to_loop (then_bb, cond_bb->loop_father);
   10862          878 :       make_edge (then_bb, else_bb, EDGE_FALLTHRU);
   10863              :     }
   10864              : 
   10865        38625 :   t = gimple_omp_target_data_arg (entry_stmt);
   10866        38625 :   if (t == NULL)
   10867              :     {
   10868         5089 :       t1 = size_zero_node;
   10869         5089 :       t2 = build_zero_cst (ptr_type_node);
   10870         5089 :       t3 = t2;
   10871         5089 :       t4 = t2;
   10872              :     }
   10873        33536 :   else if (TREE_VEC_LENGTH (t) == 3 || is_gimple_omp_oacc (entry_stmt))
   10874              :     {
   10875        33393 :       t1 = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (TREE_VEC_ELT (t, 1))));
   10876        33393 :       t1 = size_binop (PLUS_EXPR, t1, size_int (1));
   10877        33393 :       t2 = build_fold_addr_expr (TREE_VEC_ELT (t, 0));
   10878        33393 :       t3 = build_fold_addr_expr (TREE_VEC_ELT (t, 1));
   10879        33393 :       t4 = build_fold_addr_expr (TREE_VEC_ELT (t, 2));
   10880              :     }
   10881              :   else
   10882              :     {
   10883          143 :       t1 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 3), true, NULL_TREE,
   10884              :                                      true, GSI_SAME_STMT);
   10885          143 :       t2 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 0), true, NULL_TREE,
   10886              :                                      true, GSI_SAME_STMT);
   10887          143 :       t3 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 1), true, NULL_TREE,
   10888              :                                      true, GSI_SAME_STMT);
   10889          143 :       t4 = force_gimple_operand_gsi (&gsi, TREE_VEC_ELT (t, 2), true, NULL_TREE,
   10890              :                                      true, GSI_SAME_STMT);
   10891              :     }
   10892              : 
   10893        38625 :   gimple *g;
   10894        38625 :   bool tagging = false;
   10895              :   /* The maximum number used by any start_ix, without varargs.  */
   10896        38625 :   auto_vec<tree, 11> args;
   10897        38625 :   if (is_gimple_omp_oacc (entry_stmt))
   10898              :     {
   10899        14785 :       tree goacc_flags_m = fold_build1 (GOACC_FLAGS_MARSHAL_OP,
   10900              :                                         TREE_TYPE (goacc_flags), goacc_flags);
   10901        14785 :       goacc_flags_m = force_gimple_operand_gsi (&gsi, goacc_flags_m, true,
   10902              :                                                 NULL_TREE, true,
   10903              :                                                 GSI_SAME_STMT);
   10904        14785 :       args.quick_push (goacc_flags_m);
   10905              :     }
   10906              :   else
   10907        23840 :     args.quick_push (device);
   10908        38625 :   if (offloaded)
   10909        22248 :     args.quick_push (build_fold_addr_expr (child_fn2 ? child_fn2 : child_fn));
   10910        38625 :   args.quick_push (t1);
   10911        38625 :   args.quick_push (t2);
   10912        38625 :   args.quick_push (t3);
   10913        38625 :   args.quick_push (t4);
   10914        38625 :   switch (start_ix)
   10915              :     {
   10916              :     case BUILT_IN_GOACC_DATA_START:
   10917              :     case BUILT_IN_GOACC_DECLARE:
   10918              :     case BUILT_IN_GOMP_TARGET_DATA:
   10919              :       break;
   10920        22203 :     case BUILT_IN_GOMP_TARGET:
   10921        22203 :     case BUILT_IN_GOMP_TARGET_UPDATE:
   10922        22203 :     case BUILT_IN_GOMP_TARGET_ENTER_EXIT_DATA:
   10923        22203 :       args.quick_push (build_int_cst (unsigned_type_node, flags_i));
   10924        22203 :       c = omp_find_clause (clauses, OMP_CLAUSE_DEPEND);
   10925        22203 :       if (c)
   10926          329 :         depend = OMP_CLAUSE_DECL (c);
   10927              :       else
   10928        21874 :         depend = build_int_cst (ptr_type_node, 0);
   10929        22203 :       args.quick_push (depend);
   10930        22203 :       if (start_ix == BUILT_IN_GOMP_TARGET)
   10931        12853 :         args.quick_push (get_target_arguments (&gsi, entry_stmt));
   10932              :       break;
   10933         9395 :     case BUILT_IN_GOACC_PARALLEL:
   10934         9395 :       if (lookup_attribute ("oacc serial", DECL_ATTRIBUTES (child_fn)) != NULL)
   10935              :         {
   10936              :           tree dims = NULL_TREE;
   10937              :           unsigned int ix;
   10938              : 
   10939              :           /* For serial constructs we set all dimensions to 1.  */
   10940         3024 :           for (ix = GOMP_DIM_MAX; ix--;)
   10941         2268 :             dims = tree_cons (NULL_TREE, integer_one_node, dims);
   10942          756 :           oacc_replace_fn_attrib (child_fn, dims);
   10943              :         }
   10944              :       else
   10945         8639 :         oacc_set_fn_attrib (child_fn, clauses, &args);
   10946              :       tagging = true;
   10947              :       /* FALLTHRU */
   10948        12136 :     case BUILT_IN_GOACC_ENTER_DATA:
   10949        12136 :     case BUILT_IN_GOACC_EXIT_DATA:
   10950        12136 :     case BUILT_IN_GOACC_UPDATE:
   10951        12136 :       {
   10952        12136 :         tree t_async = NULL_TREE;
   10953              : 
   10954              :         /* If present, use the value specified by the respective
   10955              :            clause, making sure that is of the correct type.  */
   10956        12136 :         c = omp_find_clause (clauses, OMP_CLAUSE_ASYNC);
   10957        12136 :         if (c)
   10958         1681 :           t_async = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
   10959              :                                       integer_type_node,
   10960         1681 :                                       OMP_CLAUSE_ASYNC_EXPR (c));
   10961        10455 :         else if (!tagging)
   10962              :           /* Default values for t_async.  */
   10963         2431 :           t_async = fold_convert_loc (gimple_location (entry_stmt),
   10964              :                                       integer_type_node,
   10965              :                                       build_int_cst (integer_type_node,
   10966              :                                                      GOMP_ASYNC_SYNC));
   10967        12136 :         if (tagging && t_async)
   10968              :           {
   10969         1371 :             unsigned HOST_WIDE_INT i_async = GOMP_LAUNCH_OP_MAX;
   10970              : 
   10971         1371 :             if (TREE_CODE (t_async) == INTEGER_CST)
   10972              :               {
   10973              :                 /* See if we can pack the async arg in to the tag's
   10974              :                    operand.  */
   10975         1331 :                 i_async = TREE_INT_CST_LOW (t_async);
   10976         1331 :                 if (i_async < GOMP_LAUNCH_OP_MAX)
   10977              :                   t_async = NULL_TREE;
   10978              :                 else
   10979          825 :                   i_async = GOMP_LAUNCH_OP_MAX;
   10980              :               }
   10981         1371 :             args.safe_push (oacc_launch_pack (GOMP_LAUNCH_ASYNC, NULL_TREE,
   10982              :                                               i_async));
   10983              :           }
   10984        12136 :         if (t_async)
   10985         3566 :           args.safe_push (force_gimple_operand_gsi (&gsi, t_async, true,
   10986              :                                                     NULL_TREE, true,
   10987              :                                                     GSI_SAME_STMT));
   10988              : 
   10989              :         /* Save the argument index, and ... */
   10990        12136 :         unsigned t_wait_idx = args.length ();
   10991        12136 :         unsigned num_waits = 0;
   10992        12136 :         c = omp_find_clause (clauses, OMP_CLAUSE_WAIT);
   10993        12136 :         if (!tagging || c)
   10994              :           /* ... push a placeholder.  */
   10995         2944 :           args.safe_push (integer_zero_node);
   10996              : 
   10997        13986 :         for (; c; c = OMP_CLAUSE_CHAIN (c))
   10998         1850 :           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_WAIT)
   10999              :             {
   11000          422 :               tree arg = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
   11001              :                                            integer_type_node,
   11002          422 :                                            OMP_CLAUSE_WAIT_EXPR (c));
   11003          422 :               arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE, true,
   11004              :                                               GSI_SAME_STMT);
   11005          422 :               args.safe_push (arg);
   11006          422 :               num_waits++;
   11007              :             }
   11008              : 
   11009        12136 :         if (!tagging || num_waits)
   11010              :           {
   11011          203 :             tree len;
   11012              : 
   11013              :             /* Now that we know the number, update the placeholder.  */
   11014          203 :             if (tagging)
   11015          203 :               len = oacc_launch_pack (GOMP_LAUNCH_WAIT, NULL_TREE, num_waits);
   11016              :             else
   11017         2741 :               len = build_int_cst (integer_type_node, num_waits);
   11018         2944 :             len = fold_convert_loc (gimple_location (entry_stmt),
   11019              :                                     unsigned_type_node, len);
   11020         2944 :             args[t_wait_idx] = len;
   11021              :           }
   11022              :       }
   11023              :       break;
   11024              :     default:
   11025              :       gcc_unreachable ();
   11026              :     }
   11027        24989 :   if (tagging)
   11028              :     /*  Push terminal marker - zero.  */
   11029         9395 :     args.safe_push (oacc_launch_pack (0, NULL_TREE, 0));
   11030              : 
   11031        38625 :   if (child_fn2)
   11032              :     {
   11033              :       g = gimple_build_call_internal (IFN_GOMP_TARGET_REV, 1,
   11034              :                                       build_fold_addr_expr (child_fn));
   11035              :       gimple_set_location (g, gimple_location (entry_stmt));
   11036              :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
   11037              :     }
   11038              : 
   11039        38625 :   g = gimple_build_call_vec (builtin_decl_explicit (start_ix), args);
   11040        38625 :   gimple_set_location (g, gimple_location (entry_stmt));
   11041        38625 :   gsi_insert_before (&gsi, g, GSI_SAME_STMT);
   11042        38625 :   if (!offloaded)
   11043              :     {
   11044        16377 :       g = gsi_stmt (gsi);
   11045        16377 :       gcc_assert (g && gimple_code (g) == GIMPLE_OMP_TARGET);
   11046        16377 :       gsi_remove (&gsi, true);
   11047              :     }
   11048        38625 : }
   11049              : 
   11050              : /* Expand the parallel region tree rooted at REGION.  Expansion
   11051              :    proceeds in depth-first order.  Innermost regions are expanded
   11052              :    first.  This way, parallel regions that require a new function to
   11053              :    be created (e.g., GIMPLE_OMP_PARALLEL) can be expanded without having any
   11054              :    internal dependencies in their body.  */
   11055              : 
   11056              : static void
   11057        87030 : expand_omp (struct omp_region *region)
   11058              : {
   11059        87030 :   omp_any_child_fn_dumped = false;
   11060       221905 :   while (region)
   11061              :     {
   11062       134875 :       location_t saved_location;
   11063       134875 :       gimple *inner_stmt = NULL;
   11064              : 
   11065              :       /* First, determine whether this is a combined parallel+workshare
   11066              :          region.  */
   11067       134875 :       if (region->type == GIMPLE_OMP_PARALLEL)
   11068        17174 :         determine_parallel_type (region);
   11069              : 
   11070       134875 :       if (region->type == GIMPLE_OMP_FOR
   11071       134875 :           && gimple_omp_for_combined_p (last_nondebug_stmt (region->entry)))
   11072        15977 :         inner_stmt = last_nondebug_stmt (region->inner->entry);
   11073              : 
   11074       134875 :       if (region->inner)
   11075        61180 :         expand_omp (region->inner);
   11076              : 
   11077       134875 :       saved_location = input_location;
   11078       134875 :       if (gimple_has_location (last_nondebug_stmt (region->entry)))
   11079       134692 :         input_location = gimple_location (last_nondebug_stmt (region->entry));
   11080              : 
   11081       134875 :       switch (region->type)
   11082              :         {
   11083        21102 :         case GIMPLE_OMP_PARALLEL:
   11084        21102 :         case GIMPLE_OMP_TASK:
   11085        21102 :           expand_omp_taskreg (region);
   11086        21102 :           break;
   11087              : 
   11088        50193 :         case GIMPLE_OMP_FOR:
   11089        50193 :           expand_omp_for (region, inner_stmt);
   11090        50193 :           break;
   11091              : 
   11092          386 :         case GIMPLE_OMP_SECTIONS:
   11093          386 :           expand_omp_sections (region);
   11094          386 :           break;
   11095              : 
   11096              :         case GIMPLE_OMP_SECTION:
   11097              :           /* Individual omp sections are handled together with their
   11098              :              parent GIMPLE_OMP_SECTIONS region.  */
   11099              :           break;
   11100              : 
   11101            0 :         case GIMPLE_OMP_STRUCTURED_BLOCK:
   11102              :           /* We should have gotten rid of these in gimple lowering.  */
   11103            0 :           gcc_unreachable ();
   11104              : 
   11105         1307 :         case GIMPLE_OMP_SINGLE:
   11106         1307 :         case GIMPLE_OMP_SCOPE:
   11107         1307 :           expand_omp_single (region);
   11108         1307 :           break;
   11109              : 
   11110         1124 :         case GIMPLE_OMP_ORDERED:
   11111         1124 :           {
   11112         1124 :             gomp_ordered *ord_stmt
   11113         1124 :               = as_a <gomp_ordered *> (last_nondebug_stmt (region->entry));
   11114         1124 :             if (gimple_omp_ordered_standalone_p (ord_stmt))
   11115              :               {
   11116              :                 /* We'll expand these when expanding corresponding
   11117              :                    worksharing region with ordered(n) clause.  */
   11118          709 :                 gcc_assert (region->outer
   11119              :                             && region->outer->type == GIMPLE_OMP_FOR);
   11120          709 :                 region->ord_stmt = ord_stmt;
   11121          709 :                 break;
   11122              :               }
   11123              :           }
   11124              :           /* FALLTHRU */
   11125        11531 :         case GIMPLE_OMP_MASTER:
   11126        11531 :         case GIMPLE_OMP_MASKED:
   11127        11531 :         case GIMPLE_OMP_TASKGROUP:
   11128        11531 :         case GIMPLE_OMP_CRITICAL:
   11129        11531 :         case GIMPLE_OMP_TEAMS:
   11130        11531 :           expand_omp_synch (region);
   11131        11531 :           break;
   11132              : 
   11133        10159 :         case GIMPLE_OMP_ATOMIC_LOAD:
   11134        10159 :           expand_omp_atomic (region);
   11135        10159 :           break;
   11136              : 
   11137        38625 :         case GIMPLE_OMP_TARGET:
   11138        38625 :           expand_omp_target (region);
   11139        38625 :           break;
   11140              : 
   11141            0 :         default:
   11142            0 :           gcc_unreachable ();
   11143              :         }
   11144              : 
   11145       134875 :       input_location = saved_location;
   11146       134875 :       region = region->next;
   11147              :     }
   11148        87030 :   if (omp_any_child_fn_dumped)
   11149              :     {
   11150          109 :       if (dump_file)
   11151          109 :         dump_function_header (dump_file, current_function_decl, dump_flags);
   11152          109 :       omp_any_child_fn_dumped = false;
   11153              :     }
   11154        87030 : }
   11155              : 
   11156              : /* Helper for build_omp_regions.  Scan the dominator tree starting at
   11157              :    block BB.  PARENT is the region that contains BB.  If SINGLE_TREE is
   11158              :    true, the function ends once a single tree is built (otherwise, whole
   11159              :    forest of OMP constructs may be built).  */
   11160              : 
   11161              : static void
   11162      1472818 : build_omp_regions_1 (basic_block bb, struct omp_region *parent,
   11163              :                      bool single_tree)
   11164              : {
   11165      1472818 :   gimple_stmt_iterator gsi;
   11166      1472818 :   gimple *stmt;
   11167      1472818 :   basic_block son;
   11168              : 
   11169      1472818 :   gsi = gsi_last_nondebug_bb (bb);
   11170      1472818 :   if (!gsi_end_p (gsi) && is_gimple_omp (gsi_stmt (gsi)))
   11171              :     {
   11172       304263 :       struct omp_region *region;
   11173       304263 :       enum gimple_code code;
   11174              : 
   11175       304263 :       stmt = gsi_stmt (gsi);
   11176       304263 :       code = gimple_code (stmt);
   11177       304263 :       if (code == GIMPLE_OMP_RETURN)
   11178              :         {
   11179              :           /* STMT is the return point out of region PARENT.  Mark it
   11180              :              as the exit point and make PARENT the immediately
   11181              :              enclosing region.  */
   11182       106754 :           gcc_assert (parent);
   11183       106754 :           region = parent;
   11184       106754 :           region->exit = bb;
   11185       106754 :           parent = parent->outer;
   11186              :         }
   11187       197509 :       else if (code == GIMPLE_OMP_ATOMIC_STORE)
   11188              :         {
   11189              :           /* GIMPLE_OMP_ATOMIC_STORE is analogous to
   11190              :              GIMPLE_OMP_RETURN, but matches with
   11191              :              GIMPLE_OMP_ATOMIC_LOAD.  */
   11192        10159 :           gcc_assert (parent);
   11193        10159 :           gcc_assert (parent->type == GIMPLE_OMP_ATOMIC_LOAD);
   11194        10159 :           region = parent;
   11195        10159 :           region->exit = bb;
   11196        10159 :           parent = parent->outer;
   11197              :         }
   11198       187350 :       else if (code == GIMPLE_OMP_CONTINUE)
   11199              :         {
   11200        52089 :           gcc_assert (parent);
   11201        52089 :           parent->cont = bb;
   11202              :         }
   11203       135261 :       else if (code == GIMPLE_OMP_SECTIONS_SWITCH)
   11204              :         {
   11205              :           /* GIMPLE_OMP_SECTIONS_SWITCH is part of
   11206              :              GIMPLE_OMP_SECTIONS, and we do nothing for it.  */
   11207              :         }
   11208              :       else
   11209              :         {
   11210       134875 :           region = new_omp_region (bb, code, parent);
   11211              :           /* Otherwise...  */
   11212       134875 :           if (code == GIMPLE_OMP_TARGET)
   11213              :             {
   11214        38625 :               switch (gimple_omp_target_kind (stmt))
   11215              :                 {
   11216              :                 case GF_OMP_TARGET_KIND_REGION:
   11217              :                 case GF_OMP_TARGET_KIND_OACC_PARALLEL:
   11218              :                 case GF_OMP_TARGET_KIND_OACC_KERNELS:
   11219              :                 case GF_OMP_TARGET_KIND_OACC_SERIAL:
   11220              :                 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
   11221              :                 case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
   11222              :                   break;
   11223              :                 case GF_OMP_TARGET_KIND_UPDATE:
   11224              :                 case GF_OMP_TARGET_KIND_ENTER_DATA:
   11225              :                 case GF_OMP_TARGET_KIND_EXIT_DATA:
   11226              :                 case GF_OMP_TARGET_KIND_DATA:
   11227              :                 case GF_OMP_TARGET_KIND_OACC_DATA:
   11228              :                 case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
   11229              :                 case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
   11230              :                 case GF_OMP_TARGET_KIND_OACC_UPDATE:
   11231              :                 case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
   11232              :                 case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
   11233              :                 case GF_OMP_TARGET_KIND_OACC_DECLARE:
   11234              :                   /* ..., other than for those stand-alone directives...
   11235              :                      To be precise, target data isn't stand-alone, but
   11236              :                      gimplifier put the end API call into try finally block
   11237              :                      for it, so omp expansion can treat it as such.  */
   11238              :                   region = NULL;
   11239              :                   break;
   11240            0 :                 default:
   11241            0 :                   gcc_unreachable ();
   11242              :                 }
   11243              :             }
   11244        96250 :           else if (code == GIMPLE_OMP_ORDERED
   11245        96250 :                    && gimple_omp_ordered_standalone_p (stmt))
   11246              :             /* #pragma omp ordered depend is also just a stand-alone
   11247              :                directive.  */
   11248              :             region = NULL;
   11249        95541 :           else if (code == GIMPLE_OMP_TASK
   11250        95541 :                    && gimple_omp_task_taskwait_p (stmt))
   11251              :             /* #pragma omp taskwait depend(...) is a stand-alone directive.  */
   11252              :             region = NULL;
   11253        95457 :           else if (code == GIMPLE_OMP_TASKGROUP)
   11254              :             /* #pragma omp taskgroup isn't a stand-alone directive, but
   11255              :                gimplifier put the end API call into try finally block
   11256              :                for it, so omp expansion can treat it as such.  */
   11257              :             region = NULL;
   11258              :           /* ..., this directive becomes the parent for a new region.  */
   11259              :           if (region)
   11260              :             parent = region;
   11261              :         }
   11262              :     }
   11263              : 
   11264      1472818 :   if (single_tree && !parent)
   11265            0 :     return;
   11266              : 
   11267      1472818 :   for (son = first_dom_son (CDI_DOMINATORS, bb);
   11268      2863788 :        son;
   11269      1390970 :        son = next_dom_son (CDI_DOMINATORS, son))
   11270      1390970 :     build_omp_regions_1 (son, parent, single_tree);
   11271              : }
   11272              : 
   11273              : /* Builds the tree of OMP regions rooted at ROOT, storing it to
   11274              :    root_omp_region.  */
   11275              : 
   11276              : static void
   11277            0 : build_omp_regions_root (basic_block root)
   11278              : {
   11279            0 :   gcc_assert (root_omp_region == NULL);
   11280            0 :   build_omp_regions_1 (root, NULL, true);
   11281            0 :   gcc_assert (root_omp_region != NULL);
   11282            0 : }
   11283              : 
   11284              : /* Expands omp construct (and its subconstructs) starting in HEAD.  */
   11285              : 
   11286              : void
   11287            0 : omp_expand_local (basic_block head)
   11288              : {
   11289            0 :   build_omp_regions_root (head);
   11290            0 :   if (dump_file && (dump_flags & TDF_DETAILS))
   11291              :     {
   11292            0 :       fprintf (dump_file, "\nOMP region tree\n\n");
   11293            0 :       dump_omp_region (dump_file, root_omp_region, 0);
   11294            0 :       fprintf (dump_file, "\n");
   11295              :     }
   11296              : 
   11297            0 :   remove_exit_barriers (root_omp_region);
   11298            0 :   expand_omp (root_omp_region);
   11299              : 
   11300            0 :   omp_free_regions ();
   11301            0 : }
   11302              : 
   11303              : /* Scan the CFG and build a tree of OMP regions.  Return the root of
   11304              :    the OMP region tree.  */
   11305              : 
   11306              : static void
   11307        81848 : build_omp_regions (void)
   11308              : {
   11309        81848 :   gcc_assert (root_omp_region == NULL);
   11310        81848 :   calculate_dominance_info (CDI_DOMINATORS);
   11311        81848 :   build_omp_regions_1 (ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, false);
   11312        81848 : }
   11313              : 
   11314              : /* Main entry point for expanding OMP-GIMPLE into runtime calls.  */
   11315              : 
   11316              : static unsigned int
   11317        81848 : execute_expand_omp (void)
   11318              : {
   11319        81848 :   build_omp_regions ();
   11320              : 
   11321        81848 :   if (!root_omp_region)
   11322              :     return 0;
   11323              : 
   11324        25850 :   if (dump_file)
   11325              :     {
   11326          284 :       fprintf (dump_file, "\nOMP region tree\n\n");
   11327          284 :       dump_omp_region (dump_file, root_omp_region, 0);
   11328          284 :       fprintf (dump_file, "\n");
   11329              :     }
   11330              : 
   11331        25850 :   remove_exit_barriers (root_omp_region);
   11332              : 
   11333        25850 :   expand_omp (root_omp_region);
   11334              : 
   11335        25850 :   omp_free_regions ();
   11336              : 
   11337        25850 :   return (TODO_cleanup_cfg
   11338        51152 :           | (gimple_in_ssa_p (cfun) ? TODO_update_ssa_only_virtuals : 0));
   11339              : }
   11340              : 
   11341              : /* OMP expansion -- the default pass, run before creation of SSA form.  */
   11342              : 
   11343              : namespace {
   11344              : 
   11345              : const pass_data pass_data_expand_omp =
   11346              : {
   11347              :   GIMPLE_PASS, /* type */
   11348              :   "ompexp", /* name */
   11349              :   OPTGROUP_OMP, /* optinfo_flags */
   11350              :   TV_NONE, /* tv_id */
   11351              :   PROP_gimple_any, /* properties_required */
   11352              :   PROP_gimple_eomp, /* properties_provided */
   11353              :   0, /* properties_destroyed */
   11354              :   0, /* todo_flags_start */
   11355              :   0, /* todo_flags_finish */
   11356              : };
   11357              : 
   11358              : class pass_expand_omp : public gimple_opt_pass
   11359              : {
   11360              : public:
   11361       302985 :   pass_expand_omp (gcc::context *ctxt)
   11362       605970 :     : gimple_opt_pass (pass_data_expand_omp, ctxt)
   11363              :   {}
   11364              : 
   11365              :   /* opt_pass methods: */
   11366      3019894 :   unsigned int execute (function *) final override
   11367              :     {
   11368      3012580 :       bool gate = ((flag_openacc != 0 || flag_openmp != 0
   11369      2961259 :                     || flag_openmp_simd != 0)
   11370      3073511 :                    && !seen_error ());
   11371              : 
   11372              :       /* This pass always runs, to provide PROP_gimple_eomp.
   11373              :          But often, there is nothing to do.  */
   11374        56099 :       if (!gate)
   11375              :         return 0;
   11376              : 
   11377        56099 :       return execute_expand_omp ();
   11378              :     }
   11379              : 
   11380              : }; // class pass_expand_omp
   11381              : 
   11382              : } // anon namespace
   11383              : 
   11384              : gimple_opt_pass *
   11385       302985 : make_pass_expand_omp (gcc::context *ctxt)
   11386              : {
   11387       302985 :   return new pass_expand_omp (ctxt);
   11388              : }
   11389              : 
   11390              : namespace {
   11391              : 
   11392              : const pass_data pass_data_expand_omp_ssa =
   11393              : {
   11394              :   GIMPLE_PASS, /* type */
   11395              :   "ompexpssa", /* name */
   11396              :   OPTGROUP_OMP, /* optinfo_flags */
   11397              :   TV_NONE, /* tv_id */
   11398              :   PROP_cfg | PROP_ssa, /* properties_required */
   11399              :   PROP_gimple_eomp, /* properties_provided */
   11400              :   0, /* properties_destroyed */
   11401              :   0, /* todo_flags_start */
   11402              :   TODO_cleanup_cfg | TODO_rebuild_alias, /* todo_flags_finish */
   11403              : };
   11404              : 
   11405              : class pass_expand_omp_ssa : public gimple_opt_pass
   11406              : {
   11407              : public:
   11408       605970 :   pass_expand_omp_ssa (gcc::context *ctxt)
   11409      1211940 :     : gimple_opt_pass (pass_data_expand_omp_ssa, ctxt)
   11410              :   {}
   11411              : 
   11412              :   /* opt_pass methods: */
   11413       252630 :   bool gate (function *fun) final override
   11414              :     {
   11415       252630 :       return !(fun->curr_properties & PROP_gimple_eomp);
   11416              :     }
   11417        25749 :   unsigned int execute (function *) final override
   11418              :   {
   11419        25749 :     return execute_expand_omp ();
   11420              :   }
   11421       302985 :   opt_pass * clone () final override
   11422              :   {
   11423       302985 :     return new pass_expand_omp_ssa (m_ctxt);
   11424              :   }
   11425              : 
   11426              : }; // class pass_expand_omp_ssa
   11427              : 
   11428              : } // anon namespace
   11429              : 
   11430              : gimple_opt_pass *
   11431       302985 : make_pass_expand_omp_ssa (gcc::context *ctxt)
   11432              : {
   11433       302985 :   return new pass_expand_omp_ssa (ctxt);
   11434              : }
   11435              : 
   11436              : /* Called from tree-cfg.cc::make_edges to create cfg edges for all relevant
   11437              :    GIMPLE_* codes.  */
   11438              : 
   11439              : bool
   11440       305333 : omp_make_gimple_edges (basic_block bb, struct omp_region **region,
   11441              :                        int *region_idx)
   11442              : {
   11443       305333 :   gimple *last = last_nondebug_stmt (bb);
   11444       305333 :   enum gimple_code code = gimple_code (last);
   11445       305333 :   struct omp_region *cur_region = *region;
   11446       305333 :   bool fallthru = false;
   11447              : 
   11448       305333 :   switch (code)
   11449              :     {
   11450        79407 :     case GIMPLE_OMP_PARALLEL:
   11451        79407 :     case GIMPLE_OMP_FOR:
   11452        79407 :     case GIMPLE_OMP_SINGLE:
   11453        79407 :     case GIMPLE_OMP_TEAMS:
   11454        79407 :     case GIMPLE_OMP_MASTER:
   11455        79407 :     case GIMPLE_OMP_MASKED:
   11456        79407 :     case GIMPLE_OMP_SCOPE:
   11457        79407 :     case GIMPLE_OMP_CRITICAL:
   11458        79407 :     case GIMPLE_OMP_SECTION:
   11459        79407 :       cur_region = new_omp_region (bb, code, cur_region);
   11460        79407 :       fallthru = true;
   11461        79407 :       break;
   11462              : 
   11463          536 :     case GIMPLE_OMP_TASKGROUP:
   11464          536 :       cur_region = new_omp_region (bb, code, cur_region);
   11465          536 :       fallthru = true;
   11466          536 :       cur_region = cur_region->outer;
   11467          536 :       break;
   11468              : 
   11469         3928 :     case GIMPLE_OMP_TASK:
   11470         3928 :       cur_region = new_omp_region (bb, code, cur_region);
   11471         3928 :       fallthru = true;
   11472         3928 :       if (gimple_omp_task_taskwait_p (last))
   11473           84 :         cur_region = cur_region->outer;
   11474              :       break;
   11475              : 
   11476         1128 :     case GIMPLE_OMP_ORDERED:
   11477         1128 :       cur_region = new_omp_region (bb, code, cur_region);
   11478         1128 :       fallthru = true;
   11479         1128 :       if (gimple_omp_ordered_standalone_p (last))
   11480          713 :         cur_region = cur_region->outer;
   11481              :       break;
   11482              : 
   11483        38655 :     case GIMPLE_OMP_TARGET:
   11484        38655 :       cur_region = new_omp_region (bb, code, cur_region);
   11485        38655 :       fallthru = true;
   11486        38655 :       switch (gimple_omp_target_kind (last))
   11487              :         {
   11488              :         case GF_OMP_TARGET_KIND_REGION:
   11489              :         case GF_OMP_TARGET_KIND_OACC_PARALLEL:
   11490              :         case GF_OMP_TARGET_KIND_OACC_KERNELS:
   11491              :         case GF_OMP_TARGET_KIND_OACC_SERIAL:
   11492              :         case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_PARALLELIZED:
   11493              :         case GF_OMP_TARGET_KIND_OACC_PARALLEL_KERNELS_GANG_SINGLE:
   11494              :           break;
   11495        16379 :         case GF_OMP_TARGET_KIND_UPDATE:
   11496        16379 :         case GF_OMP_TARGET_KIND_ENTER_DATA:
   11497        16379 :         case GF_OMP_TARGET_KIND_EXIT_DATA:
   11498        16379 :         case GF_OMP_TARGET_KIND_DATA:
   11499        16379 :         case GF_OMP_TARGET_KIND_OACC_DATA:
   11500        16379 :         case GF_OMP_TARGET_KIND_OACC_HOST_DATA:
   11501        16379 :         case GF_OMP_TARGET_KIND_OACC_DATA_KERNELS:
   11502        16379 :         case GF_OMP_TARGET_KIND_OACC_UPDATE:
   11503        16379 :         case GF_OMP_TARGET_KIND_OACC_ENTER_DATA:
   11504        16379 :         case GF_OMP_TARGET_KIND_OACC_EXIT_DATA:
   11505        16379 :         case GF_OMP_TARGET_KIND_OACC_DECLARE:
   11506        16379 :           cur_region = cur_region->outer;
   11507        16379 :           break;
   11508            0 :         default:
   11509            0 :           gcc_unreachable ();
   11510              :         }
   11511              :       break;
   11512              : 
   11513          386 :     case GIMPLE_OMP_SECTIONS:
   11514          386 :       cur_region = new_omp_region (bb, code, cur_region);
   11515          386 :       fallthru = true;
   11516          386 :       break;
   11517              : 
   11518              :     case GIMPLE_OMP_SECTIONS_SWITCH:
   11519              :       fallthru = false;
   11520              :       break;
   11521              : 
   11522        20704 :     case GIMPLE_OMP_ATOMIC_LOAD:
   11523        20704 :     case GIMPLE_OMP_ATOMIC_STORE:
   11524        20704 :        fallthru = true;
   11525        20704 :        break;
   11526              : 
   11527       106328 :     case GIMPLE_OMP_RETURN:
   11528              :       /* In the case of a GIMPLE_OMP_SECTION, the edge will go
   11529              :          somewhere other than the next block.  This will be
   11530              :          created later.  */
   11531       106328 :       cur_region->exit = bb;
   11532       106328 :       if (cur_region->type == GIMPLE_OMP_TASK)
   11533              :         /* Add an edge corresponding to not scheduling the task
   11534              :            immediately.  */
   11535         3844 :         make_edge (cur_region->entry, bb, EDGE_ABNORMAL);
   11536       106328 :       fallthru = cur_region->type != GIMPLE_OMP_SECTION;
   11537       106328 :       cur_region = cur_region->outer;
   11538       106328 :       break;
   11539              : 
   11540        53875 :     case GIMPLE_OMP_CONTINUE:
   11541        53875 :       cur_region->cont = bb;
   11542        53875 :       switch (cur_region->type)
   11543              :         {
   11544        49645 :         case GIMPLE_OMP_FOR:
   11545              :           /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
   11546              :              succs edges as abnormal to prevent splitting
   11547              :              them.  */
   11548        49645 :           single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
   11549              :           /* Make the loopback edge.  */
   11550        49645 :           make_edge (bb, single_succ (cur_region->entry),
   11551              :                      EDGE_ABNORMAL);
   11552              : 
   11553              :           /* Create an edge from GIMPLE_OMP_FOR to exit, which
   11554              :              corresponds to the case that the body of the loop
   11555              :              is not executed at all.  */
   11556        49645 :           make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
   11557        49645 :           make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
   11558        49645 :           fallthru = false;
   11559        49645 :           break;
   11560              : 
   11561          386 :         case GIMPLE_OMP_SECTIONS:
   11562              :           /* Wire up the edges into and out of the nested sections.  */
   11563          386 :           {
   11564          386 :             basic_block switch_bb = single_succ (cur_region->entry);
   11565              : 
   11566          386 :             struct omp_region *i;
   11567         1249 :             for (i = cur_region->inner; i ; i = i->next)
   11568              :               {
   11569          863 :                 gcc_assert (i->type == GIMPLE_OMP_SECTION);
   11570          863 :                 make_edge (switch_bb, i->entry, 0);
   11571          863 :                 make_edge (i->exit, bb, EDGE_FALLTHRU);
   11572              :               }
   11573              : 
   11574              :             /* Make the loopback edge to the block with
   11575              :                GIMPLE_OMP_SECTIONS_SWITCH.  */
   11576          386 :             make_edge (bb, switch_bb, 0);
   11577              : 
   11578              :             /* Make the edge from the switch to exit.  */
   11579          386 :             make_edge (switch_bb, bb->next_bb, 0);
   11580          386 :             fallthru = false;
   11581              :           }
   11582          386 :           break;
   11583              : 
   11584              :         case GIMPLE_OMP_TASK:
   11585              :           fallthru = true;
   11586              :           break;
   11587              : 
   11588            0 :         default:
   11589            0 :           gcc_unreachable ();
   11590              :         }
   11591              :       break;
   11592              : 
   11593            0 :     default:
   11594            0 :       gcc_unreachable ();
   11595              :     }
   11596              : 
   11597       305333 :   if (*region != cur_region)
   11598              :     {
   11599       212656 :       *region = cur_region;
   11600       212656 :       if (cur_region)
   11601       169946 :         *region_idx = cur_region->entry->index;
   11602              :       else
   11603        42710 :         *region_idx = 0;
   11604              :     }
   11605              : 
   11606       305333 :   return fallthru;
   11607              : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.