How to get the next immediate instruction for a given instruction?

Viewed 2690

I am looking for a right way to get a next immediate instruction that follows a given instruction.

Let's assume that I have the following:

%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %17

I have a

  CmpInst *cmpInst = dyn_cast<CmpInst>(&V);

which corresponds to %10.

How do I get an access to the BranchInst that follows my CmpInst?

I assume that a solution should take both cases into account: when there is a next instruction and when there is no one i.e. it is the end of a BasicBlock.

2 Answers

I agree with the previous answer cmpInst->getNextNode(), which appears in several projects I have seen.

However, according to another answer in link, the getNextNode() is an internal implementation details of stuff deep inside LLVM API. Thus, I prefer to using cmpInst->getNextNonDebugInstruction(), which could skip intrinsic instructions such as call void @llvm.dbg.declare(...).

Related