Get a specific Instruction Type in a Basic Block

I want to get a specific type of instruction in a Basic Block.

For example, I want to get all “sub” instructions in a Basic Block.

I know I can do this by iterating the basic block from begin to end and using if statement to compare the opcode. But I don’t think this is a clean method to do this.

I checked the function in BasicBlock class, but I didn’t see any function can do this.

Maybe there are another ways. Please help me. Thanks!!

If iterating the begin and end on Basic Block is the only way, is there any recommended data structure can store all the instructions that I want? I know there is a iterator class in llvm, but I’m not sure it is a container or not.

1 Like

Iterating (or equivalents) is the only way, it’s just not really that common a thing to need.

If you can’t deal with the instructions as you iterate, I’d probably use a SmallVector<Instruction *, N> (for some N) by default.

Thanks a lot!

You mean, “Finding specific type of instruction” is not a common thing to do, right?

Yes, most optimizations & analyses are concerned with following dataflow and handling a wide range of instructions (within limitations).

1 Like