This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
genattrtab speedup 3/4
- From: Michael Matz <matz at suse dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Mon, 1 Aug 2005 00:31:19 +0200 (CEST)
- Subject: genattrtab speedup 3/4
Hi,
this third patch gives back some performance. During analyzing the
resulting insn-attrtab.c without optimization it happened sometimes that
there were such values:
switch (insn_code) {
case 1: case 2: case 42:
// whole attribute value definition verbatim, no optimization, i.e.
// big cascade of conditions:
if (cond1)
return a;
else if (cond2)
return b;
else if (cond3)
return c;
else if (cond4)
return d;
else
return e;
where it is clear from context that e.g. cond[234] are false for each of
the possible three insn codes for this case. So I introduced an
optimization which can work on multiple insn codes at once. Let's suppose
those whole if cascade above is called A, then what this optimization does
is conceptually transform the code to:
if (insn_code == 1) { A_1; }
else if (insn_code == 2) { A_2; }
else if (insn_code == 42) { A_42; }
where A_x is A specialized for the insn_code x. In this example all three
A_x would just be 'if (cond1) return a;' (remember that cond[234] were
false for all three insns). To optimize this even further I also had to
implement the distribute law of IOR with the common element being last, so
this simplifies then to:
case 1: case 2: case 42:
if (insn_code == 1 || insn_code == 2 || insn_code == 42) {
if (cond1) return a; else return e;
}
Notice how the first if is true in every case applicable to this value,
but I haven't yet implemented this optimization.
For the above I had to introduce a new pseudo attribute 'insn_code', which
is similar to 'alternative_name'. And further I sanitized the costs
associated with expression a bit, as also simple 'a==b' expressions have a
cost.
And the last change here is some code factoring, by introduction of
write_attr_switch(), which writes the whole
{ int insn_code = recog_memoized (insn);
switch (insn_code) {
case ....
}
}
construct.
Ciao,
Michael.
--
* genattrtab.c (insn_code_str): New.
(simplify_cond_insn_list): New.
(attr_rtx_cost): Return '1' for cheap cost, not '0'.
Accesses to 'insn_code' are cheap too.
(simplify_test_exp <IOR>): Handle distribute law with common term
at the end.
(<EQ_ATTR>): Simplify 'insn_code' attribute.
Increase acceptable cost for inline expressions.
(write_test_expr): Handle 'insn_code' attribute.
(write_attr_switch): New. Factored out code from ...
(write_attr_get): ... here ...
(write_eligible_delay): ... and here (three times).
(write_attr_get): Also use -2 as unknown insn index.
(write_attr_case): Move optimization of value in front of walking it.
Use simplify_cond_insn_list for values containing few insns.
(find_attr): Handle 'insn_code' attribute.
(main): Initialize insn_code_str.
--- genattrtab.noopt.c 2005-07-31 22:53:01.557627043 +0200
+++ genattrtab.icode.c 2005-07-31 22:53:26.104092740 +0200
@@ -239,6 +239,7 @@ static const char *length_str;
static const char *delay_type_str;
static const char *delay_1_0_str;
static const char *num_delay_slots_str;
+static const char *insn_code_str;
/* Indicate that REG_DEAD notes are valid if dead_or_set_p is ever
called. */
@@ -1803,6 +1804,103 @@ simplify_cond (rtx exp, int insn_code, i
return ret;
}
+static rtx
+simplify_cond_insn_list (rtx exp, struct attr_value *av)
+{
+ int i;
+ /* We store the desired contents here,
+ then build a new expression if they don't match EXP. */
+ rtx defval = XEXP (exp, 1);
+ rtx new_defval = XEXP (exp, 1);
+ int len = XVECLEN (exp, 0);
+ rtx *tests;
+ int allsame = 1;
+ rtx ret;
+
+ if (av->has_asm_insn)
+ return exp;
+
+ tests = xmalloc (len * sizeof (rtx));
+ /* This lets us free all storage allocated below, if appropriate. */
+ obstack_finish (rtl_obstack);
+
+ memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
+
+ /* See if default value needs simplification. */
+ if (GET_CODE (defval) == COND)
+ new_defval = simplify_cond_insn_list (defval, av);
+
+ for (i = 0; i < len; i += 2)
+ {
+ rtx newtest, newval;
+ struct insn_ent *ie;
+ int oldcost;
+
+ oldcost = attr_rtx_cost (XVECEXP (exp, 0, i));
+ tests[i] = false_rtx;
+ for (ie = av->first_insn; ie != 0; ie = ie->next)
+ {
+ rtx orig_test = XVECEXP (exp, 0, i);
+ gcc_assert (ie->def->insn_code >= 0);
+ newtest = simplify_test_exp_in_temp (orig_test, ie->def->insn_code,
+ ie->def->insn_index);
+ /* We couldn't simplify the expression for this insn code,
+ so it makes no sense to try further, as syntactically we
+ can't shorten the test anymore. Further, also the
+ simplifications done up to now can be removed. */
+ if (newtest == orig_test)
+ {
+ tests[i] = orig_test;
+ break;
+ }
+ newtest = attr_rtx (AND,
+ attr_eq (insn_code_str,
+ attr_numeral (ie->def->insn_code)),
+ newtest);
+ tests[i] = insert_right_side (IOR, tests[i], newtest, -2, -2);
+ }
+
+ if (attr_rtx_cost (tests[i]) > oldcost)
+ tests[i] = XVECEXP (exp, 0, i);
+
+ /* Simplify this test. */
+ newtest = simplify_test_exp_in_temp (tests[i], -2, -2);
+ tests[i] = newtest;
+
+ newval = tests[i + 1];
+ /* See if this value may need simplification. */
+ if (GET_CODE (newval) == COND)
+ newval = simplify_cond_insn_list (newval, av);
+
+ tests[i + 1] = newval;
+ }
+
+ /* See if we changed anything. */
+ if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
+ allsame = 0;
+ else
+ for (i = 0; i < len; i++)
+ if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
+ {
+ allsame = 0;
+ break;
+ }
+
+ if (allsame)
+ ret = exp;
+ else
+ {
+ rtx newexp = rtx_alloc (COND);
+
+ XVEC (newexp, 0) = rtvec_alloc (len);
+ memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
+ XEXP (newexp, 1) = new_defval;
+ ret = newexp;
+ }
+ free (tests);
+ return ret;
+}
+
/* Remove an insn entry from an attribute value. */
static void
@@ -2293,15 +2391,16 @@ attr_rtx_cost (rtx x)
if (XSTR (x, 1)[0])
return 10;
else
- return 0;
+ return 1;
case EQ_ATTR_ALT:
- return 0;
+ return 1;
case EQ_ATTR:
/* Alternatives don't result into function call. */
- if (!strcmp_check (XSTR (x, 0), alternative_name))
- return 0;
+ if (!strcmp_check (XSTR (x, 0), alternative_name)
+ || !strcmp_check (XSTR (x, 0), insn_code_str))
+ return 1;
else
return 5;
default:
@@ -2659,6 +2758,25 @@ simplify_test_exp (rtx exp, int insn_cod
return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
}
+ /* Similarly,
+ convert (ior (and (y) (x))
+ (and (z) (x)))
+ to (and (ior (y) (z))
+ (x))
+ Note that we want the common term to stay at the end.
+ */
+
+ else if (GET_CODE (left) == AND && GET_CODE (right) == AND
+ && attr_equal_p (XEXP (left, 1), XEXP (right, 1)))
+ {
+ newexp = attr_rtx (IOR, XEXP (left, 0), XEXP (right, 0));
+
+ left = newexp;
+ right = XEXP (right, 1);
+ newexp = attr_rtx (AND, left, right);
+ return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
+ }
+
/* See if all or all but one of the insn's alternatives are specified
in this tree. Optimize if so. */
@@ -2762,6 +2880,13 @@ simplify_test_exp (rtx exp, int insn_cod
break;
}
+ if (XSTR (exp, 0) == insn_code_str)
+ {
+ if (insn_code >= 0)
+ newexp = atoi (XSTR (exp, 1)) == insn_code ? true_rtx : false_rtx;
+ break;
+ }
+
/* Look at the value for this insn code in the specified attribute.
We normally can replace this comparison with the condition that
would give this insn the values being tested for. */
@@ -2774,7 +2899,7 @@ simplify_test_exp (rtx exp, int insn_cod
rtx x;
x = evaluate_eq_attr (exp, av->value, insn_code, insn_index);
x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
- if (attr_rtx_cost(x) < 20)
+ if (attr_rtx_cost(x) < 40)
return x;
}
break;
@@ -3300,6 +3425,12 @@ write_test_expr (rtx exp, int flags)
break;
}
+ if (XSTR (exp, 0) == insn_code_str)
+ {
+ printf ("insn_code == %s", XSTR (exp, 1));
+ break;
+ }
+
attr = find_attr (&XSTR (exp, 0), 0);
gcc_assert (attr);
@@ -3545,10 +3676,9 @@ walk_attr_value (rtx exp)
}
}
-/* Write out a function to obtain the attribute for a given INSN. */
-
static void
-write_attr_get (struct attr_desc *attr)
+write_attr_switch (struct attr_desc *attr, int indent, const char *prefix,
+ const char *suffix)
{
struct attr_value *av, *common_av;
@@ -3556,6 +3686,32 @@ write_attr_get (struct attr_desc *attr)
switch we will generate. */
common_av = find_most_used (attr);
+ write_indent (indent);
+ printf ("{\n");
+ write_indent (indent);
+ printf (" int insn_code = recog_memoized (insn);\n");
+ write_indent (indent);
+ printf (" switch (insn_code)\n");
+ write_indent (indent);
+ printf (" {\n");
+
+ for (av = attr->first_value; av; av = av->next)
+ if (av != common_av)
+ write_attr_case (attr, av, 1, prefix, suffix, indent + 4, true_rtx);
+
+ write_attr_case (attr, common_av, 0, prefix, suffix, indent + 4, true_rtx);
+
+ write_indent (indent);
+ printf (" }\n");
+ write_indent (indent);
+ printf ("}\n\n");
+}
+
+/* Write out a function to obtain the attribute for a given INSN. */
+
+static void
+write_attr_get (struct attr_desc *attr)
+{
/* Write out start of function, then all values with explicit `case' lines,
then a `default', then the value with the most uses. */
if (attr->static_p)
@@ -3573,6 +3729,7 @@ write_attr_get (struct attr_desc *attr)
printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
else
{
+ struct attr_value *av;
printf ("get_attr_%s (void)\n", attr->name);
printf ("{\n");
@@ -3583,22 +3740,13 @@ write_attr_get (struct attr_desc *attr)
av->first_insn->def->insn_index);
else if (av->num_insns != 0)
write_attr_set (attr, 2, av->value, "return", ";",
- true_rtx, -2, 0);
+ true_rtx, -2, -2);
printf ("}\n\n");
return;
}
- printf ("{\n");
- printf (" switch (recog_memoized (insn))\n");
- printf (" {\n");
-
- for (av = attr->first_value; av; av = av->next)
- if (av != common_av)
- write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
-
- write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
- printf (" }\n}\n\n");
+ write_attr_switch (attr, 0, "return", ";");
}
/* Given an AND tree of known true terms (because we are inside an `if' with
@@ -3773,9 +3921,25 @@ write_attr_case (struct attr_desc *attr,
printf ("default:\n");
}
+ opt_val = av->value;
+ if (GET_CODE (opt_val) == COND && av->num_insns > 1 && av->num_insns < 10)
+ opt_val = simplify_cond_insn_list (opt_val, av);
+ while (GET_CODE (opt_val) == COND)
+ {
+ rtx newexp2;
+ if (av->num_insns == 1)
+ newexp2 = simplify_cond (opt_val, av->first_insn->def->insn_code,
+ av->first_insn->def->insn_index);
+ else
+ newexp2 = simplify_cond (opt_val, -2, -2);
+ if (newexp2 == opt_val)
+ break;
+ opt_val = newexp2;
+ }
+
/* See what we have to do to output this value. */
must_extract = must_constrain = address_used = 0;
- walk_attr_value (av->value);
+ walk_attr_value (opt_val);
if (must_constrain)
{
@@ -3788,19 +3952,6 @@ write_attr_case (struct attr_desc *attr,
printf ("extract_insn_cached (insn);\n");
}
- opt_val = av->value;
- while (GET_CODE (opt_val) == COND)
- {
- rtx newexp2;
- if (av->num_insns == 1)
- newexp2 = simplify_cond (opt_val, av->first_insn->def->insn_code,
- av->first_insn->def->insn_index);
- else
- newexp2 = simplify_cond (opt_val, -2, 0);
- if (newexp2 == opt_val)
- break;
- opt_val = newexp2;
- }
if (av->num_insns == 1)
write_attr_set (attr, indent + 2, opt_val, prefix, suffix,
known_true, av->first_insn->def->insn_code,
@@ -3981,7 +4132,6 @@ write_eligible_delay (const char *kind)
char str[50];
const char *pstr;
struct attr_desc *attr;
- struct attr_value *av, *common_av;
int i;
/* Compute the maximum number of delay slots required. We use the delay
@@ -4014,19 +4164,11 @@ write_eligible_delay (const char *kind)
{
attr = find_attr (&delay_type_str, 0);
gcc_assert (attr);
- common_av = find_most_used (attr);
-
- printf (" insn = delay_insn;\n");
- printf (" switch (recog_memoized (insn))\n");
- printf (" {\n");
sprintf (str, " * %d;\n break;", max_slots);
- for (av = attr->first_value; av; av = av->next)
- if (av != common_av)
- write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
- write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
- printf (" }\n\n");
+ printf (" insn = delay_insn;\n");
+ write_attr_switch (attr, 2, "slot +=", str);
/* Ensure matched. Otherwise, shouldn't have been called. */
printf (" gcc_assert (slot >= %d);\n\n", max_slots);
@@ -4035,20 +4177,10 @@ write_eligible_delay (const char *kind)
/* If just one type of delay slot, write simple switch. */
if (num_delays == 1 && max_slots == 1)
{
- printf (" insn = candidate_insn;\n");
- printf (" switch (recog_memoized (insn))\n");
- printf (" {\n");
-
attr = find_attr (&delay_1_0_str, 0);
gcc_assert (attr);
- common_av = find_most_used (attr);
-
- for (av = attr->first_value; av; av = av->next)
- if (av != common_av)
- write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
-
- write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
- printf (" }\n");
+ printf (" insn = candidate_insn;\n");
+ write_attr_switch (attr, 2, "return", ";");
}
else
@@ -4064,21 +4196,12 @@ write_eligible_delay (const char *kind)
{
printf (" case %d:\n",
(i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
- printf (" switch (recog_memoized (insn))\n");
- printf ("\t{\n");
sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
pstr = str;
attr = find_attr (&pstr, 0);
gcc_assert (attr);
- common_av = find_most_used (attr);
-
- for (av = attr->first_value; av; av = av->next)
- if (av != common_av)
- write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
-
- write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
- printf (" }\n");
+ write_attr_switch (attr, 6, "return", ";");
}
printf (" default:\n");
@@ -4121,7 +4244,7 @@ find_attr (const char **name_p, int crea
/* Before we resort to using `strcmp', see if the string address matches
anywhere. In most cases, it should have been canonicalized to do so. */
- if (name == alternative_name)
+ if (name == alternative_name || name == insn_code_str)
return NULL;
index = name[0] & (MAX_ATTRS_INDEX - 1);
@@ -4282,6 +4405,7 @@ main (int argc, char **argv)
delay_type_str = DEF_ATTR_STRING ("*delay_type");
delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
+ insn_code_str = DEF_ATTR_STRING ("insn_code");
printf ("/* Generated automatically by the program `genattrtab'\n\
from the machine description file `md'. */\n\n");