For the semi affine expressions, whenever rhs of a floordiv, ceildiv, mod or product expression is a symbolic expression, we introduce a local variable representing the result, and store the floordiv/ceildiv, mod or product affine expression in LocalExprs. In this way the expression is flattened, and trivial addition and subtraction related simplifications are performed.
Also rule based matching for detecting and transforming "expr - q * (expr floordiv q)"
to "expr mod q", where q is a symbolic exxpression, in simplifyAdd function.
Nit: You can rewrap to use the whole width (80 columns). I know it was the existing comment that didn't.