Revert 106592 for now. It causes clang-selfhost build failure.
[oota-llvm.git] / lib / Transforms / Utils / ValueMapper.cpp
index b5b0d2e2de705733e3c579e205587fdc95be992f..87ce631ca62648cd11791d2ad44447820c6ebbd5 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Utils/ValueMapper.h"
-#include "llvm/DerivedTypes.h"  // For getNullValue(Type::Int32Ty)
+#include "ValueMapper.h"
+#include "llvm/Type.h"
 #include "llvm/Constants.h"
 #include "llvm/Function.h"
-#include "llvm/IntrinsicInst.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) {
+Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
   Value *&VMSlot = VM[V];
   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
   
   // 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, e = MD->getNumOperands(); i != e; ++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;
   
@@ -112,40 +118,20 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
     return VM[V] = C;
   }
   
-  if (BlockAddress *BA = dyn_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());
-  }
-  
-  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
 /// current values into those specified by ValueMap.
 ///
-void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
+void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &ValueMap) {
   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
     Value *V = MapValue(*op, ValueMap);
     assert(V && "Referenced value not in value map!");
     *op = V;
   }
-
-  // Map llvm.dbg.declare instruction's first operand, which points to
-  // alloca instruction through MDNode. Since MDNodes are not counted as normal
-  // uses, this will fall through cracks otherwise.
-  const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I);
-  if (!DDI) return;
-  
-  Value *AddrInsn = DDI->getAddress();
-  if (!AddrInsn) return;
-  
-  ValueMapTy::iterator VMI = ValueMap.find(AddrInsn);
-  if (VMI == ValueMap.end()) return;
-  
-  Value *Elts[] =  { VMI->second };
-  MDNode *NewAddr = MDNode::get(AddrInsn->getContext(), Elts, 1);
-  I->setOperand(1, NewAddr);
 }