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