list_sched(irg, trivial_selector, NULL);
be_liveness(irg);
be_ra_chordal(irg);
+ be_phi_congr_classes(irg);
+
dump_allocated_irg(irg);
void be_main(int argc, const char *argv[])
{
be_main_loop();
- /* next line only for statistics */
- /* do_phi_statistics(); */
+ do_phi_statistics();
}
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include "config.h"
#include "irgraph.h"
#include "irnode.h"
#include "irgwalk.h"
#include "irop.h"
#include "irprog.h"
+#include "irprintf_t.h"
+#include "debug.h"
+#include "pset.h"
+
#include "bephicongr_t.h"
+#include "beutil.h"
+#include "bechordal.h"
size_t phi_irn_data_offset = 0;
+firm_dbg_module_t *dbgmod = NULL;
void be_phi_congr_class_init(void) {
+ dbgmod = firm_dbg_register("Phi congr. classes");
+ firm_dbg_set_mask(dbgmod, 1);
phi_irn_data_offset = register_additional_node_data(sizeof(phi_info_t));
}
+#define get_phi(irn) get_irn_phi_info(irn)->phi
+#define set_phi(irn, p) get_irn_phi_info(irn)->phi = p
+#define set_phi_class(irn, cls) get_irn_phi_info(irn)->phi_class = cls
-#define set_phi_class(n,new_tgt) get_irn_phi_info(n)->phi_congr_class=new_tgt
-#define set_phi_class_size(n,v) get_irn_phi_info(n)->phi_class_size=v
-#define inc_phi_class_size(n) get_irn_phi_info(n)->phi_class_size++
-
-static void correct_phi_class(ir_node *n, ir_node *new_tgt){
- int i, max, check;
- check = get_phi_class_size(n) + get_phi_class_size(new_tgt);
+#define is_Const(n) (get_irn_opcode(n) == iro_Const)
- for (i = 0, max = get_irn_arity(n); i < max; i++) {
- ir_node *arg = get_irn_n(n, i);
- set_phi_class(arg, new_tgt);
- inc_phi_class_size(new_tgt);
- }
+/**
+ * Insert a node to the phi class of a phi node
+ * @param class The phi congruence class
+ * @param phi The phi node holding the class
+ * @param arg Node which gets assigned to the class
+ */
+static void inline phi_class_insert(pset *class, ir_node *phi, ir_node *arg) {
+ /*DBG((dbgmod, 1, "\tinsert %n in %n\n", arg, phi));*/
+ ir_debugf("\tinsert %n in %n\n", arg, phi);
+ if (!(is_Const(arg) && CONSTS_SPLIT_PHI_CLASSES))
+ set_phi(arg, phi);
+ pset_insert_ptr(class, arg);
+}
- get_irn_phi_info(n)->phi_class_size=0;
- //set_phi_class_size(n, 0);
- assert((check == get_phi_class_size(n) + get_phi_class_size(new_tgt)) && "Re-setting pointers went wrong.");
+/**
+ * Assigns all operands of a phi node
+ * to a (new) phi congruence class
+ * @param n Phi node, of which all args get reassigned
+ * @param new_tgt Phi node representing new phi congruence class
+ */
+static void phi_class_correct(ir_node *n, ir_node *new_tgt) {
+ ir_node *p;
+ pset *src, *tgt;
+
+ assert(is_Phi(n) && is_Phi(new_tgt) && "These must be phi nodes.");
+ ir_debugf("\tcorrect %n\n", n);
+
+ /* copy all class members from n to new_tgt. Duplicates eliminated by pset */
+ src = get_phi_class(n);
+ tgt = get_phi_class(new_tgt);
+ for (p = (ir_node *)pset_first(src); p; p = (ir_node *)pset_next(src))
+ phi_class_insert(tgt, new_tgt, p);
+
+ /* phi class of n is no longer needed */
+ set_phi_class(n, NULL);
+ del_pset(src);
}
-void det_phi_congr_class(ir_node *curr_phi) {
+/**
+ * Determines the phi congruence class of a phi node.
+ * This will assign a phi class to all operands of this
+ * phi node. Exceptions may be const nodes: See CONSTS_SPLIT_PHI_CLASSES
+ */
+static void det_phi_congr_class(ir_node *curr_phi) {
+ pset *pc;
int i, n;
assert(is_Phi(curr_phi) && "This must be a phi node.");
+ ir_debugf("Det. phi class of %n.\n", curr_phi);
- set_phi_class(curr_phi, curr_phi);
+ pc = get_phi_class(curr_phi);
+ if (!pc) {
+ pc = pset_new_ptr(2);
+ set_phi_class(curr_phi, pc);\
+ phi_class_insert(pc, curr_phi, curr_phi);
+ }
for (i = 0, n = get_irn_arity(curr_phi); i < n; i++) {
- ir_node *arg, *pc;
+ ir_node *arg, *phi;
arg = get_irn_n(curr_phi, i);
- pc = get_phi_class(arg);
- if (!pc)
- set_phi_class(arg, curr_phi);
- else
- correct_phi_class(pc, curr_phi);
+ ir_debugf(" Arg %n\n", arg);
+
+ phi = get_phi(arg);
+ if (phi == NULL) { /* Argument is not assigned to another phi class. */
+ phi_class_insert(pc, curr_phi, arg);
+ } else if (phi != curr_phi) {
+ assert(!is_Const(arg) && "Const nodes must not have a phi class assigned");
+ phi_class_correct(phi, curr_phi);
+ }
}
+ ir_debugf("Size now: %d\n", get_phi_class_size(curr_phi));
+}
+
+/**
+ * Determines the liveness interference information
+ * of a phi congruence class.
+ */
+static void det_interference(ir_node *n) {
+ pset *pc;
+ ir_node **members, *p;
+ int count, i, o;
+ int doit;
+
+ pc = get_phi_class(n);
+ if (!pc)
+ return;
+
+ count = pset_count(pc);
+ members = (ir_node **) malloc(count * sizeof(ir_node*));
+
+ ir_debugf("\nChecking phi class of node %n:\n", n);
+ for (i=0, p = (ir_node *)pset_first(pc); p; p = (ir_node *)pset_next(pc))
+ members[i++] = p;
+ assert(i == count);
+
+ //determine interference of phi args
+ for (i = 0; i < count-1; ++i) {
+ doit = 1;
+ for (o = i+1; o < count; ++o) {
+ ir_debugf("Checking %n\t%n", members[i], members[o]);
+ if (phi_ops_interfere(members[i], members[o])) {
+ ir_debugf("\tinterfere\n");
+ get_irn_phi_info(n)->interf_pairs++;
+ if (doit) {
+ get_irn_phi_info(n)->interf_vals++;
+ doit = 0;
+ }
+
+ } else {
+ ir_debugf("\tclean\n");
+ }
+ }
+ }
+
+ free(members);
+}
+
+
+static void phi_class_walker(ir_node *node, void *env) {
+ if (!(is_Phi(node) && mode_is_datab(get_irn_mode(node)))) return;
+ det_phi_congr_class(node);
+}
+
+
+static void phi_class_inteference_walker(ir_node *node, void *env) {
+ if (!(is_Phi(node) && mode_is_datab(get_irn_mode(node)))) return;
+ det_interference(node);
}
-void be_det_phi_congr_classes(void) {
- //forall phis: det_phi_congr_classes(n);
- assert(0 && "NYI");
+void be_phi_congr_classes(ir_graph *irg) {
+ irg_walk_graph(irg, phi_class_walker, NULL, NULL);
+ irg_walk_graph(irg, phi_class_inteference_walker, NULL, NULL);
}
#define _BEPHICONGR_T_H
#include "irnode.h"
+#include "pset.h"
+
+/**
+ * Setting this to 0 will treat const nodes like
+ * all other nodes when computing phi congruence classes.
+ * A non zero value results in splitting phi congruence
+ * classes at all const nodes (except they do share
+ * some non-const nodes too)
+ */
+#define CONSTS_SPLIT_PHI_CLASSES 1
+
typedef struct _phi_info_t {
- ir_node *phi_congr_class;
- unsigned int phi_class_size;
+ ir_node *phi; /* only set in args of phi nodes (which could be a phi itslef). Points to a phi node or NULL */
+ pset *phi_class; /* only set in phi nodes. A set containing the members of the phi congruence class this phi node belongs to */
+ int interf_pairs;
+ int interf_vals;
} phi_info_t;
extern size_t phi_irn_data_offset;
-#define get_irn_phi_info(irn) get_irn_data(irn, phi_info_t, phi_irn_data_offset)
-#define get_phi_class(n) get_irn_phi_info(n)->phi_congr_class
-#define get_phi_class_size(n) get_irn_phi_info(n)->phi_class_size
+#define get_irn_phi_info(irn) get_irn_data(irn, phi_info_t, phi_irn_data_offset)
+/* Only for phi nodes */
+#define get_phi_class(irn) get_irn_phi_info(irn)->phi_class
+#define get_phi_class_size(irn) (get_phi_class(irn)==NULL ? 0 : pset_count(get_phi_class(irn)))
void be_phi_congr_class_init(void);
-void be_det_phi_congr_classes(void);
-void det_phi_congr_class(ir_node *curr_phi);
+/**
+ * Determines the phi congruence classes of
+ * all phi nodes in a graph
+ */
+void be_phi_congr_classes(ir_graph *irg);
#endif
#define SUMMARY_FILE_NAME "all.phi"
-/*
- * 0 to 10 arg-count of phi funct
- * 11 to 20 phi-congr-class size
- * 11 # of const args
- * 12 # of args defined in cf-pred block
- * 13 # of args defines elsewhere
- * 14 size of phi congruence class
- */
-
#define ARG_CNT_MAX 10
#define PHI_CLS_MAX 10
-#define I_ARG_CNT_S 0
+#define I_PHI_CNT 0
+#define I_BLK_CNT (I_PHI_CNT+1)
+#define I_SPACE3 (I_BLK_CNT+1)
+#define I_ARG_CNT_S (I_SPACE3+1)
#define I_ARG_CNT_E (I_ARG_CNT_S+ARG_CNT_MAX)
-#define I_PHI_CLS_S (I_ARG_CNT_E+1)
+#define I_SPACE1 (I_ARG_CNT_E+1)
+#define I_PHI_CLS_S (I_SPACE1+1)
#define I_PHI_CLS_E (I_PHI_CLS_S+PHI_CLS_MAX)
-#define I_CONST (I_PHI_CLS_E+1)
-#define I_PRED (I_CONST+1)
-#define I_GLOB (I_PRED+1)
-#define ASIZE (I_GLOB+1)
+#define I_SPACE2 (I_PHI_CLS_E+1)
+#define I_INTERFP (I_SPACE2+1) /* number of interfering pairs */
+#define I_INTERFV (I_INTERFP+1) /* number of interfering values */
+#define I_PHICLSCNT (I_INTERFV+1) /* number of phi classes */
+#define I_INTERFPHI (I_PHICLSCNT+1) /* number of phi classes which have interfering vals */
+#define I_CONST (I_INTERFPHI+1)
+#define I_PRED (I_CONST+1)
+#define I_GLOB (I_PRED+1)
+#define ASIZE (I_GLOB+1)
static int curr_vals[ASIZE];
-static void phi_stat_post_walker(ir_node *node, void *env) {
- int size;
- if (!(is_Phi(node) && mode_is_datab(get_irn_mode(node)))) return;
-
- size = get_phi_class_size(node);
- if (size > PHI_CLS_MAX)
- curr_vals[I_PHI_CLS_E]++;
- else
- curr_vals[I_PHI_CLS_S + size]++;
-}
static void phi_stat_walker(ir_node *node, void *env) {
- int count, i;
+ int count, i, size;
+
+ /* count all block nodes */
+ if (is_Block(node))
+ curr_vals[I_BLK_CNT]++;
if (!(is_Phi(node) && mode_is_datab(get_irn_mode(node)))) return;
+ /* count all phi nodes */
+ curr_vals[I_PHI_CNT]++;
+
/* argument count */
count = get_irn_arity(node);
if (count > ARG_CNT_MAX)
curr_vals[I_GLOB]++;
}
- /* phi congruence class */
- det_phi_congr_class(node);
+
+ size = get_phi_class_size(node);
+ /* phi class count */
+ if (size > 0)
+ curr_vals[I_PHICLSCNT]++;
+
+ /* phi class size */
+ if (size > PHI_CLS_MAX)
+ curr_vals[I_PHI_CLS_E]++;
+ else
+ curr_vals[I_PHI_CLS_S + size]++;
+
+ /* count of interfering pairs / values */
+ curr_vals[I_INTERFP] += get_irn_phi_info(node)->interf_pairs;
+ curr_vals[I_INTERFV] += get_irn_phi_info(node)->interf_vals;
+ if (get_irn_phi_info(node)->interf_vals > 0)
+ curr_vals[I_INTERFPHI]++;
}
+/**
+ * Dump values and some annotations for current irp
+ */
static void dump_files(void) {
- int i, sum1, sum2, next;
+ int i, sum1, sum2, sum3, next;
FILE *out;
char buf[200];
sum2 = 0;
for (i = I_ARG_CNT_S; i<=I_ARG_CNT_E; i++)
sum2 += curr_vals[i];
+ sum3 = 0;
+ for (i = I_PHI_CLS_S; i<=I_PHI_CLS_E; i++)
+ sum3 += curr_vals[i];
next = sprintf(buf, get_irp_prog_name());
- sprintf(buf+next, ".phi.argcount");
-
+ sprintf(buf+next, ".phi.stat");
out = fopen(buf, "wt");
+
+ fprintf(out, "Phis %i\n", curr_vals[I_PHI_CNT]);
+ fprintf(out, "Blks %i\n", curr_vals[I_BLK_CNT]);
+
+ fprintf(out, "\nArgument counts\n");
for (i = I_ARG_CNT_S; i<=I_ARG_CNT_E; i++)
- fprintf(out, "%2i %2.2f\n", i, (double) curr_vals[i] / sum2);
- fclose(out);
+ fprintf(out, "%2i %2.4f\n", i-I_ARG_CNT_S, (double) curr_vals[i] / sum2);
- sprintf(buf+next, ".phi.defloc");
- out = fopen(buf, "wt");
- fopen(buf, "wt");
+ fprintf(out, "\nPhi class sizes\n");
+ for (i = I_PHI_CLS_S; i<=I_PHI_CLS_E; i++)
+ fprintf(out, "%2i %2.4f\n", i-I_PHI_CLS_S, (double) curr_vals[i] / sum3);
+
+ fprintf(out, "\n");
+ fprintf(out, "Interf Pairs %d\n", curr_vals[I_INTERFP]);
+ fprintf(out, "Interf Values %d\n", curr_vals[I_INTERFV]);
+ fprintf(out, "PhiCls Count %d\n", curr_vals[I_PHICLSCNT]);
+ fprintf(out, "Interf PhisCl %d\n", curr_vals[I_INTERFPHI]);
fprintf(out, "Const %2.2f\n", (double) curr_vals[I_CONST] / sum1);
fprintf(out, "Pred %2.2f\n", (double) curr_vals[I_PRED] / sum1);
fprintf(out, "Glob %2.2f\n", (double) curr_vals[I_GLOB] / sum1);
+
fclose(out);
}
+/**
+ * Updates the summary file with cumulated values
+ */
static void update_all_file(void) {
int i;
FILE *all;
void do_phi_statistics(void) {
int i, n;
+ curr_vals[I_SPACE1] = -1;
+ curr_vals[I_SPACE2] = -1;
+ curr_vals[I_SPACE3] = -1;
for (i = 0, n = get_irp_n_irgs(); i < n; i++) {
ir_graph *irg = get_irp_irg(i);
irg_walk_graph(irg, phi_stat_walker, NULL, NULL);
- irg_walk_graph(irg, phi_stat_post_walker, NULL, NULL);
+ curr_vals[I_BLK_CNT] -= 2;
}
dump_files();