llvm.org GIT mirror llvm / a84a83b
Extend RemapInstruction and friends to take an optional new parameter, a ValueMaterializer. Extend LinkModules to pass a ValueMaterializer to RemapInstruction and friends to lazily create Functions for lazily linked globals. This is a big win when linking small modules with large (mostly unused) library modules. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182776 91177308-0d34-0410-b5e6-96231b3b80d8 James Molloy 7 years ago
6 changed file(s) with 143 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
130130 SmallVectorImpl &Returns,
131131 const char *NameSuffix = "",
132132 ClonedCodeInfo *CodeInfo = 0,
133 ValueMapTypeRemapper *TypeMapper = 0);
133 ValueMapTypeRemapper *TypeMapper = 0,
134 ValueMaterializer *Materializer = 0);
134135
135136 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
136137 /// except that it does some simple constant prop and DCE on the fly. The
3232 /// remap types while mapping values.
3333 virtual Type *remapType(Type *SrcTy) = 0;
3434 };
35
36 /// ValueMaterializer - This is a class that can be implemented by clients
37 /// to materialize Values on demand.
38 class ValueMaterializer {
39 virtual void anchor(); // Out of line method.
40 public:
41 virtual ~ValueMaterializer() {}
42
43 /// materializeValueFor - The client should implement this method if they
44 /// want to generate a mapped Value on demand. For example, if linking
45 /// lazily.
46 virtual Value *materializeValueFor(Value *V) = 0;
47 };
3548
3649 /// RemapFlags - These are flags that the value mapping APIs allow.
3750 enum RemapFlags {
5467
5568 Value *MapValue(const Value *V, ValueToValueMapTy &VM,
5669 RemapFlags Flags = RF_None,
57 ValueMapTypeRemapper *TypeMapper = 0);
70 ValueMapTypeRemapper *TypeMapper = 0,
71 ValueMaterializer *Materializer = 0);
5872
5973 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
6074 RemapFlags Flags = RF_None,
61 ValueMapTypeRemapper *TypeMapper = 0);
75 ValueMapTypeRemapper *TypeMapper = 0,
76 ValueMaterializer *Materializer = 0);
6277
6378 /// MapValue - provide versions that preserve type safety for MDNode and
6479 /// Constants.
6580 inline MDNode *MapValue(const MDNode *V, ValueToValueMapTy &VM,
6681 RemapFlags Flags = RF_None,
67 ValueMapTypeRemapper *TypeMapper = 0) {
68 return cast(MapValue((const Value*)V, VM, Flags, TypeMapper));
82 ValueMapTypeRemapper *TypeMapper = 0,
83 ValueMaterializer *Materializer = 0) {
84 return cast(MapValue((const Value*)V, VM, Flags, TypeMapper,
85 Materializer));
6986 }
7087 inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
7188 RemapFlags Flags = RF_None,
72 ValueMapTypeRemapper *TypeMapper = 0) {
73 return cast(MapValue((const Value*)V, VM, Flags, TypeMapper));
89 ValueMapTypeRemapper *TypeMapper = 0,
90 ValueMaterializer *Materializer = 0) {
91 return cast(MapValue((const Value*)V, VM, Flags, TypeMapper,
92 Materializer));
7493 }
7594
7695
352352 //===----------------------------------------------------------------------===//
353353
354354 namespace {
355 class ModuleLinker;
356
357 /// ValueMaterializerTy - Creates prototypes for functions that are lazily
358 /// linked on the fly. This speeds up linking for modules with many
359 /// lazily linked functions of which few get used.
360 class ValueMaterializerTy : public ValueMaterializer {
361 TypeMapTy &TypeMap;
362 Module *DstM;
363 std::vector &LazilyLinkFunctions;
364 public:
365 ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
366 std::vector &LazilyLinkFunctions) :
367 ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
368 LazilyLinkFunctions(LazilyLinkFunctions) {
369 }
370
371 virtual Value *materializeValueFor(Value *V);
372 };
373
355374 /// ModuleLinker - This is an implementation class for the LinkModules
356375 /// function, which is the entrypoint for this file.
357376 class ModuleLinker {
358377 Module *DstM, *SrcM;
359378
360379 TypeMapTy TypeMap;
380 ValueMaterializerTy ValMaterializer;
361381
362382 /// ValueMap - Mapping of values from what they used to be in Src, to what
363383 /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves
385405 std::string ErrorMsg;
386406
387407 ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode)
388 : DstM(dstM), SrcM(srcM), TypeMap(Set), Mode(mode) { }
408 : DstM(dstM), SrcM(srcM), TypeMap(Set),
409 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions),
410 Mode(mode) { }
389411
390412 bool run();
391413
485507 return true;
486508 return false;
487509 }
510
511 Value *ValueMaterializerTy::materializeValueFor(Value *V) {
512 Function *SF = dyn_cast(V);
513 if (!SF)
514 return NULL;
515
516 Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()),
517 SF->getLinkage(), SF->getName(), DstM);
518 copyGVAttributes(DF, SF);
519
520 LazilyLinkFunctions.push_back(SF);
521 return DF;
522 }
523
488524
489525 /// getLinkageResult - This analyzes the two global values and determines what
490526 /// the result will look like in the destination module. In particular, it
801837 }
802838 }
803839
840 // If the function is to be lazily linked, don't create it just yet.
841 // The ValueMaterializerTy will deal with creating it if it's used.
842 if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
843 SF->hasAvailableExternallyLinkage())) {
844 DoNotLinkFromSource.insert(SF);
845 return false;
846 }
847
804848 // If there is no linkage to be performed or we are linking from the source,
805849 // bring SF over.
806850 Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()),
813857 // Any uses of DF need to change to NewDF, with cast.
814858 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
815859 DGV->eraseFromParent();
816 } else {
817 // Internal, LO_ODR, or LO linkage - stick in set to ignore and lazily link.
818 if (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
819 SF->hasAvailableExternallyLinkage()) {
820 DoNotLinkFromSource.insert(SF);
821 LazilyLinkFunctions.push_back(SF);
822 }
823860 }
824861
825862 ValueMap[SF] = NewDF;
886923 SmallVector Elements;
887924 getArrayElements(AVI.DstInit, Elements);
888925
889 Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap);
926 Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer);
890927 getArrayElements(SrcInit, Elements);
891928
892929 ArrayType *NewType = cast(AVI.NewGV->getType()->getElementType());
907944 GlobalVariable *DGV = cast(ValueMap[I]);
908945 // Figure out what the initializer looks like in the dest module.
909946 DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
910 RF_None, &TypeMap));
947 RF_None, &TypeMap, &ValMaterializer));
911948 }
912949 }
913950
937974 // functions and patch them up to point to the local versions.
938975 for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
939976 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
940 RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap);
977 RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries,
978 &TypeMap, &ValMaterializer);
941979
942980 } else {
943981 // Clone the body of the function into the dest function.
944982 SmallVector Returns; // Ignore returns.
945 CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", NULL, &TypeMap);
983 CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", NULL,
984 &TypeMap, &ValMaterializer);
946985 }
947986
948987 // There is no need to map the arguments anymore.
960999 continue;
9611000 if (Constant *Aliasee = I->getAliasee()) {
9621001 GlobalAlias *DA = cast(ValueMap[I]);
963 DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, &TypeMap));
1002 DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None,
1003 &TypeMap, &ValMaterializer));
9641004 }
9651005 }
9661006 }
9771017 // Add Src elements into Dest node.
9781018 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
9791019 DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
980 RF_None, &TypeMap));
1020 RF_None, &TypeMap, &ValMaterializer));
9811021 }
9821022 }
9831023
12371277 LinkedInAnyFunctions = false;
12381278
12391279 for(std::vector::iterator I = LazilyLinkFunctions.begin(),
1240 E = LazilyLinkFunctions.end(); I != E; ++I) {
1241 if (!*I)
1280 E = LazilyLinkFunctions.end(); I != E; ++I) {
1281 Function *SF = *I;
1282 if (!SF)
12421283 continue;
1284
1285 Function *DF = cast(ValueMap[SF]);
1286
1287 // Materialize if necessary.
1288 if (SF->isDeclaration()) {
1289 if (!SF->isMaterializable())
1290 continue;
1291 if (SF->Materialize(&ErrorMsg))
1292 return true;
1293 }
12431294
1244 Function *SF = *I;
1245 Function *DF = cast(ValueMap[SF]);
1246
1247 if (!DF->use_empty()) {
1248
1249 // Materialize if necessary.
1250 if (SF->isDeclaration()) {
1251 if (!SF->isMaterializable())
1252 continue;
1253 if (SF->Materialize(&ErrorMsg))
1254 return true;
1255 }
1256
1257 // Link in function body.
1258 linkFunctionBody(DF, SF);
1259 SF->Dematerialize();
1260
1261 // "Remove" from vector by setting the element to 0.
1262 *I = 0;
1263
1264 // Set flag to indicate we may have more functions to lazily link in
1265 // since we linked in a function.
1266 LinkedInAnyFunctions = true;
1267 }
1295 // Erase from vector *before* the function body is linked - linkFunctionBody could
1296 // invalidate I.
1297 LazilyLinkFunctions.erase(I);
1298
1299 // Link in function body.
1300 linkFunctionBody(DF, SF);
1301 SF->Dematerialize();
1302
1303 // Set flag to indicate we may have more functions to lazily link in
1304 // since we linked in a function.
1305 LinkedInAnyFunctions = true;
1306 break;
12681307 }
12691308 } while (LinkedInAnyFunctions);
1270
1271 // Remove any prototypes of functions that were not actually linked in.
1272 for(std::vector::iterator I = LazilyLinkFunctions.begin(),
1273 E = LazilyLinkFunctions.end(); I != E; ++I) {
1274 if (!*I)
1275 continue;
1276
1277 Function *SF = *I;
1278 Function *DF = cast(ValueMap[SF]);
1279 if (DF->use_empty())
1280 DF->eraseFromParent();
1281 }
12821309
12831310 // Now that all of the types from the source are used, resolve any structs
12841311 // copied over to the dest that didn't exist there.
7777 bool ModuleLevelChanges,
7878 SmallVectorImpl &Returns,
7979 const char *NameSuffix, ClonedCodeInfo *CodeInfo,
80 ValueMapTypeRemapper *TypeMapper) {
80 ValueMapTypeRemapper *TypeMapper,
81 ValueMaterializer *Materializer) {
8182 assert(NameSuffix && "NameSuffix cannot be null!");
8283
8384 #ifndef NDEBUG
146147 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
147148 RemapInstruction(II, VMap,
148149 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
149 TypeMapper);
150 TypeMapper, Materializer);
150151 }
151152
152153 /// CloneFunction - Return a copy of the specified function, but without
2121
2222 // Out of line method to get vtable etc for class.
2323 void ValueMapTypeRemapper::anchor() {}
24 void ValueMaterializer::anchor() {}
2425
2526 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
26 ValueMapTypeRemapper *TypeMapper) {
27 ValueMapTypeRemapper *TypeMapper,
28 ValueMaterializer *Materializer) {
2729 ValueToValueMapTy::iterator I = VM.find(V);
2830
2931 // If the value already exists in the map, use it.
3032 if (I != VM.end() && I->second) return I->second;
3133
34 // If we have a materializer and it can materialize a value, use that.
35 if (Materializer) {
36 if (Value *NewV = Materializer->materializeValueFor(const_cast(V)))
37 return VM[V] = NewV;
38 }
39
3240 // Global values do not need to be seeded into the VM if they
3341 // are using the identity mapping.
3442 if (isa(V) || isa(V))
6371 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
6472 Value *OP = MD->getOperand(i);
6573 if (OP == 0) continue;
66 Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper);
74 Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper, Materializer);
6775 // Use identity map if Mapped_Op is null and we can ignore missing
6876 // entries.
6977 if (Mapped_OP == OP ||
7886 if (Op == 0)
7987 Elts.push_back(0);
8088 else {
81 Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper);
89 Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper, Materializer);
8290 // Use identity map if Mapped_Op is null and we can ignore missing
8391 // entries.
8492 if (Mapped_Op == 0 && (Flags & RF_IgnoreMissingEntries))
108116
109117 if (BlockAddress *BA = dyn_cast(C)) {
110118 Function *F =
111 cast(MapValue(BA->getFunction(), VM, Flags, TypeMapper));
119 cast(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
112120 BasicBlock *BB = cast_or_null(MapValue(BA->getBasicBlock(), VM,
113 Flags, TypeMapper));
121 Flags, TypeMapper, Materializer));
114122 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
115123 }
116124
120128 Value *Mapped = 0;
121129 for (; OpNo != NumOperands; ++OpNo) {
122130 Value *Op = C->getOperand(OpNo);
123 Mapped = MapValue(Op, VM, Flags, TypeMapper);
131 Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
124132 if (Mapped != C) break;
125133 }
126134
148156 // Map the rest of the operands that aren't processed yet.
149157 for (++OpNo; OpNo != NumOperands; ++OpNo)
150158 Ops.push_back(MapValue(cast(C->getOperand(OpNo)), VM,
151 Flags, TypeMapper));
159 Flags, TypeMapper, Materializer));
152160 }
153161
154162 if (ConstantExpr *CE = dyn_cast(C))
172180 /// current values into those specified by VMap.
173181 ///
174182 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
175 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper){
183 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
184 ValueMaterializer *Materializer){
176185 // Remap operands.
177186 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
178 Value *V = MapValue(*op, VMap, Flags, TypeMapper);
187 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
179188 // If we aren't ignoring missing entries, assert that something happened.
180189 if (V != 0)
181190 *op = V;
203212 for (SmallVectorImpl >::iterator
204213 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
205214 MDNode *Old = MI->second;
206 MDNode *New = MapValue(Old, VMap, Flags, TypeMapper);
215 MDNode *New = MapValue(Old, VMap, Flags, TypeMapper, Materializer);
207216 if (New != Old)
208217 I->setMetadata(MI->first, New);
209218 }
0 ; @f and @g are lazily linked. @f requires @g - ensure @g is correctly linked.
1
2 ; RUN: echo -e "declare i32 @f(i32)\ndefine i32 @h(i32 %x) {\n%1 = call i32 @f(i32 %x)\nret i32 %1\n}" | llvm-as >%t.1.bc
3 ; RUN: llvm-as < %s > %t.2.bc
4 ; RUN: llvm-link %t.1.bc %t.2.bc
5
6 define available_externally i32 @f(i32 %x) {
7 %1 = call i32 @g(i32 %x)
8 ret i32 %1
9 }
10
11 define available_externally i32 @g(i32 %x) {
12 ret i32 5
13 }
14