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 is distributed under the University of Illinois Open Source
00006 // 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 "llvm/Transforms/Utils/ValueMapper.h"
00016 #include "llvm/BasicBlock.h"
00017 #include "llvm/DerivedTypes.h"  // For getNullValue(Type::Int32Ty)
00018 #include "llvm/Constants.h"
00019 #include "llvm/GlobalValue.h"
00020 #include "llvm/Instruction.h"
00021 #include "llvm/LLVMContext.h"
00022 #include "llvm/Metadata.h"
00023 #include "llvm/ADT/SmallVector.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 using namespace llvm;
00026 
00027 Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context) {
00028   Value *&VMSlot = VM[V];
00029   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
00030   
00031   // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
00032   // DenseMap.  This includes any recursive calls to MapValue.
00033 
00034   // Global values and metadata do not need to be seeded into the ValueMap if 
00035   // they are using the identity mapping.
00036   if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
00037     return VMSlot = const_cast<Value*>(V);
00038 
00039   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
00040     if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
00041         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
00042         isa<UndefValue>(C) || isa<MDString>(C))
00043       return VMSlot = C;           // Primitive constants map directly
00044     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
00045       for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
00046            i != e; ++i) {
00047         Value *MV = MapValue(*i, VM, Context);
00048         if (MV != *i) {
00049           // This array must contain a reference to a global, make a new array
00050           // and return it.
00051           //
00052           std::vector<Constant*> Values;
00053           Values.reserve(CA->getNumOperands());
00054           for (User::op_iterator j = b; j != i; ++j)
00055             Values.push_back(cast<Constant>(*j));
00056           Values.push_back(cast<Constant>(MV));
00057           for (++i; i != e; ++i)
00058             Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
00059           return VM[V] = ConstantArray::get(CA->getType(), Values);
00060         }
00061       }
00062       return VM[V] = C;
00063 
00064     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
00065       for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
00066            i != e; ++i) {
00067         Value *MV = MapValue(*i, VM, Context);
00068         if (MV != *i) {
00069           // This struct must contain a reference to a global, make a new struct
00070           // and return it.
00071           //
00072           std::vector<Constant*> Values;
00073           Values.reserve(CS->getNumOperands());
00074           for (User::op_iterator j = b; j != i; ++j)
00075             Values.push_back(cast<Constant>(*j));
00076           Values.push_back(cast<Constant>(MV));
00077           for (++i; i != e; ++i)
00078             Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
00079           return VM[V] = ConstantStruct::get(CS->getType(), Values);
00080         }
00081       }
00082       return VM[V] = C;
00083 
00084     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00085       std::vector<Constant*> Ops;
00086       for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
00087         Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
00088       return VM[V] = CE->getWithOperands(Ops);
00089     } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
00090       for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
00091            i != e; ++i) {
00092         Value *MV = MapValue(*i, VM, Context);
00093         if (MV != *i) {
00094           // This vector value must contain a reference to a global, make a new
00095           // vector constant and return it.
00096           //
00097           std::vector<Constant*> Values;
00098           Values.reserve(CP->getNumOperands());
00099           for (User::op_iterator j = b; j != i; ++j)
00100             Values.push_back(cast<Constant>(*j));
00101           Values.push_back(cast<Constant>(MV));
00102           for (++i; i != e; ++i)
00103             Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
00104           return VM[V] = ConstantVector::get(Values);
00105         }
00106       }
00107       return VM[V] = C;
00108       
00109     } else {
00110       llvm_unreachable("Unknown type of constant!");
00111     }
00112   }
00113   return 0;
00114 }
00115 
00116 /// RemapInstruction - Convert the instruction operands from referencing the
00117 /// current values into those specified by ValueMap.
00118 ///
00119 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
00120   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
00121     Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
00122     assert(V && "Referenced value not in value map!");
00123     *op = V;
00124   }
00125 }