llvm.org GIT mirror llvm / 05ea54e
Use MapValue in the Linker instead of having a private function which does the same thing. This eliminates redundant code and handles MDNodes better. MDNode linking still doesn't fully work yet though. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111941 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
6 changed file(s) with 48 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
0 //===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MapValue interface which is used by various parts of
10 // the Transforms/Utils library to implement cloning and linking facilities.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
15 #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
16
17 #include "llvm/ADT/ValueMap.h"
18
19 namespace llvm {
20 class Value;
21 class Instruction;
22 typedef ValueMap ValueToValueMapTy;
23
24 Value *MapValue(const Value *V, ValueToValueMapTy &VM);
25 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM);
26 } // End llvm namespace
27
28 #endif
2828 #include "llvm/Support/ErrorHandling.h"
2929 #include "llvm/Support/raw_ostream.h"
3030 #include "llvm/System/Path.h"
31 #include "llvm/Transforms/Utils/ValueMapper.h"
3132 #include "llvm/ADT/DenseMap.h"
3233 using namespace llvm;
3334
331332
332333
333334 return false;
334 }
335
336 #ifndef NDEBUG
337 static void PrintMap(const std::map &M) {
338 for (std::map::const_iterator I = M.begin(), E =M.end();
339 I != E; ++I) {
340 dbgs() << " Fr: " << (const void*)I->first << " ";
341 I->first->dump();
342 dbgs() << " To: " << (const void*)I->second << " ";
343 I->second->dump();
344 dbgs() << "\n";
345 }
346 }
347 #endif
348
349
350 // RemapOperand - Use ValueMap to convert constants from one module to another.
351 static Value *RemapOperand(const Value *In,
352 std::map &ValueMap) {
353 std::map::const_iterator I = ValueMap.find(In);
354 if (I != ValueMap.end())
355 return I->second;
356
357 // Check to see if it's a constant that we are interested in transforming.
358 Value *Result = 0;
359 if (const Constant *CPV = dyn_cast(In)) {
360 if ((!isa(CPV->getType()) && !isa(CPV)) ||
361 isa(CPV) || isa(CPV))
362 return const_cast(CPV); // Simple constants stay identical.
363
364 if (const ConstantArray *CPA = dyn_cast(CPV)) {
365 std::vector Operands(CPA->getNumOperands());
366 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
367 Operands[i] =cast(RemapOperand(CPA->getOperand(i), ValueMap));
368 Result = ConstantArray::get(cast(CPA->getType()), Operands);
369 } else if (const ConstantStruct *CPS = dyn_cast(CPV)) {
370 std::vector Operands(CPS->getNumOperands());
371 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
372 Operands[i] =cast(RemapOperand(CPS->getOperand(i), ValueMap));
373 Result = ConstantStruct::get(cast(CPS->getType()), Operands);
374 } else if (isa(CPV) || isa(CPV)) {
375 Result = const_cast(CPV);
376 } else if (const ConstantVector *CP = dyn_cast(CPV)) {
377 std::vector Operands(CP->getNumOperands());
378 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
379 Operands[i] = cast(RemapOperand(CP->getOperand(i), ValueMap));
380 Result = ConstantVector::get(Operands);
381 } else if (const ConstantExpr *CE = dyn_cast(CPV)) {
382 std::vector Ops;
383 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
384 Ops.push_back(cast(RemapOperand(CE->getOperand(i),ValueMap)));
385 Result = CE->getWithOperands(Ops);
386 } else if (const BlockAddress *CE = dyn_cast(CPV)) {
387 Result = BlockAddress::get(
388 cast(RemapOperand(CE->getFunction(), ValueMap)),
389 CE->getBasicBlock());
390 } else {
391 assert(!isa(CPV) && "Unmapped global?");
392 llvm_unreachable("Unknown type of derived type constant value!");
393 }
394 } else if (const MDNode *MD = dyn_cast(In)) {
395 if (MD->isFunctionLocal()) {
396 SmallVector Elts;
397 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
398 if (MD->getOperand(i))
399 Elts.push_back(RemapOperand(MD->getOperand(i), ValueMap));
400 else
401 Elts.push_back(NULL);
402 }
403 Result = MDNode::get(In->getContext(), Elts.data(), MD->getNumOperands());
404 } else {
405 Result = const_cast(In);
406 }
407 } else if (isa(In) || isa(In) || isa(In)) {
408 Result = const_cast(In);
409 }
410
411 // Cache the mapping in our local map structure
412 if (Result) {
413 ValueMap[In] = Result;
414 return Result;
415 }
416
417 #ifndef NDEBUG
418 dbgs() << "LinkModules ValueMap: \n";
419 PrintMap(ValueMap);
420
421 dbgs() << "Couldn't remap value: " << (const void*)In << " " << *In << "\n";
422 llvm_unreachable("Couldn't remap value!");
423 #endif
424 return 0;
425335 }
426336
427337 /// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict
554464 // LinkGlobals - Loop through the global variables in the src module and merge
555465 // them into the dest module.
556466 static bool LinkGlobals(Module *Dest, const Module *Src,
557 std::map &ValueMap,
467 ValueToValueMapTy &ValueMap,
558468 std::multimap &AppendingVars,
559469 std::string *Err) {
560470 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
741651 // dest module. We're assuming, that all functions/global variables were already
742652 // linked in.
743653 static bool LinkAlias(Module *Dest, const Module *Src,
744 std::map &ValueMap,
654 ValueToValueMapTy &ValueMap,
745655 std::string *Err) {
746656 // Loop over all alias in the src module
747657 for (Module::const_alias_iterator I = Src->alias_begin(),
752662
753663 // Globals were already linked, thus we can just query ValueMap for variant
754664 // of SAliasee in Dest.
755 std::map::const_iterator VMI = ValueMap.find(SAliasee);
665 ValueToValueMapTy::const_iterator VMI = ValueMap.find(SAliasee);
756666 assert(VMI != ValueMap.end() && "Aliasee not linked");
757667 GlobalValue* DAliasee = cast(VMI->second);
758668 GlobalValue* DGV = NULL;
883793 ForceRenaming(NewGA, SGA->getName());
884794
885795 // Remember this mapping so uses in the source module get remapped
886 // later by RemapOperand.
796 // later by MapValue.
887797 ValueMap[SGA] = NewGA;
888798 }
889799
894804 // LinkGlobalInits - Update the initializers in the Dest module now that all
895805 // globals that may be referenced are in Dest.
896806 static bool LinkGlobalInits(Module *Dest, const Module *Src,
897 std::map &ValueMap,
807 ValueToValueMapTy &ValueMap,
898808 std::string *Err) {
899809 // Loop over all of the globals in the src module, mapping them over as we go
900810 for (Module::const_global_iterator I = Src->global_begin(),
904814 if (SGV->hasInitializer()) { // Only process initialized GV's
905815 // Figure out what the initializer looks like in the dest module...
906816 Constant *SInit =
907 cast(RemapOperand(SGV->getInitializer(), ValueMap));
817 cast(MapValue(SGV->getInitializer(), ValueMap));
908818 // Grab destination global variable or alias.
909819 GlobalValue *DGV = cast(ValueMap[SGV]->stripPointerCasts());
910820
949859 // to the Dest function...
950860 //
951861 static bool LinkFunctionProtos(Module *Dest, const Module *Src,
952 std::map &ValueMap,
862 ValueToValueMapTy &ValueMap,
953863 std::string *Err) {
954864 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
955865
1034944 ForceRenaming(NewDF, SF->getName());
1035945
1036946 // Remember this mapping so uses in the source module get remapped
1037 // later by RemapOperand.
947 // later by MapValue.
1038948 ValueMap[SF] = NewDF;
1039949 continue;
1040950 }
1064974 // fix up references to values. At this point we know that Dest is an external
1065975 // function, and that Src is not.
1066976 static bool LinkFunctionBody(Function *Dest, Function *Src,
1067 std::map &ValueMap,
977 ValueToValueMapTy &ValueMap,
1068978 std::string *Err) {
1069979 assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration());
1070980
10911001 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
10921002 OI != OE; ++OI)
10931003 if (!isa(*OI) && !isa(*OI))
1094 *OI = RemapOperand(*OI, ValueMap);
1004 *OI = MapValue(*OI, ValueMap);
10951005
10961006 // There is no need to map the arguments anymore.
10971007 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
11061016 // source module into the DestModule. This consists basically of copying the
11071017 // function over and fixing up references to values.
11081018 static bool LinkFunctionBodies(Module *Dest, Module *Src,
1109 std::map &ValueMap,
1019 ValueToValueMapTy &ValueMap,
11101020 std::string *Err) {
11111021
11121022 // Loop over all of the functions in the src module, mapping them over as we
13141224 return true;
13151225
13161226 // ValueMap - Mapping of values from what they used to be in Src, to what they
1317 // are now in Dest.
1318 std::map ValueMap;
1227 // are now in Dest. ValueToValueMapTy is a ValueMap, which involves some
1228 // overhead due to the use of Value handles which the Linker doesn't actually
1229 // need, but this allows us to reuse the ValueMapper code.
1230 ValueToValueMapTy ValueMap;
13191231
13201232 // AppendingVars - Keep track of global variables in the destination module
13211233 // with appending linkage. After the module is linked together, they are
2222 #include "llvm/LLVMContext.h"
2323 #include "llvm/Metadata.h"
2424 #include "llvm/Support/CFG.h"
25 #include "ValueMapper.h"
25 #include "llvm/Transforms/Utils/ValueMapper.h"
2626 #include "llvm/Analysis/ConstantFolding.h"
2727 #include "llvm/Analysis/DebugInfo.h"
2828 #include "llvm/ADT/SmallVector.h"
1616 #include "llvm/DerivedTypes.h"
1717 #include "llvm/TypeSymbolTable.h"
1818 #include "llvm/Constant.h"
19 #include "ValueMapper.h"
19 #include "llvm/Transforms/Utils/ValueMapper.h"
2020 using namespace llvm;
2121
2222 /// CloneModule - Return an exact copy of the specified module. This is not as
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "ValueMapper.h"
14 #include "llvm/Transforms/Utils/ValueMapper.h"
1515 #include "llvm/Type.h"
1616 #include "llvm/Constants.h"
1717 #include "llvm/Function.h"
+0
-29
lib/Transforms/Utils/ValueMapper.h less more
None //===- ValueMapper.h - Interface shared by lib/Transforms/Utils -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MapValue interface which is used by various parts of
10 // the Transforms/Utils library to implement cloning and linking facilities.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef VALUEMAPPER_H
15 #define VALUEMAPPER_H
16
17 #include "llvm/ADT/ValueMap.h"
18
19 namespace llvm {
20 class Value;
21 class Instruction;
22 typedef ValueMap ValueToValueMapTy;
23
24 Value *MapValue(const Value *V, ValueToValueMapTy &VM);
25 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM);
26 } // End llvm namespace
27
28 #endif