add LoopToScev maps

llvm-svn: 175295
diff --git a/polly/lib/CodeGen/CodeGeneration.cpp b/polly/lib/CodeGen/CodeGeneration.cpp
index 8398c41..c639c7e 100644
--- a/polly/lib/CodeGen/CodeGeneration.cpp
+++ b/polly/lib/CodeGen/CodeGeneration.cpp
@@ -225,6 +225,19 @@
   // Map the Values from the old code to their counterparts in the new code.
   ValueMapT ValueMap;
 
+  // Map the loops from the old code to expressions function of the induction
+  // variables in the new code.  For example, when the code generator produces
+  // this AST:
+  //
+  //   for (int c1 = 0; c1 <= 1023; c1 += 1)
+  //     for (int c2 = 0; c2 <= 1023; c2 += 1)
+  //       Stmt(c2 + 3, c1);
+  //
+  // LoopToScev is a map associating:
+  //   "outer loop in the old loop nest" -> SCEV("c2 + 3"),
+  //   "inner loop in the old loop nest" -> SCEV("c1").
+  LoopToScevMapT LoopToScev;
+
   // clastVars maps from the textual representation of a clast variable to its
   // current *Value. clast variables are scheduling variables, original
   // induction variables or parameters. They are used either in loop bounds or
@@ -249,11 +262,13 @@
 
   void codegen(const clast_assignment *a, ScopStmt *Statement,
                unsigned Dimension, int vectorDim,
-               std::vector<ValueMapT> *VectorVMap = 0);
+               std::vector<ValueMapT> *VectorVMap = 0,
+               std::vector<LoopToScevMapT> *VLTS = 0);
 
   void codegenSubstitutions(const clast_stmt *Assignment, ScopStmt *Statement,
                             int vectorDim = 0,
-                            std::vector<ValueMapT> *VectorVMap = 0);
+                            std::vector<ValueMapT> *VectorVMap = 0,
+                            std::vector<LoopToScevMapT> *VLTS = 0);
 
   void codegen(const clast_user_stmt *u, std::vector<Value *> *IVS = NULL,
                const char *iterator = NULL, isl_set *scatteringDomain = 0);
@@ -353,7 +368,8 @@
 
 void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt,
                                unsigned Dim, int VectorDim,
-                               std::vector<ValueMapT> *VectorVMap) {
+                               std::vector<ValueMapT> *VectorVMap,
+                               std::vector<LoopToScevMapT> *VLTS) {
   const PHINode *PN;
   Value *RHS;
 
@@ -366,19 +382,24 @@
   if (VectorVMap)
     (*VectorVMap)[VectorDim][PN] = RHS;
 
+  const llvm::SCEV *URHS = S->getSE()->getUnknown(RHS);
+  if (VLTS)
+    (*VLTS)[VectorDim][Stmt->getLoopForDimension(Dim)] = URHS;
+
   ValueMap[PN] = RHS;
+  LoopToScev[Stmt->getLoopForDimension(Dim)] = URHS;
 }
 
 void ClastStmtCodeGen::codegenSubstitutions(
     const clast_stmt *Assignment, ScopStmt *Statement, int vectorDim,
-    std::vector<ValueMapT> *VectorVMap) {
+    std::vector<ValueMapT> *VectorVMap, std::vector<LoopToScevMapT> *VLTS) {
   int Dimension = 0;
 
   while (Assignment) {
     assert(CLAST_STMT_IS_A(Assignment, stmt_ass) &&
            "Substitions are expected to be assignments");
     codegen((const clast_assignment *)Assignment, Statement, Dimension,
-            vectorDim, VectorVMap);
+            vectorDim, VectorVMap, VLTS);
     Assignment = Assignment->next;
     Dimension++;
   }
@@ -409,11 +430,12 @@
   int VectorDimensions = IVS ? IVS->size() : 1;
 
   if (VectorDimensions == 1) {
-    BlockGenerator::generate(Builder, *Statement, ValueMap, P);
+    BlockGenerator::generate(Builder, *Statement, ValueMap, LoopToScev, P);
     return;
   }
 
   VectorValueMapT VectorMap(VectorDimensions);
+  std::vector<LoopToScevMapT> VLTS(VectorDimensions);
 
   if (IVS) {
     assert(u->substitutions && "Substitutions expected!");
@@ -421,13 +443,14 @@
     for (std::vector<Value *>::iterator II = IVS->begin(), IE = IVS->end();
          II != IE; ++II) {
       ClastVars[iterator] = *II;
-      codegenSubstitutions(u->substitutions, Statement, i, &VectorMap);
+      codegenSubstitutions(u->substitutions, Statement, i, &VectorMap, &VLTS);
       i++;
     }
   }
 
   isl_map *Schedule = extractPartialSchedule(Statement, Domain);
-  VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Schedule, P);
+  VectorBlockGenerator::generate(Builder, *Statement, VectorMap, VLTS, Schedule,
+                                 P);
   isl_map_free(Schedule);
 }