void ControlDependenceGraphBase::computeDependencies(Function &F, PostDominatorTree &pdt) {
  root = new ControlDependenceNode();
  nodes.insert(root);

  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
    ControlDependenceNode *bn = new ControlDependenceNode(BB);
    nodes.insert(bn);
    bbMap[BB] = bn;
  }

  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
    BasicBlock *A = BB;
    ControlDependenceNode *AN = bbMap[A];

    for (succ_iterator succ = succ_begin(A), end = succ_end(A); succ != end; ++succ) {
      BasicBlock *B = *succ;
      assert(A && B);
      if (A == B || !pdt.dominates(B,A)) {
	BasicBlock *L = pdt.findNearestCommonDominator(A,B);
	ControlDependenceNode::EdgeType type = ControlDependenceGraphBase::getEdgeType(A,B);
	if (A == L) {
	  switch (type) {
	  case ControlDependenceNode::TRUE:
	    AN->addTrue(AN); break;
	  case ControlDependenceNode::FALSE:
	    AN->addFalse(AN); break;
	  case ControlDependenceNode::OTHER:
	    AN->addOther(AN); break;
	  }
	  AN->addParent(AN);
	}
	for (DomTreeNode *cur = pdt[B]; cur && cur != pdt[L]; cur = cur->getIDom()) {
	  ControlDependenceNode *CN = bbMap[cur->getBlock()];
	  switch (type) {
	  case ControlDependenceNode::TRUE:
	    AN->addTrue(CN); break;
	  case ControlDependenceNode::FALSE:
	    AN->addFalse(CN); break;
	  case ControlDependenceNode::OTHER:
	    AN->addOther(CN); break;
	  }
	  assert(CN);
	  CN->addParent(AN);
	}
      }
    }
  }

  // ENTRY -> START
  for (DomTreeNode *cur = pdt[&F.getEntryBlock()]; cur; cur = cur->getIDom()) {
    if (cur->getBlock()) {
      ControlDependenceNode *CN = bbMap[cur->getBlock()];
      assert(CN);
      root->addOther(CN); CN->addParent(root);
    }
  }
}
Esempio n. 2
0
//Flooding until reach a posdominator node
void bSSA::findIR (BasicBlock *bBOring, BasicBlock *bBSuss, PostDominatorTree &PD) {

	Pred *p;
	TerminatorInst *ti = bBSuss->getTerminator();


	//If the basic block has been processed, do not advance
	for (unsigned int x=0; x<ProcessedBB.size(); x++) {
		if (ProcessedBB[x] == bBSuss) {
			return;
		}
	}

	//Including the basic block in the processed vector
	ProcessedBB.push_back(bBSuss);

	//If the basic block is a posdominator and is not the start basic block, just gate the PHI instructions
	if (PD.dominates(bBSuss, bBOring) && bBSuss != bBOring) {
		//Find PHI instructions
		for (BasicBlock::iterator bBIt = bBSuss->begin(), bBEnd = bBSuss->end(); bBIt != bBEnd; ++bBIt) {
			if (dyn_cast<Instruction>(bBIt)->getOpcode()==Instruction::PHI) {
				//if there is a PHI's argument gated, gate the PHI instruction
				p = predicatesVector.back();
				for (unsigned int k=0; k<dyn_cast<Instruction>(bBIt)->getNumOperands(); k++) {
					if (p->isGated(dyn_cast<Instruction>(dyn_cast<Instruction>(bBIt)->getOperand(k)))) {
						p->addInst(bBIt);
						break;
					}
				}
			}
		}
		return;
	}else { //Advance the flooding
		//Instruction will be gated whit the bBOring predicate
		for (BasicBlock::iterator bBIt = bBSuss->begin(), bBEnd = bBSuss->end(); bBIt != bBEnd; ++bBIt) {
			p = predicatesVector.back();

			//If is a function call which is defined on the same module
			if (CallInst *CI = dyn_cast<CallInst>(&(*bBIt))) {
				Function *F = CI->getCalledFunction();
				if (F != NULL)
					if (!F->isDeclaration() && !F->isIntrinsic()) {
						gateFunction (F, p);
					}
			}

			//Gate the other instructions
			p->addInst(bBIt);
		}
		//If there is successor, go there
		for (unsigned int i=0; i<ti->getNumSuccessors(); i++) {
			findIR (bBOring, ti->getSuccessor(i), PD);
		}
	}

}
Esempio n. 3
0
void hammock::findIR (BasicBlock *bBOring, BasicBlock *bBSuss, PostDominatorTree &PD) {

	TerminatorInst *ti = bBSuss->getTerminator();


	if (bBlocks.count(bBSuss)>0) {
		return;
	}

	//Mark BasicBlock
	bBlocks.insert(bBSuss);

	//If the basic block is a posdominator and is not the start basic block, just return
	if (PD.dominates(bBSuss, bBOring) && bBSuss != bBOring) {
		return;
	}else { //Advance the flooding
		//If there is successor, go there
		for (unsigned int i=0; i<ti->getNumSuccessors(); i++) {
			findIR (bBOring, ti->getSuccessor(i), PD);
		}
	}

}