[PATCH] ScalarEvolution backedge taken count computation on LT and GT rewrite

Andrew Trick atrick at apple.com
Mon Oct 28 21:56:18 PDT 2013


  This patch doesn't appear to be up-to-date. I didn't check if the conlicts were serious, but please be careful when you merge.

  We've been moving this code to current conventions:
  http://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly
  I realize the code is inconsistent, but let's use the current style for new helpers: ComputeBECount, DoesIV...

  There's a subtle change in behavior here with FlagNW. When stride > 1
  and NoWrap == FlagNW we now bypass DoesIVOverflowOnLT().

  Remeber with FlagNW, unsigned/signed wrap may occur, hence any ULT/SLT check may pass after the IV wraps. FlagNW only tells us that the IV can't *fully* wrap. That would be useful if we knew there were no other loop exits, or if we provide a different interface for clients that only care about one exit (in that case skipping the loop exit would eventually lead to undefined behavior). However, given the current HowManyLessThans interface, I'm not sure FlagNW is useful. We simply don't know if the loop will terminate after the computed number of iteratations but before the IV fully wraps. So I think the NoWrap check to bypass DoesIVOverflowOnLT is too aggressive. The NSW/NUW check is fine.


================
Comment at: lib/Analysis/ScalarEvolution.cpp:6403
@@ +6402,3 @@
+
+  // Avoid weired loops
+  if (!IV || IV->getLoop() != L || !IV->isAffine())
----------------
weird

================
Comment at: lib/Analysis/ScalarEvolution.cpp:6326
@@ -6327,37 +6325,3 @@
 
-/// getBECount - Subtract the end and start values and divide by the step,
-/// rounding up, to get the number of times the backedge is executed. Return
-/// CouldNotCompute if an intermediate computation overflows.
-const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
-                                        const SCEV *End,
-                                        const SCEV *Step,
-                                        bool NoWrap) {
-  assert(!isKnownNegative(Step) &&
-         "This code doesn't handle negative strides yet!");
-
-  Type *Ty = Start->getType();
-
-  // When Start == End, we have an exact BECount == 0. Short-circuit this case
-  // here because SCEV may not be able to determine that the unsigned division
-  // after rounding is zero.
-  if (Start == End)
-    return getConstant(Ty, 0);
-
-  const SCEV *NegOne = getConstant(Ty, (uint64_t)-1);
-  const SCEV *Diff = getMinusSCEV(End, Start);
-  const SCEV *RoundUp = getAddExpr(Step, NegOne);
-
-  // Add an adjustment to the difference between End and Start so that
-  // the division will effectively round up.
-  const SCEV *Add = getAddExpr(Diff, RoundUp);
-
-  if (!NoWrap) {
-    // Check Add for unsigned overflow.
-    // TODO: More sophisticated things could be done here.
-    Type *WideTy = IntegerType::get(getContext(),
-                                          getTypeSizeInBits(Ty) + 1);
-    const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
-    const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
-    const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
-    if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
-      return getCouldNotCompute();
+bool ScalarEvolution::DoesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
+                                         bool IsSigned, bool NoWrap) {
----------------
Comments please.

================
Comment at: lib/Analysis/ScalarEvolution.cpp:6328
@@ +6327,3 @@
+                                         bool IsSigned, bool NoWrap) {
+  if (NoWrap) return false;
+
----------------
Too aggressive for FlagNW.

================
Comment at: lib/Analysis/ScalarEvolution.cpp:6446
@@ +6445,3 @@
+
+  // Although we may have MAX expression we only the RHS choice because
+  // in the other case the result of the backedge taken count is always zero, 
----------------
?


http://llvm-reviews.chandlerc.com/D2008



More information about the llvm-commits mailing list