Merge pull request #1303 from YosysHQ/bogdanvuk/opt_share
[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_share (-full only)\n");
48 log(" opt_rmdff [-keepdc] [-sat]\n");
49 log(" opt_clean [-purge]\n");
50 log(" opt_expr [-mux_undef] [-mux_bool] [-undriven] [-clkinv] [-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] [-clkinv] [-fine] [-full] [-keepdc]\n");
57 log(" opt_merge [-share_all]\n");
58 log(" opt_rmdff [-keepdc] [-sat]\n");
59 log(" opt_clean [-purge]\n");
60 log(" while <changed design in opt_rmdff>\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) YS_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_rmdff_args;
74 bool opt_share = false;
75 bool fast_mode = false;
76
77 log_header(design, "Executing OPT pass (performing simple optimizations).\n");
78 log_push();
79
80 size_t argidx;
81 for (argidx = 1; argidx < args.size(); argidx++) {
82 if (args[argidx] == "-purge") {
83 opt_clean_args += " -purge";
84 continue;
85 }
86 if (args[argidx] == "-mux_undef") {
87 opt_expr_args += " -mux_undef";
88 continue;
89 }
90 if (args[argidx] == "-mux_bool") {
91 opt_expr_args += " -mux_bool";
92 continue;
93 }
94 if (args[argidx] == "-undriven") {
95 opt_expr_args += " -undriven";
96 continue;
97 }
98 if (args[argidx] == "-clkinv") {
99 opt_expr_args += " -clkinv";
100 continue;
101 }
102 if (args[argidx] == "-fine") {
103 opt_expr_args += " -fine";
104 opt_reduce_args += " -fine";
105 continue;
106 }
107 if (args[argidx] == "-full") {
108 opt_expr_args += " -full";
109 opt_reduce_args += " -full";
110 opt_share = true;
111 continue;
112 }
113 if (args[argidx] == "-keepdc") {
114 opt_expr_args += " -keepdc";
115 opt_rmdff_args += " -keepdc";
116 continue;
117 }
118 if (args[argidx] == "-sat") {
119 opt_rmdff_args += " -sat";
120 continue;
121 }
122 if (args[argidx] == "-share_all") {
123 opt_merge_args += " -share_all";
124 continue;
125 }
126 if (args[argidx] == "-fast") {
127 fast_mode = true;
128 continue;
129 }
130 break;
131 }
132 extra_args(args, argidx, design);
133
134 if (fast_mode)
135 {
136 while (1) {
137 Pass::call(design, "opt_expr" + opt_expr_args);
138 Pass::call(design, "opt_merge" + opt_merge_args);
139 design->scratchpad_unset("opt.did_something");
140 Pass::call(design, "opt_rmdff" + opt_rmdff_args);
141 if (design->scratchpad_get_bool("opt.did_something") == false)
142 break;
143 Pass::call(design, "opt_clean" + opt_clean_args);
144 log_header(design, "Rerunning OPT passes. (Removed registers in this run.)\n");
145 }
146 Pass::call(design, "opt_clean" + opt_clean_args);
147 }
148 else
149 {
150 Pass::call(design, "opt_expr" + opt_expr_args);
151 Pass::call(design, "opt_merge -nomux" + opt_merge_args);
152 while (1) {
153 design->scratchpad_unset("opt.did_something");
154 Pass::call(design, "opt_muxtree");
155 Pass::call(design, "opt_reduce" + opt_reduce_args);
156 Pass::call(design, "opt_merge" + opt_merge_args);
157 if (opt_share)
158 Pass::call(design, "opt_share");
159 Pass::call(design, "opt_rmdff" + opt_rmdff_args);
160 Pass::call(design, "opt_clean" + opt_clean_args);
161 Pass::call(design, "opt_expr" + opt_expr_args);
162 if (design->scratchpad_get_bool("opt.did_something") == false)
163 break;
164 log_header(design, "Rerunning OPT passes. (Maybe there is more to do..)\n");
165 }
166 }
167
168 design->optimize();
169 design->sort();
170 design->check();
171
172 log_header(design, fast_mode ? "Finished fast OPT passes.\n" : "Finished OPT passes. (There is nothing left to do.)\n");
173 log_pop();
174 }
175 } OptPass;
176
177 PRIVATE_NAMESPACE_END