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; } } }
// // 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. } } } }