GCC Middle and Back End API Reference
reg-stack.c File Reference

Data Structures

struct  stack_def
struct  block_info_def


typedef struct stack_defstack_ptr
typedef struct block_info_defblock_info


enum  emit_where { EMIT_AFTER, EMIT_BEFORE }


static int stack_regs_mentioned_p (const_rtx pat)
static void pop_stack (stack_ptr, int)
static rtxget_true_reg (rtx *)
static int check_asm_stack_operands (rtx)
static void get_asm_operands_in_out (rtx, int *, int *)
static rtx stack_result (tree)
static void replace_reg (rtx *, int)
static void remove_regno_note (rtx, enum reg_note, unsigned int)
static int get_hard_regnum (stack_ptr, rtx)
static rtx emit_pop_insn (rtx, stack_ptr, rtx, enum emit_where)
static void swap_to_top (rtx, stack_ptr, rtx, rtx)
static bool move_for_stack_reg (rtx, stack_ptr, rtx)
static bool move_nan_for_stack_reg (rtx, stack_ptr, rtx)
static int swap_rtx_condition_1 (rtx)
static int swap_rtx_condition (rtx)
static void compare_for_stack_reg (rtx, stack_ptr, rtx)
static bool subst_stack_regs_pat (rtx, stack_ptr, rtx)
static void subst_asm_stack_regs (rtx, stack_ptr)
static bool subst_stack_regs (rtx, stack_ptr)
static void change_stack (rtx, stack_ptr, stack_ptr, enum emit_where)
static void print_stack (FILE *, stack_ptr)
static rtx next_flags_user (rtx)
static int stack_regs_mentioned_p ()
int stack_regs_mentioned ()
static rtx next_flags_user ()
static void straighten_stack ()
static void pop_stack ()
static rtxget_true_reg ()
static int check_asm_stack_operands ()
static void get_asm_operands_in_out ()
static rtx stack_result ()
static void replace_reg ()
static void remove_regno_note ()
static int get_hard_regnum ()
static rtx emit_pop_insn ()
static void emit_swap_insn ()
static void swap_to_top ()
static bool move_for_stack_reg ()
static bool move_nan_for_stack_reg ()
static int swap_rtx_condition_1 ()
static int swap_rtx_condition ()
static void compare_for_stack_reg ()
static int subst_stack_regs_in_debug_insn ()
static void subst_all_stack_regs_in_debug_insn ()
static bool subst_stack_regs_pat ()
static void subst_asm_stack_regs ()
static bool subst_stack_regs ()
static void change_stack ()
static void print_stack ()
static int convert_regs_entry ()
static void convert_regs_exit ()
static void propagate_stack ()
static bool compensate_edge ()
static bool compensate_edges ()
static edge better_edge ()
static bool convert_regs_1 ()
static bool convert_regs_2 ()
static void convert_regs ()
static bool reg_to_stack ()
static bool gate_handle_stack_regs ()
rtl_opt_passmake_pass_stack_regs ()
static unsigned int rest_of_handle_stack_regs ()
rtl_opt_passmake_pass_stack_regs_run ()


static vec< char > stack_regs_mentioned_data
int regstack_completed = 0
static basic_block current_block
static bool starting_stack_p
static rtx not_a_num
static rtx ix86_flags_rtx
static bool any_malformed_asm

Typedef Documentation

typedef struct block_info_def * block_info
   This is used to carry information about basic blocks.  It is
   attached to the AUX field of the standard CFG block.  
typedef struct stack_def * stack_ptr
   This is the basic stack record.  TOP is an index into REG[] such
   that REG[TOP] is the top of stack.  If TOP is -1 the stack is empty.

   If TOP is -2, REG[] is not yet initialized.  Stack initialization
   consists of placing each live reg in array `reg' and setting `top'

   REG_SET indicates which registers are live.  

Enumeration Type Documentation

enum emit_where
   Passed to change_stack to indicate where to emit insns.  

Function Documentation

static edge better_edge ( )
   Select the better of two edges E1 and E2 to use to determine the
   stack layout for their shared destination basic block.  This is
   typically the more frequently executed.  The edge E1 may be NULL
   (in which case E2 is returned), but E2 is always non-NULL.  
     Prefer critical edges to minimize inserting compensation code on
     critical edges.  
     Avoid non-deterministic behavior.  
static void change_stack ( rtx  ,
stack_ptr  ,
stack_ptr  ,
enum  emit_where 

Referenced by convert_regs_entry().

static void change_stack ( )
   Change the organization of the stack so that it fits a new basic
   block.  Some registers might have to be popped, but there can never be
   a register live in the new block that is not now live.

   Insert any needed insns before or after INSN, as indicated by
   WHERE.  OLD is the original stack layout, and NEW is the desired
   form.  OLD is updated to reflect the code emitted, i.e., it will be
   the same as NEW upon return.

   This function will not preserve block_end[].  But that information
   is no longer needed once this has executed.  
     Stack adjustments for the first insn in a block update the
     current_block's stack_in instead of inserting insns directly.
     compensate_edges will add the necessary code later.  
     We will be inserting new insns "backwards".  If we are to insert
     after INSN, find the next insn, and insert before it.  
     Initialize partially dead variables.  
     Pop any registers that are not needed in the new block.  
     If the destination block's stack already has a specified layout
     and contains two or more registers, use a more intelligent algorithm
     to pop registers that minimizes the number number of fxchs below.  
         First pass to determine the free slots.  
         Second pass to allocate preferred slots.  
                     If this is a preference for the new top of stack, record
                     the fact by remembering it's old->reg in topsrc.  
         Intentionally, avoid placing the top of stack in it's correct
         location, if we still need to permute the stack below and we
         can usefully place it somewhere else.  This is the case if any
         slot is still unallocated, in which case we should place the
         top of stack there.  
         Third pass allocates remaining slots and emits pop insns.  
                 Find next free slot.  
         The following loop attempts to maximize the number of times we
         pop the top of the stack, as this permits the use of the faster
         ffreep instruction on platforms that support it.  
         If the new block has never been processed, then it can inherit
         the old stack order.  
         This block has been entered before, and we must match the
         previously selected stack order.  
         By now, the only difference should be the order of the stack,
         not their depth or liveliness.  
         If the stack is not empty (new_stack->top != -1), loop here emitting
         swaps until the stack is correct.

         The worst case number of swaps emitted is N + 2, where N is the
         depth of the stack.  In some cases, the reg at the top of
         stack may be correct, but swapped anyway in order to fix
         other regs.  But since we never swap any other reg away from
         its correct slot, this algorithm will converge.  
               Swap the reg at top of stack into the position it is
               supposed to be in, until the correct top of stack appears.  
               See if any regs remain incorrect.  If so, bring an
             incorrect reg to the top of stack, and let the while loop
             above fix it.  
         At this point there must be no differences.  

References emit_swap_insn(), stack_def::reg, and stack_def::top.

static int check_asm_stack_operands ( rtx  )
static int check_asm_stack_operands ( )
   There are many rules that an asm statement for stack-like regs must
   follow.  Those rules are explained at the top of this file: the rule
   numbers below refer to that explanation.  
     Find out what the constraints require.  If no constraint
     alternative matches, this asm is malformed.  
         Avoid further trouble with this insn.  
     Strip SUBREGs here to make the following code simpler.  
     Set up CLOBBER_REG.  
     Enforce rule #4: Output operands must specifically indicate which
     reg an output appears in after an asm.  "=f" is not allowed: the
     operand constraints must select a class with a single reg.

     Also enforce rule #5: Output operands must start at the top of
     the reg-stack: output operands may not "skip" a reg.  
     Search for first non-popped reg.  
     If there are any other popped regs, that's an error.  
     Enforce rule #2: All implicitly popped input regs must be closer
     to the top of the reg-stack than any input that is not implicitly
           An input reg is implicitly popped if it is tied to an
           output, or if there is a CLOBBER for it.  
     Search for first non-popped reg.  
     If there are any other popped regs, that's an error.  
     Enforce rule #3: If any input operand uses the "f" constraint, all
     output constraints must use the "&" earlyclobber.

     ??? Detect this more deterministically by having constrain_asm_operands
     record any earlyclobber.  
         Avoid further trouble with this insn.  
static void compare_for_stack_reg ( rtx  ,
stack_ptr  ,
static void compare_for_stack_reg ( )
   Handle a comparison.  Special care needs to be taken to avoid
   causing comparisons that a 387 cannot do correctly, such as EQ.

   Also, a pop insn may need to be emitted.  The 387 does have an
   `fcompp' insn that can pop two regs, but it is sometimes too expensive
   to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
   set up.  
     ??? If fxch turns out to be cheaper than fstp, give priority to
     registers that die in this insn - move those to stack top first.  
     We will fix any death note later.  
     If the second operand dies, handle that.  But if the operands are
     the same stack register, don't bother, because only one death is
     needed, and it was just handled.  
         As a special case, two regs may die in this insn if src2 is
         next to top of stack and the top of stack also dies.  Since
         we have already popped src1, "next to top of stack" is really
         at top (FIRST_STACK_REG) now.  
             The 386 can only represent death of the first operand in
             the case handled above.  In all other cases, emit a separate
             pop and remove the death note from here.  
static bool compensate_edge ( )
   Adjust the stack of edge E's source block on exit to match the stack
   of it's target block upon input.  The stack layouts of both blocks
   should have been defined by now.  
     Check whether stacks are identical.  
     Abnormal calls may appear to have values live in st(0), but the
     abnormal return path will not have actually loaded the values.  
         Assert that the lifetimes are as we expect -- one value
         live at st(0) on the end of the source block, and no
         values live at the beginning of the destination block.
         For complex return values, we may have st(1) live as well.  
     Handle non-call EH edges specially.  The normal return path have
     values in registers.  These will be popped en masse by the unwind
     We don't support abnormal edges.  Global takes care to
     avoid any live register across them, so we should never
     have to insert instructions on such edges.  
     Make a copy of source_stack as change_stack is destructive.  
     It is better to output directly to the end of the block
     instead of to the edge, because emit_swap can do minimal
     insn scheduling.  We can do this when there is only one
     edge out, and it is not abnormal.  
         ??? change_stack needs some point to emit insns after.  
static bool compensate_edges ( )
   Traverse all non-entry edges in the CFG, and emit the necessary
   edge compensation code to change the stack from stack_out of the
   source block to the stack_in of the destination block.  
static void convert_regs ( )
   Traverse all basic blocks in a function, converting the register
   references in each insn from the "flat" register file that gcc uses,
   to the stack-like registers the 387 uses.  
     Initialize uninitialized registers on function entry.  
     Construct the desired stack for function exit.  
     ??? Future: process inner loops first, and give them arbitrary
     initial stacks which emit_swap_insn can modify.  This ought to
     prevent double fxch that often appears at the head of a loop.  
     Process all blocks reachable from all entry points.  
     ??? Process all unreachable blocks.  Though there's no excuse
     for keeping these even when not optimizing.  
     We must fix up abnormal edges before inserting compensation code
     because both mechanisms insert insns on edges.  
static bool convert_regs_1 ( )
   Convert stack register references in one block.  Return true if the CFG
   has been modified in the process.  
     Choose an initial stack layout, if one hasn't already been chosen.  
         Select the best incoming edge (typically the most frequent) to
         use as a template for this basic block.  
             No predecessors.  Create an arbitrary input stack.  
     Process all insns in this block.  Keep track of NEXT so that we
     don't process insns emitted while substituting in INSN.  
         Ensure we have not missed a block boundary.  
         Don't bother processing unless there is a stack reg
         mentioned or if it's a CALL_INSN.  
                 Nothing must ever die at a debug insn.  If something
                 is referenced in it that becomes dead, it should have
                 died before and the reference in the debug insn
                 should have been removed so as to avoid changing code
         Since it's the first non-debug instruction that determines
         the stack requirements of the current basic block, we refrain
         from updating debug insns before it in the loop above, and
         fix them up here.  
     If the function is declared to return a value, but it returns one
     in only some cases, some registers might come live here.  Emit
     necessary moves for them.  
     Amongst the insns possibly deleted during the substitution process above,
     might have been the only trapping insn in the block.  We purge the now
     possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
     called at the end of convert_regs.  The order in which we process the
     blocks ensures that we never delete an already processed edge.

     Note that, at this point, the CFG may have been damaged by the emission
     of instructions after an abnormal call, which moves the basic block end
     (and is the reason why we call fixup_abnormal_edges later).  So we must
     be sure that the trapping insn has been deleted before trying to purge
     dead edges, otherwise we risk purging valid edges.

     ??? We are normally supposed not to delete trapping insns, so we pretend
     that the insns deleted above don't actually trap.  It would have been
     better to detect this earlier and avoid creating the EH edge in the first
     place, still, but we don't have enough information at that time.  
     Something failed if the stack lives don't match.  If we had malformed
     asms, we zapped the instruction itself, but that didn't produce the
     same pattern of register kills as before.  

References edge_def::dest.

static bool convert_regs_2 ( )
   Convert registers in all blocks reachable from BLOCK.  Return true if the
   CFG has been modified in the process.  
     We process the blocks in a top-down manner, in a way such that one block
     is only processed after all its predecessors.  The number of predecessors
     of every block has already been computed.  
         Processing BLOCK is achieved by convert_regs_1, which may purge
         some dead EH outgoing edge after the deletion of the trapping
         insn inside the block.  Since the number of predecessors of
         BLOCK's successors was computed based on the initial edge set,
         we check the necessity to process some of these successors
         before such an edge deletion may happen.  However, there is
         a pitfall: if BLOCK is the only predecessor of a successor and
         the edge between them happens to be deleted, the successor
         becomes unreachable and should not be processed.  The problem
         is that there is no way to preventively detect this case so we
         stack the successor in all cases and hand over the task of
         fixing up the discrepancy to convert_regs_1.  
static int convert_regs_entry ( )
   This function was doing life analysis.  We now let the regular live
   code do it's job, so we only need to check some extra invariants
   that reg-stack expects.  Primary among these being that all registers
   are initialized before use.

   The function returns true when code was emitted to CFG edges and
   commit_edge_insertions needs to be called.  
     Load something into each stack register live at function entry.
     Such live registers can be caused by uninitialized variables or
     functions not returning values on all paths.  In order to keep
     the push/pop code happy, and to not scrog the register stack, we
     must put something in these registers.  Use a QNaN.

     Note that we are inserting converted code here.  This code is
     never seen by the convert_regs pass.  

References change_stack(), EMIT_BEFORE, emit_note(), end_sequence(), get_insns(), insert_insn_on_edge(), and start_sequence().

static void convert_regs_exit ( )
   Construct the desired stack for function exit.  This will either
   be `empty', or the function return value at top-of-stack.  
static rtx emit_pop_insn ( rtx  ,
stack_ptr  ,
rtx  ,
enum  emit_where 
static rtx emit_pop_insn ( )
   Emit an insn to pop virtual register REG before or after INSN.
   REGSTACK is the stack state after INSN and is updated to reflect this
   pop.  WHEN is either emit_insn_before or emit_insn_after.  A pop insn
   is represented as a SET whose destination is the register to be popped
   and source is the top of stack.  A death note for the top of stack
   cases the movdf pattern to pop.  
     For complex types take care to pop both halves.  These may survive in
     CLOBBER and USE expressions.  

References get_hard_regnum(), i1, limit, stack_def::reg, stack_regs_mentioned(), and stack_def::top.

static void emit_swap_insn ( )
   Emit an insn before or after INSN to swap virtual register REG with
   the top of stack.  REGSTACK is the stack state before the swap, and
   is updated to reflect the swap.  A swap insn is represented as a
   PARALLEL of two patterns: each pattern moves one reg to the other.

   If REG is already at the top of the stack, no insn is emitted.  
         Something failed if the register wasn't on the stack.  If we had
         malformed asms, we zapped the instruction itself, but that didn't
         produce the same pattern of register sets as before.  To prevent
         further failure, adjust REGSTACK to include REG at TOP.  
     Find the previous insn involving stack regs, but don't pass a
     block boundary.  
         If the previous register stack push was from the reg we are to
         swap with, omit the swap.  
         If the previous insn wrote to the reg we are to swap with,
         omit the swap.  
     Avoid emitting the swap if this is the first register stack insn
     of the current_block.  Instead update the current_block's stack_in
     and let compensate edges take care of this for us.  

Referenced by change_stack().

static bool gate_handle_stack_regs ( )
static void get_asm_operands_in_out ( rtx  ,
int *  ,
int *   
static void get_asm_operands_in_out ( )
   Calculate the number of inputs and outputs in BODY, an
   asm_operands.  N_OPERANDS is the total number of operands, and
   N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
static int get_hard_regnum ( stack_ptr  ,
static int get_hard_regnum ( )
   Find the hard register number of virtual register REG in REGSTACK.
   The hard register number is relative to the top of the stack.  -1 is
   returned if the register is not found.  
static rtx* get_true_reg ( rtx )
static rtx* get_true_reg ( )
   Return a pointer to the REG expression within PAT.  If PAT is not a
   REG, possible enclosed by a conversion rtx, return the inner part of
   PAT that stopped the search.  
           Eliminate FP subregister accesses in favor of the
           actual FP register in use.  
rtl_opt_pass* make_pass_stack_regs ( )
rtl_opt_pass* make_pass_stack_regs_run ( )
static bool move_for_stack_reg ( rtx  ,
stack_ptr  ,
static bool move_for_stack_reg ( )
   Handle a move to or from a stack register in PAT, which is in INSN.
   REGSTACK is the current stack.  Return whether a control flow insn
   was deleted in the process.  
         Write from one stack reg to another.  If SRC dies here, then
         just change the register mapping and delete the insn.  
             If this is a no-op move, there must not be a REG_DEAD note.  
             The destination must be dead, or life analysis is borked.  
             If the source is not live, this is yet another case of
             uninitialized variables.  Load up a NaN instead.  
             It is possible that the dest is unused after this insn.
             If so, just pop the src.  
         The source reg does not die.  
         If this appears to be a no-op move, delete it, or else it
         will confuse the machine description output patterns. But if
         it is REG_UNUSED, we must pop the reg now, as per-insn processing
         for REG_UNUSED will not work for deleted insns.  
         The destination ought to be dead.  
         Save from a stack reg to MEM, or possibly integer reg.  Since
         only top of stack may be saved, emit an exchange first if
         needs be.  
             A 387 cannot write an XFmode value to a MEM without
             clobbering the source reg.  The output code can handle
             this by reading back the value from the MEM.
             But it is more efficient to use a temp register if one is
             available.  Push the source value here if the register
             stack is not full, and then write the value to memory via
             a pop.  
         Load from MEM, or possibly integer REG or constant, into the
         stack regs.  The actual target is always the top of the
         stack. The stack mapping is changed to reflect that DEST is
         now at top of stack.  
         The destination ought to be dead.  However, there is a
         special case with i387 UNSPEC_TAN, where destination is live
         (an argument to fptan) but inherent load of 1.0 is modelled
         as a load from a constant.  

References control_flow_insn_p(), delete_insn(), EMIT_AFTER, emit_pop_insn(), and find_regno_note().

static bool move_nan_for_stack_reg ( rtx  ,
stack_ptr  ,
static bool move_nan_for_stack_reg ( )
   A helper function which replaces INSN with a pattern that loads up
   a NaN into DEST, then invokes move_for_stack_reg.  

References reg_mentioned_p().

static rtx next_flags_user ( rtx  )
static rtx next_flags_user ( )
     Search forward looking for the first use of this value.
     Stop at block boundaries.  
static void pop_stack ( stack_ptr  ,
static void pop_stack ( )
   Pop a register from the stack.  
     If regno was not at the top of stack then adjust stack.  
static void print_stack ( FILE *  ,
static void print_stack ( )
   Print stack configuration.  
static void propagate_stack ( )
   Copy the stack info from the end of edge E's source block to the
   start of E's destination block.  
     Preserve the order of the original stack, but check whether
     any pops are needed.  
     Push in any partially dead values.  
static bool reg_to_stack ( )
   Convert register usage from "flat" register file usage to a "stack
   register file.  FILE is the dump file, if used.

   Construct a CFG and run life analysis.  Then convert each insn one
   by one.  Run a last cleanup_cfg pass, if optimizing, to eliminate
   code duplication created when the converter inserts pop insns on
   the edges.  
     Clean up previous run.  
     See if there is something to do.  Flow analysis is quite
     expensive so we might save some compilation time.  
     Set up block info for each basic block.  
         Set current register status at last instruction `uninitialized'.  
         Copy live_at_end and live_at_start into temporaries.  
     Create the replacement registers up front.  
     A QNaN for initializing uninitialized variables.

     ??? We can't load from constant memory in PIC mode, because
     we're inserting these instructions before the prologue and
     the PIC register hasn't been set up.  In that case, fall back
     on zero, which we can get from `fldz'.  
     Allocate a cache for stack_regs_mentioned.  
static void remove_regno_note ( rtx  ,
enum  reg_note,
unsigned  int 
static void remove_regno_note ( )
   Remove a note of type NOTE, which must be found, for register
   number REGNO from INSN.  Remove only one such note.  

References emit_pop_insn(), and get_hard_regnum().

static void replace_reg ( rtx ,
static void replace_reg ( )
   Replace REG, which is a pointer to a stack reg RTX, with an RTX for
   the desired hard REGNO.  
static unsigned int rest_of_handle_stack_regs ( )
   Convert register usage from flat register file usage to a stack
   register file.  
int stack_regs_mentioned ( )
   Return nonzero if INSN mentions stacked registers, else return zero.  
         Allocate some extra size to avoid too many reallocs, but
         do not grow too quickly.  
         This insn has yet to be examined.  Do so now.  

Referenced by emit_pop_insn().

static int stack_regs_mentioned_p ( const_rtx  pat)
   Forward declarations 

Referenced by stack_regs_mentioned_p().

static int stack_regs_mentioned_p ( )
   Return nonzero if any stack register is mentioned somewhere within PAT.  

References stack_regs_mentioned_p().

static rtx stack_result ( tree  )
static rtx stack_result ( )
   If current function returns its result in an fp stack register,
   return the REG.  Otherwise, return 0.  
     If the value is supposed to be returned in memory, then clearly
     it is not returned in a stack register.  
static void straighten_stack ( )
   Reorganize the stack into ascending numbers, before this insn.  
     If there is only a single register on the stack, then the stack is
     already in increasing order and no reorganization is needed.

     Similarly if the stack is empty.  
static void subst_all_stack_regs_in_debug_insn ( )
   Substitute hardware stack regs in debug insn INSN, using stack
   layout REGSTACK.  If we can't find a hardware stack reg for any of
   the REGs in it, reset the debug insn.  

References emit_insn_before(), get_hard_regnum(), and move_nan_for_stack_reg().

static void subst_asm_stack_regs ( rtx  ,
static void subst_asm_stack_regs ( )
   Substitute hard regnums for any stack regs in INSN, which has
   N_INPUTS inputs and N_OUTPUTS outputs.  REGSTACK is the stack info
   before the insn, and is updated with changes made here.

   There are several requirements and assumptions about the use of
   stack-like regs in asm statements.  These rules are enforced by
   record_asm_stack_regs; see comments there for details.  Any
   asm_operands left in the RTL at this point may be assume to meet the
   requirements, since record_asm_stack_regs removes any problem asm.  
     Find out what the constraints required.  If no constraint
     alternative matches, that is a compiler bug: we should have caught
     such an insn in check_asm_stack_operands.  
     Strip SUBREGs here to make the following code simpler.  
     Set up NOTE_REG, NOTE_LOC and NOTE_KIND.  
     Set up CLOBBER_REG and CLOBBER_LOC.  
     Put the input regs into the desired place in TEMP_STACK.  
           If an operand needs to be in a particular reg in
           FLOAT_REGS, the constraint was either 't' or 'u'.  Since
           these constraints are for single register classes, and
           reload guaranteed that operand[i] is already in that class,
           we can just use REGNO (recog_data.operand[i]) to know which
           actual reg this operand needs to be in.  
               recog_data.operand[i] is not in the right place.  Find
               it and swap it with whatever is already in I's place.
               K is where recog_data.operand[i] is now.  J is where it
               should be.  
     Emit insns before INSN to make sure the reg-stack is in the right
     Make the needed input register substitutions.  Do death notes and
     clobbers too, because these are for inputs, not outputs.  
         It's OK for a CLOBBER to reference a reg that is not live.
         Don't try to replace it in that case.  
             Sigh - clobbers always have QImode.  But replace_reg knows
             that these regs can't be MODE_INT and will assert.  Just put
             the right reg there without calling replace_reg.  
     Now remove from REGSTACK any inputs that the asm implicitly popped.  
           An input reg is implicitly popped if it is tied to an
           output, or if there is a CLOBBER for it.  
               recog_data.operand[i] might not be at the top of stack.
               But that's OK, because all we need to do is pop the
               right number of regs off of the top of the reg-stack.
               record_asm_stack_regs guaranteed that all implicitly
               popped regs were grouped at the top of the reg-stack.  
     Now add to REGSTACK any outputs that the asm implicitly pushed.
     Note that there isn't any need to substitute register numbers.
     ???  Explain why this is true.  
         See if there is an output for this hard reg.  
     Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
     input that the asm didn't implicitly pop.  If the asm didn't
     implicitly pop an input reg, that reg will still be live.

     Note that we can't use find_regno_note here: the register numbers
     in the death notes have already been substituted.  
static bool subst_stack_regs ( rtx  ,
static bool subst_stack_regs ( )
   Substitute stack hard reg numbers for stack virtual registers in
   INSN.  Non-stack register numbers are not changed.  REGSTACK is the
   current stack content.  Insns may be emitted as needed to arrange the
   stack for the 387 based on the contents of the insn.  Return whether
   a control flow insn was deleted in the process.  
         If there are any floating point parameters to be passed in
         registers for this call, make sure they are in the right
             Now mark the arguments as dead after the call.  
     Do the actual substitution if any stack regs are mentioned.
     Since we only record whether entire insn mentions stack regs, and
     subst_stack_regs_pat only works for patterns that contain stack regs,
     we must check each pattern in a parallel here.  A call_value_pop could
     fail otherwise.  
             This insn is an `asm' with operands.  Decode the operands,
             decide how many are inputs, and do register substitution.
             Any REG_UNUSED notes will be handled by subst_asm_stack_regs.  
     subst_stack_regs_pat may have deleted a no-op insn.  If so, any
     REG_UNUSED will already have been dealt with, so just return.  
     If this a noreturn call, we can't insert pop insns after it.
     Instead, reset the stack state to empty.  
     If there is a REG_UNUSED note on a stack register on this insn,
     the indicated reg must be popped.  The REG_UNUSED note is removed,
     since the form of the newly emitted pop insn references the reg,
     making it no longer `unset'.  
static int subst_stack_regs_in_debug_insn ( )
   Substitute new registers in LOC, which is part of a debug insn.
   REGSTACK is the current register layout.  
     If we can't find an active register, reset this debug insn.  

References EMIT_BEFORE, emit_pop_insn(), find_reg_note(), remove_note(), and replace_reg().

static bool subst_stack_regs_pat ( rtx  ,
stack_ptr  ,
static bool subst_stack_regs_pat ( )
   Substitute new registers in PAT, which is part of INSN.  REGSTACK
   is the current register layout.  Return whether a control flow insn
   was deleted in the process.  
         Deaths in USE insns can happen in non optimizing compilation.
         Handle them by popping the dying register.  
             USEs are ignored for liveness information so USEs of dead
             register might happen.  
         Uninitialized USE might happen for functions returning uninitialized
         value.  We will properly initialize the USE on the edge to EXIT_BLOCK,
         so it is safe to ignore the use here. This is consistent with behavior
         of dataflow analyzer that ignores USE too.  (This also imply that
         forcibly initializing the register to NaN here would lead to ICE later,
         since the REG_DEAD notes are not issued.)  
                   The fix_truncdi_1 pattern wants to be able to
                   allocate its own scratch register.  It does this by
                   clobbering an fp reg so that it is assured of an
                   empty reg-stack register.  If the register is live,
                   kill it now.  Remove the DEAD/UNUSED note so we
                   don't try to kill it later too.

                   In reality the UNUSED note can be absent in some
                   complicated cases when the register is reused for
                   partially set variable.  
                   A top-level clobber with no REG_DEAD, and no hard-regnum
                   indicates an uninitialized value.  Because reload removed
                   all other clobbers, this must be due to a function
                   returning without a value.  Load up a NaN.  
           See if this is a `movM' pattern, and handle elsewhere if so.  
               This is a `tstM2' case.  
               Fall through.  
               These insns only operate on the top of the stack. DEST might
               be cc0_rtx if we're processing a tstM pattern. Also, it's
               possible that the tstM case results in a REG_DEAD note on the
               On i386, reversed forms of subM3 and divM3 exist for
               MODE_FLOAT, so the same code that works for addM3 and mulM3
               can be used.  
               These insns can accept the top of stack as a destination
               from a stack reg or mem, or can use the top of stack as a
               source and some other stack register (possibly top of stack)
               as a destination.  
               We will fix any death note later.  
               If either operand is not a stack register, then the dest
               must be top of stack.  
                   Both operands are REG.  If neither operand is already
                   at the top of stack, choose to make the one that is the
                   dest the new top of stack.  
                   If the source is not live, this is yet another case of
                   uninitialized variables.  Load up a NaN instead.  
                   If the register that dies is at the top of stack, then
                   the destination is somewhere else - merely substitute it.
                   But if the reg that dies is not at top of stack, then
                   move the top of stack to the dead reg, as though we had
                   done the insn and then a store-with-pop.  
               Keep operand 1 matching with destination.  
                   These insns only operate on the top of the stack.  
                   This insn only operate on the top of the stack.  
                   Above insns operate on the top of the stack.  
                   Above insns operate on the top two stack slots,
                   first part of one input, double output insn.  
                   Input should never die, it is replaced with output.  
                   These insns operate on the top two stack slots,
                   second part of one input, double output insn.  
                   For UNSPEC_TAN, regstack->top is already increased
                   by inherent load of constant 1.0.  
                   Output value is generated in the second stack slot.
                   Move current value from second slot to the top.  
                   These insns operate on the top two stack slots.  
                   Pop both input operands from the stack.  
                   Push the result back onto the stack.  
                   These insns operate on the top two stack slots,
                   first part of double input, double output insn.  
                   Inputs should never die, they are
                   replaced with outputs.  
                   Push the result back onto stack. Empty stack slot
                   will be filled in second part of insn.  
                   These insns operate on the top two stack slots,
                   second part of double input, double output insn.  
                   Push the result back onto stack. Fill empty slot from
                   first part of insn and fix top of stack pointer.  
                   This insn operates on the top two stack slots,
                   third part of C2 setting double input insn.  
                   (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
                   The combination matches the PPRO fcomi instruction.  
                   Fall through.  
                   Combined fcomp+fnstsw generated for doing well with
                   CSE.  When optimizing this would have been broken
                   up before now.  
               This insn requires the top of stack to be the destination.  
               If the comparison operator is an FP comparison operator,
               it is handled correctly by compare_for_stack_reg () who
               will move the destination to the top of stack. But if the
               comparison operator is not an FP comparison operator, we
               have to handle it here.  
                   In case one of operands is the top of stack and the operands
                   dies, it is safe to make it the destination operand by
                   reversing the direction of cmove and avoid fxch.  
                       Make reg-stack believe that the operands are already
                       swapped on the stack 
                       Reverse condition to compensate the operand swap.
                       i386 do have comparison always reversible.  
                       If the register that dies is not at the top of
                       stack, then move the top of stack to the dead reg.
                       Top of stack should never die, as it is the
               Make dest the top of stack.  Add dest to regstack if
               not present.  
static int swap_rtx_condition ( rtx  )
static int swap_rtx_condition ( )
     We're looking for a single set to cc0 or an HImode temporary.  
     See if this is, or ends in, a fnstsw.  If so, we're not doing anything
     with the cc value right now.  We may be able to search for one
         Search forward looking for the first use of this value.
         Stop at block boundaries.  
         We haven't found it.  
         So we've found the insn using this value.  If it is anything
         other than sahf or the value does not die (meaning we'd have
         to search further), then we must give up.  
         Now we are prepared to handle this as a normal cc0 setter.  
         In case the flags don't die here, recurse to try fix
         following user too.  
static int swap_rtx_condition_1 ( rtx  )
static int swap_rtx_condition_1 ( )
   Swap the condition on a branch, if there is one.  Return true if we
   found a condition to swap.  False if the condition was not used as
static void swap_to_top ( rtx  ,
stack_ptr  ,
rtx  ,
static void swap_to_top ( )
   Emit an insns before INSN to swap virtual register SRC1 with
   the top of stack and virtual register SRC2 with second stack
   slot. REGSTACK is the stack state before the swaps, and
   is updated to reflect the swaps.  A swap insn is represented as a
   PARALLEL of two patterns: each pattern moves one reg to the other.

   If SRC1 and/or SRC2 are already at the right place, no swap insn
   is emitted.  
     Place operand 1 at the top of stack.  
     Place operand 2 next on the stack.  

References control_flow_insn_p(), delete_insn(), EMIT_AFTER, emit_pop_insn(), find_regno_note(), get_hard_regnum(), move_nan_for_stack_reg(), stack_def::reg, stack_def::reg_set, and stack_def::top.

Variable Documentation

bool any_malformed_asm
   Set if we find any malformed asms in a block.  
basic_block current_block
   The block we're currently working on.  
   This is the register file for all register after conversion.  
rtx ix86_flags_rtx
rtx not_a_num
   Used to initialize uninitialized registers.  
int regstack_completed = 0
   Nonzero after end of regstack pass.
   Set to 1 or 0 by reg-stack.c.  
vec<char> stack_regs_mentioned_data

Register to Stack convert for GNU compiler. Copyright (C) 1992-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/.

   This pass converts stack-like registers from the "flat register
   file" model that gcc uses, to a stack convention that the 387 uses.

   * The form of the input:

   On input, the function consists of insn that have had their
   registers fully allocated to a set of "virtual" registers.  Note that
   the word "virtual" is used differently here than elsewhere in gcc: for
   each virtual stack reg, there is a hard reg, but the mapping between
   them is not known until this pass is run.  On output, hard register
   numbers have been substituted, and various pop and exchange insns have
   been emitted.  The hard register numbers and the virtual register
   numbers completely overlap - before this pass, all stack register
   numbers are virtual, and afterward they are all hard.

   The virtual registers can be manipulated normally by gcc, and their
   semantics are the same as for normal registers.  After the hard
   register numbers are substituted, the semantics of an insn containing
   stack-like regs are not the same as for an insn with normal regs: for
   instance, it is not safe to delete an insn that appears to be a no-op
   move.  In general, no insn containing hard regs should be changed
   after this pass is done.

   * The form of the output:

   After this pass, hard register numbers represent the distance from
   the current top of stack to the desired register.  A reference to
   FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
   represents the register just below that, and so forth.  Also, REG_DEAD
   notes indicate whether or not a stack register should be popped.

   A "swap" insn looks like a parallel of two patterns, where each
   pattern is a SET: one sets A to B, the other B to A.

   A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
   and whose SET_DEST is REG or MEM.  Any other SET_DEST, such as PLUS,
   will replace the existing stack top, not push a new value.

   A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
   SET_SRC is REG or MEM.

   The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
   appears ambiguous.  As a special case, the presence of a REG_DEAD note
   for FIRST_STACK_REG differentiates between a load insn and a pop.

   If a REG_DEAD is present, the insn represents a "pop" that discards
   the top of the register stack.  If there is no REG_DEAD note, then the
   insn represents a "dup" or a push of the current top of stack onto the

   * Methodology:

   Existing REG_DEAD and REG_UNUSED notes for stack registers are
   deleted and recreated from scratch.  REG_DEAD is never created for a

   * asm_operands:

   There are several rules on the usage of stack-like regs in
   asm_operands insns.  These rules apply only to the operands that are
   stack-like regs:

   1. Given a set of input regs that die in an asm_operands, it is
      necessary to know which are implicitly popped by the asm, and
      which must be explicitly popped by gcc.

        An input reg that is implicitly popped by the asm must be
        explicitly clobbered, unless it is constrained to match an
        output operand.

   2. For any input reg that is implicitly popped by an asm, it is
      necessary to know how to adjust the stack to compensate for the pop.
      If any non-popped input is closer to the top of the reg-stack than
      the implicitly popped reg, it would not be possible to know what the
      stack looked like - it's not clear how the rest of the stack "slides

        All implicitly popped input regs must be closer to the top of
        the reg-stack than any input that is not implicitly popped.

   3. It is possible that if an input dies in an insn, reload might
      use the input reg for an output reload.  Consider this example:

                asm ("foo" : "=t" (a) : "f" (b));

      This asm says that input B is not popped by the asm, and that
      the asm pushes a result onto the reg-stack, i.e., the stack is one
      deeper after the asm than it was before.  But, it is possible that
      reload will think that it can use the same reg for both the input and
      the output, if input B dies in this insn.

        If any input operand uses the "f" constraint, all output reg
        constraints must use the "&" earlyclobber.

      The asm above would be written as

                asm ("foo" : "=&t" (a) : "f" (b));

   4. Some operands need to be in particular places on the stack.  All
      output operands fall in this category - there is no other way to
      know which regs the outputs appear in unless the user indicates
      this in the constraints.

        Output operands must specifically indicate which reg an output
        appears in after an asm.  "=f" is not allowed: the operand
        constraints must select a class with a single reg.

   5. Output operands may not be "inserted" between existing stack regs.
      Since no 387 opcode uses a read/write operand, all output operands
      are dead before the asm_operands, and are pushed by the asm_operands.
      It makes no sense to push anywhere but the top of the reg-stack.

        Output operands must start at the top of the reg-stack: output
        operands may not "skip" a reg.

   6. Some asm statements may need extra stack space for internal
      calculations.  This can be guaranteed by clobbering stack registers
      unrelated to the inputs and outputs.

   Here are a couple of reasonable asms to want to write.  This asm
   takes one input, which is internally popped, and produces two outputs.

        asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));

   This asm takes two inputs, which are popped by the fyl2xp1 opcode,
   and replaces them with one output.  The user must code the "st(1)"
   clobber for reg-stack.c to know that fyl2xp1 pops both inputs.

        asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
   We use this array to cache info about insns, because otherwise we
   spend too much time in stack_regs_mentioned_p.

   Indexed by insn UIDs.  A value of zero is uninitialized, one indicates
   the insn uses stack registers, two indicates the insn does not use
   stack registers.  
bool starting_stack_p
   In the current_block, whether we're processing the first register
   stack or call instruction, i.e. the regstack is currently the
   same as BLOCK_INFO(current_block)->stack_in.