Skip to content

Commit f2f9f77

Browse files
authored
[NFC][LLVM] Code cleanup in CloneFunction.cpp (llvm#162875)
- Use explicit types instead of auto when type is not obvious. - Move local function out of anonymous namespace and make them static. - Use structured bindings in some range for loops. - Simplify PHI handling code a bit.
1 parent c6e4a63 commit f2f9f77

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ using namespace llvm;
4444
STATISTIC(RemappedAtomMax, "Highest global NextAtomGroup (after mapping)");
4545

4646
void llvm::mapAtomInstance(const DebugLoc &DL, ValueToValueMapTy &VMap) {
47-
auto CurGroup = DL->getAtomGroup();
47+
uint64_t CurGroup = DL->getAtomGroup();
4848
if (!CurGroup)
4949
return;
5050

@@ -62,21 +62,20 @@ void llvm::mapAtomInstance(const DebugLoc &DL, ValueToValueMapTy &VMap) {
6262
RemappedAtomMax = std::max<uint64_t>(NewGroup, RemappedAtomMax);
6363
}
6464

65-
namespace {
66-
void collectDebugInfoFromInstructions(const Function &F,
67-
DebugInfoFinder &DIFinder) {
65+
static void collectDebugInfoFromInstructions(const Function &F,
66+
DebugInfoFinder &DIFinder) {
6867
const Module *M = F.getParent();
69-
if (M) {
70-
// Inspect instructions to process e.g. DILexicalBlocks of inlined functions
71-
for (const auto &I : instructions(F))
72-
DIFinder.processInstruction(*M, I);
73-
}
68+
if (!M)
69+
return;
70+
// Inspect instructions to process e.g. DILexicalBlocks of inlined functions
71+
for (const Instruction &I : instructions(F))
72+
DIFinder.processInstruction(*M, I);
7473
}
7574

7675
// Create a predicate that matches the metadata that should be identity mapped
7776
// during function cloning.
78-
MetadataPredicate createIdentityMDPredicate(const Function &F,
79-
CloneFunctionChangeType Changes) {
77+
static MetadataPredicate
78+
createIdentityMDPredicate(const Function &F, CloneFunctionChangeType Changes) {
8079
if (Changes >= CloneFunctionChangeType::DifferentModule)
8180
return [](const Metadata *MD) { return false; };
8281

@@ -107,7 +106,6 @@ MetadataPredicate createIdentityMDPredicate(const Function &F,
107106
return false;
108107
};
109108
}
110-
} // namespace
111109

112110
/// See comments in Cloning.h.
113111
BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
@@ -213,10 +211,9 @@ void llvm::CloneFunctionMetadataInto(Function &NewFunc, const Function &OldFunc,
213211
const MetadataPredicate *IdentityMD) {
214212
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
215213
OldFunc.getAllMetadata(MDs);
216-
for (auto MD : MDs) {
217-
NewFunc.addMetadata(MD.first,
218-
*MapMetadata(MD.second, VMap, RemapFlag, TypeMapper,
219-
Materializer, IdentityMD));
214+
for (const auto &[Kind, MD] : MDs) {
215+
NewFunc.addMetadata(Kind, *MapMetadata(MD, VMap, RemapFlag, TypeMapper,
216+
Materializer, IdentityMD));
220217
}
221218
}
222219

@@ -235,7 +232,6 @@ void llvm::CloneFunctionBodyInto(Function &NewFunc, const Function &OldFunc,
235232
// appropriate. Note that we save BE this way in order to handle cloning of
236233
// recursive functions into themselves.
237234
for (const BasicBlock &BB : OldFunc) {
238-
239235
// Create a new basic block and copy instructions into it!
240236
BasicBlock *CBB =
241237
CloneBasicBlock(&BB, VMap, NameSuffix, &NewFunc, CodeInfo);
@@ -321,7 +317,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
321317

322318
// Cloning is always a Module level operation, since Metadata needs to be
323319
// cloned.
324-
const auto RemapFlag = RF_None;
320+
const RemapFlags RemapFlag = RF_None;
325321

326322
CloneFunctionMetadataInto(*NewFunc, *OldFunc, VMap, RemapFlag, TypeMapper,
327323
Materializer, &IdentityMD);
@@ -346,16 +342,16 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
346342
// visiting the metadata attached to global values, which would allow this
347343
// code to be deleted. Alternatively, perhaps give responsibility for this
348344
// update to CloneFunctionInto's callers.
349-
auto *NewModule = NewFunc->getParent();
350-
auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
345+
Module *NewModule = NewFunc->getParent();
346+
NamedMDNode *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
351347
// Avoid multiple insertions of the same DICompileUnit to NMD.
352348
SmallPtrSet<const void *, 8> Visited(llvm::from_range, NMD->operands());
353349

354350
// Collect and clone all the compile units referenced from the instructions in
355351
// the function (e.g. as instructions' scope).
356352
DebugInfoFinder DIFinder;
357353
collectDebugInfoFromInstructions(*OldFunc, DIFinder);
358-
for (auto *Unit : DIFinder.compile_units()) {
354+
for (DICompileUnit *Unit : DIFinder.compile_units()) {
359355
MDNode *MappedUnit =
360356
MapMetadata(Unit, VMap, RF_None, TypeMapper, Materializer);
361357
if (Visited.insert(MappedUnit).second)
@@ -821,17 +817,16 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
821817
--PredCount[Pred];
822818

823819
// Figure out how many entries to remove from each PHI.
824-
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
825-
++PredCount[PN->getIncomingBlock(i)];
820+
for (BasicBlock *Pred : PN->blocks())
821+
++PredCount[Pred];
826822

827823
// At this point, the excess predecessor entries are positive in the
828824
// map. Loop over all of the PHIs and remove excess predecessor
829825
// entries.
830826
BasicBlock::iterator I = NewBB->begin();
831827
for (; (PN = dyn_cast<PHINode>(I)); ++I) {
832-
for (const auto &PCI : PredCount) {
833-
BasicBlock *Pred = PCI.first;
834-
for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
828+
for (const auto &[Pred, Count] : PredCount) {
829+
for (unsigned _ : llvm::seq<unsigned>(Count))
835830
PN->removeIncomingValue(Pred, false);
836831
}
837832
}
@@ -866,8 +861,8 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
866861
// As phi-nodes have been now remapped, allow incremental simplification of
867862
// newly-cloned instructions.
868863
const DataLayout &DL = NewFunc->getDataLayout();
869-
for (const auto &BB : *OldFunc) {
870-
for (const auto &I : BB) {
864+
for (const BasicBlock &BB : *OldFunc) {
865+
for (const Instruction &I : BB) {
871866
auto *NewI = dyn_cast_or_null<Instruction>(VMap.lookup(&I));
872867
if (!NewI)
873868
continue;
@@ -997,8 +992,8 @@ void llvm::CloneAndPruneFunctionInto(
997992
void llvm::remapInstructionsInBlocks(ArrayRef<BasicBlock *> Blocks,
998993
ValueToValueMapTy &VMap) {
999994
// Rewrite the code to refer to itself.
1000-
for (auto *BB : Blocks) {
1001-
for (auto &Inst : *BB) {
995+
for (BasicBlock *BB : Blocks) {
996+
for (Instruction &Inst : *BB) {
1002997
RemapDbgRecordRange(Inst.getModule(), Inst.getDbgRecordRange(), VMap,
1003998
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1004999
RemapInstruction(&Inst, VMap,
@@ -1151,9 +1146,9 @@ void llvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
11511146
StringRef Ext, LLVMContext &Context) {
11521147
MDBuilder MDB(Context);
11531148

1154-
for (auto *ScopeList : NoAliasDeclScopes) {
1155-
for (const auto &MDOperand : ScopeList->operands()) {
1156-
if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) {
1149+
for (MDNode *ScopeList : NoAliasDeclScopes) {
1150+
for (const MDOperand &MDOp : ScopeList->operands()) {
1151+
if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
11571152
AliasScopeNode SNANode(MD);
11581153

11591154
std::string Name;
@@ -1177,7 +1172,7 @@ void llvm::adaptNoAliasScopes(Instruction *I,
11771172
auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
11781173
bool NeedsReplacement = false;
11791174
SmallVector<Metadata *, 8> NewScopeList;
1180-
for (const auto &MDOp : ScopeList->operands()) {
1175+
for (const MDOperand &MDOp : ScopeList->operands()) {
11811176
if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
11821177
if (auto *NewMD = ClonedScopes.lookup(MD)) {
11831178
NewScopeList.push_back(NewMD);
@@ -1193,12 +1188,12 @@ void llvm::adaptNoAliasScopes(Instruction *I,
11931188
};
11941189

11951190
if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
1196-
if (auto *NewScopeList = CloneScopeList(Decl->getScopeList()))
1191+
if (MDNode *NewScopeList = CloneScopeList(Decl->getScopeList()))
11971192
Decl->setScopeList(NewScopeList);
11981193

11991194
auto replaceWhenNeeded = [&](unsigned MD_ID) {
12001195
if (const MDNode *CSNoAlias = I->getMetadata(MD_ID))
1201-
if (auto *NewScopeList = CloneScopeList(CSNoAlias))
1196+
if (MDNode *NewScopeList = CloneScopeList(CSNoAlias))
12021197
I->setMetadata(MD_ID, NewScopeList);
12031198
};
12041199
replaceWhenNeeded(LLVMContext::MD_noalias);

0 commit comments

Comments
 (0)