Use (void *)(intptr_t) to cast function addresses to void*
[oota-llvm.git] / tools / lto / LTOModule.cpp
index a72938a43b0fd5c91496c56275a0efe6ee0a1380..cbfb04861f5a01f3e33e5367a8aeb3507d99e179 100644 (file)
 #include "llvm/Support/Mangler.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/System/Host.h"
 #include "llvm/System/Path.h"
 #include "llvm/System/Process.h"
 #include "llvm/Target/SubtargetFeature.h"
 #include "llvm/Target/TargetAsmInfo.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetRegistry.h"
+#include "llvm/Target/TargetSelect.h"
 
 using namespace llvm;
 
@@ -125,6 +127,8 @@ LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
 /// subtarget. It would be better if we could encode this information into the
 /// IR. See <rdar://5972456>.
 std::string getFeatureString(const char *TargetTriple) {
+  InitializeAllTargets();
+
   SubtargetFeatures Features;
 
   if (strncmp(TargetTriple, "powerpc-apple-", 14) == 0) {
@@ -140,20 +144,25 @@ std::string getFeatureString(const char *TargetTriple) {
 LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
                                     std::string& errMsg)
 {
+    InitializeAllTargets();
+
     // parse bitcode buffer
     OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
     if ( !m )
         return NULL;
-    // find machine architecture for this module
-    const Target* march = 
-      TargetRegistry::getClosestStaticTargetForModule(*m, errMsg);
 
+    std::string Triple = m->getTargetTriple();
+    if (Triple.empty())
+      Triple = sys::getHostTriple();
+
+    // find machine architecture for this module
+    const Target* march = TargetRegistry::lookupTarget(Triple, errMsg);
     if ( march == NULL ) 
         return NULL;
 
     // construct LTModule, hand over ownership of module and target
-    std::string FeatureStr = getFeatureString(m->getTargetTriple().c_str());
-    TargetMachine* target = march->createTargetMachine(*m, FeatureStr);
+    std::string FeatureStr = getFeatureString(Triple.c_str());
+    TargetMachine* target = march->createTargetMachine(Triple, FeatureStr);
     return new LTOModule(m.take(), target);
 }
 
@@ -326,7 +335,7 @@ void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
                                  bool isFunction)
 {    
     // ignore all llvm.* symbols
-    if ( strncmp(def->getNameStart(), "llvm.", 5) == 0 )
+    if (def->getName().startswith("llvm."))
         return;
 
     // string is owned by _defines
@@ -396,14 +405,14 @@ void LTOModule::addAsmGlobalSymbol(const char *name) {
 void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
 {   
     // ignore all llvm.* symbols
-    if ( strncmp(decl->getNameStart(), "llvm.", 5) == 0 )
+    if (decl->getName().startswith("llvm."))
         return;
 
     // ignore all aliases
     if (isa<GlobalAlias>(decl))
         return;
 
-    const char* name = mangler.getMangledName(decl).c_str();
+    std::string name = mangler.getMangledName(decl);
 
     // we already have the symbol
     if (_undefines.find(name) != _undefines.end())
@@ -411,7 +420,7 @@ void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
 
     NameAndAttributes info;
     // string is owned by _undefines
-    info.name = ::strdup(name);
+    info.name = ::strdup(name.c_str());
     if (decl->hasExternalWeakLinkage())
       info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
     else
@@ -421,7 +430,7 @@ void LTOModule::addPotentialUndefinedSymbol(GlobalValue* decl, Mangler &mangler)
 
 
 
-// Find exeternal symbols referenced by VALUE. This is a recursive function.
+// Find external symbols referenced by VALUE. This is a recursive function.
 void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
 
     if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
@@ -507,7 +516,7 @@ void LTOModule::lazyParseSymbols()
                                                 it != _undefines.end(); ++it) {
             // if this symbol also has a definition, then don't make an undefine
             // because it is a tentative definition
-            if ( _defines.count(it->getKey())) {
+            if ( _defines.count(it->getKey()) == 0 ) {
               NameAndAttributes info = it->getValue();
               _symbols.push_back(info);
             }