/* Form lists of pseudo register references for autoinc optimization
   for GNU compiler.  This is part of flow optimization.
   Copyright (C) 1999-2025 Free Software Foundation, Inc.
   Originally contributed by Michael P. Hayes
             (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
   Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
             and Kenneth Zadeck (zadeck@naturalbridge.com).

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/>.  */

#ifndef GCC_DF_H
#define GCC_DF_H

#include "regset.h"
#include "alloc-pool.h"
#include "timevar.h"

struct dataflow;
class df_d;
struct df_problem;
struct df_link;
struct df_insn_info;
union df_ref_d;

/* Data flow problems.  All problems must have a unique id here.  */

/* Scanning is not really a dataflow problem, but it is useful to have
   the basic block functions in the vector so that things get done in
   a uniform manner.  The last four problems can be added or deleted
   at any time are always defined (though LIVE is always there at -O2
   or higher); the others are always there.  */
enum df_problem_id
  {
    DF_SCAN,
    DF_LR,                /* Live Registers backward. */
    DF_LR_DCE,            /* Dead code elimination post-pass for LR.  */
    DF_LIVE,              /* Live Registers & Uninitialized Registers */
    DF_RD,                /* Reaching Defs. */
    DF_CHAIN,             /* Def-Use and/or Use-Def Chains. */
    DF_WORD_LR,           /* Subreg tracking lr.  */
    DF_NOTE,              /* REG_DEAD and REG_UNUSED notes.  */
    DF_MD,                /* Multiple Definitions. */
    DF_MIR,               /* Must-initialized Registers.  */

    DF_LAST_PROBLEM_PLUS1
  };

/* Dataflow direction.  */
enum df_flow_dir
  {
    DF_NONE,
    DF_FORWARD,
    DF_BACKWARD
  };

/* Descriminator for the various df_ref types.  */
enum df_ref_class {DF_REF_BASE, DF_REF_ARTIFICIAL, DF_REF_REGULAR};

/* The first of these us a set of a registers.  The remaining three
   are all uses of a register (the mem_load and mem_store relate to
   how the register as an addressing operand).  */
enum df_ref_type {DF_REF_REG_DEF, DF_REF_REG_USE,
		  DF_REF_REG_MEM_LOAD, DF_REF_REG_MEM_STORE};

enum df_ref_flags
  {
    /* This flag is set if this ref occurs inside of a conditional
       execution instruction.  */
    DF_REF_CONDITIONAL = 1 << 0,

    /* If this flag is set for an artificial use or def, that ref
       logically happens at the top of the block.  If it is not set
       for an artificial use or def, that ref logically happens at the
       bottom of the block.  This is never set for regular refs.  */
    DF_REF_AT_TOP = 1 << 1,

    /* This flag is set if the use is inside a REG_EQUAL or REG_EQUIV
       note.  */
    DF_REF_IN_NOTE = 1 << 2,

    /* This bit is true if this ref can make regs_ever_live true for
       this regno.  */
    DF_HARD_REG_LIVE = 1 << 3,


    /* This flag is set if this ref is a partial use or def of the
       associated register.  */
    DF_REF_PARTIAL = 1 << 4,

    /* Read-modify-write refs generate both a use and a def and
       these are marked with this flag to show that they are not
       independent.  */
    DF_REF_READ_WRITE = 1 << 5,

    /* This flag is set if this ref, generally a def, may clobber the
       referenced register.  This is generally only set for hard
       registers that cross a call site.  With better information
       about calls, some of these could be changed in the future to
       DF_REF_MUST_CLOBBER.  */
    DF_REF_MAY_CLOBBER = 1 << 6,

    /* This flag is set if this ref, generally a def, is a real
       clobber. This is not currently set for registers live across a
       call because that clobbering may or may not happen.

       Most of the uses of this are with sets that have a
       GET_CODE(..)==CLOBBER.  Note that this is set even if the
       clobber is to a subreg.  So in order to tell if the clobber
       wipes out the entire register, it is necessary to also check
       the DF_REF_PARTIAL flag.  */
    DF_REF_MUST_CLOBBER = 1 << 7,


    /* If the ref has one of the following two flags set, then the
       struct df_ref can be cast to struct df_ref_extract to access
       the width and offset fields.  */

    /* This flag is set if the ref contains a SIGN_EXTRACT.  */
    DF_REF_SIGN_EXTRACT = 1 << 8,

    /* This flag is set if the ref contains a ZERO_EXTRACT.  */
    DF_REF_ZERO_EXTRACT = 1 << 9,

    /* This flag is set if the ref contains a STRICT_LOW_PART.  */
    DF_REF_STRICT_LOW_PART = 1 << 10,

    /* This flag is set if the ref contains a SUBREG.  */
    DF_REF_SUBREG = 1 << 11,


    /* This bit is true if this ref is part of a multiword hardreg.  */
    DF_REF_MW_HARDREG = 1 << 12,

    /* This flag is set if this ref is a usage of the stack pointer by
       a function call.  */
    DF_REF_CALL_STACK_USAGE = 1 << 13,

    /* This flag is used for verification of existing refs. */
    DF_REF_REG_MARKER = 1 << 14,

    /* This flag is set if this ref is inside a pre/post modify.  */
    DF_REF_PRE_POST_MODIFY = 1 << 15

  };

/* The possible ordering of refs within the df_ref_info.  */
enum df_ref_order
  {
    /* There is not table.  */
    DF_REF_ORDER_NO_TABLE,

    /* There is a table of refs but it is not (or no longer) organized
       by one of the following methods.  */
    DF_REF_ORDER_UNORDERED,
    DF_REF_ORDER_UNORDERED_WITH_NOTES,

    /* Organize the table by reg order, all of the refs with regno 0
       followed by all of the refs with regno 1 ... .  Within all of
       the regs for a particular regno, the refs are unordered.  */
    DF_REF_ORDER_BY_REG,

    /* For uses, the refs within eq notes may be added for
       DF_REF_ORDER_BY_REG.  */
    DF_REF_ORDER_BY_REG_WITH_NOTES,

    /* Organize the refs in insn order.  The insns are ordered within a
       block, and the blocks are ordered by FOR_ALL_BB_FN.  */
    DF_REF_ORDER_BY_INSN,

    /* For uses, the refs within eq notes may be added for
       DF_REF_ORDER_BY_INSN.  */
    DF_REF_ORDER_BY_INSN_WITH_NOTES
  };

/* Function prototypes added to df_problem instance.  */

/* Allocate the problem specific data.  */
typedef void (*df_alloc_function) (bitmap);

/* This function is called if the problem has global data that needs
   to be cleared when ever the set of blocks changes.  The bitmap
   contains the set of blocks that may require special attention.
   This call is only made if some of the blocks are going to change.
   If everything is to be deleted, the wholesale deletion mechanisms
   apply. */
typedef void (*df_reset_function) (bitmap);

/* Free the basic block info.  Called from the block reordering code
   to get rid of the blocks that have been squished down.   */
typedef void (*df_free_bb_function) (basic_block, void *);

/* Local compute function.  */
typedef void (*df_local_compute_function) (bitmap);

/* Init the solution specific data.  */
typedef void (*df_init_function) (bitmap);

/* Iterative dataflow function.  */
typedef void (*df_dataflow_function) (struct dataflow *, bitmap, int *, int);

/* Confluence operator for blocks with 0 out (or in) edges.  */
typedef void (*df_confluence_function_0) (basic_block);

/* Confluence operator for blocks with 1 or more out (or in) edges.
   Return true if BB input data has changed.  */
typedef bool (*df_confluence_function_n) (edge);

/* Transfer function for blocks.
   Return true if BB output data has changed.  */
typedef bool (*df_transfer_function) (int);

/* Function to massage the information after the problem solving.  */
typedef void (*df_finalizer_function) (bitmap);

/* Function to free all of the problem specific datastructures.  */
typedef void (*df_free_function) (void);

/* Function to remove this problem from the stack of dataflow problems
   without effecting the other problems in the stack except for those
   that depend on this problem.  */
typedef void (*df_remove_problem_function) (void);

/* Function to dump basic block independent results to FILE.  */
typedef void (*df_dump_problem_function) (FILE *);

/* Function to dump top or bottom of basic block results to FILE.  */
typedef void (*df_dump_bb_problem_function) (basic_block, FILE *);

/* Function to dump before or after an insn to FILE.  */
typedef void (*df_dump_insn_problem_function) (const rtx_insn *, FILE *);

/* Function to dump top or bottom of basic block results to FILE.  */
typedef void (*df_verify_solution_start) (void);

/* Function to dump top or bottom of basic block results to FILE.  */
typedef void (*df_verify_solution_end) (void);

/* The static description of a dataflow problem to solve.  See above
   typedefs for doc for the function fields.  */

struct df_problem {
  /* The unique id of the problem.  This is used it index into
     df->defined_problems to make accessing the problem data easy.  */
  enum df_problem_id id;
  enum df_flow_dir dir;			/* Dataflow direction.  */
  df_alloc_function alloc_fun;
  df_reset_function reset_fun;
  df_free_bb_function free_bb_fun;
  df_local_compute_function local_compute_fun;
  df_init_function init_fun;
  df_dataflow_function dataflow_fun;
  df_confluence_function_0 con_fun_0;
  df_confluence_function_n con_fun_n;
  df_transfer_function trans_fun;
  df_finalizer_function finalize_fun;
  df_free_function free_fun;
  df_remove_problem_function remove_problem_fun;
  df_dump_problem_function dump_start_fun;
  df_dump_bb_problem_function dump_top_fun;
  df_dump_bb_problem_function dump_bottom_fun;
  df_dump_insn_problem_function dump_insn_top_fun;
  df_dump_insn_problem_function dump_insn_bottom_fun;
  df_verify_solution_start verify_start_fun;
  df_verify_solution_end verify_end_fun;
  const struct df_problem *dependent_problem;
  unsigned int block_info_elt_size;

  /* The timevar id associated with this pass.  */
  timevar_id_t tv_id;

  /* True if the df_set_blocks should null out the basic block info if
     this block drops out of df->blocks_to_analyze.  */
  bool free_blocks_on_set_blocks;
};


/* The specific instance of the problem to solve.  */
struct dataflow
{
  const struct df_problem *problem;     /* The problem to be solved.  */

  /* Array indexed by bb->index, that contains basic block problem and
     solution specific information.  */
  void *block_info;
  unsigned int block_info_size;

  /* The pool to allocate the block_info from. */
  object_allocator<df_link> *block_pool;

  /* The lr and live problems have their transfer functions recomputed
     only if necessary.  This is possible for them because, the
     problems are kept active for the entire backend and their
     transfer functions are indexed by the REGNO.  These are not
     defined for any other problem.  */
  bitmap out_of_date_transfer_functions;

  /* Other problem specific data that is not on a per basic block
     basis.  The structure is generally defined privately for the
     problem.  The exception being the scanning problem where it is
     fully public.  */
  void *problem_data;

  /* Local flags for some of the problems. */
  unsigned int local_flags;

  /* True if this problem of this instance has been initialized.  This
     is used by the dumpers to keep garbage out of the dumps if, for
     debugging a dump is produced before the first call to
     df_analyze after a new problem is added.  */
  bool computed;

  /* True if the something has changed which invalidates the dataflow
     solutions.  Note that this bit is always true for all problems except
     lr and live.  */
  bool solutions_dirty;

  /* If true, this pass is deleted by df_finish_pass.  This is never
     true for DF_SCAN and DF_LR.  It is true for DF_LIVE if optimize >
     1.  It is always true for the other problems.  */
  bool optional_p;
};


/* The set of multiword hardregs used as operands to this
   instruction. These are factored into individual uses and defs but
   the aggregate is still needed to service the REG_DEAD and
   REG_UNUSED notes.  */
struct df_mw_hardreg
{
  df_mw_hardreg *next;		/* Next entry for this instruction.  */
  rtx mw_reg;                   /* The multiword hardreg.  */
  /* These two bitfields are intentionally oversized, in the hope that
     accesses to 16-bit fields will usually be quicker.  */
  ENUM_BITFIELD(df_ref_type) type : 16;
				/* Used to see if the ref is read or write.  */
  int flags : 16;		/* Various df_ref_flags.  */
  unsigned int start_regno;     /* First word of the multi word subreg.  */
  unsigned int end_regno;       /* Last word of the multi word subreg.  */
  unsigned int mw_order;        /* Same as df_ref.ref_order.  */
};


/* Define a register reference structure.  One of these is allocated
    for every register reference (use or def).  Note some register
    references (e.g., post_inc, subreg) generate both a def and a use.  */
struct df_base_ref
{
  /* These three bitfields are intentionally oversized, in the hope that
     accesses to 8 and 16-bit fields will usually be quicker.  */
  ENUM_BITFIELD(df_ref_class) cl : 8;

  ENUM_BITFIELD(df_ref_type) type : 8;
				/* Type of ref.  */
  int flags : 16;		/* Various df_ref_flags.  */
  unsigned int regno;		/* The register number referenced.  */
  rtx reg;			/* The register referenced.  */
  union df_ref_d *next_loc;	/* Next ref for same insn or bb.  */
  struct df_link *chain;	/* Head of def-use, use-def.  */
  /* Pointer to the insn info of the containing instruction.  FIXME!
     Currently this is NULL for artificial refs but this will be used
     when FUDs are added.  */
  struct df_insn_info *insn_info;
  /* For each regno, there are three chains of refs, one for the uses,
     the eq_uses and the defs.  These chains go through the refs
     themselves rather than using an external structure.  */
  union df_ref_d *next_reg;     /* Next ref with same regno and type.  */
  union df_ref_d *prev_reg;     /* Prev ref with same regno and type.  */
  /* Location in the ref table.  This is only valid after a call to
     df_maybe_reorganize_[use,def]_refs which is an expensive operation.  */
  int id;
  /* The index at which the operand was scanned in the insn.  This is
     used to totally order the refs in an insn.  */
  unsigned int ref_order;
};


/* The three types of df_refs.  Note that the df_ref_extract is an
   extension of the df_regular_ref, not the df_base_ref.  */
struct df_artificial_ref
{
  struct df_base_ref base;

  /* Artificial refs do not have an insn, so to get the basic block,
     it must be explicitly here.  */
  basic_block bb;
};


struct df_regular_ref
{
  struct df_base_ref base;
  /* The loc is the address in the insn of the reg.  This is not
     defined for special registers, such as clobbers and stack
     pointers that are also associated with call insns and so those
     just use the base.  */
  rtx *loc;
};

/* Union of the different kinds of defs/uses placeholders.  */
union df_ref_d
{
  struct df_base_ref base;
  struct df_regular_ref regular_ref;
  struct df_artificial_ref artificial_ref;
};
typedef union df_ref_d *df_ref;


/* One of these structures is allocated for every insn.  */
struct df_insn_info
{
  rtx_insn *insn;	        /* The insn this info comes from.  */
  df_ref defs;	                /* Head of insn-def chain.  */
  df_ref uses;	                /* Head of insn-use chain.  */
  /* Head of insn-use chain for uses in REG_EQUAL/EQUIV notes.  */
  df_ref eq_uses;
  struct df_mw_hardreg *mw_hardregs;
  /* The logical uid of the insn in the basic block.  This is valid
     after any call to df_analyze but may rot after insns are added,
     deleted or moved. */
  int luid;
};

/* These links are used for ref-ref chains.  Currently only DEF-USE and
   USE-DEF chains can be built by DF.  */
struct df_link
{
  df_ref ref;
  struct df_link *next;
};

