llvm - get label of br instruction


April 2019


551 time


My question is simple. Given an instruction of type branch, how do I extract the label out of it? For example:

br label %while.cond

Should give me while.cond

br label %while.end

Should give me while.end

br i1 %cmp1, label %if.then, label %if.end

Should give me if, if.then, if.end respectively.

2 answers


First check inst->isConditional(), then access inst->getOperand(1) and inst->getOperand(2) in case it is true and inst->getOperand(0) if it is false.

The whole BasicBlock is what BranchInst actually accepts. If you want %if.then line, then call getName() on it.


I use the following code segment to get all the above information that you have asked.

 if (inst->getNumSuccessors() == 1)     
     return; //indicates not a branching instruction 

 unsigned int i; 

  for (i = 0; i <= inst->getNumSuccessors(); i++) {       

      llvm::outs() << inst->getOperand(i)->getName();