diff options
author | Tobias Grosser <tobias@grosser.es> | 2015-06-03 17:43:01 +0300 |
---|---|---|
committer | Tobias Grosser <tobias@grosser.es> | 2015-06-03 17:43:01 +0300 |
commit | 224b16228028554e3594c0b72b3dacf3893d22d6 (patch) | |
tree | bd75077ee5fd0b2f33806b43e5e116021b4572a0 /polly | |
parent | aef6d17c8d8b39473557a2177a0d3e188c4704ff (diff) |
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@gmail.com>
llvm-svn: 238927
Diffstat (limited to 'polly')
-rw-r--r-- | polly/lib/CodeGen/IslExprBuilder.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/polly/lib/CodeGen/IslExprBuilder.cpp b/polly/lib/CodeGen/IslExprBuilder.cpp index 27c72486c7e0..e7d7d9e296ec 100644 --- a/polly/lib/CodeGen/IslExprBuilder.cpp +++ b/polly/lib/CodeGen/IslExprBuilder.cpp @@ -301,10 +301,12 @@ Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) { 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. |