Use module mechanism to register copy minimization algorithms.
[libfirm] / ir / be / becopyopt.h
index 5dd61ec..ba94d50 100644 (file)
-/**
- * Author:      Daniel Grund
- * Date:               12.04.2005
- * Copyright:   (c) Universitaet Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
  *
- * Header for copy optimization problem. Analysis and set up of the problem.
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
-#ifndef _BECOPYOPT_H
-#define _BECOPYOPT_H
-
-#include "debug.h"
-#include "obst.h"
-#include "list.h"
-#include "set.h"
-#include "pset.h"
-#include "bitset.h"
+/**
+ * @file
+ * @brief       Copy minimization driver.
+ * @author      Daniel Grund
+ * @date        11.04.2005
+ * @version     $Id$
+ *
+ * Main file for the optimization reducing the copies needed for:
+ * - Phi coalescing
+ * - Register-constrained nodes
+ * - Two-address code instructions
+ */
+#ifndef FIRM_BE_BECOPYOPT_H
+#define FIRM_BE_BECOPYOPT_H
 
-#include "irgraph.h"
-#include "irgwalk.h"
-#include "irnode.h"
-#include "irdom.h"
-#include "irouts.h"
+#include <stdio.h>
 
+#include "firm_types.h"
+#include "bechordal.h"
 #include "beutil.h"
-#include "benumb_t.h"
-#include "belive_t.h"
-#include "bechordal_t.h"
-#include "bearch.h"
-
-#define DEBUG_IRG "NONE spec.c__main__datab"
-#define DEBUG_LVL_CO   LEVEL_1
-#define DEBUG_LVL_HEUR LEVEL_1
-#define DEBUG_LVL_ILP  LEVEL_1
-
-typedef int(*cost_fct_t)(ir_node*, ir_node*, int);
 
 /**
- * Data representing the problem of copy minimization.
+ * Flags for dumping the IFG.
  */
-typedef struct _copy_opt_t {
-       be_chordal_env_t *chordal_env;
-       char *name;                                             /**< ProgName__IrgName__RegClass */
-       struct list_head units;                 /**< all units to optimize in right order */
-       pset *roots;                                    /**< used only temporary for detecting multiple appends */
-       cost_fct_t get_costs;                   /**< function ptr used to get costs for copies */
-       struct obstack ob;
-} copy_opt_t;
+enum {
+       CO_IFG_DUMP_COLORS = 1, /**< Dump the graph colored. */
+       CO_IFG_DUMP_LABELS = 2, /**< Dump node/edge labels. */
+       CO_IFG_DUMP_SHAPE  = 4, /**< Give constrained nodes special shapes. */
+       CO_IFG_DUMP_CONSTR = 8  /**< Dump the node constraints in the label. */
+};
 
 /**
- * A single unit of optimization. Lots of these form a copy-opt problem
+ * Algorithms.
  */
-typedef struct _unit_t {
-       struct list_head units;         /**< chain for all units */
-       copy_opt_t *co;                         /**< the copy_opt this unit belongs to */
-       int node_count;                         /**< size of the nodes array */
-       ir_node **nodes;                        /**< [0] is the root-node, others are non interfering args of it. */
-       int *costs;                                     /**< costs[i] are arising, if nodes[i] has a different color */
-       int complete_costs;                     /**< sum of all costs[i] */
-       int minimal_costs;                      /**< a lower bound for this ou, considering only ifg (not coloring conflicts) */
+enum {
+       CO_ALGO_NONE,
+       CO_ALGO_HEUR,
+       CO_ALGO_HEUR2,
+       CO_ALGO_HEUR4,
+       CO_ALGO_ILP,
+#ifdef FIRM_KAPS
+       CO_ALGO_PBQP,
+#endif
+       CO_ALGO_LAST
+};
+
+typedef struct _copy_opt_t copy_opt_t;
 
-       int sort_key;                           /**< maximum costs. controls the order of ou's. */
+typedef int(*cost_fct_t)(const copy_opt_t *, ir_node *, ir_node *, int);
 
-       /* for heuristic */
-       struct list_head queue;         /**< list of (mis/color) sorted by size of mis */
-} unit_t;
+typedef struct {
+       int (*copyopt)(copy_opt_t *co); /**< function ptr to run copyopt */
+       int        can_improve_existing;
+} co_algo_info;
 
-/* Helpers */
-#define set_irn_col(co, irn, col) \
-       arch_set_irn_register(co->chordal_env->arch_env, irn, 0, arch_register_for_index(co->chordal_env->cls, col))
 
-#define get_irn_col(co, irn) \
-       arch_register_get_index(arch_get_irn_register(co->chordal_env->arch_env, irn, 0))
+/**
+ * Register a new copy optimazation algorithm.
+ *
+ * @param name     the name of the copy optimazation algorithm,
+ *                 used to select it
+ * @param spiller  a copy optimazation entry
+ */
+void be_register_copyopt(const char *name, co_algo_info *copyopt);
 
-#define list_entry_units(lh) list_entry(lh, unit_t, units)
+/** The driver for copy minimization. */
+void co_driver(be_chordal_env_t *cenv);
 
+/** A coalescing algorithm. */
+typedef int (co_algo_t)(copy_opt_t *);
 
 /**
- * Generate the problem. Collect all infos and optimizable nodes.
+ * Generate the problem. Collect all information and optimizable nodes.
  */
-copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int));
+copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, cost_fct_t get_costs);
 
 /**
- * Free the space...
+ * Free the space used...
  */
 void free_copy_opt(copy_opt_t *co);
 
+/**
+ * Computes the costs of a copy according to loop depth
+ * @param co   The copy opt object.
+ * @param pos  the argument position of arg in the root arguments
+ * @return Must be >= 0 in all cases.
+ */
+int co_get_costs_loop_depth(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos);
 
-#define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
+/**
+ * Computes the costs of a copy according to execution frequency
+ * @param co   The copy opt object.
+ * @param pos  the argument position of arg in the root arguments
+ * @return Must be >= 0 in all cases.
+ */
+int co_get_costs_exec_freq(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos);
 
 /**
- * A copy is a proj haning out of perm node
+ * All costs equal 1. Using this will reduce the _number_ of copies.
+ * @param co   The copy opt object.
+ * @return Must be >= 0 in all cases.
  */
-#define is_Copy(arch_env, irn) (is_Proj(irn) && is_Perm(arch_env, get_Proj_pred(irn)))
+int co_get_costs_all_one(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos);
 
 /**
- * returns the corresponding argument of the perm node for a copy
+ * Statistics over a copy optimization module.
  */
-#define get_Copy_src(irn) (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
+typedef struct {
+       ulong64 aff_edges;            /**< number of affinity edges. */
+       ulong64 aff_nodes;            /**< number of nodes with incident affinity edges. */
+       ulong64 aff_int;              /**< number of affinity edges whose nodes also interfere. */
+       ulong64 inevit_costs;         /**< costs which cannot be evited (due to interfering affinities). */
+       ulong64 max_costs;            /**< all costs of the affinities. */
+       ulong64 costs;                /**< The costs of the current coloring. */
+       ulong64 unsatisfied_edges;    /**< The number of unequally colored affinity edges. */
+} co_complete_stats_t;
 
 /**
- * Checks if a node is optimizable, viz. is a target of a 'copy-op'
+ * Collect statistics of a copy optimization module.
+ * @param co    The copy optimization environment.
+ * @param stat  Where to put the stats.
+ * @note  This requires the graph info to be computed.
  */
-#define is_optimizable(arch_env, irn) (is_Phi(irn) || is_Copy(arch_env, irn))
+void co_complete_stats(const copy_opt_t *co, co_complete_stats_t *stat);
+
 
 /**
- * Checks if the irn is a non-interfering argument of a node which 'is_optimizable'
+ * Build internal optimization units structure
  */
-int is_optimizable_arg(const copy_opt_t *co, ir_node *irn);
+void co_build_ou_structure(copy_opt_t *co);
 
 /**
- * Computes the costs of a copy according to loop depth
- * @param root, arg: clear.
- * @param pos: -1 for perm-copies.
- *                             Else the argument position of arg in the phi node root.
- * @return Must be >= 0 in all cases.
+ * Frees the space used by the opt unit representation.
+ * Does NOT free the whole copyopt structure
  */
-int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos);
+void co_free_ou_structure(copy_opt_t *co);
 
 /**
- * All costs equal 1. Using this will reduce the number of copies.
- * @return Must be >= 0 in all cases.
+ * Solves the problem using a heuristic approach
+ * Uses the OU data structure
+ */
+int co_solve_heuristic(copy_opt_t *co);
+
+/**
+ * Apply Park/Moon coalescing to the graph.
+ * @param co The copy optimization data structure.
+ */
+void co_solve_park_moon(copy_opt_t *co);
+
+/**
+ * Solves the copy minimization problem using another heuristic approach.
+ * Uses the OU and the GRAPH data structure.
  */
-int get_costs_all_one(ir_node *root, ir_node* arg, int pos);
+int co_solve_heuristic_new(copy_opt_t *co);
 
 /**
- * Returns the current costs the copies are causing
+ * Returns the maximal costs possible, i.e. the costs if all
+ * pairs would be assigned different registers.
+ * Uses the OU data structure
+ */
+int co_get_max_copy_costs(const copy_opt_t *co);
+
+/**
+ * Returns the inevitable costs, i.e. the costs of
+ * all copy pairs which interfere.
+ * Uses the OU data structure
+ */
+int co_get_inevit_copy_costs(const copy_opt_t *co);
+
+/**
+ * Returns the current costs the copies are causing.
+ * The result includes inevitable costs and the costs
+ * of the copies regarding the current register allocation
+ * Uses the OU data structure
  */
 int co_get_copy_costs(const copy_opt_t *co);
 
 /**
- * Returns a lower bound for the costs of copies based on interfering
- * arguments and the size of a max indep. set (only ifg-edges) of the other args.
+ * Returns a lower bound for the costs of copies in this ou.
+ * The result includes inevitable costs and the costs of a
+ * minimal costs caused by the nodes of the ou.
+ * Uses the OU data structure
  */
 int co_get_lower_bound(const copy_opt_t *co);
 
 /**
- * Solves the problem using a heuristic approach
+ * Dump the interference graph according to the Appel/George coalescing contest file format.
+ * See: http://www.cs.princeton.edu/~appel/coalesce/format.html
+ * @note Requires graph structure.
+ * @param co The copy opt object.
+ * @param f  A file to dump to.
+ */
+void co_dump_appel_graph(const copy_opt_t *co, FILE *f);
+
+/**
+ * Dumps the IFG of the program splitting after each instruction in the Appel format.
+ * @param co The copy opt object.
+ * @param f  The file to dump to.
+ */
+void co_dump_appel_graph_cliques(const copy_opt_t *co, FILE *f);
+/**
+ * Dump the interference graph with the affinity edges and the coloring.
+ * @param co    The copy opt structure.
+ * @param f     The file to dump to.
+ * @param flags The dump flags (see enum above).
+ */
+void co_dump_ifg_dot(const copy_opt_t *co, FILE *f, unsigned flags);
+
+/**
+ * Constructs another internal representation of the affinity edges
  */
-void co_heur_opt(copy_opt_t *co);
+void co_build_graph_structure(copy_opt_t *co);
+
+/**
+ * Frees the space used by the graph representation.
+ * Does NOT free the whole copyopt structure
+ */
+void co_free_graph_structure(copy_opt_t *co);
 
 /**
  * Solves the problem using mixed integer programming
+ * @returns 1 iff solution state was optimal
+ * NYI
  */
-void co_ilp_opt(copy_opt_t *co);
+int co_solve_ilp1(copy_opt_t *co, double time_limit);
 
-#endif
+/**
+ * Solves the problem using mixed integer programming
+ * @returns 1 iff solution state was optimal
+ * Uses the OU and the GRAPH data structure
+ * Dependency of the OU structure can be removed
+ */
+int co_solve_ilp2(copy_opt_t *co);
+
+/**
+ * Checks if a node is optimizable, viz has something to do with coalescing.
+ * Uses the GRAPH data structure
+ */
+int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn);
+
+#endif /* FIRM_BE_BECOPYOPT_H */