如何获取基本块A可达的基本块?

How to obtain the basic blocks that are reachable from basic block A?

我想知道一个基本块能到达的所有基本块。 我该怎么做呢?

#include "llvm/IR/CFG.h"
BasicBlock *BB = ...;
for (BasicBlock *Pred : predecessors(BB)) {
}

您可以遍历继任者的继任者(以此类推),例如:

std::unordered_set<BasicBlock *> reachable;
std::queue<BasicBlock *> worklist;
worklist.insert(BB);
while (!worklist.empty()) {
  BasicBlock *front = worklist.front();
  worklist.pop();
  for (BasicBlock *succ : successors(front)) {
    if (reachable.count(succ) == 0) {
       /// We need the check here to ensure that we don't run 
       /// infinitely if the CFG has a loop in it
       /// i.e. the BB reaches itself directly or indirectly
       worklist.push(succ);
       reachable.push(succ);
    }
  }
}

如果我没有犯任何错误,那么你将在 while 循环结束时得到所有可到达的基本块:)