c3e418c075de481cd25b765a40c37f9144f10619
[yosys.git] / passes / opt / opt.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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() 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] [-noclkinv] [-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_share (-full only)\n");
48 log(" opt_dff [-nodffe] [-nosdff] [-keepdc] [-sat] (except when called with -noff)\n");
49 log(" opt_clean [-purge]\n");
50 log(" opt_expr [-mux_undef] [-mux_bool] [-undriven] [-noclkinv] [-fine] [-full] [-keepdc]\n");
51 log(" while <changed design>\n");
52 log("\n");
53 log("When called with -fast the following script is used instead:\n");
54 log("\n");
55 log(" do\n");
56 log(" opt_expr [-mux_undef] [-mux_bool] [-undriven] [-noclkinv] [-fine] [-full] [-keepdc]\n");
57 log(" opt_merge [-share_all]\n");
58 log(" opt_dff [-nodffe] [-nosdff] [-keepdc] [-sat] (except when called with -noff)\n");
59 log(" opt_clean [-purge]\n");
60 log(" while <changed design in opt_dff>\n");
61 log("\n");
62 log("Note: Options in square brackets (such as [-keepdc]) are passed through to\n");
63 log("the opt_* commands when given to 'opt'.\n");
64 log("\n");
65 log("\n");
66 }
67 void execute(std::vector<std::string> args, RTLIL::Design *design) override
68 {
69 std::string opt_clean_args;
70 std::string opt_expr_args;
71 std::string opt_reduce_args;
72 std::string opt_merge_args;
73 std::string opt_dff_args;
74 bool opt_share = false;
75 bool fast_mode = false;
76 bool noff_mode = false;
77
78 log_header(design, "Executing OPT pass (performing simple optimizations).\n");
79 log_push();
80
81 size_t argidx;
82 for (argidx = 1; argidx < args.size(); argidx++) {
83 if (args[argidx] == "-purge") {
84 opt_clean_args += " -purge";
85 continue;
86 }
87 if (args[argidx] == "-mux_undef") {
88 opt_expr_args += " -mux_undef";
89 continue;
90 }
91 if (args[argidx] == "-mux_bool") {
92 opt_expr_args += " -mux_bool";
93 continue;
94 }
95 if (args[argidx] == "-undriven") {
96 opt_expr_args += " -undriven";
97 continue;
98 }
99 if (args[argidx] == "-noclkinv") {
100 opt_expr_args += " -noclkinv";
101 continue;
102 }
103 if (args[argidx] == "-fine") {
104 opt_expr_args += " -fine";
105 opt_reduce_args += " -fine";
106 continue;
107 }
108 if (args[argidx] == "-full") {
109 opt_expr_args += " -full";
110 opt_reduce_args += " -full";
111 opt_share = true;
112 continue;
113 }
114 if (args[argidx] == "-keepdc") {
115 opt_expr_args += " -keepdc";
116 opt_dff_args += " -keepdc";
117 continue;
118 }
119 if (args[argidx] == "-nodffe") {
120 opt_dff_args += " -nodffe";
121 continue;
122 }
123 if (args[argidx] == "-nosdff") {
124 opt_dff_args += " -nosdff";
125 continue;
126 }
127 if (args[argidx] == "-sat") {
128 opt_dff_args += " -sat";
129 continue;
130 }
131 if (args[argidx] == "-share_all") {
132 opt_merge_args += " -share_all";
133 continue;
134 }
135 if (args[argidx] == "-fast") {
136 fast_mode = true;
137 continue;
138 }
139 if (args[argidx] == "-noff") {
140 noff_mode = true;
141 continue;
142 }
143 break;
144 }
145 extra_args(args, argidx, design);
146
147 if (fast_mode)
148 {
149 while (1) {
150 Pass::call(design, "opt_expr" + opt_expr_args);
151 Pass::call(design, "opt_merge" + opt_merge_args);
152 design->scratchpad_unset("opt.did_something");
153 if (!noff_mode)
154 Pass::call(design, "opt_dff" + opt_dff_args);
155 if (design->scratchpad_get_bool("opt.did_something") == false)
156 break;
157 Pass::call(design, "opt_clean" + opt_clean_args);
158 log_header(design, "Rerunning OPT passes. (Removed registers in this run.)\n");
159 }
160 Pass::call(design, "opt_clean" + opt_clean_args);
161 }
162 else
163 {
164 Pass::call(design, "opt_expr" + opt_expr_args);
165 Pass::call(design, "opt_merge -nomux" + opt_merge_args);
166 while (1) {
167 design->scratchpad_unset("opt.did_something");
168 Pass::call(design, "opt_muxtree");
169 Pass::call(design, "opt_reduce" + opt_reduce_args);
170 Pass::call(design, "opt_merge" + opt_merge_args);
171 if (opt_share)
172 Pass::call(design, "opt_share");
173 if (!noff_mode)
174 Pass::call(design, "opt_dff" + opt_dff_args);
175 Pass::call(design, "opt_clean" + opt_clean_args);
176 Pass::call(design, "opt_expr" + opt_expr_args);
177 if (design->scratchpad_get_bool("opt.did_something") == false)
178 break;
179 log_header(design, "Rerunning OPT passes. (Maybe there is more to do..)\n");
180 }
181 }
182
183 design->optimize();
184 design->sort();
185 design->check();
186
187 log_header(design, fast_mode ? "Finished fast OPT passes.\n" : "Finished OPT passes. (There is nothing left to do.)\n");
188 log_pop();
189 }
190 } OptPass;
191
192 PRIVATE_NAMESPACE_END