[llvm-commits] [llvm] r73783 - in /llvm/trunk: lib/Transforms/Scalar/LoopStrengthReduce.cpp test/CodeGen/X86/optimize-max-0.ll test/CodeGen/X86/optimize-max-1.ll

Evan Cheng evan.cheng at apple.com
Fri Jun 19 14:15:06 PDT 2009


Author: evancheng
Date: Fri Jun 19 16:15:06 2009
New Revision: 73783

URL: http://llvm.org/viewvc/llvm-project?rev=73783&view=rev
Log:
Revert 73718. It's breaking 254.gap.

Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp
    llvm/trunk/test/CodeGen/X86/optimize-max-0.ll
    llvm/trunk/test/CodeGen/X86/optimize-max-1.ll

Modified: llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp?rev=73783&r1=73782&r2=73783&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp Fri Jun 19 16:15:06 2009
@@ -143,10 +143,10 @@
     /// inside the loop then try to eliminate the cast opeation.
     void OptimizeShadowIV(Loop *L);
 
-    /// OptimizeMax - Rewrite the loop's terminating condition
-    /// if it uses a max computation.
-    ICmpInst *OptimizeMax(Loop *L, ICmpInst *Cond,
-                          IVStrideUse* &CondUse);
+    /// OptimizeSMax - Rewrite the loop's terminating condition
+    /// if it uses an smax computation.
+    ICmpInst *OptimizeSMax(Loop *L, ICmpInst *Cond,
+                           IVStrideUse* &CondUse);
 
     bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse,
                            const SCEVHandle *&CondStride);
@@ -2044,8 +2044,8 @@
   return Cond;
 }
 
-/// OptimizeMax - Rewrite the loop's terminating condition if it uses
-/// a max computation.
+/// OptimizeSMax - Rewrite the loop's terminating condition if it uses
+/// an smax computation.
 ///
 /// This is a narrow solution to a specific, but acute, problem. For loops
 /// like this:
@@ -2055,10 +2055,10 @@
 ///     p[i] = 0.0;
 ///   } while (++i < n);
 ///
-/// the trip count isn't just 'n', because 'n' might not be positive. And
-/// unfortunately this can come up even for loops where the user didn't use
-/// a C do-while loop. For example, seemingly well-behaved top-test loops
-/// will commonly be lowered like this:
+/// where the comparison is signed, the trip count isn't just 'n', because
+/// 'n' could be negative. And unfortunately this can come up even for loops
+/// where the user didn't use a C do-while loop. For example, seemingly
+/// well-behaved top-test loops will commonly be lowered like this:
 //
 ///   if (n > 0) {
 ///     i = 0;
@@ -2071,14 +2071,14 @@
 /// test in such a way that indvars can't find it.
 ///
 /// When indvars can't find the if test in loops like this, it creates a
-/// max expression, which allows it to give the loop a canonical
+/// signed-max expression, which allows it to give the loop a canonical
 /// induction variable:
 ///
 ///   i = 0;
-///   max = n < 1 ? 1 : n;
+///   smax = n < 1 ? 1 : n;
 ///   do {
 ///     p[i] = 0.0;
-///   } while (++i != max);
+///   } while (++i != smax);
 ///
 /// Canonical induction variables are necessary because the loop passes
 /// are designed around them. The most obvious example of this is the
@@ -2094,8 +2094,8 @@
 /// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting
 /// the instructions for the maximum computation.
 ///
-ICmpInst *LoopStrengthReduce::OptimizeMax(Loop *L, ICmpInst *Cond,
-                                          IVStrideUse* &CondUse) {
+ICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
+                                           IVStrideUse* &CondUse) {
   // Check that the loop matches the pattern we're looking for.
   if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
       Cond->getPredicate() != CmpInst::ICMP_NE)
@@ -2113,14 +2113,12 @@
   SCEVHandle IterationCount = SE->getAddExpr(BackedgeTakenCount, One);
 
   // Check for a max calculation that matches the pattern.
-  if (!isa<SCEVSMaxExpr>(IterationCount) && !isa<SCEVUMaxExpr>(IterationCount))
-    return Cond;
-  const SCEVNAryExpr *Max = cast<SCEVNAryExpr>(IterationCount);
-  if (Max != SE->getSCEV(Sel)) return Cond;
+  const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(IterationCount);
+  if (!SMax || SMax != SE->getSCEV(Sel)) return Cond;
 
-  SCEVHandle MaxLHS = Max->getOperand(0);
-  SCEVHandle MaxRHS = Max->getOperand(1);
-  if (!MaxLHS || MaxLHS != One) return Cond;
+  SCEVHandle SMaxLHS = SMax->getOperand(0);
+  SCEVHandle SMaxRHS = SMax->getOperand(1);
+  if (!SMaxLHS || SMaxLHS != One) return Cond;
 
   // Check the relevant induction variable for conformance to
   // the pattern.
@@ -2137,23 +2135,19 @@
   // Check the right operand of the select, and remember it, as it will
   // be used in the new comparison instruction.
   Value *NewRHS = 0;
-  if (SE->getSCEV(Sel->getOperand(1)) == MaxRHS)
+  if (SE->getSCEV(Sel->getOperand(1)) == SMaxRHS)
     NewRHS = Sel->getOperand(1);
-  else if (SE->getSCEV(Sel->getOperand(2)) == MaxRHS)
+  else if (SE->getSCEV(Sel->getOperand(2)) == SMaxRHS)
     NewRHS = Sel->getOperand(2);
   if (!NewRHS) return Cond;
 
-  // Determine the new comparison opcode. It may be signed or unsigned,
-  // and the original comparison may be either equality or inequality.
-  CmpInst::Predicate Pred =
-    isa<SCEVSMaxExpr>(Max) ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
-  if (Cond->getPredicate() == CmpInst::ICMP_EQ)
-    Pred = CmpInst::getInversePredicate(Pred);
-
   // Ok, everything looks ok to change the condition into an SLT or SGE and
   // delete the max calculation.
   ICmpInst *NewCond =
-    new ICmpInst(Pred, Cond->getOperand(0), NewRHS, "scmp", Cond);
+    new ICmpInst(Cond->getPredicate() == CmpInst::ICMP_NE ?
+                   CmpInst::ICMP_SLT :
+                   CmpInst::ICMP_SGE,
+                 Cond->getOperand(0), NewRHS, "scmp", Cond);
 
   // Delete the max calculation instructions.
   Cond->replaceAllUsesWith(NewCond);
@@ -2366,10 +2360,10 @@
     StrideNoReuse.insert(*CondStride);
   }
 
-  // If the trip count is computed in terms of a max (due to ScalarEvolution
+  // If the trip count is computed in terms of an smax (due to ScalarEvolution
   // being unable to find a sufficient guard, for example), change the loop
-  // comparison to use SLT or ULT instead of NE.
-  Cond = OptimizeMax(L, Cond, CondUse);
+  // comparison to use SLT instead of NE.
+  Cond = OptimizeSMax(L, Cond, CondUse);
 
   // If possible, change stride and operands of the compare instruction to
   // eliminate one stride.

Modified: llvm/trunk/test/CodeGen/X86/optimize-max-0.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/optimize-max-0.ll?rev=73783&r1=73782&r2=73783&view=diff

==============================================================================
--- llvm/trunk/test/CodeGen/X86/optimize-max-0.ll (original)
+++ llvm/trunk/test/CodeGen/X86/optimize-max-0.ll Fri Jun 19 16:15:06 2009
@@ -1,4 +1,5 @@
 ; RUN: llvm-as < %s | llc -march=x86 | not grep cmov
+; XFAIL: *
 
 ; LSR should be able to eliminate the max computations by
 ; making the loops use slt/ult comparisons instead of ne comparisons.

Modified: llvm/trunk/test/CodeGen/X86/optimize-max-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/optimize-max-1.ll?rev=73783&r1=73782&r2=73783&view=diff

==============================================================================
--- llvm/trunk/test/CodeGen/X86/optimize-max-1.ll (original)
+++ llvm/trunk/test/CodeGen/X86/optimize-max-1.ll Fri Jun 19 16:15:06 2009
@@ -1,4 +1,5 @@
 ; RUN: llvm-as < %s | llc -march=x86-64 | not grep cmov
+; XFAIL: *
 
 ; LSR should be able to eliminate both smax and umax expressions
 ; in loop trip counts.





More information about the llvm-commits mailing list