Skip to content

Commit 39cccf5

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 8540f25 commit 39cccf5

File tree

2 files changed

+76
-54
lines changed

2 files changed

+76
-54
lines changed

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

+21-13
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ void CloneFunctionMetadataInto(Function *NewFunc, const Function *OldFunc,
194194
ValueMapTypeRemapper *TypeMapper = nullptr,
195195
ValueMaterializer *Materializer = nullptr);
196196

197+
/// Clone OldFunc's body into NewFunc.
198+
void CloneFunctionBodyInto(Function *NewFunc, const Function *OldFunc,
199+
ValueToValueMapTy &VMap, RemapFlags RemapFlag,
200+
SmallVectorImpl<ReturnInst *> &Returns,
201+
const char *NameSuffix = "",
202+
ClonedCodeInfo *CodeInfo = nullptr,
203+
ValueMapTypeRemapper *TypeMapper = nullptr,
204+
ValueMaterializer *Materializer = nullptr);
205+
197206
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
198207
const Instruction *StartingInst,
199208
ValueToValueMapTy &VMap, bool ModuleLevelChanges,
@@ -214,7 +223,7 @@ void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
214223
///
215224
void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
216225
ValueToValueMapTy &VMap, bool ModuleLevelChanges,
217-
SmallVectorImpl<ReturnInst*> &Returns,
226+
SmallVectorImpl<ReturnInst *> &Returns,
218227
const char *NameSuffix = "",
219228
ClonedCodeInfo *CodeInfo = nullptr);
220229

@@ -360,32 +369,31 @@ void updateProfileCallee(
360369
/// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified
361370
/// basic blocks and extract their scope. These are candidates for duplication
362371
/// when cloning.
363-
void identifyNoAliasScopesToClone(
364-
ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes);
372+
void identifyNoAliasScopesToClone(ArrayRef<BasicBlock *> BBs,
373+
SmallVectorImpl<MDNode *> &NoAliasDeclScopes);
365374

366375
/// Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified
367376
/// instruction range and extract their scope. These are candidates for
368377
/// duplication when cloning.
369-
void identifyNoAliasScopesToClone(
370-
BasicBlock::iterator Start, BasicBlock::iterator End,
371-
SmallVectorImpl<MDNode *> &NoAliasDeclScopes);
378+
void identifyNoAliasScopesToClone(BasicBlock::iterator Start,
379+
BasicBlock::iterator End,
380+
SmallVectorImpl<MDNode *> &NoAliasDeclScopes);
372381

373382
/// Duplicate the specified list of noalias decl scopes.
374383
/// The 'Ext' string is added as an extension to the name.
375384
/// Afterwards, the ClonedScopes contains the mapping of the original scope
376385
/// MDNode onto the cloned scope.
377386
/// Be aware that the cloned scopes are still part of the original scope domain.
378-
void cloneNoAliasScopes(
379-
ArrayRef<MDNode *> NoAliasDeclScopes,
380-
DenseMap<MDNode *, MDNode *> &ClonedScopes,
381-
StringRef Ext, LLVMContext &Context);
387+
void cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
388+
DenseMap<MDNode *, MDNode *> &ClonedScopes,
389+
StringRef Ext, LLVMContext &Context);
382390

383391
/// Adapt the metadata for the specified instruction according to the
384392
/// provided mapping. This is normally used after cloning an instruction, when
385393
/// some noalias scopes needed to be cloned.
386-
void adaptNoAliasScopes(
387-
llvm::Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
388-
LLVMContext &Context);
394+
void adaptNoAliasScopes(llvm::Instruction *I,
395+
const DenseMap<MDNode *, MDNode *> &ClonedScopes,
396+
LLVMContext &Context);
389397

390398
/// Clone the specified noalias decl scopes. Then adapt all instructions in the
391399
/// NewBlocks basicblocks to the cloned versions.

llvm/lib/Transforms/Utils/CloneFunction.cpp

+55-41
Original file line numberDiff line numberDiff line change
@@ -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.
220273
void 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

Comments
 (0)