Extend getConstantTripCount to deal with a larger subset of loop bounds; make loop
unroll/unroll-and-jam more powerful; add additional affine expr builder methods

- use previously added analysis/simplification to infer multiple of unroll
  factor trip counts, making loop unroll/unroll-and-jam more general.

- for loop unroll, support bounds that are single result affine map's with the
  same set of operands. For unknown loop bounds, loop unroll will now work as
  long as trip count can be determined to be a multiple of unroll factor.

- extend getConstantTripCount to deal with single result affine map's with the
  same operands. move it to mlir/Analysis/LoopAnalysis.cpp

- add additional builder utility methods for affine expr arithmetic
  (difference, mod/floordiv/ceildiv w.r.t postitive constant). simplify code to
  use the utility methods.

- move affine analysis routines to AffineAnalysis.cpp/.h from
  AffineStructures.cpp/.h.

- Rename LoopUnrollJam to LoopUnrollAndJam to match class name.

- add an additional simplification for simplifyFloorDiv, simplifyCeilDiv

- Rename AffineMap::getNumOperands() getNumInputs: an affine map by itself does
  not have operands. Operands are passed to it through affine_apply, from loop
  bounds/if condition's, etc., operands are stored in the latter.

This should be sufficiently powerful for now as far as unroll/unroll-and-jam go for TPU
code generation, and can move to other analyses/transformations.

Loop nests like these are now unrolled without any cleanup loop being generated.

  for %i = 1 to 100 {
    // unroll factor 4: no cleanup loop will be generated.
    for %j = (d0) -> (d0) (%i) to (d0) -> (5*d0 + 3) (%i) {
      %x = "foo"(%j) : (affineint) -> i32
    }
  }

  for %i = 1 to 100 {
    // unroll factor 4: no cleanup loop will be generated.
    for %j = (d0) -> (d0) (%i) to (d0) -> (d0 - d mod 4 - 1) (%i) {
      %y = "foo"(%j) : (affineint) -> i32
    }
  }

  for %i = 1 to 100 {
    for %j = (d0) -> (d0) (%i) to (d0) -> (d0 + 128) (%i) {
      %x = "foo"() : () -> i32
    }
  }

TODO(bondhugula): extend this to LoopUnrollAndJam as well in the next CL (with minor
changes).

PiperOrigin-RevId: 212661212
diff --git a/lib/IR/AffineMap.cpp b/lib/IR/AffineMap.cpp
index 8d56f24..afa35ba 100644
--- a/lib/IR/AffineMap.cpp
+++ b/lib/IR/AffineMap.cpp
@@ -63,7 +63,7 @@
   // If only one of them is a symbolic expressions, make it the RHS.
   if (isa<AffineConstantExpr>(lhs) ||
       (lhs->isSymbolicOrConstant() && !rhs->isSymbolicOrConstant())) {
-    return AffineBinaryOpExpr::get(Kind::Add, rhs, lhs, context);
+    return AffineBinaryOpExpr::getAdd(rhs, lhs, context);
   }
 
   // At this point, if there was a constant, it would be on the right.
@@ -77,8 +77,8 @@
   auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs);
   if (lBin && rhsConst && lBin->getKind() == Kind::Add) {
     if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS()))
-      return AffineBinaryOpExpr::get(
-          Kind::Add, lBin->getLHS(),
+      return AffineBinaryOpExpr::getAdd(
+          lBin->getLHS(),
           AffineConstantExpr::get(lrhs->getValue() + rhsConst->getValue(),
                                   context),
           context);
@@ -88,10 +88,9 @@
   // + d1 into (d0 + d1) + 2.
   if (lBin && lBin->getKind() == Kind::Add) {
     if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) {
-      return AffineBinaryOpExpr::get(
-          Kind::Add,
-          AffineBinaryOpExpr::get(Kind::Add, lBin->getLHS(), rhs, context),
-          lrhs, context);
+      return AffineBinaryOpExpr::getAdd(
+          AffineBinaryOpExpr::getAdd(lBin->getLHS(), rhs, context), lrhs,
+          context);
     }
   }
 
@@ -115,7 +114,7 @@
   // constant. (Note that a constant is trivially symbolic).
   if (!rhs->isSymbolicOrConstant() || isa<AffineConstantExpr>(lhs)) {
     // At least one of them has to be symbolic.
-    return AffineBinaryOpExpr::get(Kind::Mul, rhs, lhs, context);
+    return AffineBinaryOpExpr::getMul(rhs, lhs, context);
   }
 
   // At this point, if there was a constant, it would be on the right.
@@ -133,8 +132,8 @@
   auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs);
   if (lBin && rhsConst && lBin->getKind() == Kind::Mul) {
     if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS()))
-      return AffineBinaryOpExpr::get(
-          Kind::Mul, lBin->getLHS(),
+      return AffineBinaryOpExpr::getMul(
+          lBin->getLHS(),
           AffineConstantExpr::get(lrhs->getValue() * rhsConst->getValue(),
                                   context),
           context);
@@ -144,10 +143,9 @@
   // * 2) * d1 into (d0 * d1) * 2.
   if (lBin && lBin->getKind() == Kind::Mul) {
     if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) {
-      return AffineBinaryOpExpr::get(
-          Kind::Mul,
-          AffineBinaryOpExpr::get(Kind::Mul, lBin->getLHS(), rhs, context),
-          lrhs, context);
+      return AffineBinaryOpExpr::getMul(
+          AffineBinaryOpExpr::getMul(lBin->getLHS(), rhs, context), lrhs,
+          context);
     }
   }
 
@@ -167,13 +165,16 @@
   // Fold floordiv of a multiply with a constant that is a multiple of the
   // divisor. Eg: (i * 128) floordiv 64 = i * 2.
   if (rhsConst) {
+    if (rhsConst->getValue() == 1)
+      return lhs;
+
     auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs);
     if (lBin && lBin->getKind() == Kind::Mul) {
       if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) {
         // rhsConst is known to be positive if a constant.
         if (lrhs->getValue() % rhsConst->getValue() == 0)
-          return AffineBinaryOpExpr::get(
-              Kind::Mul, lBin->getLHS(),
+          return AffineBinaryOpExpr::getMul(
+              lBin->getLHS(),
               AffineConstantExpr::get(lrhs->getValue() / rhsConst->getValue(),
                                       context),
               context);
@@ -199,13 +200,16 @@
   // Fold ceildiv of a multiply with a constant that is a multiple of the
   // divisor. Eg: (i * 128) ceildiv 64 = i * 2.
   if (rhsConst) {
+    if (rhsConst->getValue() == 1)
+      return lhs;
+
     auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs);
     if (lBin && lBin->getKind() == Kind::Mul) {
       if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) {
         // rhsConst is known to be positive if a constant.
         if (lrhs->getValue() % rhsConst->getValue() == 0)
-          return AffineBinaryOpExpr::get(
-              Kind::Mul, lBin->getLHS(),
+          return AffineBinaryOpExpr::getMul(
+              lBin->getLHS(),
               AffineConstantExpr::get(lrhs->getValue() / rhsConst->getValue(),
                                       context),
               context);
@@ -230,7 +234,7 @@
   // mod 64 is folded to 0, and less trivially, (i*(j*4*(k*32))) mod 128 = 0.
   if (rhsConst) {
     // rhsConst is known to be positive if a constant.
-    if (lhs->getKnownGcd() % rhsConst->getValue() == 0)
+    if (lhs->getLargestKnownDivisor() % rhsConst->getValue() == 0)
       return AffineConstantExpr::get(0, context);
   }