[llvm-commits] [llvm] r46033 - in /llvm/trunk: include/llvm/LinkTimeOptimizer.h tools/lto/lto.cpp

Devang Patel dpatel at apple.com
Tue Jan 15 15:52:34 PST 2008


Author: dpatel
Date: Tue Jan 15 17:52:34 2008
New Revision: 46033

URL: http://llvm.org/viewvc/llvm-project?rev=46033&view=rev
Log:
- Introduces versioning macro LLVM_LTO_VERSION
- Communicate symbol visibility
- Communicate code generation model

Modified:
    llvm/trunk/include/llvm/LinkTimeOptimizer.h
    llvm/trunk/tools/lto/lto.cpp

Modified: llvm/trunk/include/llvm/LinkTimeOptimizer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkTimeOptimizer.h?rev=46033&r1=46032&r2=46033&view=diff

==============================================================================
--- llvm/trunk/include/llvm/LinkTimeOptimizer.h (original)
+++ llvm/trunk/include/llvm/LinkTimeOptimizer.h Tue Jan 15 17:52:34 2008
@@ -20,6 +20,8 @@
 #include <set>
 #include <llvm/ADT/hash_map>
 
+#define LLVM_LTO_VERSION 2
+
 namespace llvm {
 
   class Module;
@@ -45,6 +47,19 @@
     LTOInternalLinkage  // Rename collisions when linking (static functions)
   };
 
+  enum LTOVisibilityTypes {
+    LTODefaultVisibility = 0,  ///< The GV is visible
+    LTOHiddenVisibility,       ///< The GV is hidden
+    LTOProtectedVisibility     ///< The GV is protected
+  };
+
+
+  enum LTOCodeGenModel {
+    LTO_CGM_Static,
+    LTO_CGM_Dynamic,
+    LTO_CGM_DynamicNoPIC
+  };
+
   /// This class represents LLVM symbol information without exposing details
   /// of LLVM global values. It encapsulates symbol linkage information. This
   /// is typically used in hash_map where associated name identifies the 
@@ -54,10 +69,13 @@
   public:
 
     LTOLinkageTypes getLinkage() const { return linkage; }
+    LTOVisibilityTypes getVisibility() const { return visibility; }
     void mayBeNotUsed();
 
-    LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n, 
-                const std::string &m, int a) : linkage(lt), gv(g), name(n), 
+    LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis, 
+                GlobalValue *g, const std::string &n, 
+                const std::string &m, int a) : linkage(lt), visibility(vis),
+                                               gv(g), name(n), 
                                                mangledName(m), alignment(a) {}
 
     const char *getName() { return name.c_str(); }
@@ -66,6 +84,7 @@
 
   private:
     enum LTOLinkageTypes linkage;
+    enum LTOVisibilityTypes visibility;
     GlobalValue *gv;
     std::string name;
     std::string mangledName;
@@ -91,11 +110,12 @@
                                               NameToSymbolMap &,
                                               std::set<std::string> &) = 0;
     virtual enum LTOStatus optimizeModules(const std::string &,
-                                           std::vector<const char*> &,
-                                           std::string &, bool, 
-                                           const char *) = 0;
+                                           std::vector<const char*> &exportList,
+                                           std::string &targetTriple,
+                                           bool saveTemps, const char *) = 0;
     virtual void getTargetTriple(const std::string &, std::string &) = 0;
     virtual void removeModule (const std::string &InputFilename) = 0;
+    virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
     virtual void printVersion () = 0;
     virtual ~LinkTimeOptimizer() = 0;
   };
@@ -115,17 +135,20 @@
                                       std::set<std::string> &references);
     enum LTOStatus optimizeModules(const std::string &OutputFilename,
                                    std::vector<const char*> &exportList,
-                                   std::string &targetTriple, bool saveTemps,
-                                   const char *);
+                                   std::string &targetTriple, 
+                                   bool saveTemps,  const char *);
     void getTargetTriple(const std::string &InputFilename, 
                          std::string &targetTriple);
     void removeModule (const std::string &InputFilename);
     void printVersion();
 
+    void setCodeGenModel(LTOCodeGenModel CGM) {
+      CGModel = CGM;
+    }
+
     // Constructors and destructors
-    LTO() { 
+    LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
       /// TODO: Use Target info, it is available at this time.
-      Target = NULL; 
     }
     ~LTO();
 
@@ -140,6 +163,7 @@
     NameToSymbolMap allSymbols;
     NameToModuleMap allModules;
     TargetMachine *Target;
+    LTOCodeGenModel CGModel;
   };
 
 } // End llvm namespace
@@ -148,6 +172,6 @@
 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
 extern "C"
-llvm::LinkTimeOptimizer *createLLVMOptimizer();
+llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);
 
 #endif

Modified: llvm/trunk/tools/lto/lto.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.cpp?rev=46033&r1=46032&r2=46033&view=diff

==============================================================================
--- llvm/trunk/tools/lto/lto.cpp (original)
+++ llvm/trunk/tools/lto/lto.cpp Tue Jan 15 17:52:34 2008
@@ -45,8 +45,15 @@
 using namespace llvm;
 
 extern "C"
-llvm::LinkTimeOptimizer *createLLVMOptimizer()
+llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION)
 {
+  // Linker records LLVM_LTO_VERSION based on llvm optimizer available
+  // during linker build. Match linker's recorded LTO VERSION number 
+  // with installed llvm optimizer version. If these numbers do not match
+  // then linker may not be able to use llvm optimizer dynamically.
+  if (VERSION != LLVM_LTO_VERSION)
+    return NULL;
+
   llvm::LTO *l = new llvm::LTO();
   return l;
 }
@@ -74,6 +81,20 @@
   return lt;
 }
 
+// MAP LLVM VisibilityType to LTO VisibilityType
+static LTOVisibilityTypes
+getLTOVisibilityType(GlobalValue *v)
+{
+  LTOVisibilityTypes vis;
+  if (v->hasHiddenVisibility()) 
+    vis = LTOHiddenVisibility;
+  else if (v->hasProtectedVisibility())
+    vis = LTOProtectedVisibility;
+  else
+    vis = LTODefaultVisibility;
+  return vis;
+}
+    
 // Find exeternal symbols referenced by VALUE. This is a recursive function.
 static void
 findExternalRefs(Value *value, std::set<std::string> &references, 
@@ -164,13 +185,12 @@
   modules.push_back(m);
   
   for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
-
     LTOLinkageTypes lt = getLTOLinkageType(f);
-
+    LTOVisibilityTypes vis = getLTOVisibilityType(f);
     if (!f->isDeclaration() && lt != LTOInternalLinkage
         && strncmp (f->getName().c_str(), "llvm.", 5)) {
       int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment());
-      LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(), 
+      LLVMSymbol *newSymbol = new LLVMSymbol(lt, vis, f, f->getName(), 
                                              mangler.getValueName(f),
                                              Log2_32(alignment));
       symbols[newSymbol->getMangledName()] = newSymbol;
@@ -180,19 +200,21 @@
     // Collect external symbols referenced by this function.
     for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b) 
       for (BasicBlock::iterator i = b->begin(), be = b->end(); 
-           i != be; ++i)
+           i != be; ++i) {
         for (unsigned count = 0, total = i->getNumOperands(); 
              count != total; ++count)
           findExternalRefs(i->getOperand(count), references, mangler);
+      }
   }
     
   for (Module::global_iterator v = m->global_begin(), e = m->global_end();
        v !=  e; ++v) {
     LTOLinkageTypes lt = getLTOLinkageType(v);
+    LTOVisibilityTypes vis = getLTOVisibilityType(v);
     if (!v->isDeclaration() && lt != LTOInternalLinkage
         && strncmp (v->getName().c_str(), "llvm.", 5)) {
       const TargetData *TD = Target->getTargetData();
-      LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(), 
+      LLVMSymbol *newSymbol = new LLVMSymbol(lt, vis, v, v->getName(), 
                                              mangler.getValueName(v),
                                              TD->getPreferredAlignmentLog(v));
       symbols[newSymbol->getMangledName()] = newSymbol;
@@ -354,8 +376,7 @@
 LTO::optimizeModules(const std::string &OutputFilename,
                      std::vector<const char *> &exportList,
                      std::string &targetTriple,
-                     bool saveTemps,
-                     const char *FinalOutputFilename)
+                     bool saveTemps, const char *FinalOutputFilename)
 {
   if (modules.empty())
     return LTO_NO_WORK;
@@ -374,6 +395,18 @@
   sys::Path FinalOutputPath(FinalOutputFilename);
   FinalOutputPath.eraseSuffix();
 
+  switch(CGModel) {
+  case LTO_CGM_Dynamic:
+    Target->setRelocationModel(Reloc::PIC_);
+    break;
+  case LTO_CGM_DynamicNoPIC:
+    Target->setRelocationModel(Reloc::DynamicNoPIC);
+    break;
+  case LTO_CGM_Static:
+    Target->setRelocationModel(Reloc::Static);
+    break;
+  }
+
   if (saveTemps) {
     std::string tempFileName(FinalOutputPath.c_str());
     tempFileName += "0.bc";





More information about the llvm-commits mailing list