eliminate some ugly code, using ConstantExpr::getWithOperands instead.
[oota-llvm.git] / lib / Linker / LinkModules.cpp
index 732fdf919e3a9ea89cc7cdbd18f217898b0020bf..3c87177df3d64f55f5df3c681c9e407bcc0609d0 100644 (file)
@@ -262,20 +262,19 @@ static void PrintMap(const std::map<const Value*, Value*> &M) {
 
 // RemapOperand - Use ValueMap to convert references from one module to another.
 // This is somewhat sophisticated in that it can automatically handle constant
-// references correctly as well...
+// references correctly as well.
 static Value *RemapOperand(const Value *In,
                            std::map<const Value*, Value*> &ValueMap) {
   std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In);
   if (I != ValueMap.end()) return I->second;
 
   // Check to see if it's a constant that we are interesting in transforming.
+  Value *Result = 0;
   if (const Constant *CPV = dyn_cast<Constant>(In)) {
     if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
         isa<ConstantAggregateZero>(CPV))
       return const_cast<Constant*>(CPV);   // Simple constants stay identical.
 
-    Constant *Result = 0;
-
     if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
       std::vector<Constant*> Operands(CPA->getNumOperands());
       for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
@@ -290,48 +289,29 @@ static Value *RemapOperand(const Value *In,
       Result = const_cast<Constant*>(CPV);
     } else if (isa<GlobalValue>(CPV)) {
       Result = cast<Constant>(RemapOperand(CPV, ValueMap));
+    } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CPV)) {
+      std::vector<Constant*> Operands(CP->getNumOperands());
+      for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
+        Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap));
+      Result = ConstantPacked::get(Operands);
     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
-      if (CE->getOpcode() == Instruction::GetElementPtr) {
-        Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap);
-        std::vector<Constant*> Indices;
-        Indices.reserve(CE->getNumOperands()-1);
-        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
-          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
-                                                        ValueMap)));
-
-        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
-      } else if (CE->getNumOperands() == 1) {
-        // Cast instruction
-        assert(CE->getOpcode() == Instruction::Cast);
-        Value *V = RemapOperand(CE->getOperand(0), ValueMap);
-        Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
-      } else if (CE->getNumOperands() == 3) {
-        // Select instruction
-        assert(CE->getOpcode() == Instruction::Select);
-        Value *V1 = RemapOperand(CE->getOperand(0), ValueMap);
-        Value *V2 = RemapOperand(CE->getOperand(1), ValueMap);
-        Value *V3 = RemapOperand(CE->getOperand(2), ValueMap);
-        Result = ConstantExpr::getSelect(cast<Constant>(V1), cast<Constant>(V2),
-                                         cast<Constant>(V3));
-      } else if (CE->getNumOperands() == 2) {
-        // Binary operator...
-        Value *V1 = RemapOperand(CE->getOperand(0), ValueMap);
-        Value *V2 = RemapOperand(CE->getOperand(1), ValueMap);
-
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
-                                   cast<Constant>(V2));
-      } else {
-        assert(0 && "Unknown constant expr type!");
-      }
-
+      std::vector<Constant*> Ops;
+      for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
+        Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap)));
+      Result = CE->getWithOperands(Ops);
     } else {
       assert(0 && "Unknown type of derived type constant value!");
     }
-
-    // Cache the mapping in our local map structure...
+  } else if (isa<InlineAsm>(In)) {
+    Result = const_cast<Value*>(In);
+  }
+  
+  // Cache the mapping in our local map structure...
+  if (Result) {
     ValueMap.insert(std::make_pair(In, Result));
     return Result;
   }
+  
 
   std::cerr << "LinkModules ValueMap: \n";
   PrintMap(ValueMap);
@@ -425,7 +405,8 @@ static bool LinkGlobals(Module *Dest, Module *Src,
   SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
 
   // Loop over all of the globals in the src module, mapping them over as we go
-  for (Module::global_iterator I = Src->global_begin(), E = Src->global_end(); I != E; ++I) {
+  for (Module::global_iterator I = Src->global_begin(), E = Src->global_end();
+       I != E; ++I) {
     GlobalVariable *SGV = I;
     GlobalVariable *DGV = 0;
     // Check to see if may have to link the global.
@@ -460,7 +441,9 @@ static bool LinkGlobals(Module *Dest, Module *Src,
         new GlobalVariable(SGV->getType()->getElementType(),
                            SGV->isConstant(), SGV->getLinkage(), /*init*/0,
                            SGV->getName(), Dest);
-
+      // Propagate alignment info.
+      NewDGV->setAlignment(SGV->getAlignment());
+      
       // If the LLVM runtime renamed the global, but it is an externally visible
       // symbol, DGV must be an existing global with internal linkage.  Rename
       // it.
@@ -482,12 +465,18 @@ static bool LinkGlobals(Module *Dest, Module *Src,
                            SGV->isConstant(), SGV->getLinkage(), /*init*/0,
                            "", Dest);
 
+      // Propagate alignment info.
+      NewDGV->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
+
       // Make sure to remember this mapping...
       ValueMap.insert(std::make_pair(SGV, NewDGV));
 
       // Keep track that this is an appending variable...
       AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
     } else {
+      // Propagate alignment info.
+      DGV->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
+
       // Otherwise, perform the mapping as instructed by GetLinkageResult.  If
       // the types don't match, and if we are to link from the source, nuke DGV
       // and create a new one of the appropriate type.
@@ -495,6 +484,7 @@ static bool LinkGlobals(Module *Dest, Module *Src,
         GlobalVariable *NewDGV =
           new GlobalVariable(SGV->getType()->getElementType(),
                              DGV->isConstant(), DGV->getLinkage());
+        NewDGV->setAlignment(DGV->getAlignment());
         Dest->getGlobalList().insert(DGV, NewDGV);
         DGV->replaceAllUsesWith(ConstantExpr::getCast(NewDGV, DGV->getType()));
         DGV->eraseFromParent();
@@ -533,7 +523,8 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
                             std::string *Err) {
 
   // Loop over all of the globals in the src module, mapping them over as we go
-  for (Module::const_global_iterator I = Src->global_begin(), E = Src->global_end(); I != E; ++I){
+  for (Module::const_global_iterator I = Src->global_begin(),
+       E = Src->global_end(); I != E; ++I) {
     const GlobalVariable *SGV = I;
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
@@ -682,7 +673,8 @@ static bool LinkFunctionBody(Function *Dest, Function *Src,
           *OI = RemapOperand(*OI, GlobalMap);
 
   // There is no need to map the arguments anymore.
-  for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); I != E; ++I)
+  for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
+       I != E; ++I)
     GlobalMap.erase(I);
 
   return false;
@@ -828,6 +820,14 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
       Dest->getTargetTriple() != Src->getTargetTriple())
     std::cerr << "WARNING: Linking two modules of different target triples!\n";
 
+  if (!Src->getModuleInlineAsm().empty()) {
+    if (Dest->getModuleInlineAsm().empty())
+      Dest->setModuleInlineAsm(Src->getModuleInlineAsm());
+    else
+      Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+
+                               Src->getModuleInlineAsm());
+  }
+  
   // Update the destination module's dependent libraries list with the libraries
   // from the source module. There's no opportunity for duplicates here as the
   // Module ensures that duplicate insertions are discarded.
@@ -857,7 +857,8 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
   // it's functionality here.
   std::map<std::string, GlobalValue*> GlobalsByName;
 
-  for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end(); I != E; ++I) {
+  for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end();
+       I != E; ++I) {
     // Add all of the appending globals already in the Dest module to
     // AppendingVars.
     if (I->hasAppendingLinkage())