GCC Middle and Back End API Reference
combine-stack-adj.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 "regs.h"
#include "hard-reg-set.h"
#include "flags.h"
#include "function.h"
#include "expr.h"
#include "basic-block.h"
#include "df.h"
#include "except.h"
#include "reload.h"
#include "tree-pass.h"
Include dependency graph for combine-stack-adj.c:

Data Structures

struct  csa_reflist
struct  record_stack_refs_data

Macros

#define STACK_GROWS_DOWNWARD   0

Functions

static int stack_memref_p (rtx)
static rtx single_set_for_csa (rtx)
static void free_csa_reflist (struct csa_reflist *)
static struct csa_reflistrecord_one_stack_ref (rtx, rtx *, struct csa_reflist *)
static int try_apply_stack_adjustment (rtx, struct csa_reflist *, HOST_WIDE_INT, HOST_WIDE_INT)
static void combine_stack_adjustments_for_block (basic_block)
static int record_stack_refs (rtx *, void *)
static void combine_stack_adjustments ()
static int stack_memref_p ()
static rtx single_set_for_csa ()
static void free_csa_reflist ()
static struct csa_reflistrecord_one_stack_ref ()
static int record_stack_refs ()
static void maybe_move_args_size_note ()
static rtx prev_active_insn_bb ()
static rtx next_active_insn_bb ()
static void force_move_args_size_note ()
static void combine_stack_adjustments_for_block ()
static bool gate_handle_stack_adjustments ()
static unsigned int rest_of_handle_stack_adjustments ()
rtl_opt_passmake_pass_stack_adjustments ()

Macro Definition Documentation

#define STACK_GROWS_DOWNWARD   0

Combine stack adjustments. Copyright (C) 1987-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/. Track stack adjustments and stack memory references. Attempt to reduce the number of stack adjustments by back-propagating across the memory references.

This is intended primarily for use with targets that do not define ACCUMULATE_OUTGOING_ARGS. It is of significantly more value to targets that define PREFERRED_STACK_BOUNDARY more aligned than STACK_BOUNDARY (e.g. x86), or if not all registers can be pushed (e.g. x86 fp regs) which would ordinarily have to be implemented as a sub/mov pair due to restrictions in calls.c.

Propagation stops when any of the insns that need adjusting are (a) no longer valid because we've exceeded their range, (b) a non-trivial push instruction, or (c) a call instruction.

Restriction B is based on the assumption that push instructions are smaller or faster. If a port really wants to remove all pushes, it should have defined ACCUMULATE_OUTGOING_ARGS. The one exception that is made is for an add immediately followed by a push. Turn STACK_GROWS_DOWNWARD into a boolean.

Referenced by push_block().


Function Documentation

static void combine_stack_adjustments ( )
static

Main entry point for stack adjustment combination.

References MEM_P, and XEXP.

static void combine_stack_adjustments_for_block ( basic_block  )
static
static void combine_stack_adjustments_for_block ( )
static

Subroutine of combine_stack_adjustments, called for each basic block.

         Find constant additions to the stack pointer.   
             If we've not seen an adjustment previously, record
             it now and continue.   
             If not all recorded refs can be adjusted, or the
             adjustment is now too large for a constant addition,
             we cannot merge the two stack adjustments.

             Also we need to be careful to not move stack pointer
             such that we create stack accesses outside the allocated
             area.  We can combine an allocation into the first insn,
             or a deallocation into the second insn.  We can not
             combine an allocation followed by a deallocation.

             The only somewhat frequent occurrence of the later is when
             a function allocates a stack frame but does not use it.
             For this case, we would need to analyze rtl stream to be
             sure that allocated area is really unused.  This means not
             only checking the memory references, but also all registers
             or global memory references possibly containing a stack
             frame address.

             Perhaps the best way to address this problem is to teach
             gcc not to allocate stack for objects never used.   
             Combine an allocation into the first instruction.   
                     It worked!   
             Otherwise we have a deallocation.  Do not combine with
             a previous allocation.  Combine into the second insn.   
                     It worked!   
             Combination failed.  Restart processing from here.  If
             deallocation+allocation conspired to cancel, we can
             delete the old deallocation insn.   
         Find a store with pre-(dec|inc)rement or pre-modify of exactly
         the previous adjustment and turn it into a simple store.  This
         is equivalent to anticipating the stack adjustment so this must
         be an allocation.   
     Otherwise, we were not able to process the instruction.
     Do not continue collecting data across such a one.   

References record_stack_refs_data::insn.

static void force_move_args_size_note ( )
static

If INSN has a REG_ARGS_SIZE note, if possible move it to PREV. Otherwise search for a nearby candidate within BB where we can stick the note.

 If PREV exists, tail-call to the logic in the other function.   
 First, make sure there's anything that needs doing.   
 We need to find a spot between the previous and next exception points
 where we can place the note and "properly" deallocate the arguments.   
 It is often the case that we have insns in the order:
    call
    add sp (previous deallocation)
    sub sp (align for next arglist)
    push arg
 and the add/sub cancel.  Therefore we begin by searching forward.   
     Found an existing note: nothing to do.   
     Found something that affects unwinding.  Stop searching.   
     Found a place that seems logical to adjust the stack.   
     Found something that affects unwinding.  Stop searching.   
     ??? We *must* have a place, lest we ICE on the lost adjustment.
     Options are: dummy clobber insn, nop, or prevent the removal of
     the sp += 0 insn.   
     TODO: Find another way to indicate to the dwarf2 code that we
     have not in fact lost an adjustment.   

References CALL_P, find_reg_note(), insn_nothrow_p(), NULL, and NULL_RTX.

static void free_csa_reflist ( struct csa_reflist )
static
static void free_csa_reflist ( )
static

Free the list of csa_reflist nodes.

References INTVAL, REG_P, csa_reflist::sp_offset, stack_pointer_rtx, and XEXP.

static bool gate_handle_stack_adjustments ( )
static

This is kind of a heuristic. We need to run combine_stack_adjustments even for machines with possibly nonzero TARGET_RETURN_POPS_ARGS and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having push instructions will have popping returns.

rtl_opt_pass* make_pass_stack_adjustments ( )
static void maybe_move_args_size_note ( )
static

If INSN has a REG_ARGS_SIZE note, move it to LAST. AFTER is true iff LAST follows INSN in the instruction stream.

The ARGS_SIZE notes are not cumulative. They represent an absolute value, and the "most recent" note wins.

Referenced by next_active_insn_bb().

static rtx next_active_insn_bb ( )
static
static rtx prev_active_insn_bb ( )
static

Return the next (or previous) active insn within BB.

static struct csa_reflist* record_one_stack_ref ( rtx  ,
rtx ,
struct csa_reflist  
)
staticread

Referenced by record_stack_refs().

static struct csa_reflist* record_one_stack_ref ( )
staticread

Create a new csa_reflist node from the given stack reference. It is already known that the reference is either a MEM satisfying the predicate stack_memref_p or a REG representing the stack pointer.

static int record_stack_refs ( rtx ,
void *   
)
static
static int record_stack_refs ( )
static
     We are not able to handle correctly all possible memrefs containing
     stack pointer, so this check is necessary.   
     Try harder for DEBUG_INSNs, handle e.g. (mem (mem (sp + 16) + 4).   
     ??? We want be able to handle non-memory stack pointer
     references later.  For now just discard all insns referring to
     stack pointer outside mem expressions.  We would probably
     want to teach validate_replace to simplify expressions first.

     We can't just compare with STACK_POINTER_RTX because the
     reference to the stack pointer might be in some other mode.
     In particular, an explicit clobber in an asm statement will
     result in a QImode clobber.

     In DEBUG_INSNs, we want to replace all occurrences, otherwise
     they will cause -fcompare-debug failures.   

References record_stack_refs_data::insn, record_one_stack_ref(), and record_stack_refs_data::reflist.

static unsigned int rest_of_handle_stack_adjustments ( )
static
static rtx single_set_for_csa ( rtx  )
static
static rtx single_set_for_csa ( )
static

Recognize either normal single_set or the hack in i386.md for tying fp and sp adjustments.

The special case is allowing a no-op set.

static int stack_memref_p ( rtx  )
static
static int stack_memref_p ( )
static

Recognize a MEM of the form (sp) or (plus sp const).

static int try_apply_stack_adjustment ( rtx  insn,
struct csa_reflist reflist,
HOST_WIDE_INT  new_adjust,
HOST_WIDE_INT  delta 
)
static

Attempt to apply ADJUST to the stack adjusting insn INSN, as well as each of the memories and stack references in REFLIST. Return true on success.

Succeeded. Update our knowledge of the stack references.