Skip to content

Commit c34f14e

Browse files
committed
[NFC][Utils] Clone basic blocks after we're done with metadata in CloneFunctionInto
Summary: Moving the cloning of BBs after the metadata makes the flow of the function a bit more straightforward and makes it easier to extract more into helper functions. Test Plan: ninja check-llvm-unit check-llvm stack-info: PR: #118621, branch: users/artempyanykh/fast-coro-upstream/2
1 parent a202a35 commit c34f14e

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

+28-28
Original file line numberDiff line numberDiff line change
@@ -210,34 +210,6 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
210210
DISubprogram *SPClonedWithinModule =
211211
CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder);
212212

213-
// Loop over all of the basic blocks in the function, cloning them as
214-
// appropriate. Note that we save BE this way in order to handle cloning of
215-
// recursive functions into themselves.
216-
for (const BasicBlock &BB : *OldFunc) {
217-
218-
// Create a new basic block and copy instructions into it!
219-
BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
220-
221-
// Add basic block mapping.
222-
VMap[&BB] = CBB;
223-
224-
// It is only legal to clone a function if a block address within that
225-
// function is never referenced outside of the function. Given that, we
226-
// want to map block addresses from the old function to block addresses in
227-
// the clone. (This is different from the generic ValueMapper
228-
// implementation, which generates an invalid blockaddress when
229-
// cloning a function.)
230-
if (BB.hasAddressTaken()) {
231-
Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
232-
const_cast<BasicBlock *>(&BB));
233-
VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
234-
}
235-
236-
// Note return instructions for the caller.
237-
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
238-
Returns.push_back(RI);
239-
}
240-
241213
if (Changes < CloneFunctionChangeType::DifferentModule &&
242214
DIFinder.subprogram_count() > 0) {
243215
// Turn on module-level changes, since we need to clone (some of) the
@@ -289,6 +261,34 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
289261
TypeMapper, Materializer));
290262
}
291263

264+
// Loop over all of the basic blocks in the function, cloning them as
265+
// appropriate. Note that we save BE this way in order to handle cloning of
266+
// recursive functions into themselves.
267+
for (const BasicBlock &BB : *OldFunc) {
268+
269+
// Create a new basic block and copy instructions into it!
270+
BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
271+
272+
// Add basic block mapping.
273+
VMap[&BB] = CBB;
274+
275+
// It is only legal to clone a function if a block address within that
276+
// function is never referenced outside of the function. Given that, we
277+
// want to map block addresses from the old function to block addresses in
278+
// the clone. (This is different from the generic ValueMapper
279+
// implementation, which generates an invalid blockaddress when
280+
// cloning a function.)
281+
if (BB.hasAddressTaken()) {
282+
Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
283+
const_cast<BasicBlock *>(&BB));
284+
VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
285+
}
286+
287+
// Note return instructions for the caller.
288+
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
289+
Returns.push_back(RI);
290+
}
291+
292292
// Loop over all of the instructions in the new function, fixing up operand
293293
// references as we go. This uses VMap to do all the hard work.
294294
for (Function::iterator

0 commit comments

Comments
 (0)