[LLVMdev] Cloning Functions

David Greene dag at cray.com
Fri Jul 11 09:59:33 PDT 2008


On Wednesday 09 July 2008 13:49, David Greene wrote:

> > then it seems you're doing
> >
> > 	for each function
> > 		generate_ir
> > 		convert_to_llvm_ir
> > 		optimize_llvm_ir
>
> Yep.

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" 
   functions.

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
   operation.

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 
   CloneModule.

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 
   Module.

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 
   cloned Module.

This should work as I understand things, but I have a question about 
Function::deleteBody.  It calls dropAllReferences which has the following 
scary comment:

  /// 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
  /// delete.
  ///
  /// Since no other object in the module can have references into the body of 
a
  /// function, dropping all references deletes the entire body of the 
function,
  /// 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?

                                                       -Dave



More information about the llvm-dev mailing list