33e1507bcd573565a878b23a13b591e535e954bd
[yosys.git] / passes / opt / opt.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "opt_status.h"
21 #include "kernel/register.h"
22 #include "kernel/log.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 bool OPT_DID_SOMETHING;
27
28 struct OptPass : public Pass {
29 OptPass() : Pass("opt", "perform simple optimizations") { }
30 virtual void help()
31 {
32 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
33 log("\n");
34 log(" opt [options] [selection]\n");
35 log("\n");
36 log("This pass calls all the other opt_* passes in a useful order. This performs\n");
37 log("a series of trivial optimizations and cleanups. This pass executes the other\n");
38 log("passes in the following order:\n");
39 log("\n");
40 log(" opt_const\n");
41 log(" opt_share -nomux\n");
42 log("\n");
43 log(" do\n");
44 log(" opt_muxtree\n");
45 log(" opt_reduce [-fine]\n");
46 log(" opt_share\n");
47 log(" opt_rmdff\n");
48 log(" opt_clean [-purge]\n");
49 log(" opt_const [-mux_undef] [-mux_bool] [-undriven] [-fine] [-keepdc]\n");
50 log(" while <changed design>\n");
51 log("\n");
52 log("Note: Options in square brackets (such as [-keepdc]) are passed through to\n");
53 log("the opt_* commands when given to 'opt'.\n");
54 log("\n");
55 }
56 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
57 {
58 std::string opt_clean_args;
59 std::string opt_const_args;
60 std::string opt_reduce_args;
61
62 log_header("Executing OPT pass (performing simple optimizations).\n");
63 log_push();
64
65 size_t argidx;
66 for (argidx = 1; argidx < args.size(); argidx++) {
67 if (args[argidx] == "-purge") {
68 opt_clean_args += " -purge";
69 continue;
70 }
71 if (args[argidx] == "-mux_undef") {
72 opt_const_args += " -mux_undef";
73 continue;
74 }
75 if (args[argidx] == "-mux_bool") {
76 opt_const_args += " -mux_bool";
77 continue;
78 }
79 if (args[argidx] == "-undriven") {
80 opt_const_args += " -undriven";
81 continue;
82 }
83 if (args[argidx] == "-fine") {
84 opt_const_args += " -fine";
85 opt_reduce_args += " -fine";
86 continue;
87 }
88 if (args[argidx] == "-keepdc") {
89 opt_const_args += " -keepdc";
90 continue;
91 }
92 break;
93 }
94 extra_args(args, argidx, design);
95
96 log_header("Optimizing in-memory representation of design.\n");
97 design->optimize();
98
99 Pass::call(design, "opt_const");
100 Pass::call(design, "opt_share -nomux");
101 while (1) {
102 OPT_DID_SOMETHING = false;
103 Pass::call(design, "opt_muxtree");
104 Pass::call(design, "opt_reduce" + opt_reduce_args);
105 Pass::call(design, "opt_share");
106 Pass::call(design, "opt_rmdff");
107 Pass::call(design, "opt_clean" + opt_clean_args);
108 Pass::call(design, "opt_const" + opt_const_args);
109 if (OPT_DID_SOMETHING == false)
110 break;
111 log_header("Rerunning OPT passes. (Maybe there is more to do..)\n");
112 }
113
114 log_header("Optimizing in-memory representation of design.\n");
115 design->optimize();
116
117 log_header("Finished OPT passes. (There is nothing left to do.)\n");
118 log_pop();
119 }
120 } OptPass;
121