Merge branch 'master' into wandwor
[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 "kernel/register.h"
21 #include "kernel/log.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 USING_YOSYS_NAMESPACE
26 PRIVATE_NAMESPACE_BEGIN
27
28 struct OptPass : public Pass {
29 OptPass() : Pass("opt", "perform simple optimizations") { }
30 void help() YS_OVERRIDE
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_expr [-mux_undef] [-mux_bool] [-undriven] [-clkinv] [-fine] [-full] [-keepdc]\n");
41 log(" opt_merge [-share_all] -nomux\n");
42 log("\n");
43 log(" do\n");
44 log(" opt_muxtree\n");
45 log(" opt_reduce [-fine] [-full]\n");
46 log(" opt_merge [-share_all]\n");
47 log(" opt_rmdff [-keepdc]\n");
48 log(" opt_clean [-purge]\n");
49 log(" opt_expr [-mux_undef] [-mux_bool] [-undriven] [-clkinv] [-fine] [-full] [-keepdc]\n");
50 log(" while <changed design>\n");
51 log("\n");
52 log("When called with -fast the following script is used instead:\n");
53 log("\n");
54 log(" do\n");
55 log(" opt_expr [-mux_undef] [-mux_bool] [-undriven] [-clkinv] [-fine] [-full] [-keepdc]\n");
56 log(" opt_merge [-share_all]\n");
57 log(" opt_rmdff [-keepdc]\n");
58 log(" opt_clean [-purge]\n");
59 log(" while <changed design in opt_rmdff>\n");
60 log("\n");
61 log("Note: Options in square brackets (such as [-keepdc]) are passed through to\n");
62 log("the opt_* commands when given to 'opt'.\n");
63 log("\n");
64 log("\n");
65 }
66 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
67 {
68 std::string opt_clean_args;
69 std::string opt_expr_args;
70 std::string opt_reduce_args;
71 std::string opt_merge_args;
72 std::string opt_rmdff_args;
73 bool fast_mode = false;
74
75 log_header(design, "Executing OPT pass (performing simple optimizations).\n");
76 log_push();
77
78 size_t argidx;
79 for (argidx = 1; argidx < args.size(); argidx++) {
80 if (args[argidx] == "-purge") {
81 opt_clean_args += " -purge";
82 continue;
83 }
84 if (args[argidx] == "-mux_undef") {
85 opt_expr_args += " -mux_undef";
86 continue;
87 }
88 if (args[argidx] == "-mux_bool") {
89 opt_expr_args += " -mux_bool";
90 continue;
91 }
92 if (args[argidx] == "-undriven") {
93 opt_expr_args += " -undriven";
94 continue;
95 }
96 if (args[argidx] == "-clkinv") {
97 opt_expr_args += " -clkinv";
98 continue;
99 }
100 if (args[argidx] == "-fine") {
101 opt_expr_args += " -fine";
102 opt_reduce_args += " -fine";
103 continue;
104 }
105 if (args[argidx] == "-full") {
106 opt_expr_args += " -full";
107 opt_reduce_args += " -full";
108 continue;
109 }
110 if (args[argidx] == "-keepdc") {
111 opt_expr_args += " -keepdc";
112 opt_rmdff_args += " -keepdc";
113 continue;
114 }
115 if (args[argidx] == "-share_all") {
116 opt_merge_args += " -share_all";
117 continue;
118 }
119 if (args[argidx] == "-fast") {
120 fast_mode = true;
121 continue;
122 }
123 break;
124 }
125 extra_args(args, argidx, design);
126
127 if (fast_mode)
128 {
129 while (1) {
130 Pass::call(design, "opt_expr" + opt_expr_args);
131 Pass::call(design, "opt_merge" + opt_merge_args);
132 design->scratchpad_unset("opt.did_something");
133 Pass::call(design, "opt_rmdff" + opt_rmdff_args);
134 if (design->scratchpad_get_bool("opt.did_something") == false)
135 break;
136 Pass::call(design, "opt_clean" + opt_clean_args);
137 log_header(design, "Rerunning OPT passes. (Removed registers in this run.)\n");
138 }
139 Pass::call(design, "opt_clean" + opt_clean_args);
140 }
141 else
142 {
143 Pass::call(design, "opt_expr" + opt_expr_args);
144 Pass::call(design, "opt_merge -nomux" + opt_merge_args);
145 while (1) {
146 design->scratchpad_unset("opt.did_something");
147 Pass::call(design, "opt_muxtree");
148 Pass::call(design, "opt_reduce" + opt_reduce_args);
149 Pass::call(design, "opt_merge" + opt_merge_args);
150 Pass::call(design, "opt_rmdff" + opt_rmdff_args);
151 Pass::call(design, "opt_clean" + opt_clean_args);
152 Pass::call(design, "opt_expr" + opt_expr_args);
153 if (design->scratchpad_get_bool("opt.did_something") == false)
154 break;
155 log_header(design, "Rerunning OPT passes. (Maybe there is more to do..)\n");
156 }
157 }
158
159 design->optimize();
160 design->sort();
161 design->check();
162
163 log_header(design, fast_mode ? "Finished fast OPT passes.\n" : "Finished OPT passes. (There is nothing left to do.)\n");
164 log_pop();
165 }
166 } OptPass;
167
168 PRIVATE_NAMESPACE_END