slowly replacing ICODE * with iterators

This commit is contained in:
Artur K 2011-12-13 21:03:35 +01:00
parent 87d1b4411c
commit b3fa570792
10 changed files with 59 additions and 47 deletions

View File

@ -6,12 +6,14 @@
#include <llvm/ADT/ilist_node.h> #include <llvm/ADT/ilist_node.h>
#include "types.h" #include "types.h"
#include "graph.h" #include "graph.h"
#include "icode.h"
/* Basic block (BB) node definition */ /* Basic block (BB) node definition */
struct Function; struct Function;
class CIcodeRec; class CIcodeRec;
struct BB; struct BB;
struct interval; struct interval;
struct ICODE; struct ICODE;
typedef union typedef union
{ {
dword ip; /* Out edge icode address */ dword ip; /* Out edge icode address */
@ -38,6 +40,8 @@ private:
public: public:
Int begin(); Int begin();
iICODE begin2();
iICODE end2();
Int end(); Int end();
Int rbegin(); Int rbegin();
Int rend(); Int rend();
@ -103,6 +107,7 @@ public:
const Function *getParent() const { return Parent; } const Function *getParent() const { return Parent; }
Function *getParent() { return Parent; } Function *getParent() { return Parent; }
void writeBB(ICODE *hli, Int lev, Function *pProc, Int *numLoc); void writeBB(ICODE *hli, Int lev, Function *pProc, Int *numLoc);
BB *rmJMP(Int marker, BB *pBB);
private: private:
Function *Parent; Function *Parent;

View File

@ -372,3 +372,4 @@ public:
boolT labelSrch(dword target, Int *pIndex); boolT labelSrch(dword target, Int *pIndex);
ICODE * GetIcode(int ip); ICODE * GetIcode(int ip);
}; };
typedef CIcodeRec::iterator iICODE;

View File

@ -4,6 +4,7 @@
****************************************************************************/ ****************************************************************************/
/* STATE TABLE */ /* STATE TABLE */
#include <cstring>
struct STATE struct STATE
{ {
dword IP; /* Offset into Image */ dword IP; /* Offset into Image */

View File

@ -354,6 +354,16 @@ int BB::begin()
{ {
return start; return start;
} }
iICODE BB::begin2()
{
return Parent->Icode.begin()+start;
}
iICODE BB::end2()
{
return Parent->Icode.begin()+start+length;
}
int BB::rbegin() int BB::rbegin()
{ {
return start+length-1; return start+length-1;

View File

@ -159,13 +159,12 @@ void Function::elimCondCodes ()
(Icode.GetLlOpcode(useAt-1) >= iJB) && (Icode.GetLlOpcode(useAt-1) >= iJB) &&
(Icode.GetLlOpcode(useAt-1) <= iJNS)) (Icode.GetLlOpcode(useAt-1) <= iJNS))
{ {
prev = Icode.GetIcode(pBB->inEdges[0]->start + ICODE & prev(pBB->back());
pBB->inEdges[0]->length - 1); if (prev.ic.hl.opcode == HLI_JCOND)
if (prev->ic.hl.opcode == HLI_JCOND)
{ {
exp = prev->ic.hl.oper.exp->clone(); exp = prev.ic.hl.oper.exp->clone();
exp->changeBoolOp (condOpJCond[Icode.GetLlOpcode(useAt-1)-iJB]); exp->changeBoolOp (condOpJCond[Icode.GetLlOpcode(useAt-1)-iJB]);
Icode[useAt-1].copyDU(*prev, eUSE, eUSE); Icode[useAt-1].copyDU(prev, eUSE, eUSE);
Icode[useAt-1].setJCond(exp); Icode[useAt-1].setJCond(exp);
} }
} }
@ -187,9 +186,9 @@ void Function::elimCondCodes ()
* analysis (eg: push si, would include si in LiveUse; although it * analysis (eg: push si, would include si in LiveUse; although it
* is not really meant to be a register that is used before defined). */ * is not really meant to be a register that is used before defined). */
void Function::genLiveKtes () void Function::genLiveKtes ()
{ Int i, j; {
Int i;
BB * pbb; BB * pbb;
ICODE * picode;
dword liveUse, def; dword liveUse, def;
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
@ -198,13 +197,12 @@ void Function::genLiveKtes ()
pbb = dfsLast[i]; pbb = dfsLast[i];
if (pbb->flg & INVALID_BB) if (pbb->flg & INVALID_BB)
continue; /* skip invalid BBs */ continue; /* skip invalid BBs */
for (j = pbb->start; j < (pbb->start + pbb->length); j++) for (auto j = pbb->begin2(); j != pbb->end2(); j++)
{ {
picode = Icode.GetIcode(j); if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE))
if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE))
{ {
liveUse |= (picode->du.use & ~def); liveUse |= (j->du.use & ~def);
def |= picode->du.def; def |= j->du.def;
} }
} }
pbb->liveUse = liveUse; pbb->liveUse = liveUse;
@ -257,8 +255,8 @@ void Function::liveRegAnalysis (dword in_liveOut)
picode = Icode.GetIcode(pbb->start + pbb->length - 1); picode = Icode.GetIcode(pbb->start + pbb->length - 1);
if (picode->ic.hl.opcode == HLI_RET) if (picode->ic.hl.opcode == HLI_RET)
{ {
picode->ic.hl.oper.exp = COND_EXPR::idID (&retVal, assert(pbb->back().loc_ip == pbb->start+pbb->length-1);
&localId, pbb->start + pbb->length - 1); picode->ic.hl.oper.exp = COND_EXPR::idID (&retVal, &localId, pbb->back().loc_ip);
picode->du.use = in_liveOut; picode->du.use = in_liveOut;
} }
} }

View File

@ -12,7 +12,7 @@
#endif #endif
#include "graph.h" #include "graph.h"
static BB * rmJMP(Function * pProc, Int marker, BB * pBB); //static BB * rmJMP(Function * pProc, Int marker, BB * pBB);
static void mergeFallThrough(Function * pProc, BB * pBB); static void mergeFallThrough(Function * pProc, BB * pBB);
static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, Int *first, Int *last); static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, Int *first, Int *last);
@ -163,12 +163,12 @@ void Function::markImpure()
{ {
if (Icode.GetLlFlag(i) & (SYM_USE | SYM_DEF)) if (Icode.GetLlFlag(i) & (SYM_USE | SYM_DEF))
{ {
psym = &symtab[Icode.GetIcode(i)->ic.ll.caseTbl.numEntries]; psym = &symtab[Icode[i].ic.ll.caseTbl.numEntries];
for (int c = (Int)psym->label; c < (Int)psym->label+psym->size; c++) for (int c = (Int)psym->label; c < (Int)psym->label+psym->size; c++)
{ {
if (BITMAP(c, BM_CODE)) if (BITMAP(c, BM_CODE))
{ {
Icode.SetLlFlag(i, IMPURE); Icode[i].SetLlFlag(IMPURE);
flg |= IMPURE; flg |= IMPURE;
break; break;
} }
@ -212,7 +212,7 @@ void Function::compressCFG()
for (i = 0; i < pBB->edges.size(); i++) for (i = 0; i < pBB->edges.size(); i++)
{ {
ip = pBB->rbegin(); ip = pBB->rbegin();
pNxt = rmJMP(this, ip, pBB->edges[i].BBptr); pNxt = pBB->edges[i].BBptr->rmJMP(ip, pBB->edges[i].BBptr);
if (not pBB->edges.empty()) /* Might have been clobbered */ if (not pBB->edges.empty()) /* Might have been clobbered */
{ {
@ -240,7 +240,6 @@ void Function::compressCFG()
pBB->index = UN_INIT; pBB->index = UN_INIT;
else else
{ {
pBB->edges.clear();
delete pBB; delete pBB;
stats.numBBaft--; stats.numBBaft--;
} }
@ -264,7 +263,7 @@ void Function::compressCFG()
/**************************************************************************** /****************************************************************************
* rmJMP - If BB addressed is just a JMP it is replaced with its target * rmJMP - If BB addressed is just a JMP it is replaced with its target
***************************************************************************/ ***************************************************************************/
static BB * rmJMP(Function * pProc, Int marker, BB * pBB) BB *BB::rmJMP(Int marker, BB * pBB)
{ {
marker += DFS_JMP; marker += DFS_JMP;
@ -290,15 +289,16 @@ static BB * rmJMP(Function * pProc, Int marker, BB * pBB)
{ {
/* We are going around in circles */ /* We are going around in circles */
pBB->nodeType = NOWHERE_NODE; pBB->nodeType = NOWHERE_NODE;
pProc->Icode.GetIcode(pBB->start)->ic.ll.immed.op = (dword)pBB->start; pBB->front().ic.ll.immed.op = (dword)pBB->start;
pProc->Icode.SetImmediateOp(pBB->start, (dword)pBB->start);
do { do {
pBB = pBB->edges[0].BBptr; pBB = pBB->edges[0].BBptr;
pBB->inEdges.pop_back(); // was --numInedges pBB->inEdges.pop_back(); // was --numInedges
if (! pBB->inEdges.empty()) if (! pBB->inEdges.empty())
{ {
pProc->Icode.SetLlFlag(pBB->start, NO_CODE); pBB->front().SetLlFlag(NO_CODE);
pProc->Icode.SetLlInvalid(pBB->start, TRUE); pBB->front().invalidate();
// pProc->Icode.SetLlFlag(pBB->start, NO_CODE);
// pProc->Icode.SetLlInvalid(pBB->start, TRUE);
} }
} while (pBB->nodeType != NOWHERE_NODE); } while (pBB->nodeType != NOWHERE_NODE);

View File

@ -183,8 +183,7 @@ void Function::highLevelGen()
break; break;
case iIMUL: rhs = COND_EXPR::boolOp (lhs, rhs, MUL); case iIMUL: rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode, lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode, NONE);
NONE);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
@ -215,8 +214,7 @@ void Function::highLevelGen()
break; break;
case iMUL: rhs = COND_EXPR::boolOp (lhs, rhs, MUL); case iMUL: rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode, lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode, NONE);
NONE);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;

View File

@ -27,6 +27,7 @@ CIcodeRec::~CIcodeRec()
ICODE * CIcodeRec::addIcode(ICODE *pIcode) ICODE * CIcodeRec::addIcode(ICODE *pIcode)
{ {
push_back(*pIcode); push_back(*pIcode);
back().loc_ip = size()-1;
return &back(); return &back();
} }

View File

@ -244,8 +244,8 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
case iJO: case iJNO: case iJP: case iJNP: case iJO: case iJNO: case iJP: case iJNP:
case iJCXZ: case iJCXZ:
{ STATE StCopy; { STATE StCopy;
int ip = Icode.GetNumIcodes()-1; /* Index of this jump */ int ip = Icode.size()-1; /* Index of this jump */
ICODE * prev = Icode.GetIcode(ip-1); /* Previous icode */ ICODE &prev(Icode.back()); /* Previous icode */
boolT fBranch = FALSE; boolT fBranch = FALSE;
pstate->JCond.regi = 0; pstate->JCond.regi = 0;
@ -253,13 +253,13 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
/* This sets up range check for indexed JMPs hopefully /* This sets up range check for indexed JMPs hopefully
* Handles JA/JAE for fall through and JB/JBE on branch * Handles JA/JAE for fall through and JB/JBE on branch
*/ */
if (ip > 0 && prev->ic.ll.opcode == iCMP && (prev->ic.ll.flg & I)) if (ip > 0 && prev.ic.ll.opcode == iCMP && (prev.ic.ll.flg & I))
{ {
pstate->JCond.immed = (int16)prev->ic.ll.immed.op; pstate->JCond.immed = (int16)prev.ic.ll.immed.op;
if (_Icode.ic.ll.opcode == iJA || _Icode.ic.ll.opcode == iJBE) if (_Icode.ic.ll.opcode == iJA || _Icode.ic.ll.opcode == iJBE)
pstate->JCond.immed++; pstate->JCond.immed++;
if (_Icode.ic.ll.opcode == iJAE || _Icode.ic.ll.opcode == iJA) if (_Icode.ic.ll.opcode == iJAE || _Icode.ic.ll.opcode == iJA)
pstate->JCond.regi = prev->ic.ll.dst.regi; pstate->JCond.regi = prev.ic.ll.dst.regi;
fBranch = (boolT) fBranch = (boolT)
(_Icode.ic.ll.opcode == iJB || _Icode.ic.ll.opcode == iJBE); (_Icode.ic.ll.opcode == iJB || _Icode.ic.ll.opcode == iJBE);
} }
@ -271,7 +271,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
if (fBranch) /* Do branching code */ if (fBranch) /* Do branching code */
{ {
pstate->JCond.regi = prev->ic.ll.dst.regi; pstate->JCond.regi = prev.ic.ll.dst.regi;
} }
/* Next icode. Note: not the same as GetLastIcode() because of the call /* Next icode. Note: not the same as GetLastIcode() because of the call
to FollowCtrl() */ to FollowCtrl() */
@ -687,8 +687,8 @@ static hlType cbType[] = {TYPE_UNKNOWN, TYPE_BYTE_UNSIGN, TYPE_WORD_SIGN,
* is checked and updated if the old size was less than the new size (ie. * is checked and updated if the old size was less than the new size (ie.
* the maximum size is always saved). */ * the maximum size is always saved). */
static SYM * updateGlobSym (dword operand, Int size, word duFlag) static SYM * updateGlobSym (dword operand, Int size, word duFlag)
{ {
Int i; Int i;
/* Check for symbol in symbol table */ /* Check for symbol in symbol table */
for (i = 0; i < symtab.size(); i++) for (i = 0; i < symtab.size(); i++)

View File

@ -22,7 +22,7 @@ static boolT isJCond (llIcode opcode)
/* Returns whether the conditions for a 2-3 long variable are satisfied */ /* Returns whether the conditions for a 2-3 long variable are satisfied */
static boolT isLong23 (Int i, BB * pbb, ICODE * icode, Int *off, Int *arc) static boolT isLong23 (Int i, BB * pbb, Int *off, Int *arc)
{ {
BB * t, * e, * obb2; BB * t, * e, * obb2;
@ -36,7 +36,7 @@ static boolT isLong23 (Int i, BB * pbb, ICODE * icode, Int *off, Int *arc)
{ {
obb2 = t->edges[THEN].BBptr; obb2 = t->edges[THEN].BBptr;
if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) && if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) &&
(icode[obb2->start].ic.ll.opcode == iCMP)) (obb2->front().ic.ll.opcode == iCMP))
{ {
*off = obb2->start - i; *off = obb2->start - i;
*arc = THEN; *arc = THEN;
@ -50,7 +50,7 @@ static boolT isLong23 (Int i, BB * pbb, ICODE * icode, Int *off, Int *arc)
{ {
obb2 = e->edges[THEN].BBptr; obb2 = e->edges[THEN].BBptr;
if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) && if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) &&
(icode[obb2->start].ic.ll.opcode == iCMP)) (obb2->front().ic.ll.opcode == iCMP))
{ {
*off = obb2->start - i; *off = obb2->start - i;
*arc = ELSE; *arc = ELSE;
@ -156,17 +156,16 @@ static void longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode,
stats.numBBaft -= 2; stats.numBBaft -= 2;
pIcode->invalidate(); pIcode->invalidate();
pProc->Icode.GetIcode(obb1->start)->invalidate(); obb1->front().invalidate();
pProc->Icode.GetIcode(obb2->start)->invalidate(); obb2->front().invalidate();
pProc->Icode.GetIcode(obb2->start+1)->invalidate(); (obb2->begin2()+1)->invalidate();
} }
/* Creates a long conditional equality or inequality at (pIcode+1). /* Creates a long conditional equality or inequality at (pIcode+1).
* Removes excess nodes from the graph by flagging them, and updates * Removes excess nodes from the graph by flagging them, and updates
* the new edges for the remaining nodes. */ * the new edges for the remaining nodes. */
static void longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode, static void longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode, Int *idx)
Int *idx)
{ {
Int j; Int j;
BB * pbb, * obb1, * tbb; BB * pbb, * obb1, * tbb;
@ -283,7 +282,7 @@ void Function::propLongStk (Int i, ID *pLocId)
} }
/* Check long conditional (i.e. 2 CMPs and 3 branches */ /* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, this->Icode.GetFirstIcode(),&off, &arc))) else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, &off, &arc)))
{ {
if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this, &rhs, &lhs, off) == TRUE) if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this, &rhs, &lhs, off) == TRUE)
longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off); longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off);
@ -448,8 +447,7 @@ void Function::propLongReg (Int i, ID *pLocId)
/* Check long conditional (i.e. 2 CMPs and 3 branches */ /* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ic.ll.opcode == iCMP) && else if ((pIcode->ic.ll.opcode == iCMP) &&
(isLong23 (idx, pIcode->inBB, this->Icode.GetFirstIcode(), (isLong23 (idx, pIcode->inBB, &off, &arc)))
&off, &arc)))
{ {
if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this, if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
&rhs, &lhs, off) == TRUE) &rhs, &lhs, off) == TRUE)