GCC Middle and Back End API Reference
compare-elim.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "tm_p.h"
#include "insn-config.h"
#include "recog.h"
#include "flags.h"
#include "basic-block.h"
#include "tree-pass.h"
#include "target.h"
#include "df.h"
#include "domwalk.h"
Include dependency graph for compare-elim.c:

Data Structures

struct  comparison_use
struct  comparison
class  find_comparison_dom_walker

Macros

#define MAX_CMP_USE   3
#define SELECT_CC_MODE(A, B, C)   (gcc_unreachable (), VOIDmode)

Typedefs

typedef struct comparisoncomparison_struct_p

Functions

static rtx conforming_compare ()
static bool arithmetic_flags_clobber_p ()
static void find_flags_uses_in_insn ()
static void find_comparisons ()
static rtx maybe_select_cc_mode (struct comparison *cmp, rtx a, rtx b)
static bool try_eliminate_compare ()
static unsigned int execute_compare_elim_after_reload ()
static bool gate_compare_elim_after_reload ()
rtl_opt_passmake_pass_compare_elim_after_reload ()

Variables

static vec< comparison_struct_pall_compares

Macro Definition Documentation

#define MAX_CMP_USE   3

Post-reload compare elimination. Copyright (C) 2010-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/. There is a set of targets whose general-purpose move or addition instructions clobber the flags. These targets cannot split their CBRANCH/CSTORE etc patterns before reload is complete, lest reload itself insert these instructions in between the flags setter and user. Because these targets cannot split the compare from the use, they cannot make use of the comparison elimination offered by the combine pass.

This is a small pass intended to provide comparison elimination similar to what is available via NOTICE_UPDATE_CC for cc0 targets. This should help encourage cc0 targets to convert to an explicit post-reload representation of the flags.

This pass assumes:

(0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.

(1) All comparison patterns are represented as

 [(set (reg:CC) (compare:CC (reg) (immediate)))]

(2) All insn patterns that modify the flags are represented as

 [(set (reg) (operation)
  (clobber (reg:CC))]

(3) If an insn of form (2) can usefully set the flags, there is another pattern of the form

[(set (reg) (operation) (set (reg:CCM) (compare:CCM (operation) (immediate)))]

The mode CCM will be chosen as if by SELECT_CC_MODE.

Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands. This could be handled as a future enhancement. These structures describe a comparison and how it is used. The choice of maximum 3 uses comes from wanting to eliminate the two duplicate compares from a three-way branch on the sign of a value. This is also sufficient to eliminate the duplicate compare against the high-part of a double-word comparison.

#define SELECT_CC_MODE (   A,
  B,
 
)    (gcc_unreachable (), VOIDmode)

Typedef Documentation


Function Documentation

static bool arithmetic_flags_clobber_p ( )
static

Look for a pattern of the "correct" form for an insn with a flags clobber for which we may be able to eliminate a compare later. We're not looking to validate any inputs at this time, merely see that the basic shape is correct. The term "arithmetic" may be somewhat misleading...

References targetm.

static rtx conforming_compare ( )
static

Look for a "conforming" comparison, as defined above. If valid, return the rtx for the COMPARE itself.

static unsigned int execute_compare_elim_after_reload ( )
static

Main entry point to the pass.

Locate all comparisons and their uses, and eliminate duplicates.

     Eliminate comparisons that are redundant with flags computation.   
static void find_comparisons ( )
static

Find all comparisons in the function.

static void find_flags_uses_in_insn ( )
static

Look for uses of FLAGS in INSN. If we find one we can analyze, record it in CMP; otherwise indicate that we've missed a use.

 If we've already lost track of uses, don't bother collecting more.   
 Find a USE of the flags register.   
       If this is an unusual use, quit.   
       If we've run out of slots to record uses, quit.   
       Unfortunately the location of the flags register, while present
       in the reference structure, doesn't help.  We need to find the
       comparison code that is outer to the actual flags use.   
           We've found a use of the flags that we understand.   
 We failed to recognize this use of the flags register.   
static bool gate_compare_elim_after_reload ( )
static

Setting this target hook value is how a backend indicates the need.

rtl_opt_pass* make_pass_compare_elim_after_reload ( )
static rtx maybe_select_cc_mode ( struct comparison cmp,
rtx  a,
rtx  b 
)
static

Select an alternate CC_MODE for a comparison insn comparing A and B. Note that inputs are almost certainly different than the IN_A and IN_B stored in CMP – we're called while attempting to eliminate the compare after all. Return the new FLAGS rtx if successful, else return NULL. Note that this function may start a change group.

 Minimize code differences when this target macro is undefined.   
 If we don't have access to all of the uses, we can't validate.   
 Find a new mode that works for all of the uses.  Special case the
 common case of exactly one use.   

References gen_rtx_REG(), comparison_use::insn, comparison_use::loc, targetm, comparison::uses, and validate_change().

static bool try_eliminate_compare ( )
static

Attempt to replace a comparison with a prior arithmetic insn that can compute the same flags value as the comparison itself. Return true if successful, having made all rtl modifications necessary.

 We must have found an interesting "clobber" preceding the compare.   
 ??? For the moment we don't handle comparisons for which IN_B
 is a register.  We accepted these during initial comparison 
 recognition in order to eliminate duplicate compares.
 An improvement here would be to handle x = a - b; if (a cmp b).   
 Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
 Given that this target requires this pass, we can assume that most
 insns do clobber the flags, and so the distance between the compare
 and the clobber is likely to be small.   
 ??? This is one point at which one could argue that DF_REF_CHAIN would
 be useful, but it is thought to be too heavy-weight a solution here.   
     Note that the BB_HEAD is always either a note or a label, but in
     any case it means that IN_A is defined outside the block.   
     Find a possible def of IN_A in INSN.   
     No definitions of IN_A; continue searching.   
     Bail if this is not a totally normal set of IN_A.   
     We've found an insn between the compare and the clobber that sets
     IN_A.  Given that pass_cprop_hardreg has not yet run, we still find
     situations in which we can usefully look through a copy insn.   
 We've reached PREV_CLOBBER without finding a modification of IN_A.
 Validate that PREV_CLOBBER itself does in fact refer to IN_A.  Do
 recall that we've already validated the shape of PREV_CLOBBER.   
 Also check operations with implicit extensions, e.g.:
 [(set (reg:DI)
       (zero_extend:DI (plus:SI (reg:SI)(reg:SI))))
  (set (reg:CCZ flags)
       (compare:CCZ
         (plus:SI (reg:SI)(reg:SI))
         (const_int 0)))]                            
 Determine if we ought to use a different CC_MODE here.   
 Generate a new comparison for installation in the setter.   
 Succeed if the new instruction is valid.  Note that we may have started
 a change group within maybe_select_cc_mode, therefore we must continue.  
 Success.  Delete the compare insn...   
 ... and any notes that are now invalid due to multiple sets.   

Variable Documentation

vec<comparison_struct_p> all_compares
static