[llvm] r270758 - ValueMaterializer: fuse materializeDeclFor and materializeInitFor (NFC)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed May 25 14:01:51 PDT 2016


Author: mehdi_amini
Date: Wed May 25 16:01:51 2016
New Revision: 270758

URL: http://llvm.org/viewvc/llvm-project?rev=270758&view=rev
Log:
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 at apple.com>

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=270758&r1=270757&r2=270758&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h Wed May 25 16:01:51 2016
@@ -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.

Modified: llvm/trunk/lib/Linker/IRMover.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/IRMover.cpp?rev=270758&r1=270757&r2=270758&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/IRMover.cpp (original)
+++ llvm/trunk/lib/Linker/IRMover.cpp Wed May 25 16:01:51 2016
@@ -350,7 +350,6 @@ class GlobalValueMaterializer final : pu
 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 : pub
 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 *G
   }
 }
 
-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(Global
   // 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, *SGV))
+    linkGlobalValueBody(*New, *SGV);
 
-  if (ForAlias || shouldLink(New, *Old))
-    linkGlobalValueBody(*New, *Old);
+  return New;
 }
 
 /// Loop through the global variables in the src module and merge them into the

Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=270758&r1=270757&r2=270758&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Wed May 25 16:01:51 2016
@@ -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;
     }
   }
 




More information about the llvm-commits mailing list