clone function with constant argument, and constant fold it


I’m writing a pass that collects some information from functions, but this requires constant propagation/constant folding to be done on those functions beforehand. However some of these functions have an integer argument that can take certain constant values, and the functions are always called with these constant values at their various call sites. What I’m trying to do is to look at the call site, see what the constant argument is, and propagate that into the function.

I don’t want to alter the function (this is purely an analysis pass), so I want to create a copy of the function beforehand (using cloning), pass the constant argument to the new copy, and perform constant folding/dead code elimination on it. Then I can do my analysis on this new, constant folded version of the original function, collect my information, and then discard it (Move on to the next call site with possibly a new constant).

Does this seem like a reasonable approach? To me this seems like a combination of function cloning, interprocedural constant propagation and dead code elimination that llvm has. Is there an easy way of using these (or certain functions of them) to perform this task, or do I just have to borrow pieces of code from them?

Any suggestion/advice would be appreciated.