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