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