2013-01-05 04:13:26 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-01-05 04:13:26 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/bitpattern.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <set>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2021-07-28 16:34:24 -05:00
|
|
|
static bool can_use_fully_defined_pool(RTLIL::SwitchRule *sw)
|
|
|
|
{
|
|
|
|
if (!GetSize(sw->signal))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const RTLIL::SigBit &bit : sw->signal)
|
|
|
|
if (bit.wire == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const RTLIL::CaseRule *cas : sw->cases)
|
|
|
|
for (const RTLIL::SigSpec &sig : cas->compare)
|
|
|
|
if (!sig.is_fully_def())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This replicates the necessary parts of BitPatternPool's interface, but rather
|
|
|
|
// than storing remaining patterns, this explicitly stores which fully-defined
|
|
|
|
// constants have already been matched.
|
|
|
|
struct FullyDefinedPool
|
|
|
|
{
|
|
|
|
FullyDefinedPool(const RTLIL::SigSpec &signal)
|
|
|
|
: max_patterns{signal.size() >= 32 ? 0ul : 1ul << signal.size()}
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool take(RTLIL::SigSpec sig)
|
|
|
|
{
|
|
|
|
if (default_reached || patterns.count(sig))
|
|
|
|
return false;
|
|
|
|
patterns.insert(sig);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void take_all()
|
|
|
|
{
|
|
|
|
default_reached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty()
|
|
|
|
{
|
|
|
|
return default_reached ||
|
|
|
|
(max_patterns && max_patterns == patterns.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
pool<RTLIL::SigSpec> patterns;
|
|
|
|
bool default_reached = false;
|
|
|
|
size_t max_patterns;
|
|
|
|
};
|
|
|
|
|
|
|
|
void proc_rmdead(RTLIL::SwitchRule *sw, int &counter, int &full_case_counter);
|
|
|
|
|
|
|
|
template <class Pool>
|
|
|
|
static void proc_rmdead_impl(RTLIL::SwitchRule *sw, int &counter, int &full_case_counter)
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2021-07-28 16:34:24 -05:00
|
|
|
Pool pool(sw->signal);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (size_t i = 0; i < sw->cases.size(); i++)
|
|
|
|
{
|
2019-04-15 19:52:45 -05:00
|
|
|
bool is_default = GetSize(sw->cases[i]->compare) == 0 && (!pool.empty() || GetSize(sw->signal) == 0);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
for (size_t j = 0; j < sw->cases[i]->compare.size(); j++) {
|
|
|
|
RTLIL::SigSpec sig = sw->cases[i]->compare[j];
|
|
|
|
if (!sig.is_fully_const())
|
|
|
|
continue;
|
|
|
|
if (!pool.take(sig))
|
|
|
|
sw->cases[i]->compare.erase(sw->cases[i]->compare.begin() + (j--));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_default) {
|
|
|
|
if (sw->cases[i]->compare.size() == 0) {
|
|
|
|
delete sw->cases[i];
|
|
|
|
sw->cases.erase(sw->cases.begin() + (i--));
|
|
|
|
counter++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-31 02:57:23 -05:00
|
|
|
// if (pool.empty())
|
|
|
|
// sw->cases[i]->compare.clear();
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto switch_it : sw->cases[i]->switches)
|
2019-04-18 08:07:43 -05:00
|
|
|
proc_rmdead(switch_it, counter, full_case_counter);
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
if (is_default)
|
|
|
|
pool.take_all();
|
|
|
|
}
|
2019-04-18 08:07:43 -05:00
|
|
|
|
2020-03-12 14:57:01 -05:00
|
|
|
if (pool.empty() && !sw->get_bool_attribute(ID::full_case)) {
|
|
|
|
sw->set_bool_attribute(ID::full_case);
|
2019-04-18 08:07:43 -05:00
|
|
|
full_case_counter++;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
2021-07-28 16:34:24 -05:00
|
|
|
void proc_rmdead(RTLIL::SwitchRule *sw, int &counter, int &full_case_counter)
|
|
|
|
{
|
|
|
|
if (can_use_fully_defined_pool(sw))
|
|
|
|
proc_rmdead_impl<FullyDefinedPool>(sw, counter, full_case_counter);
|
|
|
|
else
|
|
|
|
proc_rmdead_impl<BitPatternPool>(sw, counter, full_case_counter);
|
|
|
|
}
|
|
|
|
|
2013-01-05 04:13:26 -06:00
|
|
|
struct ProcRmdeadPass : public Pass {
|
2013-03-01 02:26:29 -06:00
|
|
|
ProcRmdeadPass() : Pass("proc_rmdead", "eliminate dead trees in decision trees") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-03-01 02:26:29 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" proc_rmdead [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass identifies unreachable branches in decision trees and removes them.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2013-01-05 04:13:26 -06:00
|
|
|
{
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing PROC_RMDEAD pass (remove dead branches from decision trees).\n");
|
2013-01-05 04:13:26 -06:00
|
|
|
|
|
|
|
extra_args(args, 1, design);
|
|
|
|
|
|
|
|
int total_counter = 0;
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto mod : design->modules()) {
|
|
|
|
if (!design->selected(mod))
|
2013-03-01 02:26:29 -06:00
|
|
|
continue;
|
2014-07-27 03:41:42 -05:00
|
|
|
for (auto &proc_it : mod->processes) {
|
|
|
|
if (!design->selected(mod, proc_it.second))
|
2013-03-01 02:26:29 -06:00
|
|
|
continue;
|
2019-04-18 08:07:43 -05:00
|
|
|
int counter = 0, full_case_counter = 0;
|
2013-03-01 02:26:29 -06:00
|
|
|
for (auto switch_it : proc_it.second->root_case.switches)
|
2019-04-18 08:07:43 -05:00
|
|
|
proc_rmdead(switch_it, counter, full_case_counter);
|
2013-03-01 02:26:29 -06:00
|
|
|
if (counter > 0)
|
|
|
|
log("Removed %d dead cases from process %s in module %s.\n", counter,
|
2019-04-18 08:07:43 -05:00
|
|
|
log_id(proc_it.first), log_id(mod));
|
|
|
|
if (full_case_counter > 0)
|
|
|
|
log("Marked %d switch rules as full_case in process %s in module %s.\n",
|
|
|
|
full_case_counter, log_id(proc_it.first), log_id(mod));
|
2013-03-01 02:26:29 -06:00
|
|
|
total_counter += counter;
|
|
|
|
}
|
2013-01-05 04:13:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
log("Removed a total of %d dead cases.\n", total_counter);
|
|
|
|
}
|
|
|
|
} ProcRmdeadPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|