Move machine code rewriter and spiller outside the register
[oota-llvm.git] / lib / Linker / LinkModules.cpp
index bdbdb7d4ccc263e9ae855387dc375ed005616e2f..529fb7714893841f231833524c1d6a8e51972b22 100644 (file)
@@ -23,8 +23,7 @@
 #include "llvm/SymbolTable.h"
 #include "llvm/iOther.h"
 #include "llvm/Assembly/Writer.h"
-
-namespace llvm {
+using namespace llvm;
 
 // Error - Simple wrapper function to conditionally assign to E and return true.
 // This just makes error return conditions a little bit simpler...
@@ -285,7 +284,8 @@ static Value *RemapOperand(const Value *In,
 
   // Check to see if it's a constant that we are interesting in transforming...
   if (const Constant *CPV = dyn_cast<Constant>(In)) {
-    if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
+    if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
+        isa<ConstantAggregateZero>(CPV))
       return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
@@ -325,12 +325,6 @@ static Value *RemapOperand(const Value *In,
         assert(CE->getOpcode() == Instruction::Cast);
         Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
         Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
-      } else if (CE->getOpcode() == Instruction::Shl ||
-                 CE->getOpcode() == Instruction::Shr) {      // Shift
-        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
-        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
-        Result = ConstantExpr::getShift(CE->getOpcode(), cast<Constant>(V1),
-                                        cast<Constant>(V2));
       } else if (CE->getNumOperands() == 2) {
         // Binary operator...
         Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
@@ -572,7 +566,6 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
 
       GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);    
       if (DGV->hasInitializer()) {
-        assert(SGV->getLinkage() == DGV->getLinkage());
         if (SGV->hasExternalLinkage()) {
           if (DGV->getInitializer() != SInit)
             return Error(Err, "Global Variable Collision on '" + 
@@ -581,6 +574,9 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
         } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) {
           // Nothing is required, mapped values will take the new global
           // automatically.
+        } else if (SGV->hasLinkOnceLinkage() || SGV->hasWeakLinkage()) {
+          // Nothing is required, mapped values will take the new global
+          // automatically.
         } else if (DGV->hasAppendingLinkage()) {
           assert(0 && "Appending linkage unimplemented!");
         } else {
@@ -803,12 +799,24 @@ static bool LinkAppendingVars(Module *M,
 
       // Merge the initializer...
       Inits.reserve(NewSize);
-      ConstantArray *I = cast<ConstantArray>(G1->getInitializer());
-      for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
-        Inits.push_back(cast<Constant>(I->getValues()[i]));
-      I = cast<ConstantArray>(G2->getInitializer());
-      for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
-        Inits.push_back(cast<Constant>(I->getValues()[i]));
+      if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
+        for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
+          Inits.push_back(cast<Constant>(I->getValues()[i]));
+      } else {
+        assert(isa<ConstantAggregateZero>(G1->getInitializer()));
+        Constant *CV = Constant::getNullValue(T1->getElementType());
+        for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
+          Inits.push_back(CV);
+      }
+      if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
+        for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
+          Inits.push_back(cast<Constant>(I->getValues()[i]));
+      } else {
+        assert(isa<ConstantAggregateZero>(G2->getInitializer()));
+        Constant *CV = Constant::getNullValue(T2->getElementType());
+        for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
+          Inits.push_back(CV);
+      }
       NG->setInitializer(ConstantArray::get(NewType, Inits));
       Inits.clear();
 
@@ -842,7 +850,7 @@ static bool LinkAppendingVars(Module *M,
 // the problem.  Upon failure, the Dest module could be in a modified state, and
 // shouldn't be relied on to be consistent.
 //
-bool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
+bool llvm::LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
   if (Dest->getEndianness() == Module::AnyEndianness)
     Dest->setEndianness(Src->getEndianness());
   if (Dest->getPointerSize() == Module::AnyPointerSize)
@@ -909,4 +917,3 @@ bool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
   return false;
 }
 
-} // End llvm namespace