[llvm-dev] Copy Function from one LLVM IR file to another
Mehdi AMINI via llvm-dev
llvm-dev at lists.llvm.org
Fri May 17 13:09:09 PDT 2019
On Thu, May 16, 2019 at 11:40 PM Kell Maresh via llvm-dev <
llvm-dev at lists.llvm.org> wrote:
> Hello everyone,
>
> I wanted to copy a function from one file to another. The file that I
> wanted to copy the function into contains a function with the same name and
> same number of instructions. I decided to just replace the instructions
> with those of the other function. I am doing all of this from within a
> function pass.
>
> *1.* I created a function pass in which I extract a function using
> "llvm-extract" from within the pass.
>
If you already go the llvm-extract route, you may want to look at llvm-cat
and/or llvm-link which should accomplish what you're trying to do manually
below.
--
Mehdi
>
> *2.* Then I read the (.ll) file created by the "llvm-extract" tool using:
>
> SMDiagnostic Err;
> LLVMContext Context;
>
> std::unique_ptr<Module> ParsedMod(parseIRFile("add.ll", Err,
> Context));
> Module &M = dynamic_cast<Module&>(*ParsedMod);
> Module* Mod_ptr = &M;
>
> *3.* Then I read the (.ll) file into which I want to copy the function
> using:
>
> std::unique_ptr<Module> ParsedMod2(parseIRFile("server.ll", Err,
> Context));
> Module &M2 = dynamic_cast<Module&>(*ParsedMod2);
> Module* Mod_ptr2 = &M2;
>
> *4.* I keep two iterators to save the points from where i have to start
> replacing instructions as:
>
> Module::iterator F1;
> Module::iterator F2;
>
> *5.* Assign them values so that F1 points to the first function and F2 to
> the second function:
>
> for(Module::iterator func = Mod_ptr->begin(), Lfunc = Mod_ptr->end();
> func!=Lfunc; ++func)
> {
> F1 = func;
> }
>
> for(Module::iterator func2 = Mod_ptr2->begin(), Lfunc2 =
> Mod_ptr2->end(); func2!=Lfunc2; ++func2)
> {
> if(func2->getName() == F.getName()) //F.getName() gives the same
> name as the function that was extracted
> {
> F2 = func2;
> }
> }
>
> *6.* Replacing each instruction of F2 with instruction from F1 using:
>
> for(Function::iterator bb = F1->begin(), Lbb = F1->end(), bb2 =
> F2->begin(), Lbb2 = F2->end(); bb2!=Lbb2; ++bb, ++bb2)
> {
> for(BasicBlock::iterator inst = bb->begin(), Linst = bb->end(),
> inst2 = bb2->begin(), Linst2 = bb2->end(); inst2 != Linst2; ++inst, ++inst2)
> {
> Instruction* I = &*inst;
> Instruction* I2 = &*inst2;
>
> //errs() << "F1's instruction: " << *I << "\n";
> //errs() << "F2's instruction: " << *I2 << "\n";
>
> ReplaceInstWithInst(I, I2);
> }
> }
>
> If I comment the ReplaceInstWithInst instruction and uncomment the
> instructions printing the instructions from two functions, correct
> instructions are printed.
>
> I get *Segmentation Fault* if I use ReplaceInstWithInst instruction.
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190517/a12c0f3f/attachment.html>
More information about the llvm-dev
mailing list