Is there any faster ways to find a specific instruction in a basic block?

For example, I want to find a add instruction in a basic block in LLVM IR.

The way a use is using the iteration container to get the instruction from a basic block, and using for loop to check all of them one by one.

But this way is not so fast. Even it’s very slow to get the instruction I want.

Excluding things like patching Clang to check the IR being generated
during lowering, I know of no other way to accomplish what you’re
trying to do in any faster manner. There are other ways of doing it,
such as InstVisitor, but none of them are any faster by design than
a for loop.

However, I also use a for loop to search for instructions of
specific types, and I do so on very large code units with no
noticeable slow-down. Are you using dyn_cast/isa or getOpcode to
check for the wanted instruction or some other method? I’ve seen
people try to use the name of the instruction as a basis for
comparison – and that can definitely result in unnecessary
slowness.