yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FsmExpand Struct Reference
+ Collaboration diagram for FsmExpand:

Public Member Functions

bool is_cell_merge_candidate (RTLIL::Cell *cell)
 
void create_current_set ()
 
void optimze_as_needed ()
 
void merge_cell_into_fsm (RTLIL::Cell *cell)
 
 FsmExpand (RTLIL::Cell *cell, RTLIL::Design *design, RTLIL::Module *mod)
 
void execute ()
 

Data Fields

RTLIL::Modulemodule
 
RTLIL::Cellfsm_cell
 
SigMap assign_map
 
SigSet< RTLIL::Cell
*, RTLIL::sort_by_name_id
< RTLIL::Cell > > 
sig2driver
 
SigSet< RTLIL::Cell
*, RTLIL::sort_by_name_id
< RTLIL::Cell > > 
sig2user
 
CellTypes ct
 
std::set< RTLIL::Cell
*, RTLIL::sort_by_name_id
< RTLIL::Cell > > 
merged_set
 
std::set< RTLIL::Cell
*, RTLIL::sort_by_name_id
< RTLIL::Cell > > 
current_set
 
std::set< RTLIL::Cell
*, RTLIL::sort_by_name_id
< RTLIL::Cell > > 
no_candidate_set
 
bool already_optimized
 
int limit_transitions
 

Detailed Description

Definition at line 31 of file fsm_expand.cc.

Constructor & Destructor Documentation

FsmExpand::FsmExpand ( RTLIL::Cell cell,
RTLIL::Design design,
RTLIL::Module mod 
)
inline

Definition at line 203 of file fsm_expand.cc.

204  {
205  module = mod;
206  fsm_cell = cell;
207 
210 
211  for (auto &cell_it : module->cells_) {
212  RTLIL::Cell *c = cell_it.second;
213  if (ct.cell_known(c->type) && design->selected(mod, c))
214  for (auto &p : c->connections()) {
215  if (ct.cell_output(c->type, p.first))
216  sig2driver.insert(assign_map(p.second), c);
217  else
218  sig2user.insert(assign_map(p.second), c);
219  }
220  }
221  }
bool selected(T1 *module) const
Definition: rtlil.h:551
SigMap assign_map
Definition: fsm_expand.cc:35
RTLIL::Cell * fsm_cell
Definition: fsm_expand.cc:34
RTLIL::IdString type
Definition: rtlil.h:854
SigSet< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > sig2user
Definition: fsm_expand.cc:36
void set(RTLIL::Module *module)
Definition: sigtools.h:273
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
SigSet< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > sig2driver
Definition: fsm_expand.cc:36
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
void setup_internals()
Definition: celltypes.h:83
CellTypes ct
Definition: fsm_expand.cc:37
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814
RTLIL::Module * module
Definition: fsm_expand.cc:33
void insert(RTLIL::SigSpec sig, T data)
Definition: sigtools.h:152

+ Here is the call graph for this function:

Member Function Documentation

void FsmExpand::create_current_set ( )
inline

Definition at line 85 of file fsm_expand.cc.

86  {
87  std::vector<RTLIL::Cell*> cell_list;
88 
89  for (auto c : sig2driver.find(assign_map(fsm_cell->getPort("\\CTRL_IN"))))
90  cell_list.push_back(c);
91 
92  for (auto c : sig2user.find(assign_map(fsm_cell->getPort("\\CTRL_OUT"))))
93  cell_list.push_back(c);
94 
95  current_set.clear();
96  for (auto c : cell_list)
97  {
98  if (merged_set.count(c) > 0 || current_set.count(c) > 0 || no_candidate_set.count(c) > 0)
99  continue;
100  for (auto &p : c->connections()) {
101  if (p.first != "\\A" && p.first != "\\B" && p.first != "\\S" && p.first != "\\Y")
102  goto next_cell;
103  }
104  if (!is_cell_merge_candidate(c)) {
105  no_candidate_set.insert(c);
106  continue;
107  }
108  current_set.insert(c);
109  next_cell:;
110  }
111  }
SigMap assign_map
Definition: fsm_expand.cc:35
RTLIL::Cell * fsm_cell
Definition: fsm_expand.cc:34
std::set< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > no_candidate_set
Definition: fsm_expand.cc:41
SigSet< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > sig2user
Definition: fsm_expand.cc:36
SigSet< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > sig2driver
Definition: fsm_expand.cc:36
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
bool is_cell_merge_candidate(RTLIL::Cell *cell)
Definition: fsm_expand.cc:46
std::set< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > current_set
Definition: fsm_expand.cc:40
std::set< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > merged_set
Definition: fsm_expand.cc:39
void find(RTLIL::SigSpec sig, std::set< T > &result)
Definition: sigtools.h:187

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FsmExpand::execute ( )
inline

Definition at line 223 of file fsm_expand.cc.

224  {
225  log("\n");
226  log("Expanding FSM `%s' from module `%s':\n", fsm_cell->name.c_str(), module->name.c_str());
227 
228  already_optimized = false;
229  limit_transitions = 16 * fsm_cell->parameters["\\TRANS_NUM"].as_int();
230 
231  for (create_current_set(); current_set.size() > 0; create_current_set()) {
232  for (auto c : current_set)
234  }
235 
236  for (auto c : merged_set)
237  module->remove(c);
238 
239  if (merged_set.size() > 0 && !already_optimized)
241 
242  log(" merged %d cells into FSM.\n", GetSize(merged_set));
243  }
const char * c_str() const
Definition: rtlil.h:178
void merge_cell_into_fsm(RTLIL::Cell *cell)
Definition: fsm_expand.cc:131
int limit_transitions
Definition: fsm_expand.cc:44
RTLIL::Cell * fsm_cell
Definition: fsm_expand.cc:34
bool already_optimized
Definition: fsm_expand.cc:43
RTLIL::IdString name
Definition: rtlil.h:853
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
void create_current_set()
Definition: fsm_expand.cc:85
static void optimize_fsm(RTLIL::Cell *cell, RTLIL::Module *module)
Definition: fsm_opt.cc:317
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::IdString name
Definition: rtlil.h:599
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
void log(const char *format,...)
Definition: log.cc:180
std::set< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > current_set
Definition: fsm_expand.cc:40
std::set< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > merged_set
Definition: fsm_expand.cc:39
RTLIL::Module * module
Definition: fsm_expand.cc:33

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool FsmExpand::is_cell_merge_candidate ( RTLIL::Cell cell)
inline

Definition at line 46 of file fsm_expand.cc.

47  {
48  if (cell->type == "$mux" || cell->type == "$pmux")
49  if (cell->getPort("\\A").size() < 2)
50  return true;
51 
52  RTLIL::SigSpec new_signals;
53  if (cell->hasPort("\\A"))
54  new_signals.append(assign_map(cell->getPort("\\A")));
55  if (cell->hasPort("\\B"))
56  new_signals.append(assign_map(cell->getPort("\\B")));
57  if (cell->hasPort("\\S"))
58  new_signals.append(assign_map(cell->getPort("\\S")));
59  if (cell->hasPort("\\Y"))
60  new_signals.append(assign_map(cell->getPort("\\Y")));
61 
62  new_signals.sort_and_unify();
63  new_signals.remove_const();
64 
65  new_signals.remove(assign_map(fsm_cell->getPort("\\CTRL_IN")));
66  new_signals.remove(assign_map(fsm_cell->getPort("\\CTRL_OUT")));
67 
68  if (new_signals.size() > 3)
69  return false;
70 
71  if (cell->hasPort("\\Y")) {
72  new_signals.append(assign_map(cell->getPort("\\Y")));
73  new_signals.sort_and_unify();
74  new_signals.remove_const();
75  new_signals.remove(assign_map(fsm_cell->getPort("\\CTRL_IN")));
76  new_signals.remove(assign_map(fsm_cell->getPort("\\CTRL_OUT")));
77  }
78 
79  if (new_signals.size() > 2)
80  return false;
81 
82  return true;
83  }
SigMap assign_map
Definition: fsm_expand.cc:35
RTLIL::Cell * fsm_cell
Definition: fsm_expand.cc:34
RTLIL::IdString type
Definition: rtlil.h:854
int size() const
Definition: rtlil.h:1019
void remove(const RTLIL::SigSpec &pattern)
Definition: rtlil.cc:2342
RTLIL_ATTRIBUTE_MEMBERS bool hasPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1766
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
void remove_const()
Definition: rtlil.cc:2464
void sort_and_unify()
Definition: rtlil.cc:2291
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FsmExpand::merge_cell_into_fsm ( RTLIL::Cell cell)
inline

Definition at line 131 of file fsm_expand.cc.

132  {
134 
135  log(" merging %s cell %s.\n", cell->type.c_str(), cell->name.c_str());
136  merged_set.insert(cell);
137  already_optimized = false;
138 
139  RTLIL::SigSpec input_sig, output_sig;
140 
141  for (auto &p : cell->connections())
142  if (ct.cell_output(cell->type, p.first))
143  output_sig.append(assign_map(p.second));
144  else
145  input_sig.append(assign_map(p.second));
146  input_sig.sort_and_unify();
147  input_sig.remove_const();
148 
149  std::vector<RTLIL::Const> truth_tab;
150 
151  for (int i = 0; i < (1 << input_sig.size()); i++) {
152  RTLIL::Const in_val(i, input_sig.size());
153  RTLIL::SigSpec A, B, S;
154  if (cell->hasPort("\\A"))
155  A = assign_map(cell->getPort("\\A"));
156  if (cell->hasPort("\\B"))
157  B = assign_map(cell->getPort("\\B"));
158  if (cell->hasPort("\\S"))
159  S = assign_map(cell->getPort("\\S"));
160  A.replace(input_sig, RTLIL::SigSpec(in_val));
161  B.replace(input_sig, RTLIL::SigSpec(in_val));
162  S.replace(input_sig, RTLIL::SigSpec(in_val));
163  log_assert(A.is_fully_const());
164  log_assert(B.is_fully_const());
165  log_assert(S.is_fully_const());
166  truth_tab.push_back(ct.eval(cell, A.as_const(), B.as_const(), S.as_const()));
167  }
168 
169  FsmData fsm_data;
170  fsm_data.copy_from_cell(fsm_cell);
171 
172  fsm_data.num_inputs += input_sig.size();
173  RTLIL::SigSpec new_ctrl_in = fsm_cell->getPort("\\CTRL_IN");
174  new_ctrl_in.append(input_sig);
175  fsm_cell->setPort("\\CTRL_IN", new_ctrl_in);
176 
177  fsm_data.num_outputs += output_sig.size();
178  RTLIL::SigSpec new_ctrl_out = fsm_cell->getPort("\\CTRL_OUT");
179  new_ctrl_out.append(output_sig);
180  fsm_cell->setPort("\\CTRL_OUT", new_ctrl_out);
181 
182  std::vector<FsmData::transition_t> new_transition_table;
183  for (auto &tr : fsm_data.transition_table) {
184  for (int i = 0; i < (1 << input_sig.size()); i++) {
185  FsmData::transition_t new_tr = tr;
186  RTLIL::Const in_val(i, input_sig.size());
187  RTLIL::Const out_val = truth_tab[i];
188  RTLIL::SigSpec ctrl_in = new_tr.ctrl_in;
189  RTLIL::SigSpec ctrl_out = new_tr.ctrl_out;
190  ctrl_in.append(in_val);
191  ctrl_out.append(out_val);
192  new_tr.ctrl_in = ctrl_in.as_const();
193  new_tr.ctrl_out = ctrl_out.as_const();
194  new_transition_table.push_back(new_tr);
195  }
196  }
197  fsm_data.transition_table.swap(new_transition_table);
198  new_transition_table.clear();
199 
200  fsm_data.copy_to_cell(fsm_cell);
201  }
const char * c_str() const
Definition: rtlil.h:178
SigMap assign_map
Definition: fsm_expand.cc:35
std::vector< transition_t > transition_table
Definition: fsmdata.h:31
RTLIL::Const as_const() const
Definition: rtlil.cc:2857
RTLIL::Cell * fsm_cell
Definition: fsm_expand.cc:34
bool already_optimized
Definition: fsm_expand.cc:43
RTLIL::IdString name
Definition: rtlil.h:853
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
void copy_to_cell(RTLIL::Cell *cell)
Definition: fsmdata.h:34
RTLIL::IdString type
Definition: rtlil.h:854
int size() const
Definition: rtlil.h:1019
RTLIL::Const ctrl_out
Definition: fsmdata.h:30
void optimze_as_needed()
Definition: fsm_expand.cc:113
RTLIL::Const ctrl_in
Definition: fsmdata.h:30
RTLIL_ATTRIBUTE_MEMBERS bool hasPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1766
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
int num_inputs
Definition: fsmdata.h:29
static RTLIL::Const eval(RTLIL::IdString type, const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
Definition: celltypes.h:219
#define log_assert(_assert_expr_)
Definition: log.h:85
void remove_const()
Definition: rtlil.cc:2464
void sort_and_unify()
Definition: rtlil.cc:2291
void log(const char *format,...)
Definition: log.cc:180
void copy_from_cell(RTLIL::Cell *cell)
Definition: fsmdata.h:79
int num_outputs
Definition: fsmdata.h:29
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
CellTypes ct
Definition: fsm_expand.cc:37
std::set< RTLIL::Cell *, RTLIL::sort_by_name_id< RTLIL::Cell > > merged_set
Definition: fsm_expand.cc:39
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FsmExpand::optimze_as_needed ( )
inline

Definition at line 113 of file fsm_expand.cc.

114  {
115  if (already_optimized)
116  return;
117 
118  int trans_num = fsm_cell->parameters["\\TRANS_NUM"].as_int();
119  if (trans_num > limit_transitions)
120  {
121  log(" grown transition table to %d entries -> optimize.\n", trans_num);
123  already_optimized = true;
124 
125  trans_num = fsm_cell->parameters["\\TRANS_NUM"].as_int();
126  log(" transition table size after optimizaton: %d\n", trans_num);
127  limit_transitions = 16 * trans_num;
128  }
129  }
int limit_transitions
Definition: fsm_expand.cc:44
RTLIL::Cell * fsm_cell
Definition: fsm_expand.cc:34
bool already_optimized
Definition: fsm_expand.cc:43
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
static void optimize_fsm(RTLIL::Cell *cell, RTLIL::Module *module)
Definition: fsm_opt.cc:317
void log(const char *format,...)
Definition: log.cc:180
RTLIL::Module * module
Definition: fsm_expand.cc:33

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

bool FsmExpand::already_optimized

Definition at line 43 of file fsm_expand.cc.

SigMap FsmExpand::assign_map

Definition at line 35 of file fsm_expand.cc.

CellTypes FsmExpand::ct

Definition at line 37 of file fsm_expand.cc.

std::set<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell> > FsmExpand::current_set

Definition at line 40 of file fsm_expand.cc.

RTLIL::Cell* FsmExpand::fsm_cell

Definition at line 34 of file fsm_expand.cc.

int FsmExpand::limit_transitions

Definition at line 44 of file fsm_expand.cc.

std::set<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell> > FsmExpand::merged_set

Definition at line 39 of file fsm_expand.cc.

RTLIL::Module* FsmExpand::module

Definition at line 33 of file fsm_expand.cc.

std::set<RTLIL::Cell*, RTLIL::sort_by_name_id<RTLIL::Cell> > FsmExpand::no_candidate_set

Definition at line 41 of file fsm_expand.cc.

Definition at line 36 of file fsm_expand.cc.

Definition at line 36 of file fsm_expand.cc.


The documentation for this struct was generated from the following file: