Skip to content

Commit 06c10b1

Browse files
committed
[NFC][Utils] Clone basic blocks after we're done with metadata in CloneFunctionInto
Summary: This 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
1 parent a611c5e commit 06c10b1

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,34 +212,6 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
212212
DISubprogram *SPClonedWithinModule =
213213
ProcessSubprogramAttachment(*OldFunc, Changes, DIFinder);
214214

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

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

0 commit comments

Comments
 (0)