Adjust the heuristics used to decide when SROA is likely to be profitable.
[oota-llvm.git] / lib / Transforms / IPO / ExtractGV.cpp
index 882097cae55dca6b9e0c25735f7b2e4b84fce526..7f67e48ade839a890a4995f2b3d9c64320dfa77b 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Instructions.h"
+#include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
 #include "llvm/Pass.h"
 #include "llvm/Constants.h"
 #include "llvm/Transforms/IPO.h"
-#include "llvm/Support/Compiler.h"
 #include <algorithm>
 using namespace llvm;
 
 namespace {
   /// @brief A pass to extract specific functions and their dependencies.
-  class VISIBILITY_HIDDEN GVExtractorPass : public ModulePass {
+  class GVExtractorPass : public ModulePass {
     std::vector<GlobalValue*> Named;
     bool deleteStuff;
     bool reLink;
@@ -35,7 +35,7 @@ namespace {
     ///
     explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true,
                              bool relinkCallees = false)
-      : ModulePass((intptr_t)&ID), Named(GVs), deleteStuff(deleteS),
+      : ModulePass(&ID), Named(GVs), deleteStuff(deleteS),
         reLink(relinkCallees) {}
 
     bool runOnModule(Module &M) {
@@ -43,6 +43,7 @@ namespace {
         return false;  // Nothing to extract
       }
       
+      
       if (deleteStuff)
         return deleteGV();
       M.setModuleInlineAsm("");
@@ -52,7 +53,7 @@ namespace {
     bool deleteGV() {
       for (std::vector<GlobalValue*>::iterator GI = Named.begin(), 
              GE = Named.end(); GI != GE; ++GI) {
-        if (Function* NamedFunc = dyn_cast<Function>(&*GI)) {
+        if (Function* NamedFunc = dyn_cast<Function>(*GI)) {
          // If we're in relinking mode, set linkage of all internal callees to
          // external. This will allow us extract function, and then - link
          // everything together
@@ -63,7 +64,7 @@ namespace {
                   I != E; ++I) {
                if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
                  Function* Callee = callInst->getCalledFunction();
-                 if (Callee && Callee->hasInternalLinkage())
+                 if (Callee && Callee->hasLocalLinkage())
                    Callee->setLinkage(GlobalValue::ExternalLinkage);
                }
              }
@@ -85,6 +86,7 @@ namespace {
 
     bool isolateGV(Module &M) {
       // Mark all globals internal
+      // FIXME: what should we do with private linkage?
       for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
         if (!I->isDeclaration()) {
           I->setLinkage(GlobalValue::InternalLinkage);
@@ -98,7 +100,8 @@ namespace {
       // by putting them in the used array
       {
         std::vector<Constant *> AUGs;
-        const Type *SBP= PointerType::getUnqual(Type::Int8Ty);
+        const Type *SBP=
+              Type::getInt8PtrTy(M.getContext());
         for (std::vector<GlobalValue*>::iterator GI = Named.begin(), 
                GE = Named.end(); GI != GE; ++GI) {
           (*GI)->setLinkage(GlobalValue::ExternalLinkage);
@@ -106,9 +109,9 @@ namespace {
         }
         ArrayType *AT = ArrayType::get(SBP, AUGs.size());
         Constant *Init = ConstantArray::get(AT, AUGs);
-        GlobalValue *gv = new GlobalVariable(AT, false, 
+        GlobalValue *gv = new GlobalVariable(M, AT, false, 
                                              GlobalValue::AppendingLinkage, 
-                                             Init, "llvm.used", &M);
+                                             Init, "llvm.used");
         gv->setSection("llvm.metadata");
       }
 
@@ -121,12 +124,9 @@ namespace {
 
       for (Module::iterator I = M.begin(); ; ++I) {
         if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) {
-          Function *New = new Function(I->getFunctionType(),
-                                       GlobalValue::ExternalLinkage);
-          New->setCallingConv(I->getCallingConv());
-          New->setParamAttrs(I->getParamAttrs());
-          if (I->hasCollector())
-            New->setCollector(I->getCollector());
+          Function *New = Function::Create(I->getFunctionType(),
+                                           GlobalValue::ExternalLinkage);
+          New->copyAttributesFrom(I);
 
           // If it's not the named function, delete the body of the function
           I->dropAllReferences();