Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

llvm

I still don't understand the differences even after referring to the 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.

and looking to the source code:

// 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;
}
like image 490
Jon Kartago Lamida Avatar asked May 28 '26 08:05

Jon Kartago Lamida


1 Answers

LLVM IR code generally has one successor for each case label or similar, so for code like this example, getUniqueSuccessor() and getSingleSuccessor() deliver different results:

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

The first block has four successors, all of them equal.

like image 183
arnt Avatar answered Jun 03 '26 08:06

arnt