Progress on PR341
[oota-llvm.git] / lib / Transforms / Utils / ValueMapper.cpp
index e6ab2974283f9893536dd467046795468752632b..7a5fcff1117fdc21d75ecb51bce54a917b1730ad 100644 (file)
@@ -1,4 +1,11 @@
 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file defines the MapValue function, which is shared by various parts of
 // the lib/Transforms/Utils library.
 #include "ValueMapper.h"
 #include "llvm/Constants.h"
 #include "llvm/Instruction.h"
+#include <iostream>
+
+using namespace llvm;
 
-Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
+Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
   Value *&VMSlot = VM[V];
   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
   
-  if (Constant *C = (Constant*)dyn_cast<Constant>(V)) {
+  // Global values do not need to be seeded into the ValueMap if they are using
+  // the identity mapping.
+  if (isa<GlobalValue>(V))
+    return VMSlot = const_cast<Value*>(V);
+
+  if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
     if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
-        isa<ConstantPointerNull>(C))
+        isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C))
       return VMSlot = C;           // Primitive constants map directly
     else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
       GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM));
@@ -33,7 +48,7 @@ Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
           for (unsigned j = 0; j != i; ++j)
             Values.push_back(cast<Constant>(Vals[j]));
           Values.push_back(cast<Constant>(MV));
-          for (; i != e; ++i)
+          for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
           return VMSlot = ConstantArray::get(CA->getType(), Values);
         }
@@ -53,7 +68,7 @@ Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
           for (unsigned j = 0; j != i; ++j)
             Values.push_back(cast<Constant>(Vals[j]));
           Values.push_back(cast<Constant>(MV));
-          for (; i != e; ++i)
+          for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
           return VMSlot = ConstantStruct::get(CS->getType(), Values);
         }
@@ -81,8 +96,27 @@ Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
       assert(0 && "Unknown type of constant!");
     }
   }
-  
+
+  V->dump();
   assert(0 && "Unknown value type: why didn't it get resolved?!");
   return 0;
 }
 
+/// RemapInstruction - Convert the instruction operands from referencing the
+/// current values into those specified by ValueMap.
+///
+void llvm::RemapInstruction(Instruction *I,
+                            std::map<const Value *, Value*> &ValueMap) {
+  for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
+    const Value *Op = I->getOperand(op);
+    Value *V = MapValue(Op, ValueMap);
+#ifndef NDEBUG
+    if (!V) {
+      std::cerr << "Val = \n" << *Op << "Addr = " << (void*)Op;
+      std::cerr << "\nInst = " << *I;
+    }
+#endif
+    assert(V && "Referenced value not in value map!");
+    I->setOperand(op, V);
+  }
+}