Stanislav Pankevich
Stanislav Pankevich

Reputation: 11368

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

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.

Upvotes: 6

Views: 3792

Answers (2)

YaFeng  Luo
YaFeng Luo

Reputation: 101

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(...).

Upvotes: 1

Stanislav Pankevich
Stanislav Pankevich

Reputation: 11368

It turned out to be as simple as this:

Instruction *instruction = cmpInst->getNextNode();

Upvotes: 9

Related Questions