Adding and removing functions from Module

Hello,

I am new to LLVM so apologies for a possibly basic question. I am trying to autopartition some codes into pieces so that I can compile these pieces with different backends. My idea is to create one module per desired piece, and transfer the functions from the module I am visiting to these newly created modules, deleting the function in the previous module and adding it to the new one.

At this point, I can create new modules but I cannot figure out how to delete them from the current module or how to add an object of type "Function" to a module. My first attempt is something such as

     Module &m1; // Created and filled somewhere else
     Module &m2("m2", getGlobalContext());

     for (Module::iterator fIt = m1.begin(); fIt != m1.end(); fIt++){

         m2.getOrInsertFunction(*fIt);
         // Here I should delete the function from the original module, no clues about how to do it
     }

but no luck; the error is that getOrInsertFunction() does not allow to be called with that parameter. I could use other prototypes of getOrInsertFunction(), but these seem to be a bit overkill: create another function and "configure" all the parameters again, etc. Any hints or pointers to previous threads?

Also, if somebody has any other idea about how to partition the code to pass it to different backends, it will be welcome.

Thanks in advance,