2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
23 #include "kernel/yosys.h"
27 int get_cell_cost(RTLIL::Cell
*cell
, dict
<RTLIL::IdString
, int> *mod_cost_cache
= nullptr);
29 inline int get_cell_cost(RTLIL::IdString type
, const dict
<RTLIL::IdString
, RTLIL::Const
> ¶meters
= dict
<RTLIL::IdString
, RTLIL::Const
>(),
30 RTLIL::Design
*design
= nullptr, dict
<RTLIL::IdString
, int> *mod_cost_cache
= nullptr)
32 static dict
<RTLIL::IdString
, int> gate_cost
= {
50 if (gate_cost
.count(type
))
51 return gate_cost
.at(type
);
53 if (parameters
.empty() && design
&& design
->module(type
))
55 RTLIL::Module
*mod
= design
->module(type
);
57 if (mod
->attributes
.count("\\cost"))
58 return mod
->attributes
.at("\\cost").as_int();
60 dict
<RTLIL::IdString
, int> local_mod_cost_cache
;
61 if (mod_cost_cache
== nullptr)
62 mod_cost_cache
= &local_mod_cost_cache
;
64 if (mod_cost_cache
->count(mod
->name
))
65 return mod_cost_cache
->at(mod
->name
);
68 for (auto c
: mod
->cells())
69 module_cost
+= get_cell_cost(c
, mod_cost_cache
);
71 (*mod_cost_cache
)[mod
->name
] = module_cost
;
75 log_warning("Can't determine cost of %s cell (%d parameters).\n", log_id(type
), GetSize(parameters
));
79 inline int get_cell_cost(RTLIL::Cell
*cell
, dict
<RTLIL::IdString
, int> *mod_cost_cache
)
81 return get_cell_cost(cell
->type
, cell
->parameters
, cell
->module
->design
, mod_cost_cache
);