Ejemplo n.º 1
0
void SSABuilder::deconvertSSA(ControlFlowGraph* fg,OpndManager& opndManager) {
    const Nodes& nodes = fg->getNodes();
    Nodes::const_iterator niter;
    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst *inst = headInst->getNextInst(); inst != NULL; ) {
            Inst *nextInst = inst->getNextInst();
            if (inst->isPhi()) {
                inst->unlink();
            } else {
                for (U_32 i = 0; i < inst->getNumSrcOperands(); i++) {
                    Opnd *opnd = inst->getSrc(i);
                    if (opnd->isSsaVarOpnd()) {
                        SsaVarOpnd *ssa = (SsaVarOpnd *)opnd;
                        VarOpnd *var = ssa->getVar();
                        inst->setSrc(i,var);
                    } else if (opnd->isVarOpnd()) {
                    }
                }
                Opnd *dst = inst->getDst();
                if (dst->isSsaVarOpnd()) {
                    SsaVarOpnd *ssa = (SsaVarOpnd *)dst;
                    inst->setDst(ssa->getVar()); 
                } 
            }
            inst = nextInst;
        }
    }
}
Ejemplo n.º 2
0
void SSABuilder::clearPhiSrcs(Node *node, const StlVectorSet<VarOpnd *> *whatVars)
{
    Inst* phi = (Inst*)node->getSecondInst();
    if (whatVars) {
        for (;phi!=NULL && phi->isPhi(); phi = phi->getNextInst()) {
            Opnd *dstOp = phi->getDst();
            VarOpnd *varOpnd = dstOp->asVarOpnd();
            if (!varOpnd) {
                SsaVarOpnd *ssaOpnd = dstOp->asSsaVarOpnd();
                assert(ssaOpnd);
                varOpnd = ssaOpnd->getVar();
            }
            if (whatVars->has(varOpnd)) {
                PhiInst *phiInst = phi->asPhiInst();
                assert(phiInst);
                phiInst->setNumSrcs(0);
            }
        }
    } else {
        for (;phi!=NULL && phi->isPhi(); phi = phi->getNextInst()) {
            PhiInst *phiInst = phi->asPhiInst();
            assert(phiInst);
            phiInst->setNumSrcs(0);
        }
    }
}
Ejemplo n.º 3
0
void
DefUseBuilder::initialize(ControlFlowGraph& fg) {
    const Nodes& nodes = fg.getNodes();
    Nodes::const_iterator i;
    for(i = nodes.begin(); i != nodes.end(); ++i) {
        Node* node = *i;
        Inst* label = (Inst*)node->getFirstInst();
        for(Inst* inst = label->getNextInst(); inst != NULL; inst = inst->getNextInst())
            addUses(inst);
    }
}
Ejemplo n.º 4
0
U_32
EscapeAnalyzer::doAnalysis() {
    MemoryManager memManager("EscapeAnalyzer::doAnalysis");
    StlDeque<Inst*> candidateSet(memManager);
    BitSet escapingInsts(memManager,irManager.getInstFactory().getNumInsts());

    const Nodes& nodes = irManager.getFlowGraph().getNodes();
    Nodes::const_iterator niter;
    //
    // Clear all marks on instructions
    // Collect instructions that are candidate for escape optimizations
    //
    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst* inst=headInst->getNextInst();inst!=NULL;inst=inst->getNextInst()) {
            if (isEscapeOptimizationCandidate(inst))
                candidateSet.push_back(inst);
        }
    }
    //
    // Iteratively mark instructions whose results escape the method
    //
    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst* inst=headInst->getNextInst();inst!=NULL;inst=inst->getNextInst()) {
            if (isPotentiallyEscapingInst(inst) == false)
                continue;
            escapingInsts.setBit(inst->getId(),true);
            for (U_32 i=0; i<inst->getNumSrcOperands(); i++) {
                if (isEscapingSrcObject(inst,i) == false)
                    continue;
                // src escapes
                markEscapingInst(inst->getSrc(i)->getInst(),escapingInsts);
            }
        }
    }
    //
    // Print out non-escaping instructions
    //
    U_32 numTrapped = 0;
    while (candidateSet.empty() == false) {
         Inst* inst = candidateSet.front();
         candidateSet.pop_front();
         if (escapingInsts.getBit(inst->getId()))
             continue;
         numTrapped++;
     }
     return numTrapped;
}
Ejemplo n.º 5
0
void SSABuilder::checkForTrivialPhis2(Node *node, 
                                      const StlVectorSet<VarOpnd *> *lookatVars,
                                      StlVector<VarOpnd *> *changedVars,
                                      StlVector<Opnd *> *removedVars)
{
    // Check that phi insts can start from the second or third position only
    // and goes in a row
    assert(phiInstsOnRightPositionsInBB(node));

    Inst* phi = (Inst*)node->getSecondInst();
    if(phi && !phi->isPhi()) {
        // try the next one (third)
        phi = phi->getNextInst();
    }

    Inst *nextphi = NULL;
#ifdef DEBUG_SSA
    if (Log::isEnabled()) {
        Log::out() << "Checking node " << (int)node->getId() 
                   << " for trivial phis2" << ::std::endl;
    }
#endif
    for (;phi->isPhi(); phi = nextphi) {
        nextphi = phi->getNextInst();
#ifdef _DEBUG
        PhiInst *phiInst = phi->asPhiInst();
        assert(phiInst);
#endif
        U_32 nSrcs = phi->getNumSrcOperands();
        if (nSrcs <= 1) {
            // phi must be trivial
#ifdef DEBUG_SSA
            ::std::ostream &cout = Log::out();
            if (Log::isEnabled()) {
                cout << "removing trivial2 instruction "; phi->print(cout); cout << ::std::endl;
            }
#endif
            Opnd *dstOp = phi->getDst();
            VarOpnd *varOp = dstOp->asVarOpnd();
            if (!varOp) {
                SsaVarOpnd *ssaOp = dstOp->asSsaVarOpnd();
                assert(ssaOp);
                varOp = ssaOp->getVar();
            }
            assert(!lookatVars || (lookatVars->has(varOp)));
            changedVars->push_back(varOp);
            removedVars->push_back(dstOp);
            phi->unlink();
        }
    }
}
Ejemplo n.º 6
0
void
PersistentInstructionIdGenerator::runPass(IRManager& irm) {
    MemoryManager mm("PersistentInstructionIdGenerator::runPass");
    
    MethodDesc& methodDesc = irm.getMethodDesc();

    StlVector<Node*> nodes(mm);
    irm.getFlowGraph().getNodesPostOrder(nodes);

    StlVector<Node*>::reverse_iterator i;
    for(i = nodes.rbegin(); i != nodes.rend(); ++i) {
        Node* node = *i;
        Inst* label = (Inst*)node->getFirstInst();
        for(Inst* inst = label->getNextInst(); inst != NULL; inst = inst->getNextInst())
            inst->setPersistentInstructionId(PersistentInstructionId(&methodDesc, inst->getId() - irm.getMinimumInstId()));
    }
}
Ejemplo n.º 7
0
bool SSABuilder::phiInstsOnRightPositionsInBB(Node* node) {
    Inst* inst = (Inst*)node->getSecondInst();
    if(inst && !inst->isPhi()) {
        // try the next one (third)
        inst = inst->getNextInst();
    }
    // skip all phis
    while ( inst!=NULL && inst->isPhi() ) {
        inst = inst->getNextInst();
    }
    // 'true' only if there is no any other phis in the node
    while ( inst!=NULL ) {
        if(inst->isPhi()) {
            return false;
        }
        inst = inst->getNextInst();
    }
    return true;
} 
Ejemplo n.º 8
0
//
// find def sites (blocks) of var operand
//
void SSABuilder::findDefSites(DefSites& allDefSites) {
    const Nodes& nodes = fg->getNodes();
    Nodes::const_iterator niter;
    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        if (!node->isBlockNode()) continue;

        // go over each instruction to find var definition
        Inst* first = (Inst*)node->getFirstInst();
        for (Inst* inst = first->getNextInst(); inst != NULL; inst = inst->getNextInst()) {
            // look for var definitions
            if (!inst->isStVar()) 
                continue;

            assert(inst->isVarAccess());
            // if inst->getVar() return NULL, then inst is accessing SSAOpnd.
            // Hence, there is no need to do SSA transformation (addVarDefSite() 
            // immediately returns.
            allDefSites.addVarDefSite(((VarAccessInst*)inst)->getVar(),node);
        }
    }
}
Ejemplo n.º 9
0
U_32 
EscapeAnalyzer::doAggressiveAnalysis() {
    //
    // Initialization:
    //
    // (1) Ptrs & refs that are incoming args are free
    // (2) Ptrs & refs returned by calls are free
    // (3) Ptrs & refs returned by the method are free
    // (4) Refs that are thrown by the method are free
    // (5) Refs pass as args to calls are free (ptrs do not escape)
    // 
    // Iteration:
    //
    // (6) Refs that are stored through free ptrs or refs are free
    // (7) Refs loaded through free ptrs or refs are free
    //
    MemoryManager memManager("EscapeAnalyzer::doAggressiveAnalysis");
    //
    // work list of instructions that define free refs & ptrs
    //
    StlDeque<Inst*> freeWorkList(memManager);
    DefUseBuilder   defUseBuilder(memManager);
    //
    // Initialization step
    //
    const Nodes& nodes = irManager.getFlowGraph().getNodes();
    Nodes::const_iterator niter;
    Opnd *returnOpnd = irManager.getReturnOpnd();
    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst* inst=headInst->getNextInst();inst!=NULL; inst=inst->getNextInst()) {
            initialize(inst,freeWorkList,defUseBuilder,returnOpnd);
        }
    }
    //
    // Iteration step 
    // 
   while (freeWorkList.empty() == false) {
       freeWorkList.pop_front();
   }
   U_32 numTrapped = 0;
   return numTrapped;
}
Ejemplo n.º 10
0
PeepHoleOpt::Changed PeepHoleOpt::handleInst_SETcc(Inst* inst)
{
    if (((BasicBlock*)inst->getNode())->getLayoutSucc() == NULL)
    {
        Mnemonic mn = inst->getMnemonic();
        
        Inst* prev = inst->getPrevInst();
        Inst *next = inst->getNextInst();

        Node *currNode = inst->getNode();
        bool methodMarkerOccur = false;
        MethodMarkerPseudoInst* methodMarker = NULL;
        // ignoring instructions that have no effect and saving method markers to correct them during optimizations
        while (next == NULL || next->getKind() == Inst::Kind_MethodEndPseudoInst || next->getMnemonic() == Mnemonic_JMP)
        {
            if (next == NULL)
            {
                currNode = currNode->getOutEdge(Edge::Kind_Unconditional)->getTargetNode();
                if (currNode->getKind() == Node::Kind_Exit)
                    return Changed_Nothing;
                next = (Inst*) currNode->getFirstInst();
            }
            else
            {
                if (next->getKind() == Inst::Kind_MethodEndPseudoInst)
                {
                    //max 1 saved method marker
                    if (methodMarkerOccur)
                    {
                        return Changed_Nothing;
                    }
                    methodMarker = (MethodMarkerPseudoInst*)next;
                    methodMarkerOccur = true;
                }
                next = next->getNextInst();
            }
        }

        Inst *next2 = next->getNextInst();

        bool step1 = true;
        currNode = inst->getNode();
        while (currNode != next->getNode())
        {
            currNode = currNode->getOutEdge(Edge::Kind_Unconditional)->getTargetNode();
            if (currNode->getInDegree()!=1)
            {
                step1 = false;
                break;
            }
        }

        // step1:
        // ------------------------------------------
        // MOV opnd, 0                  MOV opnd2, 0
        // SETcc opnd           ->      SETcc opnd2
        // MOV opnd2, opnd
        // ------------------------------------------
        // nb: applicable if opnd will not be used further
        if (step1 && prev!= NULL && prev->getMnemonic() == Mnemonic_MOV &&
            next!= NULL && next->getMnemonic() == Mnemonic_MOV)
        {
            Opnd *prevopnd1, *prevopnd2, *nextopnd1, *nextopnd2, *setopnd;
            if (prev->getKind() == Inst::Kind_CopyPseudoInst)
            {
                prevopnd1 = prev->getOpnd(0);
                prevopnd2 = prev->getOpnd(1);
            }
            else
            {
                Inst::Opnds prevuses(prev, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
                Inst::Opnds prevdefs(prev, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
                prevopnd1 = prev->getOpnd(prevdefs.begin());
                prevopnd2 = prev->getOpnd(prevuses.begin());
            }
            if (next->getKind() == Inst::Kind_CopyPseudoInst)
            {
                nextopnd1 = next->getOpnd(0);
                nextopnd2 = next->getOpnd(1);
            }
            else
            {
                Inst::Opnds nextuses(next, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
                Inst::Opnds nextdefs(next, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
                nextopnd1 = next->getOpnd(nextdefs.begin());
                nextopnd2 = next->getOpnd(nextuses.begin());
            }
            Inst::Opnds setdefs(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
            setopnd = inst->getOpnd(setdefs.begin());

            if (isReg(nextopnd1) &&
                prevopnd1->getId() == setopnd->getId() &&
                setopnd->getId() == nextopnd2->getId() &&
                isImm(prevopnd2) && prevopnd2->getImmValue() == 0
                )
            {
                BitSet ls(irManager->getMemoryManager(), irManager->getOpndCount());
                irManager->updateLivenessInfo();
                irManager->getLiveAtExit(next->getNode(), ls);
                for (Inst* i = (Inst*)next->getNode()->getLastInst(); i!=next; i = i->getPrevInst()) {
                    irManager->updateLiveness(i, ls);
                }
                bool opndNotUsed = !ls.getBit(setopnd->getId());
                if (opndNotUsed)
                {
                    if (nextopnd1->getRegName() != RegName_Null &&
                        Constraint::getAliasRegName(nextopnd1->getRegName(), OpndSize_8) == RegName_Null)
                    {
                        nextopnd1->assignRegName(setopnd->getRegName());
                    }
                    irManager->newInst(Mnemonic_MOV, nextopnd1, prevopnd2)->insertBefore(inst);
                    irManager->newInst(mn, nextopnd1)->insertBefore(inst);
                    prev->unlink();
                    inst->unlink();
                    next->unlink();
                    return Changed_Node;
                }
            }
        }

        // step2:
        // --------------------------------------------------------------
        // MOV opnd, 0                  Jcc smwh                Jcc smwh
        // SETcc opnd           ->      BB1:            v       BB1:
        // CMP opnd, 0                  ...
        // Jcc smwh                     CMP opnd, 0
        // BB1:                         Jcc smwh
        // --------------------------------------------------------------
        // nb: applicable if opnd will not be used further
        // nb: conditions of new jumps are calculated from conditions of old jump and set instructions
	    if (prev!= NULL && prev->getMnemonic() == Mnemonic_MOV &&
            next!= NULL && (next->getMnemonic() == Mnemonic_CMP || next->getMnemonic() == Mnemonic_TEST) &&
            next2!= NULL && (next2->getMnemonic() == Mnemonic_JG || next2->getMnemonic() == Mnemonic_JE || next2->getMnemonic() == Mnemonic_JNE) )
	    {
            Opnd* movopnd1;
            Opnd* movopnd2;
            if (prev->getKind() == Inst::Kind_CopyPseudoInst)
            {
                movopnd1 = prev->getOpnd(0);
                movopnd2 = prev->getOpnd(1);
            }
            else
            {
                Inst::Opnds movuses(prev, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
                Inst::Opnds movdefs(prev, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
                movopnd1 = prev->getOpnd(movdefs.begin());
                movopnd2 = prev->getOpnd(movuses.begin());
            }
            Inst::Opnds cmpuses(next, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
            Opnd* cmpopnd1 = next->getOpnd(cmpuses.begin());
            Opnd* cmpopnd2 = next->getOpnd(cmpuses.next(cmpuses.begin()));

            if (
                isImm(movopnd2) && movopnd2->getImmValue() == 0 &&
                movopnd1->getId() == cmpopnd1->getId() &&
                //case CMP:
                (next->getMnemonic() != Mnemonic_CMP || (isImm(cmpopnd2) && cmpopnd2->getImmValue() == 0)) &&
                //case TEST:
                (next->getMnemonic() != Mnemonic_TEST || cmpopnd1->getId() == cmpopnd2->getId())
                )
            {
                BitSet ls(irManager->getMemoryManager(), irManager->getOpndCount());
                irManager->updateLivenessInfo();
                irManager->getLiveAtExit(next2->getNode(), ls);
                bool opndNotUsed = !ls.getBit(movopnd1->getId());
                if (opndNotUsed)
                {
                    BranchInst* br = (BranchInst*) next2;

                    Mnemonic newjumpmn = Mnemonic_JZ;
                    if (next2->getMnemonic() == Mnemonic_JE)
                    {
                        switch (mn)
                        {
                        case Mnemonic_SETG:
                            newjumpmn = Mnemonic_JLE; break;
                        case Mnemonic_SETE:
                            newjumpmn = Mnemonic_JNE; break;
                        case Mnemonic_SETL:
                            newjumpmn = Mnemonic_JGE; break;
                        case Mnemonic_SETNE:
                            newjumpmn = Mnemonic_JE; break;
                        default:
                            assert(0); break;
                        }
                    }
                    else
                    {
                        switch (mn)
                        {
                        case Mnemonic_SETG:
                            newjumpmn = Mnemonic_JG; break;
                        case Mnemonic_SETE:
                            newjumpmn = Mnemonic_JE; break;
                        case Mnemonic_SETL:
                            newjumpmn = Mnemonic_JL; break;
                        case Mnemonic_SETNE:
                            newjumpmn = Mnemonic_JNE; break;
                        default:
                            assert(0); break;
                        }
                    }

                    if (inst->getNode()->getId() != next->getNode()->getId())
                    {
                        ControlFlowGraph* cfg = irManager->getFlowGraph();
                        cfg->removeEdge(inst->getNode()->getOutEdge(Edge::Kind_Unconditional));

                        double trueEdgeProb = next2->getNode()->getOutEdge(Edge::Kind_True)->getEdgeProb();
                        double falseEdgeProb = next2->getNode()->getOutEdge(Edge::Kind_False)->getEdgeProb();
                        cfg->addEdge(inst->getNode(), br->getTrueTarget(), trueEdgeProb);
                        cfg->addEdge(inst->getNode(), br->getFalseTarget(), falseEdgeProb);
                        irManager->newBranchInst(newjumpmn, br->getTrueTarget(), br->getFalseTarget())->insertAfter(inst);
                        if (methodMarkerOccur)
                        {
                            inst->getNode()->appendInst(irManager->newMethodEndPseudoInst(methodMarker->getMethodDesc()));
                        }
		                prev->unlink();
		                inst->unlink();
                        cfg->purgeUnreachableNodes();
                    }
                    else
                    {
                        irManager->newBranchInst(newjumpmn, br->getTrueTarget(), br->getFalseTarget())->insertAfter(next2);
                        prev->unlink();
		                inst->unlink();
		                next->unlink();
                        next2->unlink();
                    }
		            return Changed_Node;
                }// endif opndNotUsed
            }
        }
    }
	return Changed_Nothing;
}
Ejemplo n.º 11
0
PeepHoleOpt::Changed PeepHoleOpt::handleInst_ALU(Inst* inst)
{
    // The normal form is 'OPERATION left opnd, right operand'
    // except for NOT operation.
    const Mnemonic mnemonic = inst->getMnemonic();
    if (mnemonic == Mnemonic_NOT) {
        // No optimizations this time
        return Changed_Nothing;
    }
    
    // Only these mnemonics have the majestic name of ALUs.
    assert(mnemonic == Mnemonic_ADD || mnemonic == Mnemonic_SUB ||
           mnemonic == Mnemonic_ADC || mnemonic == Mnemonic_SBB ||
           mnemonic == Mnemonic_OR || mnemonic == Mnemonic_XOR ||
           mnemonic == Mnemonic_AND ||
           mnemonic == Mnemonic_CMP || mnemonic == Mnemonic_TEST);
    
    if (mnemonic == Mnemonic_AND)
    {
        Inst::Opnds defs(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
        Opnd* dst = inst->getOpnd(defs.begin());
        Inst::Opnds uses(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
        Opnd* src1= inst->getOpnd(uses.begin());
        Opnd* src2= inst->getOpnd(uses.next(uses.begin()));

        Opnd *newopnd2;
        // test can work only with operands having equal sizes
        if (isImm(src2) && src2->getSize() != src1->getSize())
            newopnd2 = irManager->newImmOpnd(src1->getType(), src2->getImmValue());
        else
            newopnd2 = src2;
        if (!isMem(dst) && !isMem(src1) && !isMem(src2))
        {
            BitSet ls(irManager->getMemoryManager(), irManager->getOpndCount());
            irManager->updateLivenessInfo();
            irManager->getLiveAtExit(inst->getNode(), ls);
            for (Inst* i = (Inst*)inst->getNode()->getLastInst(); i!=inst; i = i->getPrevInst()) {
                irManager->updateLiveness(i, ls);
            }
            bool dstNotUsed = !ls.getBit(dst->getId());
            if (dstNotUsed)
            {
                // what: AND opnd1, opnd2 => TEST opnd1, opnd2
                // nb: applicable if opnd1 will not be used further
                
                if (inst->getForm() == Inst::Form_Extended)
                    irManager->newInstEx(Mnemonic_TEST, 0, src1, newopnd2)->insertAfter(inst);
                else
                    irManager->newInst(Mnemonic_TEST, src1, newopnd2)->insertAfter(inst);
                inst->unlink();
                return Changed_Inst;
            }
        }
    } else if (mnemonic == Mnemonic_ADD) {
        /* Change "dst=src+0" to "MOV dst, src" if there is another ADD inst followed in the same BB. */
        Inst::Opnds defs(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
        Opnd* dst = inst->getOpnd(defs.begin());
        Inst::Opnds uses(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
        Opnd* src1= inst->getOpnd(uses.begin());
        Opnd* src2= inst->getOpnd(uses.next(uses.begin()));

        bool src1IsZero = false;
        bool src2IsZero = false;
        if (src1->isPlacedIn(OpndKind_Imm) && (src1->getImmValue() == 0))
            src1IsZero = true;
        if (src2->isPlacedIn(OpndKind_Imm) && (src2->getImmValue() == 0))
            src2IsZero = true;

        bool anotherADD = false;
        Inst *iter = inst->getNextInst();
        while (iter != NULL) {
            if (iter->getMnemonic() == Mnemonic_ADC)
                break;
            if (iter->getMnemonic() == Mnemonic_ADD) {
                anotherADD = true;
                break;
            }
            iter = iter->getNextInst();;
        }

        if (anotherADD) {
            if (src1IsZero) {
                irManager->newCopyPseudoInst(Mnemonic_MOV, dst, src2)->insertAfter(inst);
                inst->unlink();
                return Changed_Inst;
            } else if (src2IsZero) {
                irManager->newCopyPseudoInst(Mnemonic_MOV, dst, src1)->insertAfter(inst);
                inst->unlink();
                return Changed_Inst;
            }
        }
    }
    return Changed_Nothing;
}
Ejemplo n.º 12
0
PeepHoleOpt::Changed PeepHoleOpt::handleInst_MOV(Inst* inst)
{
    Node* node = inst->getNode();
    if (((BasicBlock*)node)->getLayoutSucc() == NULL)
    {
        Inst *next = inst->getNextInst();

        Node *currNode = node;
        bool methodMarkerOccur = false;
        MethodMarkerPseudoInst* methodMarker = NULL;
        // ignoring instructions that have no effect and saving method markers to correct them during optimizations
        while (next == NULL || next->getKind() == Inst::Kind_MethodEndPseudoInst || next->getMnemonic() == Mnemonic_JMP)
        {
            if (next == NULL)
            {
                currNode = currNode->getOutEdge(Edge::Kind_Unconditional)->getTargetNode();
                if (currNode->getKind() == Node::Kind_Exit)
                    return Changed_Nothing;
                next = (Inst*) currNode->getFirstInst();
            }
            else
            {
                if (next->getKind() == Inst::Kind_MethodEndPseudoInst)
                {
                    //max 1 saved method marker
                    if (methodMarkerOccur)
                    {
                        return Changed_Nothing;
                    }
                    methodMarker = (MethodMarkerPseudoInst*)next;
                    methodMarkerOccur = true;
                }
                next = next->getNextInst();
            }
        }

        Inst *jump = next->getNextInst();


        bool step1 = true;
        currNode = node;
        while (currNode != next->getNode())
        {
            currNode = currNode->getOutEdge(Edge::Kind_Unconditional)->getTargetNode();
            if (currNode->getInDegree()!=1)
            {
                step1 = false;
                break;
            }
        }

        // step1:
        // ---------------------------------------------
        // MOV opnd, opnd2             MOV opnd3, opnd2
        // MOV opnd3, opnd      ->
        // ---------------------------------------------
        // nb: applicable if opnd will not be used further
        if (step1 && next->getMnemonic() == Mnemonic_MOV)
        {
            Opnd *movopnd1, *movopnd2, *nextmovopnd1, *nextmovopnd2;
            bool isInstCopyPseudo = (inst->getKind() == Inst::Kind_CopyPseudoInst);
            if (isInstCopyPseudo)
            {
                movopnd1 = inst->getOpnd(0);
                movopnd2 = inst->getOpnd(1);
            }
            else
            {
                Inst::Opnds movuses(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
                Inst::Opnds movdefs(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
                movopnd1 = inst->getOpnd(movdefs.begin());
                movopnd2 = inst->getOpnd(movuses.begin());
            }
            bool isNextCopyPseudo = (next->getKind() == Inst::Kind_CopyPseudoInst);
            if (isNextCopyPseudo)
            {
                nextmovopnd1 = next->getOpnd(0);
                nextmovopnd2 = next->getOpnd(1);
            }
            else
            {
                Inst::Opnds nextmovuses(next, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
                Inst::Opnds nextmovdefs(next, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
                nextmovopnd1 = next->getOpnd(nextmovdefs.begin());
                nextmovopnd2 = next->getOpnd(nextmovuses.begin());
            }
            if (movopnd1->getId() == nextmovopnd2->getId() && 
                !isMem(movopnd2) && !isMem(nextmovopnd1) &&
                !isMem(movopnd1)
                )
            {
                BitSet ls(irManager->getMemoryManager(), irManager->getOpndCount());
                irManager->updateLivenessInfo();
                irManager->getLiveAtExit(next->getNode(), ls);
                for (Inst* i = (Inst*)next->getNode()->getLastInst(); i!=next; i = i->getPrevInst()) {
                    irManager->updateLiveness(i, ls);
                }
                bool dstNotUsed = !ls.getBit(movopnd1->getId());
                if (dstNotUsed)
                {
                    if (isInstCopyPseudo && isNextCopyPseudo)
                        irManager->newCopyPseudoInst(Mnemonic_MOV, nextmovopnd1, movopnd2)->insertAfter(inst);
                    else
                        irManager->newInst(Mnemonic_MOV, nextmovopnd1, movopnd2)->insertAfter(inst);
                    inst->unlink();
                    next->unlink();
                    return Changed_Node;
                }
            }
        }

        // step2:
        // --------------------------------------------------------------
        // MOV opnd, 0/1                Jmp smwh/BB1            Jmp smwh/BB1
        // CMP opnd, 0           ->     CMP opnd, 0     v
        // Jcc smwh                     Jcc smwh
        // BB1:                         BB1:
        // --------------------------------------------------------------
        // nb: applicable if opnd will not be used further
        if (next->getMnemonic() == Mnemonic_CMP && jump!= NULL && (jump->getMnemonic() == Mnemonic_JE ||
            jump->getMnemonic() == Mnemonic_JNE))
        {
            Opnd *movopnd1, *movopnd2;
            if (inst->getKind() == Inst::Kind_CopyPseudoInst)
            {
                movopnd1 = inst->getOpnd(0);
                movopnd2 = inst->getOpnd(1);
            }
            else
            {
                Inst::Opnds movuses(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
                Inst::Opnds movdefs(inst, Inst::OpndRole_Explicit|Inst::OpndRole_Def);
                movopnd1 = inst->getOpnd(movdefs.begin());
                movopnd2 = inst->getOpnd(movuses.begin());
            }
            Inst::Opnds cmpuses(next, Inst::OpndRole_Explicit|Inst::OpndRole_Use);
            Opnd* cmpopnd1 = next->getOpnd(cmpuses.begin());
            Opnd* cmpopnd2 = next->getOpnd(cmpuses.next(cmpuses.begin()));
            
            if (isImm(movopnd2) && (movopnd2->getImmValue() == 0 || movopnd2->getImmValue() == 1) &&
                movopnd1->getId() == cmpopnd1->getId() &&
                isImm(cmpopnd2) && cmpopnd2->getImmValue() == 0)
            {
                BitSet ls(irManager->getMemoryManager(), irManager->getOpndCount());
                irManager->updateLivenessInfo();
                irManager->getLiveAtExit(jump->getNode(), ls);
                bool opndNotUsed = !ls.getBit(movopnd1->getId());
                if (opndNotUsed)
                {
                    ControlFlowGraph* cfg = irManager->getFlowGraph();
                    Node* destination = ((BranchInst*)jump)->getTrueTarget();
                    if ((jump->getMnemonic() == Mnemonic_JNE || movopnd2->getImmValue() == 1) && !(jump->getMnemonic() == Mnemonic_JNE && movopnd2->getImmValue() == 1))
                    {
                        destination = ((BranchInst*)jump)->getFalseTarget();
                    }
                    if (node->getId() != next->getNode()->getId())
                    {
                        if (methodMarkerOccur)
                        {
                            inst->getNode()->appendInst(irManager->newMethodEndPseudoInst(methodMarker->getMethodDesc()));
                        }
                        inst->unlink();
                        Edge *outEdge = node->getOutEdge(Edge::Kind_Unconditional);
                        cfg->replaceEdgeTarget(outEdge, destination, true);
                        cfg->purgeUnreachableNodes(); // previous successor may become unreachable
                    }
                    else
                    {
                        cfg->removeEdge(node->getOutEdge(Edge::Kind_True));
                        cfg->removeEdge(node->getOutEdge(Edge::Kind_False));
                        cfg->addEdge(node, destination);
                        inst->unlink();
                        next->unlink();
                        jump->unlink();
                    }

                    return Changed_Node;
                }
            }
        }
    }
    return Changed_Nothing;
}
Ejemplo n.º 13
0
void SSABuilder::clearPhiSrcs2(Node *node, 
                               const StlVectorSet<VarOpnd *> *whatVars,
                               StlVector<VarOpnd *> *changedVars,
                               const StlVectorSet<Opnd *> *removedVars,
                               StlVector<Node *> &scratchNodeList)
{
    bool needPreds = true;
    StlVector<Node *> &preds = scratchNodeList;

    Inst* inst = (Inst*)node->getSecondInst();
    for (;inst!=NULL && inst->isPhi(); inst = inst->getNextInst()) {
        Opnd *dstOp =inst->getDst();
        VarOpnd *varOpnd = 0;
        if (whatVars) {
            varOpnd = dstOp->asVarOpnd();
            if (!varOpnd) {
                SsaVarOpnd *ssaOpnd = dstOp->asSsaVarOpnd();
                assert(ssaOpnd);
                varOpnd = ssaOpnd->getVar();
            }
            if (!whatVars->has(varOpnd))
                continue;
        }
        bool changed=false;
        U_32 numSrcs = inst->getNumSrcOperands();
        for (U_32 i=0; i<numSrcs; ++i) {
            Opnd *thisOpnd = inst->getSrc(i);

            if (!(removedVars && removedVars->has(thisOpnd))) {
                // need to test whether need to remove
                if (needPreds) {
                    needPreds = false;
                    
                    const Edges& edges2 = node->getInEdges();
                    preds.clear();
                    preds.reserve(edges2.size());
                    Edges::const_iterator eiter2;
                    for(eiter2 = edges2.begin(); eiter2 != edges2.end(); ++eiter2){
                        preds.push_back((*eiter2)->getSourceNode());
                    }
                }
                DominatorTree &domTree = frontier.getDominator();
                Inst *thisOpndInst = thisOpnd->getInst();
                Node *thisOpndInstNode = thisOpndInst->getNode();
                if (thisOpndInstNode) {
                    // the operand's source instruction was not already dead.
                    StlVector<Node *>::const_iterator 
                        predIter = preds.begin(),
                        predEnd = preds.end();
                    bool foundDom = false;
                    for ( ; predIter != predEnd; ++predIter) {
                        Node *predNode = *predIter;
                        if (domTree.dominates(thisOpndInstNode, predNode)) {
                            // we found it, leave this operand alone.
                            foundDom = true;
                            break;
                        }
                    }
                    if (foundDom) continue; // leave operand alone.
                }
            }
            // remove this operand;
            if (i < numSrcs-1) {
                inst->setSrc(i, inst->getSrc(numSrcs-1));
                --i; // re-examine this operand, which is now the last
            }
            --numSrcs; // we deleted one operand
            PhiInst *phiInst = inst->asPhiInst();
            assert(phiInst);
            phiInst->setNumSrcs(numSrcs);
            changed = true;
        }
        if (changed) {
            // note the changed var;
            if (!varOpnd) {
                varOpnd = dstOp->asVarOpnd();
                if (!varOpnd) {
                    SsaVarOpnd *ssaOpnd = dstOp->asSsaVarOpnd();
                    assert(ssaOpnd);
                    varOpnd = ssaOpnd->getVar();
                }
            }
            changedVars->push_back(varOpnd);
        }
    }
}
Ejemplo n.º 14
0
//
// traverse dominator tree and rename variables
//
void SSABuilder::renameNode(RenameStack *renameStack, DominatorNode* dt,
                            const StlVectorSet<VarOpnd *> *whatVars)
{
    if (dt == NULL) return;
    Node* node = dt->getNode();

    Inst* head = (Inst*)node->getFirstInst();
#ifdef DEBUG_SSA
    std::ostream &cout = Log::out();
    if (Log::isEnabled()) {
        cout << "renameNode "; FlowGraph::printLabel(cout, node); cout << std::endl;
    }
#endif
    for (Inst* i = head->getNextInst(); i != NULL; i = i->getNextInst()) {
        if (!i->isPhi()) {
            // replace src with ssa opnd
            U_32 nSrcs = i->getNumSrcOperands();
            for (U_32 j = 0; j < nSrcs; j++) {
                Opnd *srcj = i->getSrc(j);
                VarOpnd *srcjVar = (srcj->isSsaVarOpnd()
                                    ? srcj->asSsaVarOpnd()->getVar()
                                    : srcj->asVarOpnd());
                if (!(srcjVar && !srcjVar->isAddrTaken()))
                    continue;
                if (whatVars && !whatVars->has(srcjVar)) continue;
                SsaVarOpnd* ssa = renameStack->lookup(srcjVar);
                assert(ssa);
                i->setSrc(j,ssa);
            }
        }

        // for both Phi and non-Phi
        // we replace any non-ssa dst with a new ssa opnd
        // and record it in the RenameStack map
        Opnd* dst = i->getDst();
        VarOpnd *theVar = dst->asVarOpnd();

        if (theVar && (!theVar->isAddrTaken())
            && !(whatVars && !whatVars->has(theVar))) {

            SsaVarOpnd* ssaDst = opndManager.createSsaVarOpnd((VarOpnd*)dst);
#ifdef DEBUG_SSA
            if (Log::isEnabled()) {
                cout << "SSA "; ssaDst->print(cout); cout << ::std::endl;
            }
#endif
            renameStack->insert((VarOpnd*)dst, ssaDst);
            i->setDst(ssaDst);
#ifdef DEBUG_SSA
            if (Log::isEnabled()) {
                i->print(cout); cout << ::std::endl;
            }
#endif
            // record stVar inst
        } else if (dst->isSsaVarOpnd()) {
            SsaVarOpnd* ssaDst = dst->asSsaVarOpnd();
            theVar = ssaDst->getVar();
            if (whatVars && !whatVars->has(theVar))
                continue;
#ifdef DEBUG_SSA
            if (Log::isEnabled()) {
                cout << "SSA "; ssaDst->print(cout); cout << ::std::endl;
            }
#endif
            renameStack->insert(ssaDst->getVar(), ssaDst);
#ifdef DEBUG_SSA
            if (Log::isEnabled()) {
                i->print(cout); cout << ::std::endl;
            }
#endif
        }
    }

    // add var sources to following phi instructions
    const Edges& edges = node->getOutEdges();
    Edges::const_iterator 
        eiter = edges.begin(),
        eend = edges.end();
    for(eiter = edges.begin(); eiter != eend; ++eiter) {
        Edge* e = *eiter;
        Node* succ = e->getTargetNode();
        // Phi insts are inserted to the beginning of the block
        // if succ does not have phi insts, then we can skip  it
        Inst *phi = (Inst*)succ->getSecondInst();
        if (phi==NULL || !phi->isPhi()) continue;

        // node is jth predecessor for succ
        
        
        // replace jth var of phi insts
        Inst* nextphi = phi->getNextInst();
        for (;phi!=NULL && phi->isPhi(); phi = nextphi) {
            nextphi = phi->getNextInst();
            // get var 
            Opnd *theopnd = phi->getDst();
            VarOpnd *thevar = theopnd->asVarOpnd();

            if (!thevar) {
                SsaVarOpnd *theSsaVar = theopnd->asSsaVarOpnd();
                assert(theSsaVar);

#ifdef DEBUG_SSA
                if (Log::isEnabled()) {
                    Log::out() << "case 2" << ::std::endl;
                }
#endif
                thevar = theSsaVar->getVar();
            }

            if (whatVars && !whatVars->has(thevar)) continue;

            SsaVarOpnd* ssa = renameStack->lookup((VarOpnd*)thevar);

            if (ssa != NULL) {
#ifdef DEBUG_SSA
                if (Log::isEnabled()) {
                    cout << "redge";
//                    cout << (I_32)j;
                    cout << " with ssa "; ssa->print(cout); cout << ::std::endl;
                }
#endif
                addPhiSrc((PhiInst*)phi,ssa);
            } else {
#ifdef DEBUG_SSA
                if (Log::isEnabled()) {
                    cout << "no source for phi of var ";
                    thevar->print(cout);
                    cout << ::std::endl;
                }
#endif
                // if ssa is NULL, then the phi must be a dead phi inst
                // (no more use of var afterwards). it will be removed.
            }
        }
    }
}
Ejemplo n.º 15
0
// rename vars to make un-overlapping live ranges of a variable into
// different variables.
void SSABuilder::splitSsaWebs(ControlFlowGraph* fg,OpndManager& opndManager) {
    U_32 numSsaOpnds = opndManager.getNumSsaOpnds();
    MemoryManager localMM("SSABuilder::splitSsaWebs::memManager");
    SsaVarClique *cliques = new (localMM) SsaVarClique[numSsaOpnds];
    
    const Nodes& nodes = fg->getNodes();
    Nodes::const_iterator niter;
    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst *inst = headInst->getNextInst(); inst != NULL; ) {
            Inst *nextInst = inst->getNextInst();
            if (inst->isPhi()) {
                // do something
                VarOpnd *var0 = 0;
                SsaVarClique *clique = 0;
                for (U_32 i = 0; i < inst->getNumSrcOperands(); i++) {
                    Opnd *opnd = inst->getSrc(i);
                    if (opnd->isSsaVarOpnd()) {
                        SsaVarOpnd *ssa = (SsaVarOpnd *)opnd;
                        U_32 id = ssa->getId();
                        if (var0) {
                            assert(ssa->getVar()==var0);
                            cliques[id].link(clique);
                        } else {
                            var0 = ssa->getVar();
                            clique = &cliques[id];
                        }
                    }
                }
                Opnd *dst = inst->getDst();
                if (dst->isSsaVarOpnd()) {
                    SsaVarOpnd *ssa = (SsaVarOpnd *)dst;
                    ssa->getVar();
                    U_32 id = ssa->getId();
                    if (var0) {
                        assert(ssa->getVar()==var0);
                        cliques[id].link(clique);
                    } else {
                        var0 = ssa->getVar();
                        clique = &cliques[id];
                    }
                }
            }
            inst = nextInst;
        }
    }
    U_32 numvars = opndManager.getNumVarOpnds();
    bool *used = new (localMM) bool[numvars];
    for (U_32 i=0; i<numvars; i++) {
        used[i] = false;
    }

    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst *inst = headInst->getNextInst(); inst != NULL; ) {
            Inst *nextInst = inst->getNextInst();

            for (U_32 i = 0; i < inst->getNumSrcOperands(); i++) {
                Opnd *opnd = inst->getSrc(i);
                if (opnd->isSsaVarOpnd()) {
                    SsaVarOpnd *ssa = (SsaVarOpnd *)opnd;
                    VarOpnd *var = ssa->getVar();
                    U_32 id=ssa->getId();
                    SsaVarClique *clique = &cliques[id];
                    clique = clique->getRoot();
                    VarOpnd *cvar = clique->var;
                    if (cvar == 0) {
                        U_32 varId = var->getId();
                        if (used[varId]) {
                            cvar = opndManager.createVarOpnd(var->getType(),
                                                             var->isPinned());
                        } else {
                            cvar = var;
                            used[varId] = true;
                        }
                        clique->var = cvar;
                    }
                    if (cvar != var) {
                        ssa->setVar(cvar);
                    }
                }
            }
            Opnd *dst = inst->getDst();
            if (dst->isSsaVarOpnd()) {
                SsaVarOpnd *ssa = (SsaVarOpnd *)dst;
                VarOpnd *var = ssa->getVar();
                U_32 id=ssa->getId();

                SsaVarClique *clique = &cliques[id];
                clique = clique->getRoot();
                VarOpnd *cvar = clique->var;
                if (cvar == 0) {
                    U_32 varId = var->getId();
                    if (used[varId]) {
                        cvar = opndManager.createVarOpnd(var->getType(),
                                                         var->isPinned());
                    } else {
                        cvar = var;
                        used[varId] = true;
                    }
                    clique->var = cvar;
                }
                if (cvar != var) {
                    ssa->setVar(cvar);
                }
            } 
            
            inst = nextInst;
        }
    }
}
Ejemplo n.º 16
0
Node * JavaFlowGraphBuilder::edgesForBlock(Node* block) {
    //
    // find if this block has any region that could catch the exception
    //
    Node *dispatch = NULL;
    ExceptionInfo *exceptionInfo = (CatchBlock*)((LabelInst*)block->getFirstInst())->getState();
    if (exceptionInfo != NULL) {
        dispatch = exceptionInfo->getLabelInst()->getNode();
    }else{
        dispatch = fg->getUnwindNode();
    }
    assert(dispatch->isDispatchNode());

    //
    // split the block so that 
    //      each potentially-exceptional instruction ends a block
    //
    Inst* first = (Inst*)block->getFirstInst();
    Inst* last = (Inst*)block->getLastInst();
    Inst* lastExceptionalInstSeen = NULL;
    for (Inst* inst = first->getNextInst(); inst != NULL; inst = inst->getNextInst()) {
        if (lastExceptionalInstSeen != NULL) {
            // start a new basic block
            LabelInst* label = irBuilder.getInstFactory()->makeLabel();
            Node *newblock = createBlockNodeAfter(block, label); 
            uint16 bcOffset = ILLEGAL_BC_MAPPING_VALUE;
            for (Inst *ins = lastExceptionalInstSeen->getNextInst(), *nextIns = NULL; ins!=NULL; ins = nextIns) {
                nextIns = ins->getNextInst();
                ins->unlink();
                newblock->appendInst(ins);
                if (bcOffset == ILLEGAL_BC_MAPPING_VALUE) {
                    bcOffset = ins->getBCOffset();
                }
            }
            label->setBCOffset(bcOffset);

            // now fix up the CFG, duplicating edges
            if (!lastExceptionalInstSeen->isThrow())
                fg->addEdge(block,newblock);
            //
            // add an edge to handler entry node
            //
            assert(!block->findTargetEdge(dispatch));
            fg->addEdge(block,dispatch);
            block = newblock;
            lastExceptionalInstSeen = NULL;
        } 
        if (inst->getOperation().canThrow()) {
            lastExceptionalInstSeen = inst;
        }
    }

    // 
    // examine the last instruction and create appropriate CFG edges
    //
    switch(last->getOpcode()) {
    case Op_Jump:
        {
        fg->addEdge(block,((BranchInst*)last)->getTargetLabel()->getNode());
        last->unlink();
        }
        break;
    case Op_Branch:
    case Op_JSR:
        addEdge(block, ((BranchInst*)last)->getTargetLabel()->getNode());
        edgeForFallthrough(block);
        break;
    case Op_Throw:
    case Op_ThrowSystemException:
    case Op_ThrowLinkingException:
        // throw/rethrow creates an edge to a handler that catches the exception
        assert(dispatch != NULL);
        assert(lastExceptionalInstSeen == last);
        break;
    case Op_Return:
        addEdge(block, fg->getReturnNode());
        break;
    case Op_Ret:
        break; // do  not do anything
    case Op_Switch:
        {
            SwitchInst *sw = (SwitchInst*)last;
            U_32 num = sw->getNumTargets();
            for (U_32 i = 0; i < num; i++) {
                Node* target = sw->getTarget(i)->getNode();
                // two switch values may go to the same block
                if (!block->findTargetEdge(target)) {
                    fg->addEdge(block,target);
                }
            }
            Node* target = sw->getDefaultTarget()->getNode();
            if (!block->findTargetEdge(target)) {
                fg->addEdge(block,target);
            }
        }
        break;
    default:;
        if (block != fg->getReturnNode()) { // a fallthrough edge is needed
           // if the basic block does not have any outgoing edge, add one fall through edge
           if (block->getOutEdges().empty())
               edgeForFallthrough(block);
        }
    }
    //
    // add an edge to handler entry node
    //
    if (lastExceptionalInstSeen != NULL)
        addEdge(block,dispatch);
    return block;
}
//___________________________________________________________________________________________________
void EarlyPropagation::runImpl()
{ 
        irManager->updateLoopInfo();
        U_32 opndCount=irManager->getOpndCount();

        MemoryManager mm("early_prop");
        OpndInfo * opndInfos = new(mm) OpndInfo[opndCount];
        Node * currentLoopHeader = NULL;

        bool anyInstHandled=false;

        LoopTree* lt = irManager->getFlowGraph()->getLoopTree();
        const Nodes& postOrdered = irManager->getFlowGraph()->getNodesPostOrder();
        for (Nodes::const_reverse_iterator it = postOrdered.rbegin(), end = postOrdered.rend(); it!=end; ++it) {
            Node * node=*it;
            if (!node->isBlockNode())  {
                continue;
            }
            Node * loopHeader = lt->getLoopHeader(node, false);
            if (currentLoopHeader != loopHeader){
                currentLoopHeader = loopHeader;
                for (U_32 i = 0; i < opndCount; ++i)
                    if (opndInfos[i].sourceOpndId != EmptyUint32)
                        opndInfos[i].defCount++;
            }

            for (Inst * inst = (Inst*)node->getFirstInst(); inst != NULL; inst=inst->getNextInst()){
                bool assignedOpndPropagated = false;
                Inst::Opnds opnds(inst, Inst::OpndRole_All);
                for (Inst::Opnds::iterator it = opnds.begin(); it != opnds.end(); it = opnds.next(it)){
                    Opnd * opnd=inst->getOpnd(it);
                    U_32 roles=inst->getOpndRoles(it);
                    U_32 opndId = opnd->getId();
                    OpndInfo& opndInfo = opndInfos[opndId];

                    U_32 mask = 0;

                    if (roles & Inst::OpndRole_Def){
                        ++opndInfo.defCount;
                    }else if (roles & Inst::OpndRole_Use){
                        if (opndInfo.sourceOpndId != EmptyUint32){
                            if (opndInfo.sourceOpndDefCountAtCopy < opndInfos[opndInfo.sourceOpndId].defCount)
                                opndInfo.sourceOpndId = EmptyUint32;
                            else{
                                Opnd * srcOpnd = irManager->getOpnd(opndInfo.sourceOpndId);
                                Constraint co = srcOpnd->getConstraint(Opnd::ConstraintKind_Location);
                                if (co.getKind() == OpndKind_Mem){
                                    mask = (1<<it)-1;
                                    if ((roles & Inst::OpndRole_Explicit) == 0 ||
                                        inst->hasKind(Inst::Kind_PseudoInst) || irManager->isGCSafePoint(inst) ||
                                        opndInfo.sourceInst != inst->getPrevInst() || assignedOpndPropagated ||
                                    (inst->getConstraint(it, mask, co.getSize())&co).isNull()
                                    )
                                        opndInfo.sourceOpndId = EmptyUint32;
                                    assignedOpndPropagated = true;
                                }
                            }
                        }
                    }
                    if (opndInfo.defCount > 1){
                        opndInfo.sourceOpndId = EmptyUint32;
                    }
                }
                /*
                Here is the previous version to test whether the inst is copy or not.
                bool isCopy = inst->getMnemonic() == Mnemonic_MOV ||(
                        (inst->getMnemonic() == Mnemonic_ADD || inst->getMnemonic() == Mnemonic_SUB) && 
                        inst->getOpnd(3)->isPlacedIn(OpndKind_Imm) && inst->getOpnd(3)->getImmValue()==0
                        && inst->getOpnd(3)->getRuntimeInfo()==NULL
                    );
                It considered special case of 'dst = src +/- 0' as copy. 
                In fact there are more similar cases like 'IMUL src, 1 ; shift src, 0' etc.
                Such checks are obsolete now, Should as peephole takes care about such copies.

                Anyway, the code above had a bug: 'inst->getOpnd(3)' crashes in instructions 
                in native form (like ADD def_use, use).
                */
                const bool isCopy = inst->getMnemonic() == Mnemonic_MOV;

                if (isCopy){ // CopyPseudoInst or mov
                    Opnd * defOpnd = inst->getOpnd(0);
                    Opnd * srcOpnd = inst->getOpnd(1);
                    U_32 defOpndId = defOpnd->getId();
                    OpndInfo * opndInfo = opndInfos + defOpndId;
                    bool instHandled=false;
                    bool typeConvOk = isTypeConversionAllowed(srcOpnd, defOpnd);
                    if (typeConvOk && opndInfo->defCount == 1 && ! srcOpnd->isPlacedIn(OpndKind_Reg)){
                        if (!defOpnd->hasAssignedPhysicalLocation()){
                            opndInfo->sourceInst = inst;
                            opndInfo->sourceOpndId = srcOpnd->getId();
                            instHandled=true;
                        }
                    }
                    if (instHandled){
                        if (opndInfos[opndInfo->sourceOpndId].sourceOpndId != EmptyUint32)
                            opndInfo->sourceOpndId = opndInfos[opndInfo->sourceOpndId].sourceOpndId;
                        opndInfo->sourceOpndDefCountAtCopy = opndInfos[opndInfo->sourceOpndId].defCount;
                        anyInstHandled=true;
                    }
                }
            }
        }

        if (anyInstHandled){
            Opnd ** replacements = new(mm) Opnd* [opndCount];
            memset(replacements, 0, sizeof(Opnd*) * opndCount);
            bool hasReplacements = false;
            for (U_32 i = 0; i < opndCount; ++i){
                if (opndInfos[i].sourceOpndId != EmptyUint32){
                    Inst * inst = opndInfos[i].sourceInst;
                    if (inst !=NULL){
                        inst->unlink();
                    }
                    if (opndInfos[i].sourceOpndId != i){
                        Opnd* origOpnd= irManager->getOpnd(i);
                        Opnd* replacementOpnd = irManager->getOpnd(opndInfos[i].sourceOpndId);
                        assert(isTypeConversionAllowed(replacementOpnd, origOpnd));
                        if (origOpnd->getType()->isUnmanagedPtr() && replacementOpnd->getType()->isInteger()) {
                            replacementOpnd->setType(origOpnd->getType());
                        }/* else if (origOpnd->getType()->isObject() && replacementOpnd->getType()->isUnmanagedPtr()) {
                            replacementOpnd->setType(origOpnd->getType());
                        }*/
                        replacements[i] = replacementOpnd;
                        hasReplacements = true;
                    }
                }
            }

            if (hasReplacements){
                const Nodes& postOrdered = irManager->getFlowGraph()->getNodesPostOrder();
                for (Nodes::const_reverse_iterator it = postOrdered.rbegin(), end = postOrdered.rend(); it!=end; ++it) {
                    Node * node=*it;
                    if (!node->isBlockNode())  {
                        continue;
                    }
                    for (Inst * inst = (Inst*)node->getFirstInst(); inst != NULL; inst=inst->getNextInst()){
                        inst->replaceOpnds(replacements);
                    }   
                }
            }
        }
}
/**
 * Executes lazy exception optimization pass.
 */
void
LazyExceptionOpt::doLazyExceptionOpt() {
    MethodDesc &md = irManager.getMethodDesc();
    BitSet excOpnds(leMemManager,irManager.getOpndManager().getNumSsaOpnds());
    StlDeque<Inst*> candidateSet(leMemManager);
    optCandidates = new (leMemManager) OptCandidates(leMemManager);
    Method_Side_Effects m_sideEff = md.getSideEffect();

    const Nodes& nodes = irManager.getFlowGraph().getNodes();
    Nodes::const_iterator niter;

#ifdef _DEBUG
    mtdDesc=&md;
#endif

#ifdef _DEBUG
    if (Log::isEnabled()) {
        Log::out() << std::endl;
        for (int i=0; i<level; i++) Log::out() << " ";
        Log::out() << "doLE ";
        md.printFullName(Log::out());
        Log::out() << " SideEff " << (int)m_sideEff << std::endl;
    }
#endif

    level++;
    U_32 opndId = 0;
    isArgCheckNull = false;
    isExceptionInit = md.isInstanceInitializer() &&
                      md.getParentType()->isLikelyExceptionType();
//  core api exception init
    if (m_sideEff == MSE_Unknown && isExceptionInit
            && strncmp(md.getParentType()->getName(),"java/lang/",10) == 0) {
        m_sideEff = MSE_False;
        md.setSideEffect(m_sideEff);
#ifdef _DEBUG
        if (Log::isEnabled()) {
            Log::out() << "      core api exc ";
            md.printFullName(Log::out());
            Log::out() << " SideEff " << (int)m_sideEff << std::endl;
        }
#endif
    }

    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        for (Inst* inst=headInst->getNextInst(); inst!=NULL; inst=inst->getNextInst()) {
#ifdef _DEBUG
            if (inst->getOpcode()==Op_DefArg && isExceptionInit) {
                if (Log::isEnabled()) {
                    Log::out() << "    defarg: ";
                    inst->print(Log::out());
                    Log::out()  << std::endl;
                    Log::out() << "            ";
                    Log::out() << (int)(inst->getDefArgModifier()) << " " <<
                               (inst->getDefArgModifier()==DefArgNoModifier) << " " <<
                               (inst->getDefArgModifier()==NonNullThisArg) << " " <<
                               (inst->getDefArgModifier()==SpecializedToExactType) << " " <<
                               (inst->getDefArgModifier()==DefArgBothModifiers) << std::endl;
                }
            }
#endif
            if (inst->getOpcode()==Op_Throw) {
                if (inst->getSrc(0)->getInst()->getOpcode()==Op_NewObj) {
                    excOpnds.setBit(opndId=inst->getSrc(0)->getId(),true);
                    if (!addOptCandidates(opndId,inst))
                        excOpnds.setBit(opndId,false); // different exc. edges
#ifdef _DEBUG
                    if (excOpnds.getBit(opndId)==1) {
                        if (Log::isEnabled()) {
                            Log::out() << "      add opnd: ";
                            inst->print(Log::out());
                            Log::out() << std::endl;
                            Log::out() << "      add  obj: ";
                            inst->getSrc(0)->getInst()->print(Log::out());
                            Log::out() << std::endl;
                        }
                    }
#endif
                }
            }
            if (m_sideEff == MSE_Unknown)
                if (instHasSideEffect(inst)) {
                    m_sideEff = MSE_True;
#ifdef _DEBUG
                    if (Log::isEnabled()) {
                        Log::out() << "~~~~~~inst sideEff ";
                        inst->print(Log::out());
                        Log::out() << std::endl;
                    }
#endif
                }
        }
    }
    if (md.getSideEffect() == MSE_Unknown) {
        if (m_sideEff == MSE_Unknown) {
            if (isExceptionInit && isArgCheckNull) {
#ifdef _DEBUG
                if (Log::isEnabled()) {
                    Log::out() << "~~~~~~init sideEff reset: " << m_sideEff << " 3 ";
                    md.printFullName(Log::out());
                    Log::out() << std::endl;
                }
#endif
                m_sideEff = MSE_True_Null_Param;
            } else
                m_sideEff = MSE_False;
        }
        md.setSideEffect(m_sideEff);
    }

    for(niter = nodes.begin(); niter != nodes.end(); ++niter) {
        Node* node = *niter;
        Inst *headInst = (Inst*)node->getFirstInst();
        Opnd* opnd;
        for (Inst* inst=headInst->getNextInst(); inst!=NULL; inst=inst->getNextInst()) {
            U_32 nsrc = inst->getNumSrcOperands();
            for (U_32 i=0; i<nsrc; i++) {
                if (!(opnd=inst->getSrc(i))->isSsaOpnd())  // check ssa operands
                    continue;
                if (excOpnds.getBit(opndId=opnd->getId())==0)
                    continue;
                if (inst->getOpcode()==Op_DirectCall) {
                    MethodDesc* md = inst->asMethodInst()->getMethodDesc();
                    if (md->isInstanceInitializer() &&
                            md->getParentType()->isLikelyExceptionType()) {
                        if (!addOptCandidates(opndId,inst)) {
                            excOpnds.setBit(opndId,false);
#ifdef _DEBUG
                            if (Log::isEnabled()) {
                                Log::out() << "    - rem opnd " << opnd->getId() << " ";
                                inst->print(Log::out());
                                Log::out() << std::endl;
                            }
#endif
                        }
                    } else {
                        excOpnds.setBit(opndId,false);
#ifdef _DEBUG
                        if (Log::isEnabled()) {
                            Log::out() << "   -- rem opnd " << opnd->getId() << " ";
                            inst->print(Log::out());
                            Log::out() << std::endl;
                        }
#endif
                    }
                } else {
                    if (inst->getOpcode()!=Op_Throw) {
                        excOpnds.setBit(opndId,false);
#ifdef _DEBUG
                        if (Log::isEnabled()) {
                            Log::out() << "      rem opnd " << opnd->getId() << " ";
                            inst->print(Log::out());
                            Log::out() << std::endl;
                        }
#endif
                    }
                }
            }
        }
    }
    if (!excOpnds.isEmpty()) {
#ifdef _DEBUG
        if (Log::isEnabled()) {
            Log::out() << "------LE: ";
            md.printFullName(Log::out());
            Log::out() << std::endl;
        }
#endif
        fixOptCandidates(&excOpnds);
    }

    level--;
#ifdef _DEBUG
    if (Log::isEnabled()) {
        for (int i=0; i<level; i++) Log::out() << " ";
        Log::out() << "done ";
        md.printFullName(Log::out());
        Log::out() << " SideEff " << (int)m_sideEff << std::endl;
    }
#endif
};