source - target code of a pass

I want to compare a program before and after having run a FunctionPass. The purpose is merely didactic. What I would like to do is, during the runOnFunction() method, “save” somehow the instruction set and cfg of the function, run the optimization. and then compare the two codes, before and after the transformation.
Is there an automatic way tho do that, or a suggested approach?

I do a dump of the Module (Module.print) before and after running it
through a PassManager (you could also use the Function.print method I
think). You can put just a single pass in the PassManager to compare how
the result differs. I do this often to see how optimization is changing
my code.

I don’t want to do just a visual compare, i would like to manipulate, iterate, over the “old” version of the function.

I don't know if there is an explicit clone method, but you could do the
printing and then parse it again. That would effectively create a clone
of the object.

Otherwise you can also try using the Linker and linking the source
Module. It's not clear, but perhaps that is also creating a copy (there
is a mode enum which says not to destroy the source, but I can't see
where to use it).

It appears the "llvm" namespace has several Clone functions. Thus you
should be able to do what I suggested: Clone the function, run the
original through the optimizer, and then compare the results.

That sound promising, Thanks.