Merge pull request #1309 from whitequark/proc_clean-fix-1268
[yosys.git] / passes / proc / proc_rmdead.cc
index d5fbef0d217318e583f0ce474a8b5269aa5adccc..4f40be446827d291805f3f64c4d0482ed63f8794 100644 (file)
@@ -2,11 +2,11 @@
  *  yosys -- Yosys Open SYnthesis Suite
  *
  *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
- *  
+ *
  *  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.
- *  
+ *
  *  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
 #include <sstream>
 #include <stdlib.h>
 #include <stdio.h>
-#include <assert.h>
 #include <set>
 
-static void proc_rmdead(RTLIL::SwitchRule *sw, int &counter)
+USING_YOSYS_NAMESPACE
+PRIVATE_NAMESPACE_BEGIN
+
+void proc_rmdead(RTLIL::SwitchRule *sw, int &counter, int &full_case_counter)
 {
        BitPatternPool pool(sw->signal);
 
        for (size_t i = 0; i < sw->cases.size(); i++)
        {
-               bool is_default = sw->cases[i]->compare.size() == 0 && !pool.empty();
+               bool is_default = GetSize(sw->cases[i]->compare) == 0 && (!pool.empty() || GetSize(sw->signal) == 0);
 
                for (size_t j = 0; j < sw->cases[i]->compare.size(); j++) {
                        RTLIL::SigSpec sig = sw->cases[i]->compare[j];
@@ -49,21 +51,26 @@ static void proc_rmdead(RTLIL::SwitchRule *sw, int &counter)
                                counter++;
                                continue;
                        }
-                       if (pool.empty())
-                               sw->cases[i]->compare.clear();
+                       // if (pool.empty())
+                       //      sw->cases[i]->compare.clear();
                }
 
                for (auto switch_it : sw->cases[i]->switches)
-                       proc_rmdead(switch_it, counter);
+                       proc_rmdead(switch_it, counter, full_case_counter);
 
                if (is_default)
                        pool.take_all();
        }
+
+       if (pool.empty() && !sw->get_bool_attribute("\\full_case")) {
+               sw->set_bool_attribute("\\full_case");
+               full_case_counter++;
+       }
 }
 
 struct ProcRmdeadPass : public Pass {
        ProcRmdeadPass() : Pass("proc_rmdead", "eliminate dead trees in decision trees") { }
-       virtual void help()
+       void help() YS_OVERRIDE
        {
                //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
                log("\n");
@@ -72,25 +79,28 @@ struct ProcRmdeadPass : public Pass {
                log("This pass identifies unreachable branches in decision trees and removes them.\n");
                log("\n");
        }
-       virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
+       void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
        {
-               log_header("Executing PROC_RMDEAD pass (remove dead branches from decision trees).\n");
+               log_header(design, "Executing PROC_RMDEAD pass (remove dead branches from decision trees).\n");
 
                extra_args(args, 1, design);
 
                int total_counter = 0;
-               for (auto &mod_it : design->modules) {
-                       if (!design->selected(mod_it.second))
+               for (auto mod : design->modules()) {
+                       if (!design->selected(mod))
                                continue;
-                       for (auto &proc_it : mod_it.second->processes) {
-                               if (!design->selected(mod_it.second, proc_it.second))
+                       for (auto &proc_it : mod->processes) {
+                               if (!design->selected(mod, proc_it.second))
                                        continue;
-                               int counter = 0;
+                               int counter = 0, full_case_counter = 0;
                                for (auto switch_it : proc_it.second->root_case.switches)
-                                       proc_rmdead(switch_it, counter);
+                                       proc_rmdead(switch_it, counter, full_case_counter);
                                if (counter > 0)
                                        log("Removed %d dead cases from process %s in module %s.\n", counter,
-                                                       proc_it.first.c_str(), mod_it.first.c_str());
+                                                       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));
                                total_counter += counter;
                        }
                }
@@ -98,4 +108,5 @@ struct ProcRmdeadPass : public Pass {
                log("Removed a total of %d dead cases.\n", total_counter);
        }
 } ProcRmdeadPass;
+
+PRIVATE_NAMESPACE_END