GCC Middle and Back End API Reference
cfgloopmanip.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "basic-block.h"
#include "cfgloop.h"
#include "tree.h"
#include "gimple.h"
#include "tree-ssa-loop-manip.h"
#include "dumpfile.h"
Include dependency graph for cfgloopmanip.c:

Macros

#define SE_LATCH   0
#define SE_ORIG   1

Functions

static void copy_loops_to (struct loop **, int, struct loop *)
static void loop_redirect_edge (edge, basic_block)
static void remove_bbs (basic_block *, int)
static bool rpe_enum_p (const_basic_block, const void *)
static int find_path (edge, basic_block **)
static void fix_loop_placements (struct loop *, bool *)
static bool fix_bb_placement (basic_block)
static void fix_bb_placements (basic_block, bool *, bitmap)
static bool rpe_enum_p ()
static void remove_bbs ()
static int find_path ()
static bool fix_bb_placement ()
static bool fix_loop_placement ()
bool remove_path ()
void place_new_loop ()
void add_loop ()
void scale_loop_frequencies ()
void scale_loop_profile ()
static void update_dominators_in_loop ()
edge create_empty_if_region_on_edge ()
struct loopcreate_empty_loop_on_edge (edge entry_edge, tree initial_value, tree stride, tree upper_bound, tree iv, tree *iv_before, tree *iv_after, struct loop *outer)
struct looploopify (edge latch_edge, edge header_edge, basic_block switch_bb, edge true_edge, edge false_edge, bool redirect_all_edges, unsigned true_scale, unsigned false_scale)
void unloop (struct loop *loop, bool *irred_invalidated, bitmap loop_closed_ssa_invalidated)
static void fix_loop_placements ()
void copy_loop_info ()
struct loopduplicate_loop ()
void duplicate_subloops ()
static void copy_loops_to ()
static void loop_redirect_edge ()
bool can_duplicate_loop_p ()
static void set_zero_probability ()
bool duplicate_loop_to_header_edge (struct loop *loop, edge e, unsigned int ndupl, sbitmap wont_exit, edge orig, vec< edge > *to_remove, int flags)
bool mfb_keep_just ()
static bool has_preds_from_loop ()
basic_block create_preheader ()
void create_preheaders ()
void force_single_succ_latches ()
static basic_block lv_adjust_loop_entry_edge (basic_block first_head, basic_block second_head, edge e, void *cond_expr, unsigned then_prob)
struct looploop_version (struct loop *loop, void *cond_expr, basic_block *condition_bb, unsigned then_prob, unsigned then_scale, unsigned else_scale, bool place_after)

Variables

edge mfb_kj_edge

Macro Definition Documentation

#define SE_LATCH   0
#define SE_ORIG   1

Function Documentation

void add_loop ( )

Given LOOP structure with filled header and latch, find the body of the corresponding loop and add it to loops tree. Insert the LOOP as a son of outer.

 Add it to loop structure.   
 Find its nodes.   
     If we find a direct subloop of OUTER, move it to LOOP.   
 Update the information about loop exit edges.   
bool can_duplicate_loop_p ( )

Check whether LOOP's body can be duplicated.

void copy_loop_info ( )

Duplicate loop bounds and other information we store about the loop into its duplicate.

References can_copy_bbs_p(), get_loop_body(), and loop::num_nodes.

static void copy_loops_to ( struct loop **  ,
int  ,
struct loop  
)
static

Loop manipulation code for GNU compiler. Copyright (C) 2002-2013 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see http://www.gnu.org/licenses/.

static void copy_loops_to ( )
static

Copies structure of subloops of N loops, stored in array COPIED_LOOPS, into TARGET loop, placing newly created loops into loop tree.

References loop::header, and loop::latch.

edge create_empty_if_region_on_edge ( )

Creates an if region as shown above. CONDITION is used to create the test for the if.

| | ------------- ------------- | | pred_bb | | pred_bb | | ------------- ------------- | | | | | | ENTRY_EDGE | | ENTRY_EDGE V | | ====> ————- | | | cond_bb | | | | CONDITION | | | ------------- | V / \ | ————- e_false / \ e_true | | succ_bb | V V | ------------- ----------- ----------- | | false_bb | | true_bb | | ----------- ----------- | \ / | \ / | V V | ------------- | | join_bb | | ------------- | | exit_edge (result) | V | ----------- | | succ_bb | | ----------- |

Insert condition in cond_bb.

References add_loop(), alloc_loop(), CDI_DOMINATORS, edge_def::flags, gcc_assert, loop::header, loop::latch, make_edge(), redirect_edge_succ_nodup(), scale_loop_frequencies(), set_immediate_dominator(), single_exit(), single_succ(), single_succ_edge(), split_edge(), edge_def::src, and update_dominators_in_loop().

struct loop* create_empty_loop_on_edge ( edge  entry_edge,
tree  initial_value,
tree  stride,
tree  upper_bound,
tree  iv,
tree iv_before,
tree iv_after,
struct loop outer 
)
read

create_empty_loop_on_edge | | - pred_bb - —— pred_bb —— | | | | iv0 = initial_value | | -----|----- ---------|----------- | | ______ | entry_edge | | entry_edge / | | | | ====> | -V—V- loop_header ————- | V | | iv_before = phi (iv0, iv_after) | | - succ_bb - | —|—————————– | | | | | | ———– | —V— loop_body ————— | | | iv_after = iv_before + stride | | | | if (iv_before < upper_bound) | | | —|————–&mdash;———– | | | \ exit_e | | V \ | | - loop_latch - V- succ_bb - | | | | | | | | /————- ———– | \ ___ /

Creates an empty loop as shown above, the IV_BEFORE is the SSA_NAME that is used before the increment of IV. IV_BEFORE should be used for adding code to the body that uses the IV. OUTER is the outer loop in which the new loop should be inserted.

Both INITIAL_VALUE and UPPER_BOUND expressions are gimplified and inserted on the loop entry edge. This implies that this function should be used only when the UPPER_BOUND expression is a loop invariant.

 Create header, latch and wire up the loop.   
 Set immediate dominator information.   
 Initialize a loop structure and put it in a loop hierarchy.   
 TODO: Fix frequencies and counts.   
 Update dominators.   
 Modify edge flags.   
 Construct IV code in loop.   
 Insert loop exit condition.   
basic_block create_preheader ( )

Creates a pre-header for a LOOP. Returns newly created block. Unless CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single entry; otherwise we also force preheader block to have only one successor. When CP_FALLTHRU_PREHEADERS is set in FLAGS, we force the preheader block to be a fallthru predecessor to the loop header and to have only predecessors from outside of the loop. The function also updates dominators.

     We do not allow entry block to be the loop preheader, since we
         cannot emit code there.   
         If we want simple preheaders, also force the preheader to have
         just a single successor.   
         If we want fallthru preheaders, also create forwarder block when
         preheader ends with a jump or has predecessors from loop.   
 Try to be clever in placing the newly created preheader.  The idea is to
 avoid breaking any "fallthruness" relationship between blocks.

 The preheader was created just before the header and all incoming edges
 to the header were redirected to the preheader, except the latch edge.
 So the only problematic case is when this latch edge was a fallthru
 edge: it is not anymore after the preheader creation so we have broken
 the fallthruness.  We're therefore going to look for a better place.   

References find_edge(), FOR_EACH_LOOP, gcc_checking_assert, loop::header, loop::latch, LOOPS_HAVE_SIMPLE_LATCHES, loops_state_set(), NULL, single_succ_p(), and split_edge().

void create_preheaders ( )

Create preheaders for each loop; for meaning of FLAGS see create_preheader.

struct loop* duplicate_loop ( )
read

Copies copy of LOOP as subloop of TARGET loop, placing newly created loop into loops structure.

Mark the new loop as copy of LOOP.

 Add it to target.   

References edge_def::count, EDGE_COUNT, FOR_EACH_EDGE, gcc_assert, last, NULL, edge_def::probability, edge_def::src, and basic_block_def::succs.

bool duplicate_loop_to_header_edge ( struct loop loop,
edge  e,
unsigned int  ndupl,
sbitmap  wont_exit,
edge  orig,
vec< edge > *  to_remove,
int  flags 
)

Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating loop structure and dominators. E's destination must be LOOP header for this to work, i.e. it must be entry or latch edge of this loop; these are unique, as the loops must have preheaders for this function to work correctly (in case E is latch, the function unrolls the loop, if E is entry edge, it peels the loop). Store edges created by copying ORIG edge from copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to original LOOP body, the other copies are numbered in order given by control flow through them) into TO_REMOVE array. Returns false if duplication is impossible.

Orig must be edge out of the loop.   
 Check whether duplication is possible.   


 In case we are doing loop peeling and the loop is in the middle of
 irreducible region, the peeled copies will be inside it too.   
 Find edge from latch.   
     Calculate coefficients by that we have to scale frequencies
     of duplicated loop bodies.   
         The blocks that are dominated by a removed exit edge ORIG have
         frequencies scaled by this.   
     Complete peeling is special as the probability of exit in last
     copy becomes 1.   
         First copy has frequency of incoming edge.  Each subsequent
         frequency should be reduced by prob_pass_wont_exit.  Caller
         should've managed the flags so all except for original loop
         has won't exist set.   
         Now simulate the duplication adjustments and compute header
         frequency of the last copy.   
 Loop the new bbs will belong to.   
 Original loops.   
     Copy loops.   
     Copy bbs.   
     Note whether the blocks and edges belong to an irreducible loop.   
     Redirect the special edges.   
     Record exit edge in this copy.   
         Scale the frequencies of the blocks dominated by the exit.   
     Record the first copy in the control flow order if it is not
     the original loop (i.e. in case of peeling).   
     Set counts and frequencies.   
 Record the exit edge in the original loop body, and update the frequencies.   
     Scale the frequencies of the blocks dominated by the exit.   
 Update the original loop.   
 Update dominators of outer blocks if affected.   
void duplicate_subloops ( )

Copies structure of subloops of LOOP into TARGET loop, placing newly created loops into loop tree.

static int find_path ( edge  ,
basic_block **   
)
static
static int find_path ( )
static

Find path – i.e. the basic blocks dominated by edge E and put them into array BBS, that will be allocated large enough to contain them. E->dest must have exactly one predecessor for this to work (it is easy to achieve and we do not put it here because we do not want to alter anything by this function). The number of basic blocks in the path is returned.

Find bbs in the path.

static bool fix_bb_placement ( basic_block  )
static
static bool fix_bb_placement ( )
static

Fix placement of basic block BB inside loop hierarchy – Let L be a loop to that BB belongs. Then every successor of BB must either 1) belong to some superloop of loop L, or 2) be a header of loop K such that K->outer is superloop of L Returns true if we had to move BB into other loop to enforce this condition, false if the placement of BB was already correct (provided that placements of its successors are correct).

References edge_def::dest, EXIT_BLOCK_PTR, flow_loop_nested_p(), basic_block_def::loop_father, and loop_outer().

static void fix_bb_placements ( basic_block  from,
bool irred_invalidated,
bitmap  loop_closed_ssa_invalidated 
)
static

Fix placements of basic blocks inside loop hierarchy stored in loops; i.e. enforce condition condition stated in description of fix_bb_placement. We start from basic block FROM that had some of its successors removed, so that his placement no longer has to be correct, and iteratively fix placement of its predecessors that may change if placement of FROM changed. Also fix placement of subloops of FROM->loop_father, that might also be altered due to this change; the condition for them is similar, except that instead of successors we consider edges coming out of the loops.

If the changes may invalidate the information about irreducible regions, IRRED_INVALIDATED is set to true.

If LOOP_CLOSED_SSA_INVLIDATED is non-zero then all basic blocks with changed loop_father are collected there.

 We pass through blocks back-reachable from FROM, testing whether some
 of their successors moved to outer loop.  It may be necessary to
 iterate several times, but it is finite, as we stop unless we move
 the basic block up the loop structure.  The whole story is a bit
 more complicated due to presence of subloops, those are moved using
 fix_loop_placement.   
 If we are already in the outermost loop, the basic blocks cannot be moved
 outside of it.  If FROM is the header of the base loop, it cannot be moved
 outside of it, either.  In both cases, we can end now.   
 Prevent us from going out of the base_loop.   
         Subloop header, maybe move the loop upward.   
         Ordinary basic block.   
     Something has changed, insert predecessors into queue.   
         If it is subloop, then it either was not moved, or
         the path up the loop tree from base_loop do not contain
         it.   
             If PRED is already higher in the loop hierarchy than the
             TARGET_LOOP to that we moved FROM, the change of the position
             of FROM does not affect the position of PRED, so there is no
             point in processing it.   
         Schedule the basic block.   
static bool fix_loop_placement ( )
static

Fix placement of LOOP inside loop tree, i.e. find the innermost superloop of LOOP to that leads at least one exit edge of LOOP, and set it as the immediate superloop of LOOP. Return true if the immediate superloop of LOOP changed.

IRRED_INVALIDATED is set to true if a change in the loop structures might invalidate the information about irreducible regions.

The exit edges of LOOP no longer exits its original immediate superloops; remove them from the appropriate exit lists.

         We may need to recompute irreducible loops.   

References edge_def::dest, find_common_loop(), flow_loop_nested_p(), and basic_block_def::loop_father.

static void fix_loop_placements ( struct loop ,
bool  
)
static
static void fix_loop_placements ( )
static

Fix placement of superloops of LOOP inside loop tree, i.e. ensure that condition stated in description of fix_loop_placement holds for them. It is used in case when we removed some edges coming out of LOOP, which may cause the right placement of LOOP inside loop tree to change.

IRRED_INVALIDATED is set to true if a change in the loop structures might invalidate the information about irreducible regions.

Changing the placement of a loop in the loop tree may alter the validity of condition 2) of the description of fix_bb_placement for its preheader, because the successor is the header and belongs to the loop. So call fix_bb_placements to fix up the placement of the preheader and (possibly) of its predecessors.

References duplicate_loop(), and duplicate_subloops().

void force_single_succ_latches ( void  )

Forces all loop latches to have only single successor.

static bool has_preds_from_loop ( )
static

True when a candidate preheader BLOCK has predecessors from LOOP.

static void loop_redirect_edge ( edge  ,
basic_block   
)
static
static void loop_redirect_edge ( )
static

Redirects edge E to basic block DEST.

struct loop* loop_version ( struct loop loop,
void *  cond_expr,
basic_block condition_bb,
unsigned  then_prob,
unsigned  then_scale,
unsigned  else_scale,
bool  place_after 
)
read

Main entry point for Loop Versioning transformation.

This transformation given a condition and a loop, creates -if (condition) { loop_copy1 } else { loop_copy2 }, where loop_copy1 is the loop transformed in one way, and loop_copy2 is the loop transformed in another way (or unchanged). 'condition' may be a run time test for things that were not resolved by static analysis (overlapping ranges (anti-aliasing), alignment, etc.).

THEN_PROB is the probability of the then edge of the if. THEN_SCALE is the ratio by that the frequencies in the original loop should be scaled. ELSE_SCALE is the ratio by that the frequencies in the new loop should be scaled.

If PLACE_AFTER is true, we place the new loop after LOOP in the instruction stream, otherwise it is placed before LOOP.

 Record entry and latch edges for the loop  
 Note down head of loop as first_head.   
 Duplicate loop.   
 After duplication entry edge now points to new loop head block.
 Note down new head as second_head.   
 Split loop entry edge and insert new block with cond expr.   
 loopify redirected latch_edge. Update its PENDING_STMTS.   
 loopify redirected condition_bb's succ edge. Update its PENDING_STMTS.   
 Adjust irreducible flag.   
 At this point condition_bb is loop preheader with two successors,
 first_head and second_head.   Make sure that loop preheader has only
 one successor.   

Referenced by scale_dominated_blocks_in_loop(), and vect_create_cond_for_alias_checks().

struct loop* loopify ( edge  latch_edge,
edge  header_edge,
basic_block  switch_bb,
edge  true_edge,
edge  false_edge,
bool  redirect_all_edges,
unsigned  true_scale,
unsigned  false_scale 
)
read

Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting latch to header and update loop tree and dominators accordingly. Everything between them plus LATCH_EDGE destination must be dominated by HEADER_EDGE destination, and back-reachable from LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB, FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE. Returns the newly created loop. Frequencies and counts in the new loop are scaled by FALSE_SCALE and in the old one by TRUE_SCALE.

 Redirect edges.   
 During loop versioning, one of the switch_bb edge is already properly
 set. Do not redirect it again unless redirect_all_edges is true.   
     Update dominators.   
 Compute new loop.   
 Add switch_bb to appropriate loop.   
 Fix frequencies.   
static basic_block lv_adjust_loop_entry_edge ( basic_block  first_head,
basic_block  second_head,
edge  e,
void *  cond_expr,
unsigned  then_prob 
)
static

This function is called from loop_version. It splits the entry edge of the loop we want to version, adds the versioning condition, and adjust the edges to the two versions of the loop appropriately. e is an incoming edge. Returns the basic block containing the condition.

— edge e —- > [second_head]

Split it and insert new conditional expression and adjust edges.

— edge e —> [cond expr] —> [first_head] | +———> [second_head]

THEN_PROB is the probability of then branch of the condition.

 Split edge 'e'. This will create a new basic block, where we can
 insert conditional expr.   
 Don't set EDGE_TRUE_VALUE in RTL mode, as it's invalid there.   
 Adjust loop header phi nodes.   
bool mfb_keep_just ( )
void place_new_loop ( )

Creates place for a new LOOP in loops structure of FN.

static void remove_bbs ( basic_block ,
int   
)
static
static void remove_bbs ( )
static

Remove basic blocks BBS. NBBS is the number of the basic blocks.

bool remove_path ( )

Removes path beginning at edge E, i.e. remove basic blocks dominated by E and update loop structures and dominators. Return true if we were able to remove the path, false otherwise (and nothing is affected then).

 Keep track of whether we need to update information about irreducible
 regions.  This is the case if the removed area is a part of the
 irreducible region, or if the set of basic blocks that belong to a loop
 that is inside an irreducible region is changed, or if such a loop is
 removed.   
 We need to check whether basic blocks are dominated by the edge
 e, but we only have basic block dominators.  This is easy to
 fix &ndash; when e->dest has exactly one predecessor, this corresponds
 to blocks dominated by e->dest, if not, split the edge.   
 It may happen that by removing path we remove one or more loops
 we belong to.  In this case first unloop the loops, then proceed
 normally.   We may assume that e->dest is not a header of any loop,
 as it now has exactly one predecessor.   
 Identify the path.   
 Find "border" hexes &ndash; i.e. those with predecessor in removed path.   
 Remove the path.   
 Cancel loops contained in the path.   
 Find blocks whose dominators may be affected.   
 Recount dominators.   
 Fix placements of basic blocks inside loops and the placement of
 loops in the loop tree.   
static bool rpe_enum_p ( const_basic_block  ,
const void *   
)
static
static bool rpe_enum_p ( )
static

Checks whether basic block BB is dominated by DATA.

void scale_loop_frequencies ( )

Multiply all frequencies in LOOP by NUM/DEN.

void scale_loop_profile ( )

Multiply all frequencies in LOOP by SCALE/REG_BR_PROB_BASE. If ITERATION_BOUND is non-zero, scale even further if loop is predicted to iterate too many times.

 See if loop is predicted to iterate too many times.   
     Fixing loop profile for different trip count is not trivial; the exit
     probabilities has to be updated to match and frequencies propagated down
     to the loop body.

     We fully update only the simple case of loop with single exit that is
     either from the latch or BB just before latch and leads from BB with
     simple conditional jump.   This is OK for use in vectorizer.   
         Probability of exit must be 1/iterations.   
         Adjust counts accordingly.   
         If latch exists, change its frequency and count, since we changed
         probability of exit.  Theoretically we should update everything from
         source of exit edge to latch, but for vectorizer this is enough.   
     Roughly speaking we want to reduce the loop body profile by the
     the difference of loop iterations.  We however can do better if
     we look at the actual profile, if it is available.   
 Scale the actual probabilities.   

References basic_block_def::count, basic_block_def::frequency, and loop::latch.

static void set_zero_probability ( )
static

Sets probability and count of edge E to zero. The probability and count is redistributed evenly to the remaining edges coming from E->src.

Move the rest to one of the edges.

void unloop ( struct loop loop,
bool irred_invalidated,
bitmap  loop_closed_ssa_invalidated 
)

Remove the latch edge of a LOOP and update loops to indicate that the LOOP was removed. After this function, original loop latch will have no successor, which caller is expected to fix somehow.

If this may cause the information about irreducible regions to become invalid, IRRED_INVALIDATED is set to true.

LOOP_CLOSED_SSA_INVALIDATED, if non-NULL, is a bitmap where we store basic blocks that had non-trivial update on their loop_father.

 This is relatively straightforward.  The dominators are unchanged, as
 loop header dominates loop latch, so the only thing we have to care of
 is the placement of loops and basic blocks inside the loop tree.  We
 move them all to the loop->outer, and then let fix_bb_placements do
 its work.   
 Remove the loop and free its data.   
 We do not pass IRRED_INVALIDATED to fix_bb_placements here, as even if
 there is an irreducible region inside the cancelled loop, the flags will
 be still correct.   

Variable Documentation

edge mfb_kj_edge

A callback for make_forwarder block, to redirect all edges except for MFB_KJ_EDGE to the entry part. E is the edge for that we should decide whether to redirect it.