Lower signed-divisions without rounding to ashr instructions
authorTobias Grosser <tobias@grosser.es>
Wed, 3 Jun 2015 15:14:58 +0000 (15:14 +0000)
committerTobias Grosser <tobias@grosser.es>
Wed, 3 Jun 2015 15:14:58 +0000 (15:14 +0000)
llvm-svn: 238929

polly/lib/CodeGen/IslExprBuilder.cpp
polly/test/Isl/CodeGen/exprModDiv.ll

index e7d7d9e..701a2a5 100644 (file)
@@ -295,6 +295,13 @@ Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
     Res = Builder.CreateNSWMul(LHS, RHS);
     break;
   case isl_ast_op_div:
+    if (auto *Const = dyn_cast<ConstantInt>(RHS)) {
+      auto &Val = Const->getValue();
+      if (Val.isPowerOf2() && Val.isNonNegative()) {
+        Res = Builder.CreateAShr(LHS, Val.ceilLogBase2(), "pexp.div.shr");
+        break;
+      }
+    }
     Res = Builder.CreateSDiv(LHS, RHS, "pexp.div");
     break;
   case isl_ast_op_pdiv_q: // Dividend is non-negative
index 91d27fa..81a8f49 100644 (file)
 ; POW2:  %polly.access.A6 = getelementptr float, float* %A, i64 %pexp.pdiv_r
 
 ; A[i / 128]
-; POW2:  %pexp.div = sdiv i64 %polly.indvar, 128
+; POW2:  %pexp.div.shr = ashr i64 %polly.indvar, 7
 ; POW2:  %polly.access.B8 = getelementptr float, float* %B, i64 %pexp.div
-;
-; FIXME: Make isl mark this as an udiv expression.
 
 ; #define floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
 ; A[p + 128 * floord(-p - 1, 128) + 128]
@@ -62,8 +60,8 @@
 ; POW2:  %polly.access.A10 = getelementptr float, float* %A, i64 %24
 
 ; A[p / 128]
-; POW2:  %pexp.div12 = sdiv i64 %p, 128
-; POW2:  %polly.access.B13 = getelementptr float, float* %B, i64 %pexp.div12
+; POW2:  %pexp.div.shr12 = ashr i64 %p, 7
+; POW2:  %polly.access.B13 = getelementptr float, float* %B, i64 %pexp.div.shr12
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"