aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-02-18 11:57:23 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-02-24 10:49:15 +0100
commitadc4d32ef551907128637600a98ba28153ef79d8 (patch)
tree9c1b93e1875814709d4db12ca225b64ea970e83b
parent08fff567c55a9cf8c3854389d2575afdca6d529e (diff)
downloadsparse-dev-adc4d32ef551907128637600a98ba28153ef79d8.tar.gz
move the optimization loop in its own file
Once linearized, a few essentials optimizations are done but the logic of the succession of these optimizations is open-coded at the end of linearize_fn(). Obviously, this logic have nothing to do with the linearization and moving it to a separate file will help to clarify this logic and change it when needed. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--Makefile1
-rw-r--r--linearize.c64
-rw-r--r--optimize.c76
-rw-r--r--optimize.h9
4 files changed, 88 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index ddd3f2ab..5548f848 100644
--- a/Makefile
+++ b/Makefile
@@ -47,6 +47,7 @@ LIB_OBJS += linearize.o
LIB_OBJS += liveness.o
LIB_OBJS += memops.o
LIB_OBJS += opcode.o
+LIB_OBJS += optimize.o
LIB_OBJS += parse.o
LIB_OBJS += pre-process.o
LIB_OBJS += ptrlist.o
diff --git a/linearize.c b/linearize.c
index 211dabf4..63abe7a9 100644
--- a/linearize.c
+++ b/linearize.c
@@ -19,6 +19,7 @@
#include "parse.h"
#include "expression.h"
#include "linearize.h"
+#include "optimize.h"
#include "flow.h"
#include "target.h"
@@ -778,15 +779,6 @@ pseudo_t alloc_pseudo(struct instruction *def)
return pseudo;
}
-static void clear_symbol_pseudos(struct entrypoint *ep)
-{
- pseudo_t pseudo;
-
- FOR_EACH_PTR(ep->accesses, pseudo) {
- pseudo->sym->pseudo = NULL;
- } END_FOR_EACH_PTR(pseudo);
-}
-
static pseudo_t symbol_pseudo(struct entrypoint *ep, struct symbol *sym)
{
pseudo_t pseudo;
@@ -2217,59 +2209,7 @@ static struct entrypoint *linearize_fn(struct symbol *sym, struct symbol *base_t
add_one_insn(ep, insn);
}
- if (fdump_ir & PASS_LINEARIZE)
- show_entry(ep);
-
- /*
- * Do trivial flow simplification - branches to
- * branches, kill dead basicblocks etc
- */
- kill_unreachable_bbs(ep);
-
- /*
- * Turn symbols into pseudos
- */
- if (fpasses & PASS_MEM2REG)
- simplify_symbol_usage(ep);
- if (fdump_ir & PASS_MEM2REG)
- show_entry(ep);
-
- if (!(fpasses & PASS_OPTIM))
- return ep;
-repeat:
- /*
- * Remove trivial instructions, and try to CSE
- * the rest.
- */
- do {
- cleanup_and_cse(ep);
- pack_basic_blocks(ep);
- } while (repeat_phase & REPEAT_CSE);
-
- kill_unreachable_bbs(ep);
- vrfy_flow(ep);
-
- /* Cleanup */
- clear_symbol_pseudos(ep);
-
- /* And track pseudo register usage */
- track_pseudo_liveness(ep);
-
- /*
- * Some flow optimizations can only effectively
- * be done when we've done liveness analysis. But
- * if they trigger, we need to start all over
- * again
- */
- if (simplify_flow(ep)) {
- clear_liveness(ep);
- goto repeat;
- }
-
- /* Finally, add deathnotes to pseudos now that we have them */
- if (dbg_dead)
- track_pseudo_death(ep);
-
+ optimize(ep);
return ep;
}
diff --git a/optimize.c b/optimize.c
new file mode 100644
index 00000000..8cf24351
--- /dev/null
+++ b/optimize.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: MIT
+//
+// optimize.c - main optimization loop
+//
+// Copyright (C) 2004 Linus Torvalds
+// Copyright (C) 2004 Christopher Li
+
+#include "optimize.h"
+#include "linearize.h"
+#include "flow.h"
+
+
+static void clear_symbol_pseudos(struct entrypoint *ep)
+{
+ pseudo_t pseudo;
+
+ FOR_EACH_PTR(ep->accesses, pseudo) {
+ pseudo->sym->pseudo = NULL;
+ } END_FOR_EACH_PTR(pseudo);
+}
+
+void optimize(struct entrypoint *ep)
+{
+ if (fdump_ir & PASS_LINEARIZE)
+ show_entry(ep);
+
+ /*
+ * Do trivial flow simplification - branches to
+ * branches, kill dead basicblocks etc
+ */
+ kill_unreachable_bbs(ep);
+
+ /*
+ * Turn symbols into pseudos
+ */
+ if (fpasses & PASS_MEM2REG)
+ simplify_symbol_usage(ep);
+ if (fdump_ir & PASS_MEM2REG)
+ show_entry(ep);
+
+ if (!(fpasses & PASS_OPTIM))
+ return;
+repeat:
+ /*
+ * Remove trivial instructions, and try to CSE
+ * the rest.
+ */
+ do {
+ cleanup_and_cse(ep);
+ pack_basic_blocks(ep);
+ } while (repeat_phase & REPEAT_CSE);
+
+ kill_unreachable_bbs(ep);
+ vrfy_flow(ep);
+
+ /* Cleanup */
+ clear_symbol_pseudos(ep);
+
+ /* And track pseudo register usage */
+ track_pseudo_liveness(ep);
+
+ /*
+ * Some flow optimizations can only effectively
+ * be done when we've done liveness analysis. But
+ * if they trigger, we need to start all over
+ * again
+ */
+ if (simplify_flow(ep)) {
+ clear_liveness(ep);
+ goto repeat;
+ }
+
+ /* Finally, add deathnotes to pseudos now that we have them */
+ if (dbg_dead)
+ track_pseudo_death(ep);
+}
diff --git a/optimize.h b/optimize.h
new file mode 100644
index 00000000..31e2cf08
--- /dev/null
+++ b/optimize.h
@@ -0,0 +1,9 @@
+#ifndef OPTIMIZE_H
+#define OPTIMIZE_H
+
+struct entrypoint;
+
+/* optimize.c */
+void optimize(struct entrypoint *ep);
+
+#endif