ValueMaterializer: fuse materializeDeclFor and materializeInitFor (NFC)

They were originally separated to handle the co-recursion between
the ValueMapper and the ValueMaterializer. This recursion does not
exist anymore: the ValueMapper now uses a Worklist and the
ValueMaterializer is scheduling job on the Worklist.

Differential Revision: http://reviews.llvm.org/D20593

From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 270758
This commit is contained in:
Mehdi Amini 2016-05-25 21:01:51 +00:00
parent 638f8c1e34
commit ad6c68d47a
3 changed files with 25 additions and 45 deletions

View File

@ -47,15 +47,9 @@ protected:
ValueMaterializer &operator=(const ValueMaterializer &) = default;
public:
/// The client should implement this method if they want to generate a mapped
/// Value on demand. For example, if linking lazily.
/// This method can be implemented to generate a mapped Value on demand. For
/// example, if linking lazily. Returns null if the value is not materialized.
virtual Value *materializeDeclFor(Value *V) = 0;
/// If the data being mapped is recursive, the above function can map just
/// the declaration and this is called to compute the initializer. It is
/// called after the mapping is recorded, so it doesn't need to worry about
/// recursion.
virtual void materializeInitFor(GlobalValue *New, GlobalValue *Old);
};
/// These are flags that the value mapping APIs allow.

View File

@ -350,7 +350,6 @@ class GlobalValueMaterializer final : public ValueMaterializer {
public:
GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
Value *materializeDeclFor(Value *V) override;
void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
};
class LocalValueMaterializer final : public ValueMaterializer {
@ -359,7 +358,6 @@ class LocalValueMaterializer final : public ValueMaterializer {
public:
LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
Value *materializeDeclFor(Value *V) override;
void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
};
/// Type of the Metadata map in \a ValueToValueMapTy.
@ -490,8 +488,7 @@ public:
~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
bool run();
Value *materializeDeclFor(Value *V, bool ForAlias);
void materializeInitFor(GlobalValue *New, GlobalValue *Old, bool ForAlias);
Value *materialize(Value *V, bool ForAlias);
};
}
@ -516,45 +513,38 @@ static void forceRenaming(GlobalValue *GV, StringRef Name) {
}
}
Value *GlobalValueMaterializer::materializeDeclFor(Value *V) {
return TheIRLinker.materializeDeclFor(V, false);
Value *GlobalValueMaterializer::materializeDeclFor(Value *SGV) {
return TheIRLinker.materialize(SGV, false);
}
void GlobalValueMaterializer::materializeInitFor(GlobalValue *New,
GlobalValue *Old) {
TheIRLinker.materializeInitFor(New, Old, false);
Value *LocalValueMaterializer::materializeDeclFor(Value *SGV) {
return TheIRLinker.materialize(SGV, true);
}
Value *LocalValueMaterializer::materializeDeclFor(Value *V) {
return TheIRLinker.materializeDeclFor(V, true);
}
void LocalValueMaterializer::materializeInitFor(GlobalValue *New,
GlobalValue *Old) {
TheIRLinker.materializeInitFor(New, Old, true);
}
Value *IRLinker::materializeDeclFor(Value *V, bool ForAlias) {
Value *IRLinker::materialize(Value *V, bool ForAlias) {
auto *SGV = dyn_cast<GlobalValue>(V);
if (!SGV)
return nullptr;
return linkGlobalValueProto(SGV, ForAlias);
}
Constant *NewProto = linkGlobalValueProto(SGV, ForAlias);
if (!NewProto)
return NewProto;
GlobalValue *New = dyn_cast<GlobalValue>(NewProto);
if (!New)
return NewProto;
void IRLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old,
bool ForAlias) {
// If we already created the body, just return.
if (auto *F = dyn_cast<Function>(New)) {
if (!F->isDeclaration())
return;
return New;
} else if (auto *V = dyn_cast<GlobalVariable>(New)) {
if (V->hasInitializer() || V->hasAppendingLinkage())
return;
return New;
} else {
auto *A = cast<GlobalAlias>(New);
if (A->getAliasee())
return;
return New;
}
// When linking a global for an alias, it will always be linked. However we
@ -565,11 +555,13 @@ void IRLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old,
// different, it means that the value already had a definition in the
// destination module (linkonce for instance), but we need a new definition
// for the alias ("New" will be different.
if (ForAlias && ValueMap.lookup(Old) == New)
return;
if (ForAlias && ValueMap.lookup(SGV) == New)
return New;
if (ForAlias || shouldLink(New, *Old))
linkGlobalValueBody(*New, *Old);
if (ForAlias || shouldLink(New, *SGV))
linkGlobalValueBody(*New, *SGV);
return New;
}
/// Loop through the global variables in the src module and merge them into the

View File

@ -29,8 +29,6 @@ using namespace llvm;
// Out of line method to get vtable etc for class.
void ValueMapTypeRemapper::anchor() {}
void ValueMaterializer::anchor() {}
void ValueMaterializer::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
}
namespace {
@ -365,11 +363,7 @@ Value *Mapper::mapValue(const Value *V) {
if (auto *Materializer = getMaterializer()) {
if (Value *NewV =
Materializer->materializeDeclFor(const_cast<Value *>(V))) {
getVM()[V] = NewV;
if (auto *NewGV = dyn_cast<GlobalValue>(NewV))
Materializer->materializeInitFor(
NewGV, cast<GlobalValue>(const_cast<Value *>(V)));
return NewV;
return getVM()[V] = NewV;
}
}