# New Ticket Created by Matt Fowles
# Please include the string: [perl #22558]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=22558 >
All~
To get around the problem from the last attempt, the constant
propogation has become more conservative. It no longer propogates past
labels...
This time there are tests to go with it ;-)
Matt
-- attachment 1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/58706/43700/cd8e7a/constant_prop_try2.patch
Index: main.c
===================================================================
RCS file: /cvs/public/parrot/languages/imcc/main.c,v
retrieving revision 1.25
diff -u -r1.25 main.c
--- main.c 2 Jun 2003 08:52:27 -0000 1.25
+++ main.c 4 Jun 2003 04:05:41 -0000
@@ -85,7 +85,7 @@
{ 'd', 'd', OPTION_required_FLAG, { "--debug" } },
{ 'w', 'w', 0, { "--warnings" } },
{ 'G', 'G', 0, { "--no-gc" } },
- { '.', '.', 0, { } },
+ { '.', '.', 0, { NULL } },
{ 'a', 'a', 0, { "--pasm" } },
{ 'h', 'h', 0, { "--help" } },
{ 'V', 'V', 0, { "--version" } },
@@ -96,7 +96,7 @@
{ 'o', 'o', OPTION_required_FLAG, { "--output" } },
{ 'O', 'O', OPTION_required_FLAG, { "--optimize" } },
{ OPT_GC_DEBUG, '\0', 0, { "--gc-debug" } },
- { 0, 0, 0, { } }
+ { 0, 0, 0, { NULL } }
};
/* most stolen from test_main.c */
Index: optimizer.c
===================================================================
RCS file: /cvs/public/parrot/languages/imcc/optimizer.c,v
retrieving revision 1.27
diff -u -r1.27 optimizer.c
--- optimizer.c 31 May 2003 23:37:42 -0000 1.27
+++ optimizer.c 4 Jun 2003 04:05:41 -0000
@@ -57,7 +57,7 @@
static void subst_constants_c(struct Parrot_Interp *interp);
static void subst_constants_if(struct Parrot_Interp *interp);
-/* static void constant_propagation(struct Parrot_Interp *interp); */
+static void constant_propagation(struct Parrot_Interp *interp);
static int used_once(void);
static int loop_optimization(struct Parrot_Interp *);
static int clone_remove(void);
@@ -97,7 +97,7 @@
if (optimizer_level & OPT_CFG) {
info(2, "optimize\n");
- /* constant_propagation(interp); */
+ constant_propagation(interp);
if (clone_remove())
return 1;
if (used_once())
@@ -292,32 +292,11 @@
}
}
-#if 0
+
/*
- * Patch #22387 modified so that its working -- somehow
- * BUT: a register might get different constant values in different code
- * paths, there may be loops and so on
- *
- * ...
- * set I2, 10
- * set I1, P0["key"] # some value coming out of the aggregate
- * if I1, nxt
- * add:
- * add I0, I2, I2
- * print I0
- * end
- * nxt:
- * set I2, 20
- * branch add
- *
- * now I0 is what?
- *
- * The patch could be ok inside one basic block.
- *
- * So this patch is left here to show just the necessary code piese
- * how to substitute the constant.
- *
- * This code is only for documentation -lt
+ * does conservative constant propogation
+ * this code will not propogate constants past labels or saves
+ * even though sometimes it may be safe
*/
static void
@@ -340,7 +319,8 @@
debug(DEBUG_OPT1, "propagating constant %s => \n", ins_string(ins));
for (ins2 = ins->next; ins2; ins2 = ins2->next) {
- if (ins2->type & ITSAVES)
+ if (ins2->type & ITSAVES ||
+ ins2->type & ITLABEL) /* restrict to within a basic block */
goto next_constant;
/* parrot opsize has opcode too, so argument count is
* opsize - 1
@@ -361,7 +341,7 @@
debug(DEBUG_OPT2," - no %s\n", fullname);
}
else {
- --ins2->r[i]->use_count;
+ --old->use_count;
ins2->opnum = op;
debug(DEBUG_OPT2," -> %s\n", ins_string(ins2));
}
@@ -376,7 +356,6 @@
}/*for(ins ... )*/
}
-#endif
/*
* rewrite e.g. add_n_nc_ic => add_n_nc_nc
Index: t/imcpasm/opt2.t
===================================================================
RCS file: /cvs/public/parrot/languages/imcc/t/imcpasm/opt2.t,v
retrieving revision 1.3
diff -u -r1.3 opt2.t
--- t/imcpasm/opt2.t 27 Apr 2003 07:42:25 -0000 1.3
+++ t/imcpasm/opt2.t 4 Jun 2003 04:05:41 -0000
@@ -1,6 +1,6 @@
#!perl
use strict;
-use TestCompiler tests => 2;
+use TestCompiler tests => 4;
# these tests are run with -O2 by TestCompiler and show
# generated PASM code for various optimizations at level 2
@@ -15,13 +15,12 @@
.end
CODE
_main:
- set I0, 2
- print I0
+ print 2
end
OUT
##############################
-output_is(<<'CODE', <<'OUT', "move invariant out of loop");
+output_is(<<'CODE', <<'OUT', "constant propogation and resulting dead code");
.sub _main
set I0, 5
loop:
@@ -34,10 +33,66 @@
CODE
_main:
set I0, 5
- set I1, 2
loop:
- add I0, I1
+ add I0, 2
lt I0, 20, loop
print I0
end
+OUT
+
+
+##############################
+output_is(<<'CODE', <<'OUT', "don't move constant past a label");
+.sub _main
+ set I1, 10
+ set I0, 5
+ lt I1, 20, nxt
+add:
+ add I0, I1, I1
+ print I0
+nxt:
+ set I1, 20
+ branch add
+.end
+CODE
+_main:
+ set I1, 10
+ set I0, 5
+ lt 10, 20, nxt
+add:
+ add I0, I1, I1
+ print I0
+nxt:
+ set I1, 20
+ branch add
+OUT
+
+##############################
+output_is(<<'CODE', <<'OUT', "remove invariant from loop");
+.sub _main
+ set I0, 5
+loop:
+ set I1, 2
+ add I0, I1
+ lt I0, 20, loop
+next:
+ print I0
+ add I0, I1
+ print I0
+ lt I1, 4, next
+ end
+.end
+CODE
+_main:
+ set I0, 5
+ set I1, 2
+loop:
+ add I0, 2
+ lt I0, 20, loop
+next:
+ print I0
+ add I0, I1
+ print I0
+ lt I1, 4, next
+ end
OUT