// Returns the number of descendants with the specified type. private int countsOperations(int op) { int ret = 0; if (children == null) return ret; for (SimpleExpression child : children) ret += child.countsOperations(op); if (sop == op) ret += children.size() - 1; return ret; }
// Returns se1<op>se2 with constant evaluation. private static SimpleExpression compute(SimpleExpression se1, int op, SimpleExpression se2) { SimpleExpression ret = null; if (se1.sop == LIT && se2.sop == LIT) { Double result = null, v1 = se1.getValue(), v2 = se2.getValue(); switch (op) { case ADD: result = v1 + v2; break; case MUL: result = v1 * v2; break; case DIV: result = v1 / v2; break; case MOD: result = v1 % v2; break; default: Tools.exit("[SimpleExpression] unknown operation in normalization"); } if (se1.expr instanceof IntegerLiteral && se2.expr instanceof IntegerLiteral) ret = getInt(result.intValue()); else ret = getDouble(result.doubleValue()); } else { ret = new SimpleExpression(se1, op, se2); ret = ret.normalize(); } // Tools.printlnStatus("[COM] "+se1+" "+cop.get(op)+" "+se2+" --> "+ret, 1); 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; }
/** * Returns a clone of this simple expression. * * @return a clone object. */ public Object clone() { SimpleExpression ret = new SimpleExpression(sop); ret.order = order; ret.expr = expr; ret.contains_side_effect = contains_side_effect; ret.children.addAll(children); return ret; }
// Computes the least common multiple of the two simple expression. // Assumes se1 and se2 have been normalized. private static SimpleExpression computeLCM(SimpleExpression se1, SimpleExpression se2) { se1 = se1.factorize(); se2 = se2.factorize(); List<SimpleExpression> gcd = computeGCD(se1, se2); SimpleExpression ret = multiply(gcd.get(0), gcd.get(1)); ret = multiply(ret, gcd.get(2)); return ret; }
/** * Returns if a simple expression is greater than, less than, or equal to the other. * * @param se1 the first simple expression. * @param se2 the second simple expression. * @return the comparison result (-1, 0, 1). */ public int compare(SimpleExpression se1, SimpleExpression se2) { if (se1.order < se2.order) return -1; else if (se1.order > se2.order) return 1; se1 = se1.getTerm(); se2 = se2.getTerm(); if (se1.children.size() < se2.children.size()) return -1; else if (se1.children.size() > se2.children.size()) return 1; else return se1.compareTo(se2); }
// Distributes terms; a*(b+c) --> a*b+a*c private SimpleExpression distribute() { if (!allow(DISTRIBUTE) || sop != MUL || !containsChildOfType(ADD)) return this; SimpleExpression ret = sone; for (SimpleExpression child : children) { SimpleExpression lhs = new SimpleExpression(szero, ADD, ret); SimpleExpression rhs = new SimpleExpression(szero, ADD, child); ret = new SimpleExpression(ADD); for (SimpleExpression lhs_child : lhs.children) for (SimpleExpression rhs_child : rhs.children) ret.add(multiply(lhs_child, rhs_child)); } ret = ret.normalizeADD(); // Tools.printlnStatus("[DIST] "+this+" --> "+ret, 1); return ret; }
// Returns the non-literal term in the simple expression. protected SimpleExpression getTerm() { SimpleExpression ret = null; if (sop == LIT) ret = sone; else if (sop != MUL) ret = this; else { ret = new SimpleExpression(MUL); for (SimpleExpression child : children) if (child.sop != LIT) ret.add(child); if (ret.children.size() == 0) ret = sone; else if (ret.children.size() == 1) ret = ret.getChild(0); } // Tools.printlnStatus("[TERM] "+this+" --> "+ret, 1); 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; }
// 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; }
// Parses a binary expression private void parse(BinaryExpression be) { BinaryOperator bop = be.getOperator(); SimpleExpression lhs = new SimpleExpression(be.getLHS()); SimpleExpression rhs = new SimpleExpression(be.getRHS()); contains_side_effect |= (lhs.contains_side_effect || rhs.contains_side_effect); if (bop == BinaryOperator.SUBTRACT) { sop = ADD; SimpleExpression new_rhs = new SimpleExpression(MUL); new_rhs.add(getInt(-1)); new_rhs.add(rhs); add(lhs); add(new_rhs); } else { sop = cop.indexOf(bop); if (sop == -1) { sop = TREE; expr = be; } add(lhs); add(rhs); } }
// Removes division by replacing with modulus operations. // The returned list contains the modified simple expression (get(0)) and // the additionally multiplied value (get(1)). // e.g., a*(b/c)*(d/e) returns {a*(b-b%c)*(d-d%e), c*e}. // It is important to notice that the legality check of this transformation // is up to the callers. // It is assumed that the original simple expressions has been normalized. protected List<SimpleExpression> multiplyByLCM() { List<SimpleExpression> ret = new ArrayList<SimpleExpression>(2); if (sop == DIV) { ret.add(subtract(getChild(0), mod(getChild(0), getChild(1)))); ret.add(getChild(1)); } else if (sop == ADD) { List<SimpleExpression> terms = new LinkedList<SimpleExpression>(); List<SimpleExpression> factors = new LinkedList<SimpleExpression>(); SimpleExpression lcm = sone; for (SimpleExpression child : children) { List<SimpleExpression> ret0 = child.multiplyByLCM(); terms.add(ret0.get(0)); factors.add(ret0.get(1)); lcm = computeLCM(lcm, ret0.get(1)); } SimpleExpression ret1 = new SimpleExpression(ADD); for (int i = 0; i < terms.size(); i++) ret1.add(multiply(divide(lcm, factors.get(i)), terms.get(i))); ret.add(ret1.normalize()); ret.add(lcm); } else if (sop == MUL) { SimpleExpression terms = new SimpleExpression(MUL); SimpleExpression factors = sone; for (SimpleExpression child : children) { List<SimpleExpression> ret0 = child.multiplyByLCM(); terms.add(ret0.get(0)); factors = multiply(factors, ret0.get(1)); } ret.add(terms.normalize()); ret.add(factors); } else { ret.add(this); ret.add(sone); } 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 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 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 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; }
// Assumes se1 and se2 have been normalized. // Returns a triplet (gcd, dividend1, dividend2). private static List<SimpleExpression> computeGCD(SimpleExpression se1, SimpleExpression se2) { // Compute symbolic parts. List<SimpleExpression> terms1 = new LinkedList<SimpleExpression>(); List<SimpleExpression> terms2 = new LinkedList<SimpleExpression>(); SimpleExpression term1 = se1.getTerm(), term2 = se2.getTerm(); if (term1.sop == MUL) terms1.addAll(term1.children); else terms1.add(term1); if (term2.sop == MUL) terms2.addAll(term2.children); else terms2.add(term2); TreeSet<SimpleExpression> gcd_terms = new TreeSet<SimpleExpression>(terms1); gcd_terms.retainAll(terms2); for (SimpleExpression gcd_term : gcd_terms) { terms1.remove(gcd_term); // removes the first occurrence terms2.remove(gcd_term); // of the gcd term } // Compute numeric parts. SimpleExpression s1 = se1.getCoef(), s2 = se2.getCoef(), sgcd = null; if (s1.expr instanceof IntegerLiteral && s2.expr instanceof IntegerLiteral) { int n1 = s1.getValue().intValue(), n2 = s2.getValue().intValue(); int gcd = cetus.analysis.GCD.compute(n1, n2); s1 = getInt(n1 / gcd); s2 = getInt(n2 / gcd); sgcd = getInt(gcd); } else sgcd = sone; // Combine two parts. for (SimpleExpression child : gcd_terms) sgcd = multiply(sgcd, child); for (SimpleExpression child : terms1) s1 = multiply(s1, child); for (SimpleExpression child : terms2) s2 = multiply(s2, child); List<SimpleExpression> ret = new LinkedList<SimpleExpression>(); ret.add(sgcd); ret.add(s1); ret.add(s2); // Tools.printlnStatus("[GCD] "+se1+" and "+se2+" --> "+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; }
// Negates this simple expression private SimpleExpression negate() { SimpleExpression ret = null; if (sop == NEG) ret = getChild(0); else if (sop == LIT) ret = (equals(szero)) ? sone : szero; else if (sop >= EQ && sop <= GT) { ret = (SimpleExpression) clone(); ret.sop = negateOp(sop); } else if (sop == AND || sop == OR) { ret = new SimpleExpression((sop == AND) ? OR : AND); for (SimpleExpression child : children) ret.add(child.negate()); } else { ret = new SimpleExpression(NEG); ret.add(this); } return ret; }
// Computes the least common denominator of the two simple expression. private static SimpleExpression computeLCD(SimpleExpression se1, SimpleExpression se2) { SimpleExpression divider1 = sone, divider2 = sone; if (se1.sop == DIV) divider1 = se1.getChild(1); if (se2.sop == DIV) divider2 = se2.getChild(1); return computeLCM(divider1, divider2); }
/** * Compares this simple expression with the given simple expression. * * @param the given simple expression. * @return the result of Expression.compareTo(Expression). */ public int compareTo(SimpleExpression se) { return getExpression().compareTo(se.getExpression()); }
// Sorts the child terms if they are commutative and associative protected void sort() { for (SimpleExpression child : children) child.sort(); if (isCommAssoc()) Collections.sort(children, this); }
// Normalizes this simple expression recursively. protected SimpleExpression normalize() { SimpleExpression ret = new SimpleExpression(this); for (SimpleExpression child : children) ret.add(child.normalize()); if (contains_side_effect) return ret; switch (ret.sop) { case ID: case LIT: case LEAF: ret = this; break; case ADD: ret = ret.normalizeADD(); break; case MUL: ret = ret.normalizeMUL(); break; case DIV: ret = ret.normalizeDIV(); break; case MOD: ret = ret.normalizeMOD(); break; case SFTL: case SFTR: case BAND: case BOR: case BXOR: ret = ret.normalizeBitOperation(); break; case BCMP: ret = ret.normalizeBCMP(); break; case AND: case OR: ret = ret.normalizeLogic(); break; case EQ: case NE: case LE: case LT: case GE: case GT: ret = ret.normalizeCompare(); break; case NEG: ret = ret.normalizeNEG(); break; case MIN: case MAX: ret = ret.normalizeMINMAX(); break; default: } if (ret.isCommAssoc()) ret.sort(); // Tools.printlnStatus("[NORM] "+this+" --> "+ret, 1); return ret; }
// Normalizes a MIN/MAX expression private SimpleExpression normalizeMINMAX() { // Literal/non-literal separation TreeSet<Double> literals = new TreeSet<Double>(); TreeSet<SimpleExpression> exprs = new TreeSet<SimpleExpression>(); SimpleExpression ret = new SimpleExpression(sop); for (SimpleExpression child : children) { if (child.sop == LIT) literals.add(child.getValue()); else exprs.add(child); } if (sop == MIN) ret.add(getInt(literals.first().intValue())); else ret.add(getInt(literals.last().intValue())); ret.addAll(exprs); // Quick return for single-entry min/max. if (ret.children.size() == 1) return ret.getChild(0); // Match min(a,max(a,b))=a or max(a,min(a,b))=a if (ret.sop == MIN && ret.children.size() == 2 && ret.getChild(1).sop == MAX && ret.getChild(1).children.size() == 2 && ret.getChild(1).children.contains(ret.getChild(0)) || ret.sop == MAX && ret.children.size() == 2 && ret.getChild(1).sop == MIN && ret.getChild(1).children.size() == 2 && ret.getChild(1).children.contains(ret.getChild(0))) return ret.getChild(0); return ret; }
// 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; }