From c0724a7e97f024a1d113038272fa3233366a36fb Mon Sep 17 00:00:00 2001
From: David Shah <davey1576@gmail.com>
Date: Wed, 27 Jun 2018 16:24:44 +0200
Subject: ice40: Only pack up to one SB_CARRY into a LC

Signed-off-by: David Shah <davey1576@gmail.com>
---
 ice40/pack.cc | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/ice40/pack.cc b/ice40/pack.cc
index 9191255d..38542f59 100644
--- a/ice40/pack.cc
+++ b/ice40/pack.cc
@@ -128,7 +128,7 @@ static bool net_is_constant(const Context *ctx, NetInfo *net, bool &value)
 static void pack_carries(Context *ctx)
 {
     log_info("Packing carries..\n");
-
+    std::unordered_set<IdString> exhausted_cells;
     std::unordered_set<IdString> packed_cells;
     std::vector<std::unique_ptr<CellInfo>> new_cells;
 
@@ -153,7 +153,8 @@ static void pack_carries(Context *ctx)
             if (i0_net) {
                 for (auto usr : i0_net->users) {
                     if (is_lc(ctx, usr.cell) && usr.port == ctx->id("I1")) {
-                        if (ctx->cells.find(usr.cell->name) != ctx->cells.end()) {
+                        if (ctx->cells.find(usr.cell->name) != ctx->cells.end() &&
+                            exhausted_cells.find(usr.cell->name) == exhausted_cells.end()) {
                             // This clause stops us double-packing cells
                             i0_matches.insert(usr.cell->name);
                             if (!i1_net) {
@@ -161,14 +162,14 @@ static void pack_carries(Context *ctx)
                                 i1_matches.insert(usr.cell->name);
                             }
                         }
-
                     }
                 }
             }
             if (i1_net) {
                 for (auto usr : i1_net->users) {
                     if (is_lc(ctx, usr.cell) && usr.port == ctx->id("I2")) {
-                        if (ctx->cells.find(usr.cell->name) != ctx->cells.end()) {
+                        if (ctx->cells.find(usr.cell->name) != ctx->cells.end() &&
+                            exhausted_cells.find(usr.cell->name) == exhausted_cells.end()) {
                             // This clause stops us double-packing cells
                             i1_matches.insert(usr.cell->name);
                             if (!i0_net) {
@@ -242,6 +243,7 @@ static void pack_carries(Context *ctx)
                             }));
                 }
             }
+            exhausted_cells.insert(carry_lc->name);
         }
     }
     for (auto pcell : packed_cells) {
@@ -381,7 +383,6 @@ static void pack_io(Context *ctx)
 {
     std::unordered_set<IdString> packed_cells;
     std::vector<std::unique_ptr<CellInfo>> new_cells;
-
     log_info("Packing IOs..\n");
 
     for (auto cell : sorted(ctx->cells)) {
-- 
cgit v1.2.3