llvm.org GIT mirror llvm / 6cb8c23
Reapply r112091 and r111922, support for metadata linking, with a fix: add a flag to MapValue and friends which indicates whether any module-level mappings are being made. In the common case of inlining, no module-level mappings are needed, so MapValue doesn't need to examine non-function-local metadata, which can be very expensive in the case of a large module with really deep metadata (e.g. a large C++ program compiled with -g). This flag is a little awkward; perhaps eventually it can be moved into the ClonedCodeInfo class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112190 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
12 changed file(s) with 154 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
120120 /// the function from their old to new values. The final argument captures
121121 /// information about the cloned code if non-null.
122122 ///
123 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
124 /// mappings.
125 ///
123126 Function *CloneFunction(const Function *F,
124127 ValueMap &VMap,
128 bool ModuleLevelChanges,
125129 ClonedCodeInfo *CodeInfo = 0);
126130
127131 /// CloneFunction - Version of the function that doesn't need the VMap.
132136 }
133137
134138 /// Clone OldFunc into NewFunc, transforming the old arguments into references
135 /// to ArgMap values. Note that if NewFunc already has basic blocks, the ones
139 /// to VMap values. Note that if NewFunc already has basic blocks, the ones
136140 /// cloned into it will be added to the end of the function. This function
137141 /// fills in a list of return instructions, and can optionally append the
138142 /// specified suffix to all values cloned.
139143 ///
144 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
145 /// mappings.
146 ///
140147 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
141148 ValueMap &VMap,
149 bool ModuleLevelChanges,
142150 SmallVectorImpl &Returns,
143151 const char *NameSuffix = "",
144152 ClonedCodeInfo *CodeInfo = 0);
150158 /// constant arguments cause a significant amount of code in the callee to be
151159 /// dead. Since this doesn't produce an exactly copy of the input, it can't be
152160 /// used for things like CloneFunction or CloneModule.
161 ///
162 /// If ModuleLevelChanges is false, VMap contains no non-identity GlobalValue
163 /// mappings.
164 ///
153165 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
154166 ValueMap &VMap,
167 bool ModuleLevelChanges,
155168 SmallVectorImpl &Returns,
156169 const char *NameSuffix = "",
157170 ClonedCodeInfo *CodeInfo = 0,
2121 class Instruction;
2222 typedef ValueMap ValueToValueMapTy;
2323
24 Value *MapValue(const Value *V, ValueToValueMapTy &VM);
25 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM);
24 Value *MapValue(const Value *V, ValueToValueMapTy &VM,
25 bool ModuleLevelChanges);
26 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
27 bool ModuleLevelChanges);
2628 } // End llvm namespace
2729
2830 #endif
459459 // Add Src elements into Dest node.
460460 for (unsigned i = 0, e = SrcNMD->getNumOperands(); i != e; ++i)
461461 DestNMD->addOperand(cast(MapValue(SrcNMD->getOperand(i),
462 ValueMap)));
462 ValueMap,
463 true)));
463464 }
464465 }
465466
822823 if (SGV->hasInitializer()) { // Only process initialized GV's
823824 // Figure out what the initializer looks like in the dest module...
824825 Constant *SInit =
825 cast(MapValue(SGV->getInitializer(), ValueMap));
826 cast(MapValue(SGV->getInitializer(), ValueMap, true));
826827 // Grab destination global variable or alias.
827828 GlobalValue *DGV = cast(ValueMap[SGV]->stripPointerCasts());
828829
10041005 // the Source function as operands. Loop through all of the operands of the
10051006 // functions and patch them up to point to the local versions...
10061007 //
1008 // This is the same as RemapInstruction, except that it avoids remapping
1009 // instruction and basic block operands.
1010 //
10071011 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
1008 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1012 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1013 // Remap operands.
10091014 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
10101015 OI != OE; ++OI)
10111016 if (!isa(*OI) && !isa(*OI))
1012 *OI = MapValue(*OI, ValueMap);
1017 *OI = MapValue(*OI, ValueMap, true);
1018
1019 // Remap attached metadata.
1020 SmallVector, 4> MDs;
1021 I->getAllMetadata(MDs);
1022 for (SmallVectorImpl >::iterator
1023 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
1024 Value *Old = MI->second;
1025 if (!isa(Old) && !isa(Old)) {
1026 Value *New = MapValue(Old, ValueMap, true);
1027 if (New != Old)
1028 I->setMetadata(MI->first, cast(New));
1029 }
1030 }
1031 }
10131032
10141033 // There is no need to map the arguments anymore.
10151034 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
255255 CloneAutos(OrgF);
256256
257257 // Now create the clone.
258 ClonedF = CloneFunction(OrgF, VMap);
258 ClonedF = CloneFunction(OrgF, VMap, /*ModuleLevelChanges=*/false);
259259
260260 // The new function should be for interrupt line. Therefore should have
261261 // the name suffixed with IL and section attribute marked with IL.
6767
6868 // Clone the function, so that we can hack away on it.
6969 ValueMap VMap;
70 Function* duplicateFunction = CloneFunction(F, VMap);
70 Function* duplicateFunction = CloneFunction(F, VMap,
71 /*ModuleLevelChanges=*/false);
7172 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
7273 F->getParent()->getFunctionList().push_back(duplicateFunction);
7374 BasicBlock* newEntryBlock = cast(VMap[entryBlock]);
7373 deleted[arg->getArgNo()] = arg;
7474 }
7575
76 Function* NF = CloneFunction(F, replacements);
76 Function* NF = CloneFunction(F, replacements,
77 /*ModuleLevelChanges=*/false);
7778 NF->setLinkage(GlobalValue::InternalLinkage);
7879 F->getParent()->getFunctionList().push_back(NF);
7980
6868 }
6969
7070 // Clone OldFunc into NewFunc, transforming the old arguments into references to
71 // ArgMap values.
71 // VMap values.
7272 //
7373 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
7474 ValueToValueMapTy &VMap,
75 bool ModuleLevelChanges,
7576 SmallVectorImpl &Returns,
7677 const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
7778 assert(NameSuffix && "NameSuffix cannot be null!");
125126 BE = NewFunc->end(); BB != BE; ++BB)
126127 // Loop over all instructions, fixing each one as we find it...
127128 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
128 RemapInstruction(II, VMap);
129 RemapInstruction(II, VMap, ModuleLevelChanges);
129130 }
130131
131132 /// CloneFunction - Return a copy of the specified function, but without
138139 ///
139140 Function *llvm::CloneFunction(const Function *F,
140141 ValueToValueMapTy &VMap,
142 bool ModuleLevelChanges,
141143 ClonedCodeInfo *CodeInfo) {
142144 std::vector ArgTypes;
143145
166168 }
167169
168170 SmallVector Returns; // Ignore returns cloned.
169 CloneFunctionInto(NewF, F, VMap, Returns, "", CodeInfo);
171 CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
170172 return NewF;
171173 }
172174
179181 Function *NewFunc;
180182 const Function *OldFunc;
181183 ValueToValueMapTy &VMap;
184 bool ModuleLevelChanges;
182185 SmallVectorImpl &Returns;
183186 const char *NameSuffix;
184187 ClonedCodeInfo *CodeInfo;
186189 public:
187190 PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
188191 ValueToValueMapTy &valueMap,
192 bool moduleLevelChanges,
189193 SmallVectorImpl &returns,
190194 const char *nameSuffix,
191195 ClonedCodeInfo *codeInfo,
192196 const TargetData *td)
193 : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap), Returns(returns),
194 NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) {
197 : NewFunc(newFunc), OldFunc(oldFunc),
198 VMap(valueMap), ModuleLevelChanges(moduleLevelChanges),
199 Returns(returns), NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) {
195200 }
196201
197202 /// CloneBlock - The specified block is found to be reachable, clone it and
312317 SmallVector Ops;
313318 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
314319 if (Constant *Op = dyn_cast_or_null(MapValue(I->getOperand(i),
315 VMap)))
320 VMap, ModuleLevelChanges)))
316321 Ops.push_back(Op);
317322 else
318323 return 0; // All operands not constant!
354359 /// used for things like CloneFunction or CloneModule.
355360 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
356361 ValueToValueMapTy &VMap,
362 bool ModuleLevelChanges,
357363 SmallVectorImpl &Returns,
358364 const char *NameSuffix,
359365 ClonedCodeInfo *CodeInfo,
367373 assert(VMap.count(II) && "No mapping from source argument specified!");
368374 #endif
369375
370 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, Returns,
371 NameSuffix, CodeInfo, TD);
376 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
377 Returns, NameSuffix, CodeInfo, TD);
372378
373379 // Clone the entry block, and anything recursively reachable from it.
374380 std::vector CloneWorklist;
448454 I->setDebugLoc(DebugLoc());
449455 }
450456 }
451 RemapInstruction(I, VMap);
457 RemapInstruction(I, VMap, ModuleLevelChanges);
452458 }
453459 }
454460
470476 if (BasicBlock *MappedBlock =
471477 cast_or_null(VMap[PN->getIncomingBlock(pred)])) {
472478 Value *InVal = MapValue(PN->getIncomingValue(pred),
473 VMap);
479 VMap, ModuleLevelChanges);
474480 assert(InVal && "Unknown input value?");
475481 PN->setIncomingValue(pred, InVal);
476482 PN->setIncomingBlock(pred, MappedBlock);
8888 GlobalVariable *GV = cast(VMap[I]);
8989 if (I->hasInitializer())
9090 GV->setInitializer(cast(MapValue(I->getInitializer(),
91 VMap)));
91 VMap,
92 true)));
9293 GV->setLinkage(I->getLinkage());
9394 GV->setThreadLocal(I->isThreadLocal());
9495 GV->setConstant(I->isConstant());
107108 }
108109
109110 SmallVector Returns; // Ignore returns cloned.
110 CloneFunctionInto(F, I, VMap, Returns);
111 CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
111112 }
112113
113114 F->setLinkage(I->getLinkage());
119120 GlobalAlias *GA = cast(VMap[I]);
120121 GA->setLinkage(I->getLinkage());
121122 if (const Constant* C = I->getAliasee())
122 GA->setAliasee(cast(MapValue(C, VMap)));
123 GA->setAliasee(cast(MapValue(C, VMap, true)));
123124 }
124125
125126 // And named metadata....
128129 const NamedMDNode &NMD = *I;
129130 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
130131 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
131 NewNMD->addOperand(cast(MapValue(NMD.getOperand(i), VMap)));
132 NewNMD->addOperand(cast(MapValue(NMD.getOperand(i), VMap, true)));
132133 }
133134
134 // Update metadata attach with instructions.
135 for (Module::iterator MI = New->begin(), ME = New->end(); MI != ME; ++MI)
136 for (Function::iterator FI = MI->begin(), FE = MI->end();
137 FI != FE; ++FI)
138 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
139 BI != BE; ++BI) {
140 SmallVector, 4 > MDs;
141 BI->getAllMetadata(MDs);
142 for (SmallVector, 4>::iterator
143 MDI = MDs.begin(), MDE = MDs.end(); MDI != MDE; ++MDI) {
144 Value *MappedValue = MapValue(MDI->second, VMap);
145 if (MDI->second != MappedValue && MappedValue)
146 BI->setMetadata(MDI->first, cast(MappedValue));
147 }
148 }
149135 return New;
150136 }
364364 // have no dead or constant instructions leftover after inlining occurs
365365 // (which can happen, e.g., because an argument was constant), but we'll be
366366 // happy with whatever the cloner can do.
367 CloneAndPruneFunctionInto(Caller, CalledFunc, VMap, Returns, ".i",
367 CloneAndPruneFunctionInto(Caller, CalledFunc, VMap,
368 /*ModuleLevelChanges=*/false, Returns, ".i",
368369 &InlinedFunctionInfo, IFI.TD, TheCall);
369370
370371 // Remember the first block that is newly cloned over.
1919 #include "llvm/ADT/SmallVector.h"
2020 using namespace llvm;
2121
22 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
22 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
23 bool ModuleLevelChanges) {
2324 Value *&VMSlot = VM[V];
2425 if (VMSlot) return VMSlot; // Does it exist in the map yet?
2526
2627 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
2728 // DenseMap. This includes any recursive calls to MapValue.
2829
29 // Global values and non-function-local metadata do not need to be seeded into
30 // the VM if they are using the identity mapping.
30 // Global values do not need to be seeded into the VM if they
31 // are using the identity mapping.
3132 if (isa(V) || isa(V) || isa(V) ||
32 (isa(V) && !cast(V)->isFunctionLocal()))
33 (isa(V) && !cast(V)->isFunctionLocal() &&
34 !ModuleLevelChanges))
3335 return VMSlot = const_cast(V);
3436
3537 if (const MDNode *MD = dyn_cast(V)) {
36 SmallVector Elts;
37 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
38 Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0);
39 return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
38 // Start by assuming that we'll use the identity mapping.
39 VMSlot = const_cast(V);
40
41 // Check all operands to see if any need to be remapped.
42 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
43 Value *OP = MD->getOperand(i);
44 if (!OP || MapValue(OP, VM, ModuleLevelChanges) == OP) continue;
45
46 // Ok, at least one operand needs remapping.
47 MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
48 VM[V] = Dummy;
49 SmallVector Elts;
50 Elts.reserve(MD->getNumOperands());
51 for (i = 0; i != e; ++i)
52 Elts.push_back(MD->getOperand(i) ?
53 MapValue(MD->getOperand(i), VM, ModuleLevelChanges) : 0);
54 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
55 Dummy->replaceAllUsesWith(NewMD);
56 MDNode::deleteTemporary(Dummy);
57 return VM[V] = NewMD;
58 }
59
60 // No operands needed remapping; keep the identity map.
61 return const_cast(V);
4062 }
4163
4264 Constant *C = const_cast(dyn_cast(V));
43 if (C == 0) return 0;
65 if (C == 0)
66 return 0;
4467
4568 if (isa(C) || isa(C) ||
4669 isa(C) || isa(C) ||
5073 if (ConstantArray *CA = dyn_cast(C)) {
5174 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
5275 i != e; ++i) {
53 Value *MV = MapValue(*i, VM);
76 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
5477 if (MV != *i) {
5578 // This array must contain a reference to a global, make a new array
5679 // and return it.
6184 Values.push_back(cast(*j));
6285 Values.push_back(cast(MV));
6386 for (++i; i != e; ++i)
64 Values.push_back(cast(MapValue(*i, VM)));
87 Values.push_back(cast(MapValue(*i, VM,
88 ModuleLevelChanges)));
6589 return VM[V] = ConstantArray::get(CA->getType(), Values);
6690 }
6791 }
7195 if (ConstantStruct *CS = dyn_cast(C)) {
7296 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
7397 i != e; ++i) {
74 Value *MV = MapValue(*i, VM);
98 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
7599 if (MV != *i) {
76100 // This struct must contain a reference to a global, make a new struct
77101 // and return it.
82106 Values.push_back(cast(*j));
83107 Values.push_back(cast(MV));
84108 for (++i; i != e; ++i)
85 Values.push_back(cast(MapValue(*i, VM)));
109 Values.push_back(cast(MapValue(*i, VM,
110 ModuleLevelChanges)));
86111 return VM[V] = ConstantStruct::get(CS->getType(), Values);
87112 }
88113 }
92117 if (ConstantExpr *CE = dyn_cast(C)) {
93118 std::vector Ops;
94119 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
95 Ops.push_back(cast(MapValue(*i, VM)));
120 Ops.push_back(cast(MapValue(*i, VM, ModuleLevelChanges)));
96121 return VM[V] = CE->getWithOperands(Ops);
97122 }
98123
99124 if (ConstantVector *CV = dyn_cast(C)) {
100125 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
101126 i != e; ++i) {
102 Value *MV = MapValue(*i, VM);
127 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
103128 if (MV != *i) {
104129 // This vector value must contain a reference to a global, make a new
105130 // vector constant and return it.
110135 Values.push_back(cast(*j));
111136 Values.push_back(cast(MV));
112137 for (++i; i != e; ++i)
113 Values.push_back(cast(MapValue(*i, VM)));
138 Values.push_back(cast(MapValue(*i, VM,
139 ModuleLevelChanges)));
114140 return VM[V] = ConstantVector::get(Values);
115141 }
116142 }
118144 }
119145
120146 BlockAddress *BA = cast(C);
121 Function *F = cast(MapValue(BA->getFunction(), VM));
122 BasicBlock *BB = cast_or_null(MapValue(BA->getBasicBlock(),VM));
147 Function *F = cast(MapValue(BA->getFunction(), VM,
148 ModuleLevelChanges));
149 BasicBlock *BB = cast_or_null(MapValue(BA->getBasicBlock(),VM,
150 ModuleLevelChanges));
123151 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
124152 }
125153
126154 /// RemapInstruction - Convert the instruction operands from referencing the
127155 /// current values into those specified by VMap.
128156 ///
129 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
157 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
158 bool ModuleLevelChanges) {
159 // Remap operands.
130160 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
131 Value *V = MapValue(*op, VMap);
161 Value *V = MapValue(*op, VMap, ModuleLevelChanges);
132162 assert(V && "Referenced value not in value map!");
133163 *op = V;
134164 }
165
166 // Remap attached metadata.
167 SmallVector, 4> MDs;
168 I->getAllMetadata(MDs);
169 for (SmallVectorImpl >::iterator
170 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
171 Value *Old = MI->second;
172 Value *New = MapValue(Old, VMap, ModuleLevelChanges);
173 if (New != Old)
174 I->setMetadata(MI->first, cast(New));
175 }
135176 }
136
0 ; RUN: llvm-link %s %p/metadata-b.ll -S -o - | FileCheck %s
1
2 ; CHECK: define void @foo(i32 %a)
3 ; CHECK: ret void, !attach !0, !also !{i32 %a}
4 ; CHECK: define void @goo(i32 %b)
5 ; CHECK: ret void, !attach !1, !and !{i32 %b}
6 ; CHECK: !0 = metadata !{i32 524334, void (i32)* @foo}
7 ; CHECK: !1 = metadata !{i32 524334, void (i32)* @goo}
8
9 define void @foo(i32 %a) nounwind {
10 entry:
11 ret void, !attach !0, !also !{ i32 %a }
12 }
13
14 !0 = metadata !{i32 524334, void (i32)* @foo}
0 ; This file is for use with metadata-a.ll
1 ; RUN: true
2
3 define void @goo(i32 %b) nounwind {
4 entry:
5 ret void, !attach !0, !and !{ i32 %b }
6 }
7
8 !0 = metadata !{i32 524334, void (i32)* @goo}