// Converts ADD and MUL to division. e.g., a/2+1 --> (a+2)/2 // This is called only when the expression is divisible. protected SimpleExpression toDivision() { SimpleExpression ret; if (sop == ADD) { List<SimpleExpression> converted = new LinkedList<SimpleExpression>(); for (SimpleExpression child : children) converted.add(child.toDivision()); SimpleExpression lcd = getLCD(converted); if (lcd.equals(sone)) ret = this; else { SimpleExpression dividend = szero; for (SimpleExpression child : converted) { if (child.sop == DIV) dividend = add(dividend, multiply(child.getChild(0), divide(lcd, child.getChild(1)))); else dividend = add(dividend, multiply(child, lcd)); } ret = divide(dividend, lcd); } } else if (sop == MUL) { SimpleExpression dividend = sone, divider = sone; for (SimpleExpression child : children) { if (child.sop == DIV) { dividend = multiply(dividend, child.getChild(0)); divider = multiply(divider, child.getChild(1)); } else dividend = multiply(dividend, child); } if (divider.equals(sone)) ret = this; else ret = divide(dividend, divider); } else ret = this; return ret; }
// Normalizes a comparison operation private SimpleExpression normalizeCompare() { SimpleExpression lhs = getChild(0), rhs = getChild(1); // Before normalization ( lhs <op> rhs ) if (lhs.sop == LIT && rhs.sop == LIT) { double diff = lhs.getValue().doubleValue() - rhs.getValue().doubleValue(); switch (sop) { case EQ: return (diff == 0) ? sone : szero; case NE: return (diff != 0) ? sone : szero; case LE: return (diff <= 0) ? sone : szero; case LT: return (diff < 0) ? sone : szero; case GE: return (diff >= 0) ? sone : szero; case GT: return (diff > 0) ? sone : szero; default: Tools.exit("[SimpleExpression] unknown comparison expression"); } } else if (lhs.equals(rhs)) return (sop == EQ || sop == LE || sop == GE) ? sone : szero; // Normalization ( lhs-rhs <op> 0 ) SimpleExpression ret = new SimpleExpression(sop); if (compare(lhs, rhs) < 0) { ret.add(subtract(rhs, lhs)); ret.sop = exchangeOp(sop); } else ret.add(subtract(lhs, rhs)); ret.add(szero); return ret; }
// Aggressively normalize divisible expressions to minimize ADD operations. // This method is called only by induction variable substitution where // the divisibility of an expression is defined well. protected SimpleExpression normalizeDivisible() { SimpleExpression ret = this; if (sop == DIV) { if (getChild(0).sop == DIV) ret = divide(getChild(0).getChild(0), multiply(getChild(0).getChild(1), getChild(1))); } else if (sop == MUL) { ret = toDivision().normalize(); } else if (sop == ADD) { ret = toDivision(); if (ret.sop == DIV && ret.getChild(0).sop == ADD) { SimpleExpression non_div = szero, dividend = szero; SimpleExpression divider = ret.getChild(1); for (SimpleExpression child : ret.getChild(0).children) { SimpleExpression divided = divide(child, divider); if (divided.sop == DIV) dividend = add(dividend, child); else non_div = add(non_div, divided); } if (non_div.equals(szero) && ret.countsOperations(ADD) >= countsOperations(ADD)) ret = this; // heuristics: no benefit from the simplification. else ret = add(non_div, divide(dividend, divider)); } } return ret; }
// Normalizes a MOD expression private SimpleExpression normalizeMOD() { if (!allow(DIVIDE)) return this; SimpleExpression lhs = getChild(0), rhs = getChild(1), ret = null; if (rhs.equals(szero)) ret = this; else if (lhs.sop == LIT && rhs.sop == LIT) ret = mod(lhs, rhs); else if (rhs.equals(sone) || rhs.equals(getInt(-1))) ret = szero; if (ret == null) { lhs = lhs.factorize(); rhs = rhs.factorize(); List<SimpleExpression> gcd = computeGCD(lhs, rhs); if (gcd.get(0).equals(sone)) ret = this; else // (a*x)%(b*x) = (a%b)*x ret = multiply(gcd.get(0), new SimpleExpression(gcd.get(1), MOD, gcd.get(2))); } // Tools.printlnStatus("[MOD] "+this+" --> "+ret, 1); return ret; }
// Normalizes a DIV expression private SimpleExpression normalizeDIV() { if (!allow(DIVIDE)) return this; SimpleExpression lhs = getChild(0), rhs = getChild(1), ret = null; if (rhs.equals(szero)) ret = this; // Don't do anything with division by zero else if (lhs.equals(szero)) ret = szero; // 0/<expr>: expr=0 is an exception anyhow else if (lhs.sop == LIT && rhs.sop == LIT) ret = divide(lhs, rhs); // Call compute method else if (rhs.equals(sone) || rhs.equals(getInt(-1))) ret = multiply(rhs, lhs); // Division by one -> multiplication by one if (ret == null) { lhs = lhs.factorize(); rhs = rhs.factorize(); List<SimpleExpression> gcd = computeGCD(lhs, rhs); if (gcd.get(0).equals(sone)) ret = this; else ret = divide(gcd.get(1), gcd.get(2)); } // Tools.printlnStatus("[DIV] "+this+" --> "+ret, 1); return ret; }
// Normalizes a MUL expression private SimpleExpression normalizeMUL() { SimpleExpression ret = this; if (allow(FOLD)) { SimpleExpression coef = sone; LinkedList<SimpleExpression> terms = new LinkedList<SimpleExpression>(); for (SimpleExpression child : children) { if (child.sop == LIT) coef = multiply(coef, child); else terms.add(child); } ret = new SimpleExpression(MUL); if (coef.equals(szero)) ret = szero; else { if (!coef.equals(sone) || terms.size() == 0) ret.add(coef); ret.addAll(terms); if (ret.children.size() == 1) ret = ret.getChild(0); } } ret = ret.distribute(); // Tools.printlnStatus("[MUL] "+this+" --> "+ret, 1); return ret; }
// Normalizes an ADD expression private SimpleExpression normalizeADD() { if (!allow(FOLD)) return this; TreeMap<SimpleExpression, SimpleExpression> terms = new TreeMap<SimpleExpression, SimpleExpression>(); for (SimpleExpression child : children) { SimpleExpression term = child.getTerm(), coef = child.getCoef(); if (terms.containsKey(term)) terms.put(term, add(terms.get(term), coef)); else terms.put(term, coef); } SimpleExpression ret = new SimpleExpression(ADD); for (SimpleExpression term : terms.keySet()) { SimpleExpression coef = terms.get(term); if (!coef.equals(szero)) ret.add((coef.equals(sone)) ? term : multiply(coef, term)); } if (ret.children.size() == 0) ret = szero; else if (ret.children.size() == 1) ret = ret.getChild(0); // Tools.printlnStatus("[ADD] "+this+" --> "+ret, 1); return ret; }
// Normalizes an AND|OR operation private SimpleExpression normalizeLogic() { if (!allow(LOGIC)) return this; TreeSet<SimpleExpression> set = new TreeSet<SimpleExpression>(children); TreeSet<SimpleExpression> neg = new TreeSet<SimpleExpression>(); SimpleExpression ret = new SimpleExpression(sop); for (SimpleExpression child : set) { if (sop == AND) { if (child.equals(szero) || neg.contains(child)) return szero; else if (child.sop != LIT) // ==LIT means non-zero literal. ret.add(child); } else // sop == OR { if ((child.sop == LIT && !child.equals(szero)) || neg.contains(child)) return sone; else if (child.sop != LIT) // ==LIT means zero literal. ret.add(child); } neg.add(child.negate()); } if (ret.children.size() == 0) // skipped literals. ret = (ret.sop == AND) ? sone : szero; else if (ret.children.size() == 1) ret = ret.getChild(0); // Tools.printlnStatus("[LOGIC] "+this+" --> "+ret, 1); return ret; }
// Factorizes terms; a*b+a*c --> a*(b+c) private SimpleExpression factorize() { if (!allow(FACTORIZE) || sop != ADD) return this; SimpleExpression ret = null, gcd = getChild(0); // for normalized form. for (SimpleExpression child : children) { gcd = computeGCD(gcd, child).get(0); if (gcd.equals(sone)) { ret = this; break; } } if (ret == null) { SimpleExpression rhs = szero; for (SimpleExpression child : children) rhs = add(rhs, computeGCD(gcd, child).get(2)); ret = new SimpleExpression(gcd, MUL, rhs); ret.sort(); } // Tools.printlnStatus("[FACT] "+this+" --> "+ret, 1); return ret; }