Skip to content

Commit b268200

Browse files
committed
[NFC][Utils] Extract CloneFunctionBodyInto from CloneFunctionInto
Summary: This and previously extracted functions will be used in a later diff. Test Plan: ninja check-llvm-unit
1 parent 53d8285 commit b268200

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

llvm/include/llvm/Transforms/Utils/Cloning.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ void CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc,
192192
ValueMapTypeRemapper *TypeMapper = nullptr,
193193
ValueMaterializer *Materializer = nullptr);
194194

195+
/// Clone OldFunc's body NewFunct.
196+
void CloneFunctionBodyInto(Function *NewFunc, const Function *OldFunc,
197+
ValueToValueMapTy &VMap, RemapFlags RemapFlag,
198+
SmallVectorImpl<ReturnInst *> &Returns,
199+
const char *NameSuffix = "",
200+
ClonedCodeInfo *CodeInfo = nullptr,
201+
ValueMapTypeRemapper *TypeMapper = nullptr,
202+
ValueMaterializer *Materializer = nullptr);
203+
195204
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
196205
const Instruction *StartingInst,
197206
ValueToValueMapTy &VMap, bool ModuleLevelChanges,

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,59 @@ void llvm::CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc,
216216
}
217217
}
218218

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+
219272
// Clone OldFunc into NewFunc, transforming the old arguments into references to
220273
// VMap values.
221274
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
@@ -283,47 +336,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
283336
CloneFunctionMetadataInto(NewFunc, OldFunc, VMap, RemapFlag, TypeMapper,
284337
Materializer);
285338

286-
// Loop over all of the basic blocks in the function, cloning them as
287-
// appropriate. Note that we save BE this way in order to handle cloning of
288-
// recursive functions into themselves.
289-
for (const BasicBlock &BB : *OldFunc) {
290-
291-
// Create a new basic block and copy instructions into it!
292-
BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
293-
294-
// Add basic block mapping.
295-
VMap[&BB] = CBB;
296-
297-
// It is only legal to clone a function if a block address within that
298-
// function is never referenced outside of the function. Given that, we
299-
// want to map block addresses from the old function to block addresses in
300-
// the clone. (This is different from the generic ValueMapper
301-
// implementation, which generates an invalid blockaddress when
302-
// cloning a function.)
303-
if (BB.hasAddressTaken()) {
304-
Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
305-
const_cast<BasicBlock *>(&BB));
306-
VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
307-
}
308-
309-
// Note return instructions for the caller.
310-
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
311-
Returns.push_back(RI);
312-
}
313-
314-
// Loop over all of the instructions in the new function, fixing up operand
315-
// references as we go. This uses VMap to do all the hard work.
316-
for (Function::iterator
317-
BB = cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
318-
BE = NewFunc->end();
319-
BB != BE; ++BB)
320-
// Loop over all instructions, fixing each one as we find it, and any
321-
// attached debug-info records.
322-
for (Instruction &II : *BB) {
323-
RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer);
324-
RemapDbgRecordRange(II.getModule(), II.getDbgRecordRange(), VMap,
325-
RemapFlag, TypeMapper, Materializer);
326-
}
339+
CloneFunctionBodyInto(NewFunc, OldFunc, VMap, RemapFlag, Returns, NameSuffix,
340+
CodeInfo, TypeMapper, Materializer);
327341

328342
// Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the
329343
// same module, the compile unit will already be listed (or not). When

0 commit comments

Comments
 (0)