I'm not sure this should actually be committed. I'm mostly putting it here as a way to make it available to anyone who is interested.
If anyone would like to see it added to the code base, feel free to make comments and I'll clean it up accordingly.
Right now, it only supports the legacy pass manager. Otherwise, I think it works OK.
Why isn't this a function pass?