[llvm] r265835 - ValueMapper: Extract llvm::RemapFunction from IRMover.cpp, NFC

Sean Silva via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 8 12:43:02 PDT 2016


On Fri, Apr 8, 2016 at 12:26 PM, Duncan P. N. Exon Smith via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

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

This function is very aesthetically pleasing.

-- Sean Silva


> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160408/bce69edc/attachment.html>


More information about the llvm-commits mailing list