}
} // namespace enco
+
+//
+// Dead Bag Elimination
+//
+#include <set>
+
+namespace
+{
+
+// @brief Return true if a given bag is marked as either input or output
+bool is_public(const coco::Bag *b) { return b->isInput() || b->isOutput(); }
+
+// @brief Return the set of "dead" bags in a given module
+std::set<coco::Bag *> dead_bags(const coco::Module *m)
+{
+ std::set<coco::Bag *> res;
+
+ for (uint32_t n = 0; n < m->entity()->bag()->size(); ++n)
+ {
+ auto bag = m->entity()->bag()->at(n);
+
+ if (coco::readers(bag).empty() && !is_public(bag))
+ {
+ res.insert(bag);
+ }
+ }
+
+ return res;
+}
+
+} // namespace
+
+namespace enco
+{
+
+void eliminate_dead_bag(enco::Code *code)
+{
+ auto m = code->module();
+
+ // Destroy a dead bag and its updaters
+ for (auto bag : dead_bags(m))
+ {
+ for (auto updater : coco::updaters(bag))
+ {
+ auto ins = updater->loc();
+
+ assert(ins != nullptr);
+
+ ins->detach();
+ m->entity()->instr()->destroy(ins);
+ }
+
+ bag->replaceWith(nullptr);
+ m->entity()->bag()->destroy(bag);
+ }
+}
+
+} // namespace enco
// TODO Add a comment
void generate_bypass_shuffle(enco::Code *code);
+/**
+ * @brief Eliminate dead bags
+ *
+ * A bag is referred to as dead if it is neither input nor output, and has no read. If a bag is
+ * dead, it is unnecessary to updates its values as these values are never used.
+ *
+ * "eliminate_dead_bag" removes all the dead bags and its updaters from IR.
+ */
+void eliminate_dead_bag(enco::Code *code);
+
} // namespace enco
#endif // __ENCO_OPTIMIZATIONS_H__