make -fno-rtti the default unless a directory builds with REQUIRES_RTTI.
[oota-llvm.git] / lib / Transforms / Utils / ValueMapper.cpp
index 7b1ae240da62fdce26ef86826b764bf03000ef26..a6e6701d0736535b99d96bc2f64e3502223de3c6 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Utils/ValueMapper.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/DerivedTypes.h"  // For getNullValue(Type::Int32Ty)
+#include "llvm/Type.h"
 #include "llvm/Constants.h"
-#include "llvm/GlobalValue.h"
-#include "llvm/Instruction.h"
+#include "llvm/Function.h"
 #include "llvm/Metadata.h"
 #include "llvm/ADT/SmallVector.h"
-#include "llvm/Support/ErrorHandling.h"
 using namespace llvm;
 
 Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
@@ -30,11 +27,19 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
   // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
   // DenseMap.  This includes any recursive calls to MapValue.
 
-  // Global values and metadata do not need to be seeded into the ValueMap if 
-  // they are using the identity mapping.
-  if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
+  // Global values and non-function-local metadata do not need to be seeded into
+  // the ValueMap if they are using the identity mapping.
+  if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
+      (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal()))
     return VMSlot = const_cast<Value*>(V);
 
+  if (const MDNode *MD = dyn_cast<MDNode>(V)) {
+    SmallVector<Value*, 4> Elts;
+    for (unsigned i = 0; i != MD->getNumOperands(); i++)
+      Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0);
+    return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
+  }
+
   Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
   if (C == 0) return 0;
   
@@ -92,8 +97,8 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
     return VM[V] = CE->getWithOperands(Ops);
   }
   
-  if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
-    for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
+  if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
+    for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
          i != e; ++i) {
       Value *MV = MapValue(*i, VM);
       if (MV != *i) {
@@ -101,7 +106,7 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
         // vector constant and return it.
         //
         std::vector<Constant*> Values;
-        Values.reserve(CP->getNumOperands());
+        Values.reserve(CV->getNumOperands());
         for (User::op_iterator j = b; j != i; ++j)
           Values.push_back(cast<Constant>(*j));
         Values.push_back(cast<Constant>(MV));
@@ -111,11 +116,12 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
       }
     }
     return VM[V] = C;
-    
   }
   
-  llvm_unreachable("Unknown type of constant!");
-  return 0;
+  BlockAddress *BA = cast<BlockAddress>(C);
+  Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
+  BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
+  return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
 }
 
 /// RemapInstruction - Convert the instruction operands from referencing the
@@ -128,3 +134,4 @@ void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
     *op = V;
   }
 }
+