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/IR/Constants.h"
00017 #include "llvm/IR/Function.h"
00018 #include "llvm/IR/InlineAsm.h"
00019 #include "llvm/IR/Instructions.h"
00020 #include "llvm/IR/Metadata.h"
00021 using namespace llvm;
00022 
00023 // Out of line method to get vtable etc for class.
00024 void ValueMapTypeRemapper::anchor() {}
00025 void ValueMaterializer::anchor() {}
00026 
00027 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
00028                       ValueMapTypeRemapper *TypeMapper,
00029                       ValueMaterializer *Materializer) {
00030   ValueToValueMapTy::iterator I = VM.find(V);
00031   
00032   // If the value already exists in the map, use it.
00033   if (I != VM.end() && I->second) return I->second;
00034   
00035   // If we have a materializer and it can materialize a value, use that.
00036   if (Materializer) {
00037     if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
00038       return VM[V] = NewV;
00039   }
00040 
00041   // Global values do not need to be seeded into the VM if they
00042   // are using the identity mapping.
00043   if (isa<GlobalValue>(V) || isa<MDString>(V))
00044     return VM[V] = const_cast<Value*>(V);
00045   
00046   if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
00047     // Inline asm may need *type* remapping.
00048     FunctionType *NewTy = IA->getFunctionType();
00049     if (TypeMapper) {
00050       NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
00051 
00052       if (NewTy != IA->getFunctionType())
00053         V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
00054                            IA->hasSideEffects(), IA->isAlignStack());
00055     }
00056     
00057     return VM[V] = const_cast<Value*>(V);
00058   }
00059   
00060 
00061   if (const MDNode *MD = dyn_cast<MDNode>(V)) {
00062     // If this is a module-level metadata and we know that nothing at the module
00063     // level is changing, then use an identity mapping.
00064     if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
00065       return VM[V] = const_cast<Value*>(V);
00066     
00067     // Create a dummy node in case we have a metadata cycle.
00068     MDNode *Dummy = MDNode::getTemporary(V->getContext(), None);
00069     VM[V] = Dummy;
00070     
00071     // Check all operands to see if any need to be remapped.
00072     for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
00073       Value *OP = MD->getOperand(i);
00074       if (!OP) continue;
00075       Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper, Materializer);
00076       // Use identity map if Mapped_Op is null and we can ignore missing
00077       // entries.
00078       if (Mapped_OP == OP ||
00079           (Mapped_OP == nullptr && (Flags & RF_IgnoreMissingEntries)))
00080         continue;
00081 
00082       // Ok, at least one operand needs remapping.  
00083       SmallVector<Value*, 4> Elts;
00084       Elts.reserve(MD->getNumOperands());
00085       for (i = 0; i != e; ++i) {
00086         Value *Op = MD->getOperand(i);
00087         if (!Op)
00088           Elts.push_back(nullptr);
00089         else {
00090           Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper, Materializer);
00091           // Use identity map if Mapped_Op is null and we can ignore missing
00092           // entries.
00093           if (Mapped_Op == nullptr && (Flags & RF_IgnoreMissingEntries))
00094             Mapped_Op = Op;
00095           Elts.push_back(Mapped_Op);
00096         }
00097       }
00098       MDNode *NewMD = MDNode::get(V->getContext(), Elts);
00099       Dummy->replaceAllUsesWith(NewMD);
00100       VM[V] = NewMD;
00101       MDNode::deleteTemporary(Dummy);
00102       return NewMD;
00103     }
00104 
00105     VM[V] = const_cast<Value*>(V);
00106     MDNode::deleteTemporary(Dummy);
00107 
00108     // No operands needed remapping.  Use an identity mapping.
00109     return const_cast<Value*>(V);
00110   }
00111 
00112   // Okay, this either must be a constant (which may or may not be mappable) or
00113   // is something that is not in the mapping table.
00114   Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
00115   if (!C)
00116     return nullptr;
00117   
00118   if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
00119     Function *F = 
00120       cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
00121     BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
00122                                                        Flags, TypeMapper, Materializer));
00123     return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
00124   }
00125   
00126   // Otherwise, we have some other constant to remap.  Start by checking to see
00127   // if all operands have an identity remapping.
00128   unsigned OpNo = 0, NumOperands = C->getNumOperands();
00129   Value *Mapped = nullptr;
00130   for (; OpNo != NumOperands; ++OpNo) {
00131     Value *Op = C->getOperand(OpNo);
00132     Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
00133     if (Mapped != C) break;
00134   }
00135   
00136   // See if the type mapper wants to remap the type as well.
00137   Type *NewTy = C->getType();
00138   if (TypeMapper)
00139     NewTy = TypeMapper->remapType(NewTy);
00140 
00141   // If the result type and all operands match up, then just insert an identity
00142   // mapping.
00143   if (OpNo == NumOperands && NewTy == C->getType())
00144     return VM[V] = C;
00145   
00146   // Okay, we need to create a new constant.  We've already processed some or
00147   // all of the operands, set them all up now.
00148   SmallVector<Constant*, 8> Ops;
00149   Ops.reserve(NumOperands);
00150   for (unsigned j = 0; j != OpNo; ++j)
00151     Ops.push_back(cast<Constant>(C->getOperand(j)));
00152   
00153   // If one of the operands mismatch, push it and the other mapped operands.
00154   if (OpNo != NumOperands) {
00155     Ops.push_back(cast<Constant>(Mapped));
00156   
00157     // Map the rest of the operands that aren't processed yet.
00158     for (++OpNo; OpNo != NumOperands; ++OpNo)
00159       Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
00160                              Flags, TypeMapper, Materializer));
00161   }
00162   
00163   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
00164     return VM[V] = CE->getWithOperands(Ops, NewTy);
00165   if (isa<ConstantArray>(C))
00166     return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
00167   if (isa<ConstantStruct>(C))
00168     return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
00169   if (isa<ConstantVector>(C))
00170     return VM[V] = ConstantVector::get(Ops);
00171   // If this is a no-operand constant, it must be because the type was remapped.
00172   if (isa<UndefValue>(C))
00173     return VM[V] = UndefValue::get(NewTy);
00174   if (isa<ConstantAggregateZero>(C))
00175     return VM[V] = ConstantAggregateZero::get(NewTy);
00176   assert(isa<ConstantPointerNull>(C));
00177   return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
00178 }
00179 
00180 /// RemapInstruction - Convert the instruction operands from referencing the
00181 /// current values into those specified by VMap.
00182 ///
00183 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
00184                             RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
00185                             ValueMaterializer *Materializer){
00186   // Remap operands.
00187   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
00188     Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
00189     // If we aren't ignoring missing entries, assert that something happened.
00190     if (V)
00191       *op = V;
00192     else
00193       assert((Flags & RF_IgnoreMissingEntries) &&
00194              "Referenced value not in value map!");
00195   }
00196 
00197   // Remap phi nodes' incoming blocks.
00198   if (PHINode *PN = dyn_cast<PHINode>(I)) {
00199     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00200       Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
00201       // If we aren't ignoring missing entries, assert that something happened.
00202       if (V)
00203         PN->setIncomingBlock(i, cast<BasicBlock>(V));
00204       else
00205         assert((Flags & RF_IgnoreMissingEntries) &&
00206                "Referenced block not in value map!");
00207     }
00208   }
00209 
00210   // Remap attached metadata.
00211   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
00212   I->getAllMetadata(MDs);
00213   for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
00214        MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
00215     MDNode *Old = MI->second;
00216     MDNode *New = MapValue(Old, VMap, Flags, TypeMapper, Materializer);
00217     if (New != Old)
00218       I->setMetadata(MI->first, New);
00219   }
00220   
00221   // If the instruction's type is being remapped, do so now.
00222   if (TypeMapper)
00223     I->mutateType(TypeMapper->remapType(I->getType()));
00224 }