mirror of
https://github.com/YosysHQ/yosys
synced 2026-07-18 13:15:46 +00:00
This commit introduces the negopt pass with pre/post optimization modes for handling negation patterns in arithmetic circuits. Pre-optimization patterns (expose for tree balancing): - manual2sub: (a + ~b) + 1 → a - b - sub2neg: a - b → a + (-b) - negexpand: -(a + b) → (-a) + (-b) [with output width fix] - negneg: -(-a) → a - negmux: -(s ? a : b) → s ? (-a) : (-b) Post-optimization patterns (cleanup/rebuild): - negrebuild: (-a) + (-b) → -(a + b) - muxneg: s ? (-a) : (-b) → -(s ? a : b) - neg2sub: a + (-b) → a - b All patterns use nusers() for fanout checking (standard Yosys style). Comprehensive test coverage with positive/negative cases and formal verification via equiv_opt.
60 lines
1.2 KiB
Text
60 lines
1.2 KiB
Text
log -header "Simple positive case"
|
|
log -push
|
|
design -reset
|
|
read_verilog <<EOF
|
|
module top(a, y);
|
|
input wire signed [7:0] a;
|
|
output wire signed [7:0] y;
|
|
assign y = -(-a);
|
|
endmodule
|
|
EOF
|
|
proc; opt
|
|
check -assert
|
|
equiv_opt -assert negopt -pre
|
|
design -load postopt
|
|
select -assert-none t:$neg
|
|
design -reset
|
|
log -pop
|
|
|
|
log -header "With intermediate signal"
|
|
log -push
|
|
design -reset
|
|
read_verilog <<EOF
|
|
module top(a, y);
|
|
input wire signed [7:0] a;
|
|
output wire signed [7:0] y;
|
|
wire signed [7:0] neg_a;
|
|
assign neg_a = -a;
|
|
assign y = -neg_a;
|
|
endmodule
|
|
EOF
|
|
proc; opt
|
|
check -assert
|
|
equiv_opt -assert negopt -pre
|
|
design -load postopt
|
|
select -assert-none t:$neg
|
|
design -reset
|
|
log -pop
|
|
|
|
log -header "Negative case: extra fanout on inner neg"
|
|
log -push
|
|
design -reset
|
|
read_verilog <<EOF
|
|
module top(a, y, z);
|
|
input wire signed [7:0] a;
|
|
output wire signed [7:0] y;
|
|
output wire signed [7:0] z;
|
|
(* keep *) wire signed [7:0] neg_a;
|
|
assign neg_a = -a;
|
|
assign y = -neg_a;
|
|
assign z = neg_a;
|
|
endmodule
|
|
EOF
|
|
proc; opt
|
|
check -assert
|
|
equiv_opt -assert negopt -pre
|
|
design -load postopt
|
|
# Should NOT transform due to extra fanout on inner neg output
|
|
select -assert-count 2 t:$neg
|
|
design -reset
|
|
log -pop
|