yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cost.h
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 #ifndef COST_H
21 #define COST_H
22 
23 #include <kernel/yosys.h>
24 
26 
27 int get_cell_cost(RTLIL::Cell *cell, std::map<RTLIL::Module*, int> *mod_cost_cache = nullptr);
28 
29 int get_cell_cost(RTLIL::IdString type, const std::map<RTLIL::IdString, RTLIL::Const> &parameters = std::map<RTLIL::IdString, RTLIL::Const>(),
30  RTLIL::Design *design = nullptr, std::map<RTLIL::Module*, int> *mod_cost_cache = nullptr)
31 {
32  static std::map<RTLIL::IdString, int> gate_cost = {
33  { "$_BUF_", 1 },
34  { "$_NOT_", 2 },
35  { "$_AND_", 4 },
36  { "$_NAND_", 4 },
37  { "$_OR_", 4 },
38  { "$_NOR_", 4 },
39  { "$_XOR_", 8 },
40  { "$_XNOR_", 8 },
41  { "$_AOI3_", 6 },
42  { "$_OAI3_", 6 },
43  { "$_AOI4_", 8 },
44  { "$_OAI4_", 8 },
45  { "$_MUX_", 4 }
46  };
47 
48  if (gate_cost.count(type))
49  return gate_cost.at(type);
50 
51  if (parameters.empty() && design && design->module(type))
52  {
53  RTLIL::Module *mod = design->module(type);
54 
55  if (mod->attributes.count("\\cost"))
56  return mod->attributes.at("\\cost").as_int();
57 
58  std::map<RTLIL::Module*, int> local_mod_cost_cache;
59  if (mod_cost_cache == nullptr)
60  mod_cost_cache = &local_mod_cost_cache;
61 
62  if (mod_cost_cache->count(mod))
63  return mod_cost_cache->at(mod);
64 
65  int module_cost = 1;
66  for (auto c : mod->cells())
67  module_cost += get_cell_cost(c, mod_cost_cache);
68 
69  (*mod_cost_cache)[mod] = module_cost;
70  return module_cost;
71  }
72 
73  log_warning("Can't determine cost of %s cell (%d parameters).\n", log_id(type), GetSize(parameters));
74  return 1;
75 }
76 
77 int get_cell_cost(RTLIL::Cell *cell, std::map<RTLIL::Module*, int> *mod_cost_cache)
78 {
79  return get_cell_cost(cell->type, cell->parameters, cell->module->design, mod_cost_cache);
80 }
81 
83 
84 #endif
void log_warning(const char *format,...)
Definition: log.cc:196
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
RTLIL::IdString type
Definition: rtlil.h:854
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
YOSYS_NAMESPACE_BEGIN int get_cell_cost(RTLIL::Cell *cell, std::map< RTLIL::Module *, int > *mod_cost_cache=nullptr)
Definition: cost.h:77
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
RTLIL::Design * design
Definition: rtlil.h:589
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283
RTLIL::Module * module
Definition: rtlil.h:852