Fix SCEVAddRecExpr::isLoopInvariant to test if all of its operands
authorDan Gohman <gohman@apple.com>
Fri, 26 Jun 2009 22:17:21 +0000 (22:17 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 26 Jun 2009 22:17:21 +0000 (22:17 +0000)
are loop invariant, not just the start operand.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74338 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolution.cpp

index c98414d66a4b456bdabe3622b0c305b8309fd216..2d8bd7d53a6ac4105bbb4b65e98c64f75ef8b710 100644 (file)
@@ -327,12 +327,22 @@ SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym,
 
 
 bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
-  // This recurrence is invariant w.r.t to QueryLoop iff QueryLoop doesn't
-  // contain L and if the start is invariant.
   // Add recurrences are never invariant in the function-body (null loop).
-  return QueryLoop &&
-         !QueryLoop->contains(L->getHeader()) &&
-         getOperand(0)->isLoopInvariant(QueryLoop);
+  if (!QueryLoop)
+    return false;
+
+  // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L.
+  if (QueryLoop->contains(L->getHeader()))
+    return false;
+
+  // This recurrence is variant w.r.t. QueryLoop if any of its operands
+  // are variant.
+  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+    if (!getOperand(i)->isLoopInvariant(QueryLoop))
+      return false;
+
+  // Otherwise it's loop-invariant.
+  return true;
 }