[llvm] r344433 - move GetOrCreateFunctionComdat to Instrumentation.cpp/Instrumentation.h

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 12 16:21:48 PDT 2018


Author: kcc
Date: Fri Oct 12 16:21:48 2018
New Revision: 344433

URL: http://llvm.org/viewvc/llvm-project?rev=344433&view=rev
Log:
move GetOrCreateFunctionComdat to Instrumentation.cpp/Instrumentation.h

Summary:
GetOrCreateFunctionComdat is currently used in SanitizerCoverage,
where it's defined. I'm planing to use it in HWASAN as well,
so moving it into a common location.
NFC

Reviewers: morehouse

Reviewed By: morehouse

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D53218

Modified:
    llvm/trunk/include/llvm/Transforms/Instrumentation.h
    llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp
    llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Modified: llvm/trunk/include/llvm/Transforms/Instrumentation.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Instrumentation.h?rev=344433&r1=344432&r2=344433&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Instrumentation.h (original)
+++ llvm/trunk/include/llvm/Transforms/Instrumentation.h Fri Oct 12 16:21:48 2018
@@ -27,6 +27,7 @@ namespace llvm {
 class FunctionPass;
 class ModulePass;
 class OptimizationRemarkEmitter;
+class Comdat;
 
 /// Instrumentation passes often insert conditional checks into entry blocks.
 /// Call this function before splitting the entry block to move instructions
@@ -41,6 +42,11 @@ GlobalVariable *createPrivateGlobalForSt
                                              bool AllowMerging,
                                              const char *NamePrefix = "");
 
+// Returns F.getComdat() if it exists.
+// Otherwise creates a new comdat, sets F's comdat, and returns it.
+// Returns nullptr on failure.
+Comdat *GetOrCreateFunctionComdat(Function &F, const std::string &ModuleId);
+
 // Insert GCOV profiling instrumentation
 struct GCOVOptions {
   static GCOVOptions getDefault();

Modified: llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp?rev=344433&r1=344432&r2=344433&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp Fri Oct 12 16:21:48 2018
@@ -70,6 +70,21 @@ GlobalVariable *llvm::createPrivateGloba
   return GV;
 }
 
+Comdat *llvm::GetOrCreateFunctionComdat(Function &F,
+                                        const std::string &ModuleId) {
+  if (auto Comdat = F.getComdat()) return Comdat;
+  assert(F.hasName());
+  Module *M = F.getParent();
+  std::string Name = F.getName();
+  if (F.hasLocalLinkage()) {
+    if (ModuleId.empty())
+      return nullptr;
+    Name += ModuleId;
+  }
+  F.setComdat(M->getOrInsertComdat(Name));
+  return F.getComdat();
+}
+
 /// initializeInstrumentation - Initialize all passes in the TransformUtils
 /// library.
 void llvm::initializeInstrumentation(PassRegistry &Registry) {

Modified: llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp?rev=344433&r1=344432&r2=344433&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp Fri Oct 12 16:21:48 2018
@@ -220,8 +220,6 @@ private:
                    MDNode::get(*C, None));
   }
 
-  Comdat *GetOrCreateFunctionComdat(Function &F);
-
   std::string getSectionName(const std::string &Section) const;
   std::string getSectionStart(const std::string &Section) const;
   std::string getSectionEnd(const std::string &Section) const;
@@ -590,28 +588,16 @@ bool SanitizerCoverageModule::runOnFunct
   return true;
 }
 
-Comdat *SanitizerCoverageModule::GetOrCreateFunctionComdat(Function &F) {
-  if (auto Comdat = F.getComdat()) return Comdat;
-  if (!TargetTriple.isOSBinFormatELF()) return nullptr;
-  assert(F.hasName());
-  std::string Name = F.getName();
-  if (F.hasLocalLinkage()) {
-    if (CurModuleUniqueId.empty()) return nullptr;
-    Name += CurModuleUniqueId;
-  }
-  auto Comdat = CurModule->getOrInsertComdat(Name);
-  F.setComdat(Comdat);
-  return Comdat;
-}
-
 GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
     size_t NumElements, Function &F, Type *Ty, const char *Section) {
   ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
   auto Array = new GlobalVariable(
       *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
       Constant::getNullValue(ArrayTy), "__sancov_gen_");
-  if (auto Comdat = GetOrCreateFunctionComdat(F))
-    Array->setComdat(Comdat);
+
+  if (TargetTriple.isOSBinFormatELF())
+    if (auto Comdat = GetOrCreateFunctionComdat(F, CurModuleUniqueId))
+      Array->setComdat(Comdat);
   Array->setSection(getSectionName(Section));
   Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
                                         : Ty->getPrimitiveSizeInBits() / 8);




More information about the llvm-commits mailing list