Skip to content

Commit

Permalink
ValueMapper: Extract llvm::RemapFunction from IRMover.cpp, NFC
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265835 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dexonsmith committed Apr 8, 2016
1 parent 16395b1 commit 287cd84
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
11 changes: 11 additions & 0 deletions include/llvm/Transforms/Utils/ValueMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
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,
Expand Down
33 changes: 8 additions & 25 deletions lib/Linker/IRMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,43 +963,26 @@ bool IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
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;
}

Expand Down
30 changes: 30 additions & 0 deletions lib/Transforms/Utils/ValueMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Mapper {

Value *mapValue(const Value *V);
void remapInstruction(Instruction *I);
void remapFunction(Function &F);

/// Map metadata.
///
Expand Down Expand Up @@ -801,3 +802,32 @@ void Mapper::remapInstruction(Instruction *I) {
}
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);
}

0 comments on commit 287cd84

Please sign in to comment.