ref: f143320b261f68c3fe03eb42ba5fc4dbc93ecba5
parent: 67bd2a9810f0891b26ed05830a565fa452aa04da
author: Ori Bernstein <[email protected]>
date: Fri Jun 15 08:07:52 EDT 2012
Add rudimentary constant folding.
--- a/8/ra.c
+++ b/8/ra.c
@@ -539,11 +539,13 @@
size_t i, j;
int has;
- printf("Want to combine ");
- locprint(stdout, locmap[u]);
- printf(" with ");
- locprint(stdout, locmap[v]);
- printf("\n");
+ if (debug) {
+ printf("Combine ");
+ locprint(stdout, locmap[u]);
+ printf(" ==> ");
+ locprint(stdout, locmap[v]);
+ printf("\n");
+ }
if (wlhas(s->wlfreeze, s->nwlfreeze, v, &idx))
ldel(&s->wlfreeze, &s->nwlfreeze, idx);
else if (wlhas(s->wlspill, s->nwlspill, v, &idx))
@@ -707,11 +709,13 @@
found = 0;
for (i = 0; i < K; i++) {
if (!taken[i]) {
- locprint(stdout, n);
- printf(" ==> ");
- n->reg.colour = regmap[i][n->mode];
- locprint(stdout, n);
- printf("\n");
+ if (debug) {
+ locprint(stdout, n);
+ printf(" ==> ");
+ n->reg.colour = regmap[i][n->mode];
+ locprint(stdout, n);
+ printf("\n");
+ }
found = 1;
break;
}
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -48,6 +48,10 @@
static Node *lval(Simp *s, Node *n);
static void declarelocal(Simp *s, Node *n);
+static Node *one;
+static Node *ptrsz;
+
+
static void append(Simp *s, Node *n)
{
lappend(&s->stmts, &s->nstmts, n);
@@ -275,8 +279,6 @@
return -1;
}
-static Node *one;
-
static Node *membaddr(Simp *s, Node *n)
{
Node *t, *u, *r;
@@ -287,8 +289,7 @@
t = mkexpr(-1, Oaddr, args[0], NULL);
else
t = args[0];
- u = mkint(-1, offsetof(args[0], args[1]));
- u = mkexpr(-1, Olit, u, NULL);
+ u = mkintlit(-1, offsetof(args[0], args[1]));
r = mkexpr(-1, Oadd, t, u, NULL);
return r;
}
@@ -310,7 +311,7 @@
die("Can't index type %s\n", tystr(n->expr.type));
u = rval(s, args[1]);
sz = size(n);
- v = mkexpr(-1, Omul, u, mkexpr(-1, Olit, mkint(-1, sz), NULL), NULL);
+ v = mkexpr(-1, Omul, u, mkintlit(-1, sz), NULL);
r = mkexpr(-1, Oadd, t, v, NULL);
return r;
}
@@ -330,7 +331,7 @@
}
/* safe: all types we allow here have a sub[0] that we want to grab */
sz = tysize(n->expr.type->sub[0]);
- v = mkexpr(-1, Omul, off, mkexpr(-1, Olit, mkint(-1, sz), NULL), NULL);
+ v = mkexpr(-1, Omul, off, mkintlit(-1, sz), NULL);
return mkexpr(-1, Oadd, u, v, NULL);
}
@@ -338,8 +339,6 @@
{
Node *r;
- if (!one)
- one = mkexpr(-1, Olit, mkint(-1, 1), NULL);
switch (exprop(n)) {
case Ovar: r = n; break;
case Omemb: r = membaddr(s, n); break;
@@ -384,8 +383,8 @@
/* base = (void*)base + off*sz */
base = slicebase(s, n->expr.args[0], n->expr.args[1]);
len = mkexpr(-1, Osub, n->expr.args[2], n->expr.args[1], NULL);
- stbase = mkexpr(-1, Ostor, mkexpr(-1, Oaddr, mkexpr(-1, Oslbase, t, NULL), NULL), base, NULL);
- stlen = mkexpr(-1, Ostor, mkexpr(-1, Oaddr, mkexpr(-1, Osllen, t, NULL), NULL), len, NULL);
+ stbase = mkexpr(-1, Ostor, mkexpr(-1, Oaddr, t, NULL), base, NULL);
+ stlen = mkexpr(-1, Ostor, mkexpr(-1, Oaddr, mkexpr(-1, Oadd, t, ptrsz, NULL), NULL), len, NULL);
append(s, stbase);
append(s, stlen);
return t;
@@ -411,7 +410,7 @@
if (!one)
- one = mkexpr(-1, Olit, mkint(-1, 1), NULL);
+ one = mkintlit(-1, 1);
r = NULL;
args = n->expr.args;
switch (exprop(n)) {
@@ -421,7 +420,7 @@
simplazy(s, n, r);
break;
case Osize:
- r = mkexpr(-1, Olit, mkint(-1, size(args[0])), NULL);
+ r = mkintlit(-1, size(args[0]));
break;
case Oslice:
r = lowerslice(s, n);
@@ -501,7 +500,7 @@
if (size(n) > 4) {
t = mkexpr(-1, Oaddr, t, NULL);
u = mkexpr(-1, Oaddr, u, NULL);
- v = mkexpr(-1, Olit, mkint(-1, size(n)), NULL);
+ v = mkintlit(-1, size(n));
r = mkexpr(-1, Oblit, t, u, v, NULL);
} else {
r = mkexpr(-1, Ostor, t, u, NULL);
@@ -624,6 +623,11 @@
if (debug)
for (i = 0; i < s.nstmts; i++)
dump(s.stmts[i], stdout);
+ for (i = 0; i < s.nstmts; i++) {
+ if (s.stmts[i]->type != Nexpr)
+ continue;
+ s.stmts[i] = fold(s.stmts[i]);
+ }
cfg = mkcfg(s.stmts, s.nstmts);
if (debug)
dumpcfg(cfg, stdout);
@@ -660,10 +664,14 @@
Htab *globls;
Sym *s;
+ /* declrae useful constants */
+ one = mkintlit(-1, 1);
+ ptrsz = mkintlit(-1, 4);
+
n = file->file.stmts;
nn = file->file.nstmts;
-
globls = mkht(ptrhash, ptreq);
+
/* We need to declare all variables before use */
for (i = 0; i < nn; i++)
if (n[i]->type == Ndecl)
--- a/opt/Makefile
+++ b/opt/Makefile
@@ -1,5 +1,6 @@
LIB=libopt.a
OBJ=cfg.o \
+ fold.o \
df.o \
DEPS=../parse/libparse.a
--- a/opt/opt.h
+++ b/opt/opt.h
@@ -24,6 +24,8 @@
Bitset *succ;
};
+/* expression folding */
+Node *fold(Node *n);
/* Takes a reduced block, and returns a flow graph. */
Cfg *mkcfg(Node **nl, size_t nn);
void dumpcfg(Cfg *c, FILE *fd);
--- a/parse/node.c
+++ b/parse/node.c
@@ -128,6 +128,11 @@
return n;
}
+Node *mkintlit(int line, uvlong val)
+{
+ return mkexpr(line, Olit, mkint(line, val), NULL);
+}
+
Node *mklbl(int line, char *lbl)
{
Node *n;
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -321,6 +321,7 @@
Node *mkif(int line, Node *cond, Node *iftrue, Node *iffalse);
Node *mkloop(int line, Node *init, Node *cond, Node *incr, Node *body);
Node *mkblock(int line, Stab *scope);
+Node *mkintlit(int line, uvlong val);
Node *mkbool(int line, int val);
Node *mkint(int line, uint64_t val);