Skip to content

Commit a1e62aa

Browse files
committed
Minor reflow of FloorDivSIOp/CeilDivSIOp folder to limit the number of APInt API calls (NFC)
Cache the result of the comparison in boolean, and check early for 0 to leverage `(a < 0) == !(a > 0)`.
1 parent 8506c8c commit a1e62aa

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,25 +357,30 @@ OpFoldResult arith::CeilDivSIOp::fold(ArrayRef<Attribute> operands) {
357357
overflowOrDiv0 = true;
358358
return a;
359359
}
360+
if (!a)
361+
return a;
362+
// After this point we know that neither a or b are zero.
360363
unsigned bits = a.getBitWidth();
361364
APInt zero = APInt::getZero(bits);
362-
if (a.sgt(zero) && b.sgt(zero)) {
365+
bool aGtZero = a.sgt(zero);
366+
bool bGtZero = b.sgt(zero);
367+
if (aGtZero && bGtZero) {
363368
// Both positive, return ceil(a, b).
364369
return signedCeilNonnegInputs(a, b, overflowOrDiv0);
365370
}
366-
if (a.slt(zero) && b.slt(zero)) {
371+
if (!aGtZero && !bGtZero) {
367372
// Both negative, return ceil(-a, -b).
368373
APInt posA = zero.ssub_ov(a, overflowOrDiv0);
369374
APInt posB = zero.ssub_ov(b, overflowOrDiv0);
370375
return signedCeilNonnegInputs(posA, posB, overflowOrDiv0);
371376
}
372-
if (a.slt(zero) && b.sgt(zero)) {
377+
if (!aGtZero && bGtZero) {
373378
// A is negative, b is positive, return - ( -a / b).
374379
APInt posA = zero.ssub_ov(a, overflowOrDiv0);
375380
APInt div = posA.sdiv_ov(b, overflowOrDiv0);
376381
return zero.ssub_ov(div, overflowOrDiv0);
377382
}
378-
// A is positive (or zero), b is negative, return - (a / -b).
383+
// A is positive, b is negative, return - (a / -b).
379384
APInt posB = zero.ssub_ov(b, overflowOrDiv0);
380385
APInt div = a.sdiv_ov(posB, overflowOrDiv0);
381386
return zero.ssub_ov(div, overflowOrDiv0);
@@ -407,19 +412,24 @@ OpFoldResult arith::FloorDivSIOp::fold(ArrayRef<Attribute> operands) {
407412
overflowOrDiv0 = true;
408413
return a;
409414
}
415+
if (!a)
416+
return a;
417+
// After this point we know that neither a or b are zero.
410418
unsigned bits = a.getBitWidth();
411419
APInt zero = APInt::getZero(bits);
412-
if (a.sge(zero) && b.sgt(zero)) {
413-
// Both positive (or a is zero), return a / b.
420+
bool aGtZero = a.sgt(zero);
421+
bool bGtZero = b.sgt(zero);
422+
if (aGtZero && bGtZero) {
423+
// Both positive, return a / b.
414424
return a.sdiv_ov(b, overflowOrDiv0);
415425
}
416-
if (a.sle(zero) && b.slt(zero)) {
417-
// Both negative (or a is zero), return -a / -b.
426+
if (!aGtZero && !bGtZero) {
427+
// Both negative, return -a / -b.
418428
APInt posA = zero.ssub_ov(a, overflowOrDiv0);
419429
APInt posB = zero.ssub_ov(b, overflowOrDiv0);
420430
return posA.sdiv_ov(posB, overflowOrDiv0);
421431
}
422-
if (a.slt(zero) && b.sgt(zero)) {
432+
if (!aGtZero && bGtZero) {
423433
// A is negative, b is positive, return - ceil(-a, b).
424434
APInt posA = zero.ssub_ov(a, overflowOrDiv0);
425435
APInt ceil = signedCeilNonnegInputs(posA, b, overflowOrDiv0);

0 commit comments

Comments
 (0)