Optimizer: Put constants on left side of compares to match other AstBiOps and extend optimizations. No runtime result change intended.
This commit is contained in:
parent
45bbae80e7
commit
dce4519995
|
|
@ -327,27 +327,27 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operandBiExtendConst(AstNodeBiop* nodep) {
|
bool operandBiExtendConst(AstNodeBiop* nodep) {
|
||||||
// Loop unrolling likes standalone compares
|
// Loop unrolling favors standalone compares
|
||||||
// EQ(EXTEND(xx{width3}), const{width32}) -> EQ(xx{3},const{3})
|
// EQ(const{width32}, EXTEND(xx{width3})) -> EQ(const{3}, xx{3})
|
||||||
// Beware that the constant must have zero bits (+ 1 if signed) or compare
|
// Beware that the constant must have zero bits (+ 1 if signed) or compare
|
||||||
// would be incorrect
|
// would be incorrect
|
||||||
AstExtend* extendp = nodep->lhsp()->castExtend();
|
AstExtend* extendp = nodep->rhsp()->castExtend();
|
||||||
if (!extendp) return false;
|
if (!extendp) return false;
|
||||||
AstNode* smallerp = extendp->lhsp();
|
AstNode* smallerp = extendp->lhsp();
|
||||||
int subsize = smallerp->width();
|
int subsize = smallerp->width();
|
||||||
AstConst* constp = nodep->rhsp()->castConst();
|
AstConst* constp = nodep->lhsp()->castConst();
|
||||||
if (!constp) return false;
|
if (!constp) return false;
|
||||||
if (!constp->num().isBitsZero(constp->width()-1, subsize)) return false;
|
if (!constp->num().isBitsZero(constp->width()-1, subsize)) return false;
|
||||||
//
|
//
|
||||||
if (debug()>=9) nodep->dumpTree(cout,"BI(EXTEND)-in:");
|
if (debug()>=9) nodep->dumpTree(cout,"BI(EXTEND)-in:");
|
||||||
smallerp->unlinkFrBack();
|
smallerp->unlinkFrBack();
|
||||||
extendp->unlinkFrBack()->deleteTree(); // aka nodep->lhsp.
|
extendp->unlinkFrBack()->deleteTree(); // aka nodep->lhsp.
|
||||||
nodep->lhsp(smallerp);
|
nodep->rhsp(smallerp);
|
||||||
|
|
||||||
constp->unlinkFrBack();
|
constp->unlinkFrBack();
|
||||||
V3Number num (constp->fileline(), subsize);
|
V3Number num (constp->fileline(), subsize);
|
||||||
num.opAssign(constp->num());
|
num.opAssign(constp->num());
|
||||||
nodep->rhsp(new AstConst(constp->fileline(), num));
|
nodep->lhsp(new AstConst(constp->fileline(), num));
|
||||||
constp->deleteTree(); constp=NULL;
|
constp->deleteTree(); constp=NULL;
|
||||||
if (debug()>=9) nodep->dumpTree(cout,"BI(EXTEND)-ou:");
|
if (debug()>=9) nodep->dumpTree(cout,"BI(EXTEND)-ou:");
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -1812,6 +1812,14 @@ private:
|
||||||
TREEOP ("AstNodeBiComAsv{operandAsvSame(nodep)}", "replaceAsv(nodep)");
|
TREEOP ("AstNodeBiComAsv{operandAsvSame(nodep)}", "replaceAsv(nodep)");
|
||||||
TREEOP ("AstNodeBiComAsv{operandAsvLUp(nodep)}", "replaceAsvLUp(nodep)");
|
TREEOP ("AstNodeBiComAsv{operandAsvLUp(nodep)}", "replaceAsvLUp(nodep)");
|
||||||
TREEOP ("AstNodeBiComAsv{operandAsvRUp(nodep)}", "replaceAsvRUp(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
|
// v--- *1* as These ops are always first, as we warn before replacing
|
||||||
TREEOP1("AstLt {$lhsp, $rhsp.isZero}", "replaceNumSigned(nodep,0)");
|
TREEOP1("AstLt {$lhsp, $rhsp.isZero}", "replaceNumSigned(nodep,0)");
|
||||||
TREEOP1("AstGte {$lhsp, $rhsp.isZero}", "replaceNumSigned(nodep,1)");
|
TREEOP1("AstGte {$lhsp, $rhsp.isZero}", "replaceNumSigned(nodep,1)");
|
||||||
|
|
@ -1861,12 +1869,12 @@ private:
|
||||||
TREEOP ("AstShiftR{operandShiftShift(nodep)}", "replaceShiftShift(nodep)");
|
TREEOP ("AstShiftR{operandShiftShift(nodep)}", "replaceShiftShift(nodep)");
|
||||||
TREEOP ("AstWordSel{operandWordOOB(nodep)}", "replaceZero(nodep)");
|
TREEOP ("AstWordSel{operandWordOOB(nodep)}", "replaceZero(nodep)");
|
||||||
// Compress out EXTENDs to appease loop unroller
|
// Compress out EXTENDs to appease loop unroller
|
||||||
TREEOPV("AstEq {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
TREEOPV("AstEq {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
||||||
TREEOPV("AstNeq {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
TREEOPV("AstNeq {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
||||||
TREEOPV("AstGt {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
TREEOPV("AstGt {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
||||||
TREEOPV("AstGte {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
TREEOPV("AstGte {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
||||||
TREEOPV("AstLt {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
TREEOPV("AstLt {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
||||||
TREEOPV("AstLte {$lhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
TREEOPV("AstLte {$rhsp.castExtend,operandBiExtendConst(nodep)}", "DONE");
|
||||||
// Identical operands on both sides
|
// Identical operands on both sides
|
||||||
// AstLogAnd/AstLogOr already converted to AstAnd/AstOr for these rules
|
// AstLogAnd/AstLogOr already converted to AstAnd/AstOr for these rules
|
||||||
// AstAdd->ShiftL(#,1) but uncommon
|
// AstAdd->ShiftL(#,1) but uncommon
|
||||||
|
|
|
||||||
|
|
@ -225,9 +225,9 @@ private:
|
||||||
constwidthp->dtypeFrom (nodep->rhsp()); // unsigned
|
constwidthp->dtypeFrom (nodep->rhsp()); // unsigned
|
||||||
AstCond* newp =
|
AstCond* newp =
|
||||||
new AstCond (nodep->fileline(),
|
new AstCond (nodep->fileline(),
|
||||||
new AstLte (nodep->fileline(),
|
new AstGte (nodep->fileline(),
|
||||||
nodep->rhsp()->cloneTree(false),
|
constwidthp,
|
||||||
constwidthp),
|
nodep->rhsp()->cloneTree(false)),
|
||||||
nodep,
|
nodep,
|
||||||
constzerop);
|
constzerop);
|
||||||
replaceHandle.relink(newp);
|
replaceHandle.relink(newp);
|
||||||
|
|
|
||||||
|
|
@ -342,9 +342,9 @@ private:
|
||||||
V3Number maxlsbnum (nodep->fileline(), nodep->lsbp()->width(), maxlsb);
|
V3Number maxlsbnum (nodep->fileline(), nodep->lsbp()->width(), maxlsb);
|
||||||
|
|
||||||
// See if the condition is constant true
|
// See if the condition is constant true
|
||||||
AstNode* condp = new AstLte (nodep->fileline(),
|
AstNode* condp = new AstGte (nodep->fileline(),
|
||||||
nodep->lsbp()->cloneTree(false),
|
new AstConst(nodep->fileline(), maxlsbnum),
|
||||||
new AstConst(nodep->fileline(), maxlsbnum));
|
nodep->lsbp()->cloneTree(false));
|
||||||
// Note below has null backp(); the Edit function knows how to deal with that.
|
// Note below has null backp(); the Edit function knows how to deal with that.
|
||||||
condp = V3Const::constifyEdit(condp);
|
condp = V3Const::constifyEdit(condp);
|
||||||
if (condp->isOne()) {
|
if (condp->isOne()) {
|
||||||
|
|
@ -400,9 +400,9 @@ private:
|
||||||
V3Number widthnum (nodep->fileline(), nodep->bitp()->width(), declElements-1);
|
V3Number widthnum (nodep->fileline(), nodep->bitp()->width(), declElements-1);
|
||||||
|
|
||||||
// See if the condition is constant true
|
// See if the condition is constant true
|
||||||
AstNode* condp = new AstLte (nodep->fileline(),
|
AstNode* condp = new AstGte (nodep->fileline(),
|
||||||
nodep->bitp()->cloneTree(false),
|
new AstConst(nodep->fileline(), widthnum),
|
||||||
new AstConst(nodep->fileline(), widthnum));
|
nodep->bitp()->cloneTree(false));
|
||||||
// Note below has null backp(); the Edit function knows how to deal with that.
|
// Note below has null backp(); the Edit function knows how to deal with that.
|
||||||
condp = V3Const::constifyEdit(condp);
|
condp = V3Const::constifyEdit(condp);
|
||||||
if (condp->isOne()) {
|
if (condp->isOne()) {
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,7 @@ private:
|
||||||
nodep->v3error("Unsupported: Can't unroll generate for; "<<reason);
|
nodep->v3error("Unsupported: Can't unroll generate for; "<<reason);
|
||||||
}
|
}
|
||||||
UINFO(3," Can't Unroll: "<<reason<<" :"<<nodep<<endl);
|
UINFO(3," Can't Unroll: "<<reason<<" :"<<nodep<<endl);
|
||||||
|
//if (debug()>=9) nodep->dumpTree(cout,"-cant-");
|
||||||
V3Stats::addStatSum(string("Unrolling gave up, ")+reason, 1);
|
V3Stats::addStatSum(string("Unrolling gave up, ")+reason, 1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -158,20 +159,20 @@ private:
|
||||||
if (!constIncp) return cantUnroll(nodep, "non-constant increment");
|
if (!constIncp) return cantUnroll(nodep, "non-constant increment");
|
||||||
if (constIncp->isZero()) return cantUnroll(nodep, "zero increment"); // Or we could loop forever below...
|
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 lte = condp->castLte() || condp->castLteS();
|
||||||
bool gt = condp->castGt() || condp->castGtS();
|
bool gt = condp->castGt() || condp->castGtS();
|
||||||
bool gte = condp->castGte() || condp->castGteS();
|
bool gte = condp->castGte() || condp->castGteS();
|
||||||
if (!lt && !lte && !gt && !gte)
|
if (!lt && !lte && !gt && !gte)
|
||||||
return cantUnroll(nodep, "condition not <= or <");
|
return cantUnroll(nodep, "condition not <= or <");
|
||||||
AstNodeBiop* condBip = condp->castNodeBiop();
|
AstNodeBiop* condBip = condp->castNodeBiop();
|
||||||
if (!condBip->lhsp()->castVarRef())
|
if (!condBip->rhsp()->castVarRef())
|
||||||
return cantUnroll(nodep, "no variable on lhs of condition");
|
return cantUnroll(nodep, "no variable on rhs of condition");
|
||||||
if (condBip->lhsp()->castVarRef()->varp() != m_forVarp
|
if (condBip->rhsp()->castVarRef()->varp() != m_forVarp
|
||||||
|| condBip->lhsp()->castVarRef()->varScopep() != m_forVscp)
|
|| condBip->rhsp()->castVarRef()->varScopep() != m_forVscp)
|
||||||
return cantUnroll(nodep, "different variable in condition");
|
return cantUnroll(nodep, "different variable in condition");
|
||||||
if (m_generate) V3Const::constifyParamsEdit(condBip->rhsp()); // rhsp may change
|
if (m_generate) V3Const::constifyParamsEdit(condBip->lhsp()); // rhsp may change
|
||||||
AstConst* constStopp = condBip->rhsp()->castConst();
|
AstConst* constStopp = condBip->lhsp()->castConst();
|
||||||
if (!constStopp) return cantUnroll(nodep, "non-constant final value");
|
if (!constStopp) return cantUnroll(nodep, "non-constant final value");
|
||||||
UINFO(8, " Stop expr ok: "<<constStopp<<endl);
|
UINFO(8, " Stop expr ok: "<<constStopp<<endl);
|
||||||
//
|
//
|
||||||
|
|
@ -181,7 +182,7 @@ private:
|
||||||
return cantUnroll(nodep, "init/final/increment too large or four state");
|
return cantUnroll(nodep, "init/final/increment too large or four state");
|
||||||
vlsint32_t valInit = constInitp->num().toSInt();
|
vlsint32_t valInit = constInitp->num().toSInt();
|
||||||
vlsint32_t valStop = constStopp->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();
|
vlsint32_t valInc = constIncp->num().toSInt();
|
||||||
if (subtract) valInc = -valInc;
|
if (subtract) valInc = -valInc;
|
||||||
UINFO(8," In Numbers: for (v="<<valInit<<"; v<"<<valStop<<"; v=v+"<<valInc<<")\n");
|
UINFO(8," In Numbers: for (v="<<valInit<<"; v<"<<valStop<<"; v=v+"<<valInc<<")\n");
|
||||||
|
|
@ -269,7 +270,7 @@ private:
|
||||||
UINFO(8," Looping "<<loopValue<<endl);
|
UINFO(8," Looping "<<loopValue<<endl);
|
||||||
// if loopValue<valStop
|
// if loopValue<valStop
|
||||||
V3Number contin (nodep->fileline(), 1);
|
V3Number contin (nodep->fileline(), 1);
|
||||||
cmpInstrp->numberOperate(contin, loopValue, numStop);
|
cmpInstrp->numberOperate(contin, numStop, loopValue);
|
||||||
if (contin.isEqZero()) {
|
if (contin.isEqZero()) {
|
||||||
break; // Done with the loop
|
break; // Done with the loop
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue