Ok, I've mostly got a mechanism to do what I want:
1. As each function comes in for op/codegen, clone it and save off
the clone and its associated ValueMap (I call these clones "pristine"
2. After all processing is done, clone the resulting Module (this has all of
the global data and the optimized functions). Save the ValueMap from this
3. Merge the ValueMaps from the Module clone and all the pristine function
ValueMaps so that if V->V' in the Module map and V->V'' in the pristine
function map, make V''->V' in the Module map.
4. Fix up arguments to the pristine functions _a_la_ what is done in
5. Delete the function bodies of all functions in the cloned Module (these are
the optimized bodies) by calling deleteBody on them.
6. Call CloneFunctionInto to clone the pristine functions into the cloned
7. Fix up linkages (set function linkages in the Module to what they are in
the pristine function clones (they were changed to External by deleteBody).
8. Delete all the pristine Function clones, they are now cloned into the
This should work as I understand things, but I have a question about
Function::deleteBody. It calls dropAllReferences which has the following
/// dropAllReferences() - This method causes all the subinstructions to "let
/// go" of all references that they are maintaining. This allows one to
/// 'delete' a whole module at a time, even though there may be circular
/// references... first all references are dropped, and all use counts go to
/// zero. Then everything is deleted for real. Note that no operations are
/// valid on an object that has "dropped all references", except operator
/// Since no other object in the module can have references into the body of
/// function, dropping all references deletes the entire body of the
/// including any contained basic blocks.
Is it really true I can't do anything else to a Function after calling this?
Can I clone into it via CloneFunctionInto?