Skip to content

Commit bf06ecc

Browse files
author
Martin Blicha
committed
ArithLogic: Avoid expensive call to mkTimes in mkNeg
The method mkTimes is general a does a lot of work to ensure the term is properly normalized. When we wnat to negate en xisting term, we know that it is already normalized, so we can compute the proper result much cheaper than passing multiplying it by -1 and letting mkTimes do the simplifications.
1 parent e492771 commit bf06ecc

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/logics/ArithLogic.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,12 @@ bool ArithLogic::isNumTerm(PTRef tr) const
328328
PTRef ArithLogic::mkNeg(PTRef tr)
329329
{
330330
assert(!isNeg(tr)); // MB: The invariant now is that there is no "Minus" node
331-
if (isConstant(tr)) {
331+
SymRef symref = getSymRef(tr);
332+
if (isConstant(symref)) {
332333
const opensmt::Number& v = getNumConst(tr);
333334
return mkConst(getSortRef(tr), -v);
334335
}
335-
if (isPlus(tr)) {
336+
if (isPlus(symref)) {
336337
vec<PTRef> args;
337338
args.capacity(getPterm(tr).size());
338339
// Note: Do this in two phases to avoid calling mkNeg that invalidates the Pterm reference
@@ -343,9 +344,19 @@ PTRef ArithLogic::mkNeg(PTRef tr)
343344
for (PTRef & tr_arg: args) {
344345
tr_arg = mkNeg(tr_arg);
345346
}
346-
PTRef tr_n = mkFun(yieldsSortInt(tr) ? get_sym_Int_PLUS() : get_sym_Real_PLUS(), std::move(args));
347+
PTRef tr_n = mkFun(symref, std::move(args));
347348
return tr_n;
348349
}
350+
if (isTimes(symref)) { // constant * var-like
351+
assert(getPterm(tr).size() == 2);
352+
auto [var, constant] = splitTermToVarAndConst(tr);
353+
return constant == getMinusOneForSort(getSortRef(symref)) ? var : mkFun(symref, {var, mkNeg(constant)});
354+
}
355+
if (isNumVarLike(symref)) {
356+
auto sortRef = getSortRef(symref);
357+
return mkFun(getTimesForSort(sortRef), {tr, getMinusOneForSort(sortRef)});
358+
}
359+
assert(false); // MB: All cases should be covered, but let's have a default just in case.
349360
PTRef mo = yieldsSortInt(tr) ? getTerm_IntMinusOne() : getTerm_RealMinusOne();
350361
return mkTimes(mo, tr);
351362
}

0 commit comments

Comments
 (0)