<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 8, 2016 at 12:26 PM, Duncan P. N. Exon Smith via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: dexonsmith<br>
Date: Fri Apr  8 14:26:32 2016<br>
New Revision: 265835<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=265835&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=265835&view=rev</a><br>
Log:<br>
ValueMapper: Extract llvm::RemapFunction from IRMover.cpp, NFC<br>
<br>
Strip out the remapping parts of IRLinker::linkFunctionBody and put them<br>
in ValueMapper.cpp under the name Mapper::remapFunction (with a<br>
top-level entry-point llvm::RemapFunction).<br>
<br>
This is a nice cleanup on its own since it puts the remapping code<br>
together and shares a single Mapper context for the entire<br>
IRLinker::linkFunctionBody Call.  Besides that, this will make it easier<br>
to break the co-recursion between IRMover.cpp and ValueMapper.cpp in<br>
follow ups.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h<br>
    llvm/trunk/lib/Linker/IRMover.cpp<br>
    llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h?rev=265835&r1=265834&r2=265835&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h?rev=265835&r1=265834&r2=265835&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h Fri Apr  8 14:26:32 2016<br>
@@ -159,6 +159,17 @@ void RemapInstruction(Instruction *I, Va<br>
                       ValueMapTypeRemapper *TypeMapper = nullptr,<br>
                       ValueMaterializer *Materializer = nullptr);<br>
<br>
+/// Remap the operands, metadata, arguments, and instructions of a function.<br>
+///<br>
+/// Calls \a MapValue() on prefix data, prologue data, and personality<br>
+/// function; calls \a MapMetadata() on each attached MDNode; remaps the<br>
+/// argument types using the provided \c TypeMapper; and calls \a<br>
+/// RemapInstruction() on every instruction.<br>
+void RemapFunction(Function &F, ValueToValueMapTy &VM,<br>
+                   RemapFlags Flags = RF_None,<br>
+                   ValueMapTypeRemapper *TypeMapper = nullptr,<br>
+                   ValueMaterializer *Materializer = nullptr);<br>
+<br>
 /// Version of MapValue with type safety for Constant.<br>
 inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,<br>
                           RemapFlags Flags = RF_None,<br>
<br>
Modified: llvm/trunk/lib/Linker/IRMover.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/IRMover.cpp?rev=265835&r1=265834&r2=265835&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/IRMover.cpp?rev=265835&r1=265834&r2=265835&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Linker/IRMover.cpp (original)<br>
+++ llvm/trunk/lib/Linker/IRMover.cpp Fri Apr  8 14:26:32 2016<br>
@@ -963,43 +963,26 @@ bool IRLinker::linkFunctionBody(Function<br>
   if (std::error_code EC = Src.materialize())<br>
     return emitError(EC.message());<br>
<br>
-  // Link in the prefix data.<br>
+  // Link in the operands without remapping.<br>
   if (Src.hasPrefixData())<br>
-    Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, ValueMapperFlags,<br>
-                               &TypeMap, &GValMaterializer));<br>
-<br>
-  // Link in the prologue data.<br>
+    Dst.setPrefixData(Src.getPrefixData());<br>
   if (Src.hasPrologueData())<br>
-    Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,<br>
-                                 ValueMapperFlags, &TypeMap,<br>
-                                 &GValMaterializer));<br>
-<br>
-  // Link in the personality function.<br>
+    Dst.setPrologueData(Src.getPrologueData());<br>
   if (Src.hasPersonalityFn())<br>
-    Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,<br>
-                                  ValueMapperFlags, &TypeMap,<br>
-                                  &GValMaterializer));<br>
+    Dst.setPersonalityFn(Src.getPersonalityFn());<br>
<br>
-  // Copy over the metadata attachments.<br>
+  // Copy over the metadata attachments without remapping.<br>
   SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;<br>
   Src.getAllMetadata(MDs);<br>
   for (const auto &I : MDs)<br>
-    Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, ValueMapperFlags,<br>
-                                         &TypeMap, &GValMaterializer));<br>
+    Dst.setMetadata(I.first, I.second);<br>
<br>
   // Steal arguments and splice the body of Src into Dst.<br>
   Dst.stealArgumentListFrom(Src);<br>
   Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());<br>
<br>
-  // At this point, everything has been moved over, but the types and non-local<br>
-  // operands will be wrong.  Loop through everything and patch it up.<br>
-  for (Argument &A : Dst.args())<br>
-    A.mutateType(TypeMap.get(A.getType()));<br>
-  for (BasicBlock &BB : Dst)<br>
-    for (Instruction &I : BB)<br>
-      RemapInstruction(&I, ValueMap, ValueMapperFlags, &TypeMap,<br>
-                       &GValMaterializer);<br>
-<br>
+  // Everything has been moved over.  Remap it.<br>
+  RemapFunction(Dst, ValueMap, ValueMapperFlags, &TypeMap, &GValMaterializer);<br>
   return false;<br>
 }<br>
<br>
<br>
Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=265835&r1=265834&r2=265835&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=265835&r1=265834&r2=265835&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Fri Apr  8 14:26:32 2016<br>
@@ -80,6 +80,7 @@ public:<br>
<br>
   Value *mapValue(const Value *V);<br>
   void remapInstruction(Instruction *I);<br>
+  void remapFunction(Function &F);<br>
<br>
   /// Map metadata.<br>
   ///<br>
@@ -801,3 +802,32 @@ void Mapper::remapInstruction(Instructio<br>
   }<br>
   I->mutateType(TypeMapper->remapType(I->getType()));<br>
 }<br>
+<br>
+void llvm::RemapFunction(Function &F, ValueToValueMapTy &VM, RemapFlags Flags,<br>
+                         ValueMapTypeRemapper *TypeMapper,<br>
+                         ValueMaterializer *Materializer) {<br>
+  Mapper(VM, Flags, TypeMapper, Materializer).remapFunction(F);<br>
+}<br>
+<br>
+void Mapper::remapFunction(Function &F) {<br>
+  // Remap the operands.<br>
+  for (Use &Op : F.operands())<br>
+    if (Op)<br>
+      Op = mapValue(Op);<br>
+<br>
+  // Remap the metadata attachments.<br>
+  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;<br>
+  F.getAllMetadata(MDs);<br>
+  for (const auto &I : MDs)<br>
+    F.setMetadata(I.first, cast_or_null<MDNode>(mapMetadata(I.second)));<br>
+<br>
+  // Remap the argument types.<br>
+  if (TypeMapper)<br>
+    for (Argument &A : F.args())<br>
+      A.mutateType(TypeMapper->remapType(A.getType()));<br>
+<br>
+  // Remap the instructions.<br>
+  for (BasicBlock &BB : F)<br>
+    for (Instruction &I : BB)<br>
+      remapInstruction(&I);<br></blockquote><div><br></div><div>This function is very aesthetically pleasing.</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>