[llvm-commits] CVS: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Chris Lattner lattner at cs.uiuc.edu
Tue Oct 11 11:41:15 PDT 2005



Changes in directory llvm/lib/Transforms/Scalar:

LoopStrengthReduce.cpp updated: 1.67 -> 1.68
---
Log message:

Fix (hopefully the last) issue where LSR is nondeterminstic.  When pulling
out CSE's of base expressions it could build a result whose order was 
nondet.


---
Diffs of the changes:  (+14 -8)

 LoopStrengthReduce.cpp |   22 ++++++++++++++--------
 1 files changed, 14 insertions(+), 8 deletions(-)


Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
diff -u llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:1.67 llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:1.68
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:1.67	Tue Oct 11 13:30:57 2005
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp	Tue Oct 11 13:41:04 2005
@@ -715,6 +715,10 @@
   // If any subexpressions are used Uses.size() times, they are common.
   std::map<SCEVHandle, unsigned> SubExpressionUseCounts;
   
+  // UniqueSubExprs - Keep track of all of the subexpressions we see in the
+  // order we see them.
+  std::vector<SCEVHandle> UniqueSubExprs;
+
   std::vector<SCEVHandle> SubExprs;
   for (unsigned i = 0; i != NumUses; ++i) {
     // If the base is zero (which is common), return zero now, there are no
@@ -725,22 +729,24 @@
     SeparateSubExprs(SubExprs, Uses[i].Base);
     // Add one to SubExpressionUseCounts for each subexpr present.
     for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
-      SubExpressionUseCounts[SubExprs[j]]++;
+      if (++SubExpressionUseCounts[SubExprs[j]] == 1)
+        UniqueSubExprs.push_back(SubExprs[j]);
     SubExprs.clear();
   }
 
-
-  // Now that we know how many times each is used, build Result.
-  for (std::map<SCEVHandle, unsigned>::iterator I =
-       SubExpressionUseCounts.begin(), E = SubExpressionUseCounts.end();
-       I != E; )
+  // Now that we know how many times each is used, build Result.  Iterate over
+  // UniqueSubexprs so that we have a stable ordering.
+  for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) {
+    std::map<SCEVHandle, unsigned>::iterator I = 
+       SubExpressionUseCounts.find(UniqueSubExprs[i]);
+    assert(I != SubExpressionUseCounts.end() && "Entry not found?");
     if (I->second == NumUses) {  // Found CSE!
       Result = SCEVAddExpr::get(Result, I->first);
-      ++I;
     } else {
       // Remove non-cse's from SubExpressionUseCounts.
-      SubExpressionUseCounts.erase(I++);
+      SubExpressionUseCounts.erase(I);
     }
+  }
   
   // If we found no CSE's, return now.
   if (Result == Zero) return Result;






More information about the llvm-commits mailing list