Skip to content

Commit 6845960

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 9dcfc56 commit 6845960

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

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

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

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

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

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

llvm/lib/Transforms/Utils/CloneFunction.cpp

+55-41
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,
@@ -282,47 +335,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
282335
CloneFunctionMetadataInto(NewFunc, OldFunc, VMap, RemapFlag, TypeMapper,
283336
Materializer);
284337

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

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

0 commit comments

Comments
 (0)