LLVM API Documentation

ValueMapper.cpp

Go to the documentation of this file.
00001 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MapValue function, which is shared by various parts of
00011 // the lib/Transforms/Utils library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "ValueMapper.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/GlobalValue.h"
00018 #include "llvm/Instruction.h"
00019 using namespace llvm;
00020 
00021 Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
00022   Value *&VMSlot = VM[V];
00023   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
00024 
00025   // Global values do not need to be seeded into the ValueMap if they are using
00026   // the identity mapping.
00027   if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
00028     return VMSlot = const_cast<Value*>(V);
00029 
00030   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
00031     if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
00032         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
00033         isa<UndefValue>(C))
00034       return VMSlot = C;           // Primitive constants map directly
00035     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
00036       for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
00037         Value *MV = MapValue(CA->getOperand(i), VM);
00038         if (MV != CA->getOperand(i)) {
00039           // This array must contain a reference to a global, make a new array
00040           // and return it.
00041           //
00042           std::vector<Constant*> Values;
00043           Values.reserve(CA->getNumOperands());
00044           for (unsigned j = 0; j != i; ++j)
00045             Values.push_back(CA->getOperand(j));
00046           Values.push_back(cast<Constant>(MV));
00047           for (++i; i != e; ++i)
00048             Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
00049           return VMSlot = ConstantArray::get(CA->getType(), Values);
00050         }
00051       }
00052       return VMSlot = C;
00053 
00054     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
00055       for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
00056         Value *MV = MapValue(CS->getOperand(i), VM);
00057         if (MV != CS->getOperand(i)) {
00058           // This struct must contain a reference to a global, make a new struct
00059           // and return it.
00060           //
00061           std::vector<Constant*> Values;
00062           Values.reserve(CS->getNumOperands());
00063           for (unsigned j = 0; j != i; ++j)
00064             Values.push_back(CS->getOperand(j));
00065           Values.push_back(cast<Constant>(MV));
00066           for (++i; i != e; ++i)
00067             Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
00068           return VMSlot = ConstantStruct::get(CS->getType(), Values);
00069         }
00070       }
00071       return VMSlot = C;
00072 
00073     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00074       std::vector<Constant*> Ops;
00075       for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
00076         Ops.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
00077       return VMSlot = CE->getWithOperands(Ops);
00078     } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
00079       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
00080         Value *MV = MapValue(CP->getOperand(i), VM);
00081         if (MV != CP->getOperand(i)) {
00082           // This packed value must contain a reference to a global, make a new
00083           // packed constant and return it.
00084           //
00085           std::vector<Constant*> Values;
00086           Values.reserve(CP->getNumOperands());
00087           for (unsigned j = 0; j != i; ++j)
00088             Values.push_back(CP->getOperand(j));
00089           Values.push_back(cast<Constant>(MV));
00090           for (++i; i != e; ++i)
00091             Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
00092           return VMSlot = ConstantPacked::get(Values);
00093         }
00094       }
00095       return VMSlot = C;
00096       
00097     } else {
00098       assert(0 && "Unknown type of constant!");
00099     }
00100   }
00101 
00102   return 0;
00103 }
00104 
00105 /// RemapInstruction - Convert the instruction operands from referencing the
00106 /// current values into those specified by ValueMap.
00107 ///
00108 void llvm::RemapInstruction(Instruction *I,
00109                             std::map<const Value *, Value*> &ValueMap) {
00110   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
00111     const Value *Op = I->getOperand(op);
00112     Value *V = MapValue(Op, ValueMap);
00113     assert(V && "Referenced value not in value map!");
00114     I->setOperand(op, V);
00115   }
00116 }