<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 16, 2019 at 11:40 PM Kell Maresh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">Hello everyone,<br><br>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.<br><br><b>1.</b> I created a function pass in which I extract a function using "llvm-extract" from within the pass.<br></div></div></blockquote><div><br></div><div>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.</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br><b>2.</b> Then I read the (.ll) file created by the "llvm-extract" tool using:<br><br> SMDiagnostic Err;<br> LLVMContext Context;<br><br> std::unique_ptr<Module> ParsedMod(parseIRFile("add.ll", Err, Context));<br> Module &M = dynamic_cast<Module&>(*ParsedMod);<br> Module* Mod_ptr = &M;<br><br><b>3.</b> Then I read the (.ll) file into which I want to copy the function using:<br><br> std::unique_ptr<Module> ParsedMod2(parseIRFile("server.ll", Err, Context));<br> Module &M2 = dynamic_cast<Module&>(*ParsedMod2);<br> Module* Mod_ptr2 = &M2;<br><br><b>4.</b> I keep two iterators to save the points from where i have to start replacing instructions as:<br><br> Module::iterator F1;<br> Module::iterator F2;<br><br><b>5.</b> Assign them values so that F1 points to the first function and F2 to the second function:<br><br> for(Module::iterator func = Mod_ptr->begin(), Lfunc = Mod_ptr->end(); func!=Lfunc; ++func)<br> {<br> F1 = func;<br> }<br><br> for(Module::iterator func2 = Mod_ptr2->begin(), Lfunc2 = Mod_ptr2->end(); func2!=Lfunc2; ++func2)<br> {<br> if(func2->getName() == F.getName()) //F.getName() gives the same name as the function that was extracted<br> {<br> F2 = func2;<br> }<br> }<br><br><b>6.</b> Replacing each instruction of F2 with instruction from F1 using:<br><br>
for(Function::iterator bb = F1->begin(), Lbb = F1->end(), bb2 =
F2->begin(), Lbb2 = F2->end(); bb2!=Lbb2; ++bb, ++bb2)<br> {<br>
for(BasicBlock::iterator inst = bb->begin(), Linst = bb->end(),
inst2 = bb2->begin(), Linst2 = bb2->end(); inst2 != Linst2;
++inst, ++inst2)<br> {<br> Instruction* I = &*inst;<br> Instruction* I2 = &*inst2;<br> <br> //errs() << "F1's instruction: " << *I << "\n";<br> //errs() << "F2's instruction: " << *I2 << "\n";<br> <br> ReplaceInstWithInst(I, I2);<br> }<br> }<br><br>If
I comment the ReplaceInstWithInst instruction and uncomment the
instructions printing the instructions from two functions, correct
instructions are printed.<br><br>I get <b>Segmentation Fault</b> if I use ReplaceInstWithInst instruction.<br></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>