How to get a defined op and pass it as an operand

How to get a reference to a previously defined op of specific type to pass it as an operand to another op. i have already looked at Operation::[getNextNode/getPrevNode] but not sure if i could use it to iterate over all instances of an op.

I would appreciate an example.

I have something like this but cant figure out how to make it more efficient as well as return the desired type:

const auto myType = [](const FooOp& declOp, int id) -> Optional<MyTypeOp> {

auto op = declOp.getOperation();

      while (true){
        auto tmp = op->getPrevNode();
        if (tmp == nullptr){break;}
        op = tmp;
      }

      while (true){
        auto tmp = op->getNextNode();
        if (tmp == nullptr){break;}
        op = tmp;
        if (op->getName().getStringRef() == "myTypeName"){
          return op; // -> wont work
        }
    }
    return llvm::None;
    }

Go to the parent op and do walk() or getOps there? I believe you can find lots of examples searching the codebase.

Thanks a lot. this answers my first question.

Do i need to cast from Operation to get my type? - is this recommended?

dyn_cast<MyType>(op) 

Yeah. Using symbols is preferable than checking the raw string name. Also if you use getOps, you’ll need to provide the op name symbol too given it’s a template.

when you replace this with if (auto myOp = dyn_cast<MyType>(op)) you trade a string comparison for a simple pointer comparison.