LLVM 中 BasicBlock::getSingleSuccessor() 和 BasicBlock::getUniqueSuccessor() 有什么区别?

What is the differences between BasicBlock::getSingleSuccessor() and BasicBlock::getUniqueSuccessor() in LLVM?

参考了doxygen page我还是不明白其中的区别:

getSingleSuccessor()

Return the successor of this block if it has a single successor.

Otherwise return a null pointer.

getUniqueSuccessor()

Return the successor of this block if it has a unique successor.

Otherwise return a null pointer.

查看源代码:

// BasicBlock.cpp
const BasicBlock *BasicBlock::getSingleSuccessor() const {
  const_succ_iterator SI = succ_begin(this), E = succ_end(this);
  if (SI == E) return nullptr; // no successors
  const BasicBlock *TheSucc = *SI;
  ++SI;
  return (SI == E) ? TheSucc : nullptr /* multiple successors */;
}

const BasicBlock *BasicBlock::getUniqueSuccessor() const {
  const_succ_iterator SI = succ_begin(this), E = succ_end(this);
  if (SI == E) return nullptr; // No successors
  const BasicBlock *SuccBB = *SI;
  ++SI;
  for (;SI != E; ++SI) {
    if (*SI != SuccBB)
      return nullptr;
    // The same successor appears multiple times in the successor list.
    // This is OK.
  }
  return SuccBB;
}

LLVM IR 代码通常每个 case 标签或类似标签都有一个后继者,因此对于像这个例子的代码,getUniqueSuccessor()getSingleSuccessor() 会产生不同的结果:

switch(foo) {
  case 0:
  case 1:
  case 2:
  default:
    printf("Hello, world\n";
}

第一个块有四个后继块,它们都相等。