Implement linking of ConstExprs
authorChris Lattner <sabre@nondot.org>
Thu, 18 Jul 2002 00:13:08 +0000 (00:13 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 18 Jul 2002 00:13:08 +0000 (00:13 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2946 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Linker/LinkModules.cpp
lib/Transforms/Utils/Linker.cpp
lib/VMCore/Linker.cpp

index fb83e027b06ced8aac737b54b5f982ca570ec0e2..094c46fef84b40090a1630dfe992276320c47c49 100644 (file)
@@ -97,7 +97,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
 
   // 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()))
+    if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
       return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
@@ -122,6 +122,33 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
                       dyn_cast<ConstantPointerRef>(CPV)) {
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
+    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
+      if (CE->getNumOperands() == 1) {
+        // Cast instruction, unary operator
+        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V),
+                                   CE->getType());
+      } else if (CE->getNumOperands() == 2) {
+        // Binary operator...
+        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
+
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
+                                   cast<Constant>(V2), CE->getType());        
+      } else {
+        // GetElementPtr Expression
+        assert(CE->getOpcode() == Instruction::GetElementPtr);
+        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        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),
+                                                        LocalMap, GlobalMap)));
+
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
+                                   Indices, CE->getType());
+      }
+
     } else {
       assert(0 && "Unknown type of derived type constant value!");
     }
@@ -139,9 +166,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
     PrintMap(*GlobalMap);
   }
 
-  cerr << "Couldn't remap value: " << (void*)In << " ";
-  In->dump();
-  cerr << "\n";
+  cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
   assert(0 && "Couldn't remap value!");
   return 0;
 }
@@ -398,15 +423,19 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   map<const Value*, Value*> ValueMap;
 
+  // FIXME:
+  // FIXME: This should be a two step process:
+  // FIXME:   1. LinkGlobals & LinkFunctionProtos
+  // FIXME:   2. LinkGlobalContents
+  // FIXME:
+  // FIXME: Global variables and functions are the same!
+  // FIXME:
+
+
   // Insert all of the globals in src into the Dest module... without
   // initializers
   if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
 
-  // Update the initializers in the Dest module now that all globals that may
-  // be referenced are in Dest.
-  //
-  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
-
   // Link the functions together between the two modules, without doing function
   // bodies... this just adds external function prototypes to the Dest
   // function...  We do this so that when we begin processing function bodies,
@@ -415,6 +444,11 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
 
+  // Update the initializers in the Dest module now that all globals that may
+  // be referenced are in Dest.
+  //
+  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
+
   // Link in the function bodies that are defined in the source module into the
   // DestModule.  This consists basically of copying the function over and
   // fixing up references to values.
index fb83e027b06ced8aac737b54b5f982ca570ec0e2..094c46fef84b40090a1630dfe992276320c47c49 100644 (file)
@@ -97,7 +97,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
 
   // 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()))
+    if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
       return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
@@ -122,6 +122,33 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
                       dyn_cast<ConstantPointerRef>(CPV)) {
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
+    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
+      if (CE->getNumOperands() == 1) {
+        // Cast instruction, unary operator
+        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V),
+                                   CE->getType());
+      } else if (CE->getNumOperands() == 2) {
+        // Binary operator...
+        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
+
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
+                                   cast<Constant>(V2), CE->getType());        
+      } else {
+        // GetElementPtr Expression
+        assert(CE->getOpcode() == Instruction::GetElementPtr);
+        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        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),
+                                                        LocalMap, GlobalMap)));
+
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
+                                   Indices, CE->getType());
+      }
+
     } else {
       assert(0 && "Unknown type of derived type constant value!");
     }
@@ -139,9 +166,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
     PrintMap(*GlobalMap);
   }
 
-  cerr << "Couldn't remap value: " << (void*)In << " ";
-  In->dump();
-  cerr << "\n";
+  cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
   assert(0 && "Couldn't remap value!");
   return 0;
 }
@@ -398,15 +423,19 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   map<const Value*, Value*> ValueMap;
 
+  // FIXME:
+  // FIXME: This should be a two step process:
+  // FIXME:   1. LinkGlobals & LinkFunctionProtos
+  // FIXME:   2. LinkGlobalContents
+  // FIXME:
+  // FIXME: Global variables and functions are the same!
+  // FIXME:
+
+
   // Insert all of the globals in src into the Dest module... without
   // initializers
   if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
 
-  // Update the initializers in the Dest module now that all globals that may
-  // be referenced are in Dest.
-  //
-  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
-
   // Link the functions together between the two modules, without doing function
   // bodies... this just adds external function prototypes to the Dest
   // function...  We do this so that when we begin processing function bodies,
@@ -415,6 +444,11 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
 
+  // Update the initializers in the Dest module now that all globals that may
+  // be referenced are in Dest.
+  //
+  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
+
   // Link in the function bodies that are defined in the source module into the
   // DestModule.  This consists basically of copying the function over and
   // fixing up references to values.
index fb83e027b06ced8aac737b54b5f982ca570ec0e2..094c46fef84b40090a1630dfe992276320c47c49 100644 (file)
@@ -97,7 +97,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
 
   // 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()))
+    if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
       return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
@@ -122,6 +122,33 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
                       dyn_cast<ConstantPointerRef>(CPV)) {
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
+    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
+      if (CE->getNumOperands() == 1) {
+        // Cast instruction, unary operator
+        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V),
+                                   CE->getType());
+      } else if (CE->getNumOperands() == 2) {
+        // Binary operator...
+        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
+
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
+                                   cast<Constant>(V2), CE->getType());        
+      } else {
+        // GetElementPtr Expression
+        assert(CE->getOpcode() == Instruction::GetElementPtr);
+        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        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),
+                                                        LocalMap, GlobalMap)));
+
+        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
+                                   Indices, CE->getType());
+      }
+
     } else {
       assert(0 && "Unknown type of derived type constant value!");
     }
@@ -139,9 +166,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
     PrintMap(*GlobalMap);
   }
 
-  cerr << "Couldn't remap value: " << (void*)In << " ";
-  In->dump();
-  cerr << "\n";
+  cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
   assert(0 && "Couldn't remap value!");
   return 0;
 }
@@ -398,15 +423,19 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   map<const Value*, Value*> ValueMap;
 
+  // FIXME:
+  // FIXME: This should be a two step process:
+  // FIXME:   1. LinkGlobals & LinkFunctionProtos
+  // FIXME:   2. LinkGlobalContents
+  // FIXME:
+  // FIXME: Global variables and functions are the same!
+  // FIXME:
+
+
   // Insert all of the globals in src into the Dest module... without
   // initializers
   if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
 
-  // Update the initializers in the Dest module now that all globals that may
-  // be referenced are in Dest.
-  //
-  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
-
   // Link the functions together between the two modules, without doing function
   // bodies... this just adds external function prototypes to the Dest
   // function...  We do this so that when we begin processing function bodies,
@@ -415,6 +444,11 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
 
+  // Update the initializers in the Dest module now that all globals that may
+  // be referenced are in Dest.
+  //
+  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
+
   // Link in the function bodies that are defined in the source module into the
   // DestModule.  This consists basically of copying the function over and
   // fixing up references to values.