4

I'm trying to write a pass that will check the control flow of a code. Given a br instruction, I need to access the basic blocks referred to in the label fields.

For example, for the code:

 for(i = 0; i < count; i++){
    sum = add(sum, array[i]);
  }

I get the IR representation

br i1 %cmp, label %for.body, label %for.end

Now when I come across this instruction in the pass, I need to know exactly where these labels are pointing to. Can anybody please tell me how I can do that?

mikasa
  • 783
  • 1
  • 11
  • 29

1 Answers1

1

Call ->getOperand(1) and ->getOperand(2) methods for that instruction. This would give you 1st and 2nd BB respectively. See http://llvm.org/doxygen/classllvm_1_1User.html#abe1de1520a21f77ac57cc210bf0fb0b4

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • 1
    Thanks for the reply! That works too but I'm instead using BranchInst* branch = cast(I); for (unsigned II = 0, NSucc = branch->getNumSuccessors(); II < NSucc; ++II) { BasicBlock *Succ = branch->getSuccessor(II);} – mikasa Dec 04 '17 at 06:20
  • Sorry for the bad formatting, I'm new to SO and I don't know how to format code in the comments. – mikasa Dec 04 '17 at 06:22