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
This commit is contained in:
Tobias Grosser 2015-06-03 14:43:01 +00:00
parent aef6d17c8d
commit 224b162280
1 changed files with 6 additions and 4 deletions

View File

@ -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.