diff --git a/src/V3Const.cpp b/src/V3Const.cpp index 8a3397a89..1f4e517e6 100644 --- a/src/V3Const.cpp +++ b/src/V3Const.cpp @@ -327,27 +327,27 @@ private: } bool operandBiExtendConst(AstNodeBiop* nodep) { - // Loop unrolling likes standalone compares - // EQ(EXTEND(xx{width3}), const{width32}) -> EQ(xx{3},const{3}) + // Loop unrolling favors standalone compares + // EQ(const{width32}, EXTEND(xx{width3})) -> EQ(const{3}, xx{3}) // Beware that the constant must have zero bits (+ 1 if signed) or compare // would be incorrect - AstExtend* extendp = nodep->lhsp()->castExtend(); + AstExtend* extendp = nodep->rhsp()->castExtend(); if (!extendp) return false; AstNode* smallerp = extendp->lhsp(); int subsize = smallerp->width(); - AstConst* constp = nodep->rhsp()->castConst(); + AstConst* constp = nodep->lhsp()->castConst(); if (!constp) return false; if (!constp->num().isBitsZero(constp->width()-1, subsize)) return false; // if (debug()>=9) nodep->dumpTree(cout,"BI(EXTEND)-in:"); smallerp->unlinkFrBack(); extendp->unlinkFrBack()->deleteTree(); // aka nodep->lhsp. - nodep->lhsp(smallerp); + nodep->rhsp(smallerp); constp->unlinkFrBack(); V3Number num (constp->fileline(), subsize); num.opAssign(constp->num()); - nodep->rhsp(new AstConst(constp->fileline(), num)); + nodep->lhsp(new AstConst(constp->fileline(), num)); constp->deleteTree(); constp=NULL; if (debug()>=9) nodep->dumpTree(cout,"BI(EXTEND)-ou:"); return true; @@ -1812,6 +1812,14 @@ private: TREEOP ("AstNodeBiComAsv{operandAsvSame(nodep)}", "replaceAsv(nodep)"); TREEOP ("AstNodeBiComAsv{operandAsvLUp(nodep)}", "replaceAsvLUp(nodep)"); TREEOP ("AstNodeBiComAsv{operandAsvRUp(nodep)}", "replaceAsvRUp(nodep)"); + TREEOP ("AstLt {!$lhsp.castConst,$rhsp.castConst}", "AstGt {$rhsp,$lhsp}"); + TREEOP ("AstLtS {!$lhsp.castConst,$rhsp.castConst}", "AstGtS {$rhsp,$lhsp}"); + TREEOP ("AstLte {!$lhsp.castConst,$rhsp.castConst}", "AstGte {$rhsp,$lhsp}"); + TREEOP ("AstLteS {!$lhsp.castConst,$rhsp.castConst}", "AstGteS{$rhsp,$lhsp}"); + TREEOP ("AstGt {!$lhsp.castConst,$rhsp.castConst}", "AstLt {$rhsp,$lhsp}"); + TREEOP ("AstGtS {!$lhsp.castConst,$rhsp.castConst}", "AstLtS {$rhsp,$lhsp}"); + TREEOP ("AstGte {!$lhsp.castConst,$rhsp.castConst}", "AstLte {$rhsp,$lhsp}"); + TREEOP ("AstGteS {!$lhsp.castConst,$rhsp.castConst}", "AstLteS{$rhsp,$lhsp}"); // v--- *1* as These ops are always first, as we warn before replacing TREEOP1("AstLt {$lhsp, $rhsp.isZero}", "replaceNumSigned(nodep,0)"); TREEOP1("AstGte {$lhsp, $rhsp.isZero}", "replaceNumSigned(nodep,1)"); @@ -1861,12 +1869,12 @@ private: TREEOP ("AstShiftR{operandShiftShift(nodep)}", "replaceShiftShift(nodep)"); TREEOP ("AstWordSel{operandWordOOB(nodep)}", "replaceZero(nodep)"); // Compress out EXTENDs to appease loop unroller - TREEOPV("AstEq {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); - TREEOPV("AstNeq {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); - TREEOPV("AstGt {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); - TREEOPV("AstGte {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); - TREEOPV("AstLt {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); - TREEOPV("AstLte {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); + TREEOPV("AstEq {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); + TREEOPV("AstNeq {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); + TREEOPV("AstGt {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); + TREEOPV("AstGte {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); + TREEOPV("AstLt {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); + TREEOPV("AstLte {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE"); // Identical operands on both sides // AstLogAnd/AstLogOr already converted to AstAnd/AstOr for these rules // AstAdd->ShiftL(#,1) but uncommon diff --git a/src/V3Premit.cpp b/src/V3Premit.cpp index 5d7dddaed..68cbcbba7 100644 --- a/src/V3Premit.cpp +++ b/src/V3Premit.cpp @@ -225,9 +225,9 @@ private: constwidthp->dtypeFrom (nodep->rhsp()); // unsigned AstCond* newp = new AstCond (nodep->fileline(), - new AstLte (nodep->fileline(), - nodep->rhsp()->cloneTree(false), - constwidthp), + new AstGte (nodep->fileline(), + constwidthp, + nodep->rhsp()->cloneTree(false)), nodep, constzerop); replaceHandle.relink(newp); diff --git a/src/V3Unknown.cpp b/src/V3Unknown.cpp index f44523f74..be10ab7bb 100644 --- a/src/V3Unknown.cpp +++ b/src/V3Unknown.cpp @@ -342,9 +342,9 @@ private: V3Number maxlsbnum (nodep->fileline(), nodep->lsbp()->width(), maxlsb); // See if the condition is constant true - AstNode* condp = new AstLte (nodep->fileline(), - nodep->lsbp()->cloneTree(false), - new AstConst(nodep->fileline(), maxlsbnum)); + AstNode* condp = new AstGte (nodep->fileline(), + new AstConst(nodep->fileline(), maxlsbnum), + nodep->lsbp()->cloneTree(false)); // Note below has null backp(); the Edit function knows how to deal with that. condp = V3Const::constifyEdit(condp); if (condp->isOne()) { @@ -400,9 +400,9 @@ private: V3Number widthnum (nodep->fileline(), nodep->bitp()->width(), declElements-1); // See if the condition is constant true - AstNode* condp = new AstLte (nodep->fileline(), - nodep->bitp()->cloneTree(false), - new AstConst(nodep->fileline(), widthnum)); + AstNode* condp = new AstGte (nodep->fileline(), + new AstConst(nodep->fileline(), widthnum), + nodep->bitp()->cloneTree(false)); // Note below has null backp(); the Edit function knows how to deal with that. condp = V3Const::constifyEdit(condp); if (condp->isOne()) { diff --git a/src/V3Unroll.cpp b/src/V3Unroll.cpp index e54971534..80d66f436 100644 --- a/src/V3Unroll.cpp +++ b/src/V3Unroll.cpp @@ -73,6 +73,7 @@ private: nodep->v3error("Unsupported: Can't unroll generate for; "<=9) nodep->dumpTree(cout,"-cant-"); V3Stats::addStatSum(string("Unrolling gave up, ")+reason, 1); return false; } @@ -158,20 +159,20 @@ private: if (!constIncp) return cantUnroll(nodep, "non-constant increment"); if (constIncp->isZero()) return cantUnroll(nodep, "zero increment"); // Or we could loop forever below... - bool lt = condp->castLt() || condp->castLtS(); + bool lt = condp->castLt() || condp->castLtS(); bool lte = condp->castLte() || condp->castLteS(); - bool gt = condp->castGt() || condp->castGtS(); + bool gt = condp->castGt() || condp->castGtS(); bool gte = condp->castGte() || condp->castGteS(); if (!lt && !lte && !gt && !gte) return cantUnroll(nodep, "condition not <= or <"); AstNodeBiop* condBip = condp->castNodeBiop(); - if (!condBip->lhsp()->castVarRef()) - return cantUnroll(nodep, "no variable on lhs of condition"); - if (condBip->lhsp()->castVarRef()->varp() != m_forVarp - || condBip->lhsp()->castVarRef()->varScopep() != m_forVscp) + if (!condBip->rhsp()->castVarRef()) + return cantUnroll(nodep, "no variable on rhs of condition"); + if (condBip->rhsp()->castVarRef()->varp() != m_forVarp + || condBip->rhsp()->castVarRef()->varScopep() != m_forVscp) return cantUnroll(nodep, "different variable in condition"); - if (m_generate) V3Const::constifyParamsEdit(condBip->rhsp()); // rhsp may change - AstConst* constStopp = condBip->rhsp()->castConst(); + if (m_generate) V3Const::constifyParamsEdit(condBip->lhsp()); // rhsp may change + AstConst* constStopp = condBip->lhsp()->castConst(); if (!constStopp) return cantUnroll(nodep, "non-constant final value"); UINFO(8, " Stop expr ok: "<num().toSInt(); vlsint32_t valStop = constStopp->num().toSInt(); - if (lte) valStop++; if (gte) valStop--; + if (gte) valStop++; if (lte) valStop--; // 23 >= a, handle as if 24 > a vlsint32_t valInc = constIncp->num().toSInt(); if (subtract) valInc = -valInc; UINFO(8," In Numbers: for (v="<fileline(), 1); - cmpInstrp->numberOperate(contin, loopValue, numStop); + cmpInstrp->numberOperate(contin, numStop, loopValue); if (contin.isEqZero()) { break; // Done with the loop } else {