<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 14, 2016, at 7:58 AM, Pierre Gagelin via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class="">Hi,<br class=""><br class=""></div>I am trying to use the CloneFunction from llvm/Transforms/Utils/Cloning.h and I don't understand how the ValueToValueMapTy argument should be initialized. For instance, let say I want to clone this function (to add an argument):<br class=""><br class="">define void @function(i32 %i) #0 {<br class="">entry:<br class=""> %i.addr = alloca i32, align 4<br class=""> store i32 %i, i32* %i.addr, align 4<br class=""> ret void<br class="">}<br class=""><br class=""></div>to another function which should, after the CloneFunction, be like:<br class=""><br class="">define void @cloned_function(i32 %i, i8* %ptr) #0 {<br class="">entry:<br class=""> %i.addr = alloca i32, align 4<br class=""> store i32 %i, i32* %i.addr, align 4<br class=""> ret void<br class="">}<br class=""><br class=""></div>To do so, I first declared the clone_function with its arguments into the module then I should call CloneFunction like:<br class=""><br class=""></div>Function *function = M.getFunction("function");<br class=""></div>Function *cloned_function = M.getFunction("cloned_function”);<br class=""></div></div></blockquote><div><br class=""></div><div>This last statement is dead code, you overwrite the cloned_function on the next line.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">cloned_function = CloneFunction(<br class=""> function,<br class=""> VM,<br class=""> false /* or true? didn't get the difference */<br class=""> );<br class=""></div></div></blockquote><div><br class=""></div><div>Current LLVM has this prototype:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;" class="">Function</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> *</span>CloneFunction(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Function</span><span style="font-variant-ligatures: no-common-ligatures" class=""> *F, </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ValueToValueMapTy</span><span style="font-variant-ligatures: no-common-ligatures" class=""> &VMap,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ClonedCodeInfo</span><span style="font-variant-ligatures: no-common-ligatures" class=""> *CodeInfo = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">nullptr</span><span style="font-variant-ligatures: no-common-ligatures" class="">);</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Notice that the last argument is not a boolean (You may be using a different version of LLVM?).</span></div><div class="">Also, the API takes a function and returns another one. I’m not sure what you expect by first declaring another function with its arguments: it won’t be magically picked as a “destination” for the clone.</div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">You have to design this as a two-part process:</div><div class=""><br class=""></div><div class="">1) Clone the function (if you need to keep the original around)</div><div class="">2) Change the function signature. For this there is no helper (that I know of), you may look at how it is done in the DeadArgElimination pass.</div><div class=""><br class=""></div><div class="">Apparently you can do part of the transformation during the clone, first creating your new function and mapping the old argument to the new ones, but you’ll still have to splice the function body from the created clone to your new function. I’m not sure if it really buys you anything over RAUW it after the fact though.</div><div class=""><br class=""></div><div class="">— </div><div class="">Mehdi</div><div class=""><br class=""></div><div class=""><br class=""></div></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div><div class="">The question is how should I setup VM so that the job is correctly done? I assumed I had to link arguments from function to their equivalent in cloned_function but I don't see why VM should contain pair<Value*, WeakVH> instead of pair<Value*, Value*>...<br class=""><br class=""></div><div class="">Thank you for the help,<br class=""></div><div class="">Pierre<br class=""></div></div></div>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></blockquote></div><br class=""></body></html>