yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
opt.cc
Go to the documentation of this file.
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 
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 [-mux_undef] [-mux_bool] [-undriven] [-fine] [-full] [-keepdc]\n");
41  log(" opt_share -nomux\n");
42  log("\n");
43  log(" do\n");
44  log(" opt_muxtree\n");
45  log(" opt_reduce [-fine] [-full]\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] [-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_const [-mux_undef] [-mux_bool] [-undriven] [-fine] [-full] [-keepdc]\n");
56  log(" opt_share\n");
57  log(" opt_rmdff\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  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
67  {
68  std::string opt_clean_args;
69  std::string opt_const_args;
70  std::string opt_reduce_args;
71  bool fast_mode = false;
72 
73  log_header("Executing OPT pass (performing simple optimizations).\n");
74  log_push();
75 
76  size_t argidx;
77  for (argidx = 1; argidx < args.size(); argidx++) {
78  if (args[argidx] == "-purge") {
79  opt_clean_args += " -purge";
80  continue;
81  }
82  if (args[argidx] == "-mux_undef") {
83  opt_const_args += " -mux_undef";
84  continue;
85  }
86  if (args[argidx] == "-mux_bool") {
87  opt_const_args += " -mux_bool";
88  continue;
89  }
90  if (args[argidx] == "-undriven") {
91  opt_const_args += " -undriven";
92  continue;
93  }
94  if (args[argidx] == "-fine") {
95  opt_const_args += " -fine";
96  opt_reduce_args += " -fine";
97  continue;
98  }
99  if (args[argidx] == "-full") {
100  opt_const_args += " -full";
101  opt_reduce_args += " -full";
102  continue;
103  }
104  if (args[argidx] == "-keepdc") {
105  opt_const_args += " -keepdc";
106  continue;
107  }
108  if (args[argidx] == "-fast") {
109  fast_mode = true;
110  continue;
111  }
112  break;
113  }
114  extra_args(args, argidx, design);
115 
116  if (fast_mode)
117  {
118  while (1) {
119  Pass::call(design, "opt_const" + opt_const_args);
120  Pass::call(design, "opt_share");
121  design->scratchpad_unset("opt.did_something");
122  Pass::call(design, "opt_rmdff");
123  if (design->scratchpad_get_bool("opt.did_something") == false)
124  break;
125  Pass::call(design, "opt_clean" + opt_clean_args);
126  log_header("Rerunning OPT passes. (Removed registers in this run.)\n");
127  }
128  Pass::call(design, "opt_clean" + opt_clean_args);
129  }
130  else
131  {
132  Pass::call(design, "opt_const" + opt_const_args);
133  Pass::call(design, "opt_share -nomux");
134  while (1) {
135  design->scratchpad_unset("opt.did_something");
136  Pass::call(design, "opt_muxtree");
137  Pass::call(design, "opt_reduce" + opt_reduce_args);
138  Pass::call(design, "opt_share");
139  Pass::call(design, "opt_rmdff");
140  Pass::call(design, "opt_clean" + opt_clean_args);
141  Pass::call(design, "opt_const" + opt_const_args);
142  if (design->scratchpad_get_bool("opt.did_something") == false)
143  break;
144  log_header("Rerunning OPT passes. (Maybe there is more to do..)\n");
145  }
146  }
147 
148  log_header(fast_mode ? "Finished fast OPT passes." : "Finished OPT passes. (There is nothing left to do.)\n");
149  log_pop();
150  }
151 } OptPass;
152 
OptPass()
Definition: opt.cc:29
void log_header(const char *format,...)
Definition: log.cc:188
void scratchpad_unset(std::string varname)
Definition: rtlil.cc:286
void log_pop()
Definition: log.cc:237
virtual void help()
Definition: opt.cc:30
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: opt.cc:66
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
bool scratchpad_get_bool(std::string varname, bool default_value=false) const
Definition: rtlil.cc:324
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
OptPass OptPass
Definition: opt.cc:28
void log(const char *format,...)
Definition: log.cc:180
void log_push()
Definition: log.cc:232
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
static void call(RTLIL::Design *design, std::string command)
Definition: register.cc:146