anahitH/program-dependence-graph

No edge of type control

Closed this issue · 1 comments

mr-ma commented

I'm not getting any edges between blocks of type control in my tests. Am I missing something?

 std::vector<std::tuple<std::size_t, std::size_t, std::string>>
  getOutEdgeBB(std::shared_ptr<pdg::FunctionPDG> fpdg, llvm::Value *I, llvm::Function *F,
               std::size_t blockId) {
    std::vector<std::tuple<std::size_t, std::size_t, std::string>> outBBEdges;
    if (!fpdg->hasNode(I)) {
      return outBBEdges;
    }
    auto *srcBB = llvm::dyn_cast<llvm::Instruction>(I)->getParent();
    auto node = fpdg->getNode(I);
    for (auto edge_it = node->outEdgesBegin();
         edge_it != node->outEdgesEnd(); ++edge_it) {
      pdg::PDGNode* destNode = (*edge_it)->getDestination().get();
      // Check if the edge is to or from another BB
      auto* llvmNode = llvm::dyn_cast<pdg::PDGLLVMNode>(destNode);
      if (!llvmNode) {
        continue;
      }
      auto nodeValue = llvmNode->getNodeValue();
      if (!nodeValue) {
            continue;
      }
      //llvm::dbgs() << "   Node value " << *nodeValue << "\n";
      // TODO: check this
      if (llvm::isa<pdg::PDGLLVMInstructionNode>(llvmNode)) {
        auto *destInst = llvm::dyn_cast<llvm::Instruction>(nodeValue);
        auto *nodeBB = destInst->getParent();
        if (nodeBB != nullptr && nodeBB!=srcBB) {
            std::string edge_label = "c";
            if(llvm::isa<pdg::PDGDataEdge>((*edge_it).get())){
              edge_label="d";
            } //same result when using isDataEdge() method
            outBBEdges.push_back(std::make_tuple(
                blockId, getUniqueBlockName(nodeBB, nodeBB->getParent()),edge_label));
        }
      }
    }
    return outBBEdges;
  }

PDGBuilder does not create control flow edges between a branching instruction and instructions in a Basic Block the branching instruction leads to. Instead it creates a node for the BasicBlock, connects the node of the branch instruction to it with control flow edge, then connects the node of the BB to all its instruction nodes with control flow edge.
In the code snippet only PDGLLVMInstruction nodes are processed, thus the control flow connections are missed.
To find instructions that control depend on I you'd need to continue traversing PDG when hitting a Basic Block node - PDGLLVMBasicBlockNode.