[PATCH] D42971: [ThinLTO] - Simplify code in ThinLTOBitcodeWriter.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 6 08:19:20 PST 2018


grimar created this revision.
grimar added reviewers: pcc, tejohnson, espindola.
Herald added subscribers: eraman, inglorion, mehdi_amini.

Recently introduced `convertToDeclaration` is very similar
to code used in `filterModule` function.
Patch reuses it to reduce duplication.


https://reviews.llvm.org/D42971

Files:
  lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp


Index: lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
===================================================================
--- lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/IPO/FunctionAttrs.h"
+#include "llvm/Transforms/IPO/FunctionImport.h"
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 using namespace llvm;
@@ -169,45 +170,17 @@
   }
 }
 
-void filterModule(
-    Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
-  for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end();
-       I != E;) {
-    GlobalAlias *GA = &*I++;
-    if (ShouldKeepDefinition(GA))
+static void filterModule(Module *M,
+                         function_ref<bool(const GlobalValue *)> ShouldKeep) {
+  auto I = M->global_values().begin();
+  auto E = M->global_values().end();
+  while (I != E) {
+    GlobalValue &GA = *I++;
+    if (ShouldKeep(&GA))
       continue;
-
-    GlobalObject *GO;
-    if (GA->getValueType()->isFunctionTy())
-      GO = Function::Create(cast<FunctionType>(GA->getValueType()),
-                            GlobalValue::ExternalLinkage, "", M);
-    else
-      GO = new GlobalVariable(
-          *M, GA->getValueType(), false, GlobalValue::ExternalLinkage,
-          nullptr, "", nullptr,
-          GA->getThreadLocalMode(), GA->getType()->getAddressSpace());
-    GO->takeName(GA);
-    GA->replaceAllUsesWith(GO);
-    GA->eraseFromParent();
-  }
-
-  for (Function &F : *M) {
-    if (ShouldKeepDefinition(&F))
+    if (convertToDeclaration(GA))
       continue;
-
-    F.deleteBody();
-    F.setComdat(nullptr);
-    F.clearMetadata();
-  }
-
-  for (GlobalVariable &GV : M->globals()) {
-    if (ShouldKeepDefinition(&GV))
-      continue;
-
-    GV.setInitializer(nullptr);
-    GV.setLinkage(GlobalValue::ExternalLinkage);
-    GV.setComdat(nullptr);
-    GV.clearMetadata();
+    GA.eraseFromParent();
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42971.133007.patch
Type: text/x-patch
Size: 2079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180206/6c296ec5/attachment.bin>


More information about the llvm-commits mailing list