[polly] r238927 - Only convert power-of-two floor-division with non-negative denominator

Tobias Grosser tobias at grosser.es
Wed Jun 3 07:43:01 PDT 2015


Author: grosser
Date: Wed Jun  3 09:43:01 2015
New Revision: 238927

URL: http://llvm.org/viewvc/llvm-project?rev=238927&view=rev
Log:
Only convert power-of-two floor-division with non-negative denominator

floord(a,b) === a ashr log_2 (b) holds for positive and negative a's, but
shifting only makes sense for positive values of b. The previous patch did
not consider this as isl currently always produces postive b's. To avoid future
surprises, we check that b is positive and only then apply the optimization.

We also now correctly check the return value of the dyn-cast.

No additional test case, as isl currently does not produce negative
denominators.

Reported-by: David Majnemer <david.majnemer at gmail.com>

Modified:
    polly/trunk/lib/CodeGen/IslExprBuilder.cpp

Modified: polly/trunk/lib/CodeGen/IslExprBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGen/IslExprBuilder.cpp?rev=238927&r1=238926&r2=238927&view=diff
==============================================================================
--- polly/trunk/lib/CodeGen/IslExprBuilder.cpp (original)
+++ polly/trunk/lib/CodeGen/IslExprBuilder.cpp Wed Jun  3 09:43:01 2015
@@ -301,10 +301,12 @@ Value *IslExprBuilder::createOpBin(__isl
     Res = Builder.CreateUDiv(LHS, RHS, "pexp.p_div_q");
     break;
   case isl_ast_op_fdiv_q: { // Round towards -infty
-    auto &Int = dyn_cast<ConstantInt>(RHS)->getValue();
-    if (Int.isPowerOf2()) {
-      Res = Builder.CreateAShr(LHS, Int.ceilLogBase2(), "polly.fdiv_q.shr");
-      break;
+    if (auto *Const = dyn_cast<ConstantInt>(RHS)) {
+      auto &Val = Const->getValue();
+      if (Val.isPowerOf2() && Val.isNonNegative()) {
+        Res = Builder.CreateAShr(LHS, Val.ceilLogBase2(), "polly.fdiv_q.shr");
+        break;
+      }
     }
     // TODO: Review code and check that this calculation does not yield
     //       incorrect overflow in some bordercases.





More information about the llvm-commits mailing list