@@ -216,6 +216,59 @@ void llvm::CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc,
216
216
}
217
217
}
218
218
219
+ void llvm::CloneFunctionBodyInto (Function *NewFunc, const Function *OldFunc,
220
+ ValueToValueMapTy &VMap, RemapFlags RemapFlag,
221
+ SmallVectorImpl<ReturnInst *> &Returns,
222
+ const char *NameSuffix,
223
+ ClonedCodeInfo *CodeInfo,
224
+ ValueMapTypeRemapper *TypeMapper,
225
+ ValueMaterializer *Materializer) {
226
+ if (OldFunc->isDeclaration ())
227
+ return ;
228
+
229
+ // Loop over all of the basic blocks in the function, cloning them as
230
+ // appropriate. Note that we save BE this way in order to handle cloning of
231
+ // recursive functions into themselves.
232
+ for (const BasicBlock &BB : *OldFunc) {
233
+
234
+ // Create a new basic block and copy instructions into it!
235
+ BasicBlock *CBB = CloneBasicBlock (&BB, VMap, NameSuffix, NewFunc, CodeInfo);
236
+
237
+ // Add basic block mapping.
238
+ VMap[&BB] = CBB;
239
+
240
+ // It is only legal to clone a function if a block address within that
241
+ // function is never referenced outside of the function. Given that, we
242
+ // want to map block addresses from the old function to block addresses in
243
+ // the clone. (This is different from the generic ValueMapper
244
+ // implementation, which generates an invalid blockaddress when
245
+ // cloning a function.)
246
+ if (BB.hasAddressTaken ()) {
247
+ Constant *OldBBAddr = BlockAddress::get (const_cast <Function *>(OldFunc),
248
+ const_cast <BasicBlock *>(&BB));
249
+ VMap[OldBBAddr] = BlockAddress::get (NewFunc, CBB);
250
+ }
251
+
252
+ // Note return instructions for the caller.
253
+ if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator ()))
254
+ Returns.push_back (RI);
255
+ }
256
+
257
+ // Loop over all of the instructions in the new function, fixing up operand
258
+ // references as we go. This uses VMap to do all the hard work.
259
+ for (Function::iterator
260
+ BB = cast<BasicBlock>(VMap[&OldFunc->front ()])->getIterator (),
261
+ BE = NewFunc->end ();
262
+ BB != BE; ++BB)
263
+ // Loop over all instructions, fixing each one as we find it, and any
264
+ // attached debug-info records.
265
+ for (Instruction &II : *BB) {
266
+ RemapInstruction (&II, VMap, RemapFlag, TypeMapper, Materializer);
267
+ RemapDbgRecordRange (II.getModule (), II.getDbgRecordRange (), VMap,
268
+ RemapFlag, TypeMapper, Materializer);
269
+ }
270
+ }
271
+
219
272
// Clone OldFunc into NewFunc, transforming the old arguments into references to
220
273
// VMap values.
221
274
void llvm::CloneFunctionInto (Function *NewFunc, const Function *OldFunc,
@@ -282,47 +335,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
282
335
CloneFunctionMetadataInto (NewFunc, OldFunc, VMap, RemapFlag, TypeMapper,
283
336
Materializer);
284
337
285
- // Loop over all of the basic blocks in the function, cloning them as
286
- // appropriate. Note that we save BE this way in order to handle cloning of
287
- // recursive functions into themselves.
288
- for (const BasicBlock &BB : *OldFunc) {
289
-
290
- // Create a new basic block and copy instructions into it!
291
- BasicBlock *CBB = CloneBasicBlock (&BB, VMap, NameSuffix, NewFunc, CodeInfo);
292
-
293
- // Add basic block mapping.
294
- VMap[&BB] = CBB;
295
-
296
- // It is only legal to clone a function if a block address within that
297
- // function is never referenced outside of the function. Given that, we
298
- // want to map block addresses from the old function to block addresses in
299
- // the clone. (This is different from the generic ValueMapper
300
- // implementation, which generates an invalid blockaddress when
301
- // cloning a function.)
302
- if (BB.hasAddressTaken ()) {
303
- Constant *OldBBAddr = BlockAddress::get (const_cast <Function *>(OldFunc),
304
- const_cast <BasicBlock *>(&BB));
305
- VMap[OldBBAddr] = BlockAddress::get (NewFunc, CBB);
306
- }
307
-
308
- // Note return instructions for the caller.
309
- if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator ()))
310
- Returns.push_back (RI);
311
- }
312
-
313
- // Loop over all of the instructions in the new function, fixing up operand
314
- // references as we go. This uses VMap to do all the hard work.
315
- for (Function::iterator
316
- BB = cast<BasicBlock>(VMap[&OldFunc->front ()])->getIterator (),
317
- BE = NewFunc->end ();
318
- BB != BE; ++BB)
319
- // Loop over all instructions, fixing each one as we find it, and any
320
- // attached debug-info records.
321
- for (Instruction &II : *BB) {
322
- RemapInstruction (&II, VMap, RemapFlag, TypeMapper, Materializer);
323
- RemapDbgRecordRange (II.getModule (), II.getDbgRecordRange (), VMap,
324
- RemapFlag, TypeMapper, Materializer);
325
- }
338
+ CloneFunctionBodyInto (NewFunc, OldFunc, VMap, RemapFlag, Returns, NameSuffix,
339
+ CodeInfo, TypeMapper, Materializer);
326
340
327
341
// Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the
328
342
// same module, the compile unit will already be listed (or not). When
0 commit comments