Skip to content

Commit 1560bb0

Browse files
committed
[NFC][Utils] Extract CloneFunctionBodyInto from CloneFunctionInto
Summary: This and previously extracted `CloneFunction*Into` functions will be used in later diffs. Test Plan: ninja check-llvm-unit check-llvm stack-info: PR: #118624, branch: users/artempyanykh/fast-coro-upstream/5
1 parent a91e783 commit 1560bb0

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ void CloneFunctionMetadataInto(Function &NewFunc, const Function &OldFunc,
195195
ValueMapTypeRemapper *TypeMapper = nullptr,
196196
ValueMaterializer *Materializer = nullptr);
197197

198+
/// Clone OldFunc's body into NewFunc.
199+
void CloneFunctionBodyInto(Function &NewFunc, const Function &OldFunc,
200+
ValueToValueMapTy &VMap, RemapFlags RemapFlag,
201+
SmallVectorImpl<ReturnInst *> &Returns,
202+
const char *NameSuffix = "",
203+
ClonedCodeInfo *CodeInfo = nullptr,
204+
ValueMapTypeRemapper *TypeMapper = nullptr,
205+
ValueMaterializer *Materializer = nullptr);
206+
198207
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
199208
const Instruction *StartingInst,
200209
ValueToValueMapTy &VMap, bool ModuleLevelChanges,

llvm/lib/Transforms/Utils/CloneFunction.cpp

+56-41
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,60 @@ void llvm::CloneFunctionMetadataInto(Function &NewFunc, const Function &OldFunc,
213213
}
214214
}
215215

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+
216270
// Clone OldFunc into NewFunc, transforming the old arguments into references to
217271
// VMap values.
218272
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
@@ -279,47 +333,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
279333
CloneFunctionMetadataInto(*NewFunc, *OldFunc, VMap, RemapFlag, TypeMapper,
280334
Materializer);
281335

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);
323338

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

0 commit comments

Comments
 (0)