[llvm] [InstCombine] Simplify fractions when there is no overflow (PR #92949)

via llvm-commits llvm-commits at lists.llvm.org
Fri May 24 10:02:04 PDT 2024


================
@@ -1176,6 +1176,45 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
         Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
         return Mul;
       }
+
+      // We can reduce expressions of things like * 150 / 100 to * 3 / 2
+      if (Op0->hasOneUse() && !C2->isZero() &&
+          !(IsSigned && C1->isMinSignedValue() && C2->isAllOnes())) {
+        assert(!C2->isMinSignedValue() &&
+               "This should have been folded away by InstSimplify");
+        APInt GCD = APIntOps::GreatestCommonDivisor(C1->abs(), C2->abs());
+        if (!GCD.isOne() && !GCD.isZero()) {
----------------
goldsteinn wrote:

can GCD be zero?

https://github.com/llvm/llvm-project/pull/92949


More information about the llvm-commits mailing list