2017-09-11 19:18:26 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2017 Claire Xenia Wolf <claire@yosyshq.com>
|
2017-09-11 19:18:26 -05: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.
|
|
|
|
*
|
|
|
|
* 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/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/modtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void demorgan_worker(
|
|
|
|
ModIndex& index,
|
|
|
|
Cell *cell,
|
|
|
|
unsigned int& cells_changed)
|
|
|
|
{
|
|
|
|
SigMap& sigmap = index.sigmap;
|
|
|
|
auto m = cell->module;
|
|
|
|
|
|
|
|
//TODO: Add support for reduce_xor
|
|
|
|
//DeMorgan of XOR is either XOR (if even number of inputs) or XNOR (if odd number)
|
|
|
|
|
2019-08-09 11:58:14 -05:00
|
|
|
if( (cell->type != ID($reduce_and)) && (cell->type != ID($reduce_or)) )
|
2017-09-11 19:18:26 -05:00
|
|
|
return;
|
|
|
|
|
2019-08-15 16:50:10 -05:00
|
|
|
auto insig = sigmap(cell->getPort(ID::A));
|
2024-09-24 08:20:40 -05:00
|
|
|
|
|
|
|
if (GetSize(insig) < 1)
|
|
|
|
return;
|
|
|
|
|
2017-09-14 12:34:45 -05:00
|
|
|
log("Inspecting %s cell %s (%d inputs)\n", log_id(cell->type), log_id(cell->name), GetSize(insig));
|
2017-09-11 19:18:26 -05:00
|
|
|
int num_inverted = 0;
|
2017-09-14 12:34:45 -05:00
|
|
|
for(int i=0; i<GetSize(insig); i++)
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
auto b = insig[i];
|
|
|
|
|
|
|
|
//See if this bit is driven by a $not cell
|
|
|
|
//TODO: do other stuff like nor/nand?
|
|
|
|
pool<ModIndex::PortInfo> ports = index.query_ports(b);
|
|
|
|
bool inverted = false;
|
|
|
|
for(auto x : ports)
|
|
|
|
{
|
2019-08-15 16:50:10 -05:00
|
|
|
if(x.port == ID::Y && x.cell->type == ID($_NOT_))
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
inverted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inverted)
|
|
|
|
num_inverted ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Stop if less than half of the inputs are inverted
|
2017-09-14 12:34:45 -05:00
|
|
|
if(num_inverted*2 < GetSize(insig))
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
2017-09-14 12:34:45 -05:00
|
|
|
log(" %d / %d inputs are inverted, not pushing\n", num_inverted, GetSize(insig));
|
2017-09-11 19:18:26 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//More than half of the inputs are inverted! Push through
|
|
|
|
cells_changed ++;
|
2017-09-14 12:34:45 -05:00
|
|
|
log(" %d / %d inputs are inverted, pushing inverter through reduction\n", num_inverted, GetSize(insig));
|
2017-09-11 19:18:26 -05:00
|
|
|
|
|
|
|
//For each input, either add or remove the inverter as needed
|
|
|
|
//TODO: this duplicates the loop up above, can we refactor it?
|
2017-09-14 12:34:45 -05:00
|
|
|
for(int i=0; i<GetSize(insig); i++)
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
auto b = insig[i];
|
|
|
|
|
|
|
|
//See if this bit is driven by a $not cell
|
|
|
|
//TODO: do other stuff like nor/nand?
|
|
|
|
pool<ModIndex::PortInfo> ports = index.query_ports(b);
|
|
|
|
RTLIL::Cell* srcinv = NULL;
|
|
|
|
for(auto x : ports)
|
|
|
|
{
|
2019-08-15 16:50:10 -05:00
|
|
|
if(x.port == ID::Y && x.cell->type == ID($_NOT_))
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
srcinv = x.cell;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//We are NOT inverted! Add an inverter
|
|
|
|
if(!srcinv)
|
|
|
|
{
|
|
|
|
auto inverted_b = m->addWire(NEW_ID);
|
|
|
|
m->addNot(NEW_ID, RTLIL::SigSpec(b), RTLIL::SigSpec(inverted_b));
|
|
|
|
insig[i] = inverted_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
//We ARE inverted - bypass it
|
|
|
|
//Don't automatically delete the inverter since other stuff might still use it
|
|
|
|
else
|
2019-08-15 16:50:10 -05:00
|
|
|
insig[i] = srcinv->getPort(ID::A);
|
2017-09-11 19:18:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Cosmetic fixup: If our input is just a scrambled version of one bus, rearrange it
|
|
|
|
//Reductions are all commutative, so there's no point in having them in a weird order
|
|
|
|
bool same_signal = true;
|
|
|
|
RTLIL::Wire* srcwire = insig[0].wire;
|
2017-09-14 12:34:45 -05:00
|
|
|
dict<int, int> seen_bits;
|
|
|
|
for(int i=0; i<GetSize(insig); i++)
|
2017-09-11 19:18:26 -05:00
|
|
|
seen_bits[i] = 0;
|
2017-09-14 12:34:45 -05:00
|
|
|
for(int i=0; i<GetSize(insig); i++)
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
seen_bits[insig[i].offset] ++;
|
|
|
|
if(insig[i].wire != srcwire)
|
|
|
|
{
|
|
|
|
same_signal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(same_signal)
|
|
|
|
{
|
|
|
|
//Make sure we've seen every bit exactly once
|
|
|
|
bool every_bit_once = true;
|
2017-09-14 12:34:45 -05:00
|
|
|
for(int i=0; i<GetSize(insig); i++)
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
if(seen_bits[i] != 1)
|
|
|
|
{
|
|
|
|
every_bit_once = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//All good? Just use the whole wire as-is without any reordering
|
|
|
|
//We do have to swap MSB to LSB b/c that's the way the reduction cells seem to work?
|
|
|
|
//Unclear on why this isn't sorting properly
|
|
|
|
//TODO: can we do SigChunks instead of single bits if we have subsets of a bus?
|
2017-09-14 12:34:45 -05:00
|
|
|
if(every_bit_once && (GetSize(insig) == srcwire->width) )
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
log("Rearranging bits\n");
|
|
|
|
RTLIL::SigSpec newsig;
|
2017-09-14 12:34:45 -05:00
|
|
|
for(int i=0; i<GetSize(insig); i++)
|
|
|
|
newsig.append(RTLIL::SigBit(srcwire, GetSize(insig) - i - 1));
|
2017-09-11 19:18:26 -05:00
|
|
|
insig = newsig;
|
|
|
|
insig.sort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Push the new input signal back to the reduction (after bypassing/adding inverters)
|
2019-08-15 16:50:10 -05:00
|
|
|
cell->setPort(ID::A, insig);
|
2017-09-11 19:18:26 -05:00
|
|
|
|
|
|
|
//Change the cell type
|
2019-08-09 11:58:14 -05:00
|
|
|
if(cell->type == ID($reduce_and))
|
|
|
|
cell->type = ID($reduce_or);
|
|
|
|
else if(cell->type == ID($reduce_or))
|
|
|
|
cell->type = ID($reduce_and);
|
2017-09-11 19:18:26 -05:00
|
|
|
//don't change XOR
|
|
|
|
|
|
|
|
//Add an inverter to the output
|
2019-08-15 16:50:10 -05:00
|
|
|
auto inverted_output = cell->getPort(ID::Y);
|
2017-09-11 19:18:26 -05:00
|
|
|
auto uninverted_output = m->addWire(NEW_ID);
|
|
|
|
m->addNot(NEW_ID, RTLIL::SigSpec(uninverted_output), inverted_output);
|
2019-08-15 16:50:10 -05:00
|
|
|
cell->setPort(ID::Y, uninverted_output);
|
2017-09-11 19:18:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct OptDemorganPass : public Pass {
|
|
|
|
OptDemorganPass() : Pass("opt_demorgan", "Optimize reductions with DeMorgan equivalents") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" opt_demorgan [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass pushes inverters through $reduce_* cells if this will reduce the\n");
|
|
|
|
log("overall gate count of the circuit\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2017-09-11 19:18:26 -05:00
|
|
|
{
|
|
|
|
log_header(design, "Executing OPT_DEMORGAN pass (push inverters through $reduce_* cells).\n");
|
|
|
|
|
2024-04-03 03:02:53 -05:00
|
|
|
int argidx = 1;
|
2017-09-14 12:34:45 -05:00
|
|
|
extra_args(args, argidx, design);
|
2017-09-11 19:18:26 -05:00
|
|
|
|
|
|
|
unsigned int cells_changed = 0;
|
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
{
|
|
|
|
ModIndex index(module);
|
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
demorgan_worker(index, cell, cells_changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cells_changed)
|
|
|
|
log("Pushed inverters through %u reductions\n", cells_changed);
|
|
|
|
}
|
|
|
|
} OptDemorganPass;
|
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|