From ae4125cbd72cc5932e85a4ee25db10b54d147dc8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien.villemot@ens.fr>
Date: Mon, 11 Oct 2010 12:52:27 +0200
Subject: [PATCH] Preprocessor: don't create auxiliary variables for
 expressions appearing only in unused model local variables (manually
 cherry-picked from e2ce87b7d58944f755bc380f3f60fd7455537a70)

---
 preprocessor/DynamicModel.cc | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 0369f97622..f5b67915ab 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -2681,30 +2681,35 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type)
   ExprNode::subst_table_t subst_table;
   vector<BinaryOpNode *> neweqs;
 
-  // Substitute in model local variables
-  for (map<int, NodeID>::iterator it = local_variables_table.begin();
-       it != local_variables_table.end(); it++)
+  // Substitute in used model local variables
+  set<int> used_local_vars;
+  for (size_t i = 0; i < equations.size(); i++)
+    equations[i]->collectModelLocalVariables(used_local_vars);
+
+  for (set<int>::const_iterator it = used_local_vars.begin();
+       it != used_local_vars.end(); ++it)
     {
+      const NodeID value = local_variables_table.find(*it)->second;
       NodeID subst;
       switch (type)
         {
         case avEndoLead:
-          subst = it->second->substituteEndoLeadGreaterThanTwo(subst_table, neweqs);
+          subst = value->substituteEndoLeadGreaterThanTwo(subst_table, neweqs);
           break;
         case avEndoLag:
-          subst = it->second->substituteEndoLagGreaterThanTwo(subst_table, neweqs);
+          subst = value->substituteEndoLagGreaterThanTwo(subst_table, neweqs);
           break;
         case avExoLead:
-          subst = it->second->substituteExoLead(subst_table, neweqs);
+          subst = value->substituteExoLead(subst_table, neweqs);
           break;
         case avExoLag:
-          subst = it->second->substituteExoLag(subst_table, neweqs);
+          subst = value->substituteExoLag(subst_table, neweqs);
           break;
         default:
           cerr << "DynamicModel::substituteLeadLagInternal: impossible case" << endl;
           exit(EXIT_FAILURE);
         }
-      it->second = subst;
+      local_variables_table[*it] = subst;
     }
 
   // Substitute in equations
-- 
GitLab