GCC Middle and Back End API Reference
tree-ssa-loop-prefetch.c File Reference

Data Structures

struct  mem_ref_group
struct  mem_ref
struct  ar_data


static void dump_mem_details (FILE *file, tree base, tree step, HOST_WIDE_INT delta, bool write_p)
static void dump_mem_ref ()
static struct mem_ref_groupfind_or_create_group ()
static void record_ref (struct mem_ref_group *group, gimple stmt, tree mem, HOST_WIDE_INT delta, bool write_p)
static void release_mem_refs ()
static bool idx_analyze_ref ()
static bool analyze_ref (struct loop *loop, tree *ref_p, tree *base, tree *step, HOST_WIDE_INT *delta, gimple stmt)
static bool gather_memory_references_ref (struct loop *loop, struct mem_ref_group **refs, tree ref, bool write_p, gimple stmt)
static struct mem_ref_groupgather_memory_references ()
static void prune_ref_by_self_reuse ()
static HOST_WIDE_INT ddown ()
static bool is_miss_rate_acceptable (unsigned HOST_WIDE_INT cache_line_size, HOST_WIDE_INT step, HOST_WIDE_INT delta, unsigned HOST_WIDE_INT distinct_iters, int align_unit)
static void prune_ref_by_group_reuse (struct mem_ref *ref, struct mem_ref *by, bool by_is_before)
static void prune_ref_by_reuse ()
static void prune_group_by_reuse ()
static void prune_by_reuse ()
static bool should_issue_prefetch_p ()
static bool schedule_prefetches (struct mem_ref_group *groups, unsigned unroll_factor, unsigned ahead)
static bool nothing_to_prefetch_p ()
static int estimate_prefetch_count ()
static void issue_prefetch_ref ()
static void issue_prefetches (struct mem_ref_group *groups, unsigned unroll_factor, unsigned ahead)
static bool nontemporal_store_p ()
static bool mark_nontemporal_store ()
static void emit_mfence_after_loop ()
static bool may_use_storent_in_loop_p ()
static void mark_nontemporal_stores ()
static bool should_unroll_loop_p (struct loop *loop, struct tree_niter_desc *desc, unsigned factor)
static unsigned determine_unroll_factor (struct loop *loop, struct mem_ref_group *refs, unsigned ninsns, struct tree_niter_desc *desc, HOST_WIDE_INT est_niter)
static unsigned volume_of_references ()
static unsigned volume_of_dist_vector ()
static void add_subscript_strides (tree access_fn, unsigned stride, HOST_WIDE_INT *strides, unsigned n, struct loop *loop)
static unsigned self_reuse_distance (data_reference_p dr, unsigned *loop_sizes, unsigned n, struct loop *loop)
static bool determine_loop_nest_reuse (struct loop *loop, struct mem_ref_group *refs, bool no_other_refs)
static bool trip_count_to_ahead_ratio_too_small_p ()
static bool mem_ref_count_reasonable_p ()
static bool insn_to_prefetch_ratio_too_small_p (unsigned ninsns, unsigned prefetch_count, unsigned unroll_factor)
static bool loop_prefetch_arrays ()
unsigned int tree_ssa_prefetch_arrays ()
static unsigned int tree_ssa_loop_prefetch ()
static bool gate_tree_ssa_loop_prefetch ()
gimple_opt_passmake_pass_loop_prefetch ()

Function Documentation

static void add_subscript_strides ( tree  access_fn,
unsigned  stride,
HOST_WIDE_INT strides,
unsigned  n,
struct loop loop 
   Add the steps of ACCESS_FN multiplied by STRIDE to the array STRIDE
   at the position corresponding to the loop of the step.  N is the depth
   of the considered loop nest, and, LOOP is its innermost loop.  

References HOST_WIDE_INT, loop::inner, loop_outer(), loop::next, and vNULL.

static bool analyze_ref ( struct loop loop,
tree ref_p,
tree base,
tree step,
gimple  stmt 
   Tries to express REF_P in shape &BASE + STEP * iter + DELTA, where DELTA and
   STEP are integer constants and iter is number of iterations of LOOP.  The
   reference occurs in statement STMT.  Strips nonaddressable component
   references from REF_P.  
     First strip off the component references.  Ignore bitfields.
     Also strip off the real and imagine parts of a complex, so that
     they can have the same base.  
static HOST_WIDE_INT ddown ( )
   Divides X by BY, rounding down.  

Referenced by is_miss_rate_acceptable().

static bool determine_loop_nest_reuse ( struct loop loop,
struct mem_ref_group refs,
bool  no_other_refs 
   Determines the distance till the first reuse of each reference in REFS
   in the loop nest of LOOP.  NO_OTHER_REFS is true if there are no other
   memory references in the loop.  Return false if the analysis fails.  
     Find the outermost loop of the loop nest of loop (we require that
     there are no sibling loops inside the nest).  
     For each loop, determine the amount of data accessed in each iteration.
     We use this to estimate whether the reference is evicted from the
     cache before its reuse.  
         Bound the volume by the L2 cache size, since above this bound,
         all dependence distances are equivalent.  
     Prepare the references in the form suitable for data dependence
     analysis.  We ignore unanalyzable data references (the results
     are used just as a heuristics to estimate temporality of the
     references, hence we do not need to worry about correctness).  
             If the dependence cannot be analyzed, assume that there might be
             a reuse.  
             The distance vectors are normalized to be always lexicographically
             positive, hence we cannot tell just from them whether DDR_A comes
             before DDR_B or vice versa.  However, it is not important,
             anyway -- if DDR_A is close to DDR_B, then it is either reused in
             DDR_B (and it is not nontemporal), or it reuses the value of DDR_B
             in cache (and marking it as nontemporal would not affect
                 If this is a dependence in the innermost loop (i.e., the
                 distances in all superloops are zero) and it is not
                 the trivial self-dependence with distance zero, record that
                 the references are not completely independent.  
                 Ignore accesses closer than
                 so that we use nontemporal prefetches e.g. if single memory
                 location is accessed several times in a single iteration of
                 the loop.  
static unsigned determine_unroll_factor ( struct loop loop,
struct mem_ref_group refs,
unsigned  ninsns,
struct tree_niter_desc desc,
HOST_WIDE_INT  est_niter 
   Determine the coefficient by that unroll LOOP, from the information
   contained in the list of memory references REFS.  Description of
   umber of iterations of LOOP is stored to DESC.  NINSNS is the number of
   insns of the LOOP.  EST_NITER is the estimated number of iterations of
   the loop, or -1 if no estimate is available.  
     First check whether the loop is not too large to unroll.  We ignore
     PARAM_MAX_UNROLL_TIMES, because for small loops, it prevented us
     from unrolling them enough to make exactly one cache line covered by each
     iteration.  Also, the goal of PARAM_MAX_UNROLL_TIMES is to prevent
     us from unrolling the loops too many times in cases where we only expect
     gains from better scheduling and decreasing loop overhead, which is not
     the case here.  
     If we unrolled the loop more times than it iterates, the unrolled version
     of the loop would be never entered.  
     Choose the factor so that we may prefetch each cache just once,
     but bound the unrolling by UPPER_BOUND.  
static void dump_mem_details ( FILE *  file,
tree  base,
tree  step,
bool  write_p 
   Dumps information about memory reference 

References mem_ref_group::base, mem_ref::delta, mem_ref::group, mem_ref_group::step, and mem_ref::write_p.

static void dump_mem_ref ( )
   Dumps information about reference REF to FILE.  
static void emit_mfence_after_loop ( )
   Issue a memory fence instruction after LOOP.  
             If possible, we prefer not to insert the fence on other paths
             in cfg.  
static int estimate_prefetch_count ( )
   Estimate the number of prefetches in the given GROUPS.
   UNROLL_FACTOR is the factor by which LOOP was unrolled.  

References mem_ref::issue_prefetch_p, issue_prefetch_ref(), mem_ref_group::next, mem_ref::next, and mem_ref_group::refs.

static struct mem_ref_group* find_or_create_group ( )
   Finds a group with BASE and STEP in GROUPS, or creates one if it does not
         If step is an integer constant, keep the list of groups sorted
         by decreasing step.  
static bool gate_tree_ssa_loop_prefetch ( )
static struct mem_ref_group* gather_memory_references ( )
   Record the suitable memory references in LOOP.  NO_OTHER_REFS is set to
   true if there are no other memory references inside the loop.  
     Scan the loop body in order, so that the former references precede the
     later ones.  

References gather_memory_references_ref().

static bool gather_memory_references_ref ( struct loop loop,
struct mem_ref_group **  refs,
tree  ref,
bool  write_p,
gimple  stmt 
   Record a memory reference REF to the list REFS.  The reference occurs in
   LOOP in statement STMT and it is write if WRITE_P.  Returns true if the
   reference was recorded, false otherwise.  
     If analyze_ref fails the default is a NULL_TREE.  We can stop here.  
     Stop if the address of BASE could not be taken.  
     Limit non-constant step prefetching only to the innermost loops and 
     only when the step is loop invariant in the entire loop nest. 
     Now we know that REF = &BASE + STEP * iter + DELTA, where DELTA and STEP
     are integer constants.  

Referenced by gather_memory_references().

static bool idx_analyze_ref ( )
   Analyzes a single INDEX of a memory reference to obtain information
   described at analyze_ref.  Callback for for_each_index.  
static bool insn_to_prefetch_ratio_too_small_p ( unsigned  ninsns,
unsigned  prefetch_count,
unsigned  unroll_factor 
   Determine whether or not the instruction to prefetch ratio in the loop is
   too small based on the profitablity consideration.
   NINSNS: estimated number of instructions in the loop,
   PREFETCH_COUNT: an estimate of the number of prefetches,
   UNROLL_FACTOR:  the factor to unroll the loop if prefetching.  
     Prefetching most likely causes performance degradation when the instruction
     to prefetch ratio is too small.  Too many prefetch instructions in a loop
     may reduce the I-cache performance.
     (unroll_factor * ninsns) is used to estimate the number of instructions in
     the unrolled loop.  This implementation is a bit simplistic -- the number
     of issued prefetch instructions is also affected by unrolling.  So,
     prefetch_mod and the unroll factor should be taken into account when
     determining prefetch_count.  Also, the number of insns of the unrolled
     loop will usually be significantly smaller than the number of insns of the
     original loop * unroll_factor (at least the induction variable increases
     and the exit branches will get eliminated), so it might be better to use
     tree_estimate_loop_size + estimated_unrolled_size.  

References single_dom_exit(), and tree_unroll_loop().

static bool is_miss_rate_acceptable ( unsigned HOST_WIDE_INT  cache_line_size,
unsigned HOST_WIDE_INT  distinct_iters,
int  align_unit 
   Given a CACHE_LINE_SIZE and two inductive memory references
   with a common STEP greater than CACHE_LINE_SIZE and an address
   difference DELTA, compute the probability that they will fall
   in different cache lines.  Return true if the computed miss rate
   is not greater than the ACCEPTABLE_MISS_RATE.  DISTINCT_ITERS is the
   number of distinct iterations after which the pattern repeats itself.
   ALIGN_UNIT is the unit of alignment in bytes.  
     It always misses if delta is greater than or equal to the cache
     line size.  
     Iterate through all possible alignments of the first
     memory reference within its cache line.  
       Iterate through all distinct iterations.  

References cst_and_fits_in_hwi(), ddown(), mem_ref::delta, mem_ref::group, HOST_WIDE_INT, int_cst_value(), mem_ref::prefetch_before, and mem_ref_group::step.

static void issue_prefetch_ref ( )
   Issue prefetches for the reference REF into loop as decided before.
   HEAD is the number of iterations to prefetch ahead.  UNROLL_FACTOR
   is the factor by which LOOP was unrolled.  
             Determine the address to prefetch.  
             The step size is non-constant but loop-invariant.  We use the
             heuristic to simply prefetch ahead iterations ahead.  
         Create the prefetch instruction.  

Referenced by estimate_prefetch_count().

static void issue_prefetches ( struct mem_ref_group groups,
unsigned  unroll_factor,
unsigned  ahead 
   Issue prefetches for the references in GROUPS into loop as decided before.
   HEAD is the number of iterations to prefetch ahead.  UNROLL_FACTOR is the
   factor by that LOOP was unrolled.  
static bool loop_prefetch_arrays ( )
   Issue prefetch instructions for array references in LOOP.  Returns
   true if the LOOP was unrolled.  
     FIXME: the time should be weighted by the probabilities of the blocks in
     the loop body.  
     Prefetching is not likely to be profitable if the trip count to ahead
     ratio is too small.  
     Step 1: gather the memory references.  
     Give up prefetching if the number of memory references in the
     loop is not reasonable based on profitablity and compilation time
     Step 2: estimate the reuse effects.  
     Step 3: determine unroll factor.  
     Estimate prefetch count for the unrolled loop.  
     Prefetching is not likely to be profitable if the instruction to prefetch
     ratio is too small.  
     Step 4: what to prefetch?  
     Step 5: unroll the loop.  TODO -- peeling of first and last few
     iterations so that we do not issue superfluous prefetches.  
     Step 6: issue the prefetches.  
gimple_opt_pass* make_pass_loop_prefetch ( )
static bool mark_nontemporal_store ( )
   If REF is a nontemporal store, we mark the corresponding modify statement
   and return true.  Otherwise, we return false.  
static void mark_nontemporal_stores ( )
   Marks nontemporal stores in LOOP.  GROUPS contains the description of memory
   references in the loop.  

References mem_ref_group::next, mem_ref::next, mem_ref::prefetch_before, and mem_ref_group::refs.

static bool may_use_storent_in_loop_p ( )
   Returns true if we can use storent in loop, false otherwise.  
     If we must issue a mfence insn after using storent, check that there
     is a suitable place for it at each of the loop exits.  

References least_common_multiple(), and mem_ref::prefetch_mod.

static bool mem_ref_count_reasonable_p ( )
   Determine whether or not the number of memory references in the loop is
   reasonable based on the profitablity and compilation time considerations.
   NINSNS: estimated number of instructions in the loop,
   MEM_REF_COUNT: total number of memory references in the loop.  
     Miss rate computation (is_miss_rate_acceptable) and dependence analysis
     (compute_all_dependences) have high costs based on quadratic complexity.
     To avoid huge compilation time, we give up prefetching if mem_ref_count
     is too large.  
     Prefetching improves performance by overlapping cache missing
     memory accesses with CPU operations.  If the loop does not have
     enough CPU operations to overlap with memory operations, prefetching
     won't give a significant benefit.  One approximate way of checking
     this is to require the ratio of instructions to memory references to
     be above a certain limit.  This approximation works well in practice.
     TODO: Implement a more precise computation by estimating the time
     for each CPU or memory op in the loop. Time estimates for memory ops
     should account for cache misses.  
static bool nontemporal_store_p ( )
   Returns true if REF is a memory write for that a nontemporal store insn
   can be used.  
     REF must be a write that is not reused.  We require it to be independent
     on all other memory references in the loop, as the nontemporal stores may
     be reordered with respect to other memory references.  
     Check that we have the storent instruction for the mode.  

References edge_def::dest, edge_def::flags, and get_loop_exit_edges().

static bool nothing_to_prefetch_p ( )
   Return TRUE if no prefetch is going to be generated in the given

References force_gimple_operand_gsi(), mem_ref::group, GSI_SAME_STMT, mem_ref_group::step, and unshare_expr().

static void prune_by_reuse ( )
   Prune the list of prefetch candidates GROUPS using the reuse analysis.  

References mem_ref::prefetch_mod, and should_issue_prefetch_p().

static void prune_group_by_reuse ( )
   Prune the prefetch candidates in GROUP using the reuse analysis.  

References dump_file, and dump_flags.

static void prune_ref_by_group_reuse ( struct mem_ref ref,
struct mem_ref by,
bool  by_is_before 
   Prune the prefetch candidate REF using the reuse with BY.
   If BY_IS_BEFORE is true, BY is before REF in the loop.  
     If the step is non constant we cannot calculate prefetch_before.  
         If the references has the same address, only prefetch the
         If the reference addresses are invariant and fall into the
         same cache line, prefetch just the first one.  
     Only prune the reference that is behind in the array.  
         Transform the data so that we may assume that the accesses
         are forward.  
     Check whether the two references are likely to hit the same cache
     line, and how distant the iterations in that it occurs are from
     each other.  
         The accesses are sure to meet.  Let us check when.  
         Do not reduce prefetch_before if we meet beyond cache size.  
     A more complicated case with step > prefetch_block.  First reduce
     the ratio between the step and the cache line size to its simplest
     terms.  The resulting denominator will then represent the number of
     distinct iterations after which each address will go back to its
     initial location within the cache line.  This computation assumes
     that PREFETCH_BLOCK is a power of two.  
         Do not reduce prefetch_before if we meet beyond cache size.  
     Try also the following iteration.  
     The ref probably does not reuse by.  

References mem_ref_group::step.

static void prune_ref_by_reuse ( )
   Prune the prefetch candidate REF using the reuses with other references
   in REFS.  
static void prune_ref_by_self_reuse ( )
   Prune the prefetch candidate REF using the self-reuse.  
     If the step size is non constant, we cannot calculate prefetch_mod.  
         Prefetch references to invariant address just once.  
static void record_ref ( struct mem_ref_group group,
gimple  stmt,
tree  mem,
bool  write_p 
   Records a memory reference MEM in GROUP with offset DELTA and write status
   WRITE_P.  The reference occurs in statement STMT.  
     Do not record the same address twice.  
         It does not have to be possible for write reference to reuse the read
         prefetch, or vice versa.  
static void release_mem_refs ( )
static bool schedule_prefetches ( struct mem_ref_group groups,
unsigned  unroll_factor,
unsigned  ahead 
   Decide which of the prefetch candidates in GROUPS to prefetch.
   AHEAD is the number of iterations to prefetch ahead (which corresponds
   to the number of simultaneous instances of one prefetch running at a
   time).  UNROLL_FACTOR is the factor by that the loop is going to be
   unrolled.  Returns true if there is anything to prefetch.  
     At most SIMULTANEOUS_PREFETCHES should be running at the same time.  
     The prefetch will run for AHEAD iterations of the original loop, i.e.,
     AHEAD / UNROLL_FACTOR iterations of the unrolled loop.  In each iteration,
     it will need a prefetch slot.  
     For now we just take memory references one by one and issue
     prefetches for as many as possible.  The groups are sorted
     starting with the largest step, since the references with
     large step are more likely to cause many cache misses.  
           The loop is far from being sufficiently unrolled for this
           prefetch.  Do not generate prefetch to avoid many redudant
           If we need to prefetch the reference each PREFETCH_MOD iterations,
           and we unroll the loop UNROLL_FACTOR times, we need to insert
           ceil (UNROLL_FACTOR / PREFETCH_MOD) instructions in each
           If more than half of the prefetches would be lost anyway, do not
           issue the prefetch.  
static unsigned self_reuse_distance ( data_reference_p  dr,
unsigned *  loop_sizes,
unsigned  n,
struct loop loop 
   Returns the volume of memory references accessed between two consecutive
   self-reuses of the reference DR.  We consider the subscripts of DR in N
   loops, and LOOP_SIZES contains the volumes of accesses in each of the
   loops.  LOOP is the innermost loop of the current loop nest.  
     In the following example:

     for (i = 0; i < N; i++)
       for (j = 0; j < N; j++)
         use (a[j][i]);
     the same cache line is accessed each N steps (except if the change from
     i to i + 1 crosses the boundary of the cache line).  Thus, for self-reuse,
     we cannot rely purely on the results of the data dependence analysis.

     Instead, we compute the stride of the reference in each loop, and consider
     the innermost loop in that the stride is less than cache size.  
         Keep track of the reference corresponding to the subscript, so that we
         know its stride.  
static bool should_issue_prefetch_p ( )
   Returns true if we should issue prefetch for REF.  
     For now do not issue prefetches for only first few of the
     Do not prefetch nontemporal stores.  

Referenced by prune_by_reuse().

static bool should_unroll_loop_p ( struct loop loop,
struct tree_niter_desc desc,
unsigned  factor 
   Determines whether we can profitably unroll LOOP FACTOR times, and if
   this is the case, fill in DESC by the description of number of
     We only consider loops without control flow for unrolling.  This is not
     a hard restriction -- tree_unroll_loop works with arbitrary loops
     as well; but the unrolling/prefetching is usually more profitable for
     loops consisting of a single basic block, and we want to limit the
     code growth.  
static unsigned int tree_ssa_loop_prefetch ( )
unsigned int tree_ssa_prefetch_arrays ( )
   Issue prefetch instructions for array references in loops.  
         It is possible to ask compiler for say -mtune=i486 -march=pentium4.
         -mtune=i486 causes us having PREFETCH_BLOCK 0, since this is part
         of processor costs and i486 does not have prefetch, but
         -march=pentium4 causes HAVE_prefetch to be true.  Ugh.  
     We assume that size of cache line is a power of two, so verify this
static bool trip_count_to_ahead_ratio_too_small_p ( )
   Determine whether or not the trip count to ahead ratio is too small based
   on prefitablility consideration.
   AHEAD: the iteration ahead distance,
   EST_NITER: the estimated trip count.  
     Assume trip count to ahead ratio is big enough if the trip count could not
     be estimated at compile time.  

References dump_file, and dump_flags.

static unsigned volume_of_dist_vector ( )
   Returns the volume of memory references accessed across VEC iterations of
   loops, whose sizes are described in the LOOP_SIZES array.  N is the number
   of the loops in the nest (length of VEC and LOOP_SIZES vectors).  
     We ignore the parts of the distance vector in subloops, since usually
     the numbers of iterations are much smaller.  
static unsigned volume_of_references ( )
   Returns the total volume of the memory references REFS, taking into account
   reuses in the innermost loop and cache line size.  TODO -- we should also
   take into account reuses across the iterations of the loops in the loop
           Almost always reuses another value?  
           If several iterations access the same cache line, use the size of
           the line divided by this number.  Otherwise, a cache line is
           accessed in each iteration.  TODO -- in the latter case, we should
           take the size of the reference into account, rounding it up on cache
           line size multiple.