diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp --- a/llvm/lib/Transforms/Utils/CloneFunction.cpp +++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp @@ -125,6 +125,11 @@ AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(), OldAttrs.getRetAttributes(), NewArgAttrs)); + // Everything else beyond this point deals with function instructions, + // so if we are dealing with a function declaration, we're done. + if (OldFunc->isDeclaration()) + return; + // When we remap instructions within the same module, we want to avoid // duplicating inlined DISubprograms, so record all subprograms we find as we // duplicate instructions and then freeze them in the MD map. We also record @@ -149,7 +154,7 @@ } else { assert((NewFunc->getParent() == nullptr || NewFunc->getParent() != OldFunc->getParent()) && - "Set SameModule to true if the new function is in the same module"); + "expected NewFunc to have different parents, or no parent"); if (Changes == CloneFunctionChangeType::DifferentModule) { assert(NewFunc->getParent() && @@ -160,17 +165,10 @@ } } - // Everything else beyond this point deals with function instructions, - // so if we are dealing with a function declaration, we're done. - if (OldFunc->isDeclaration()) - return; - // Loop over all of the basic blocks in the function, cloning them as // appropriate. Note that we save BE this way in order to handle cloning of // recursive functions into themselves. - for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); - BI != BE; ++BI) { - const BasicBlock &BB = *BI; + for (const BasicBlock &BB : *OldFunc) { // Create a new basic block and copy instructions into it! BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo, @@ -186,8 +184,8 @@ // implementation, which generates an invalid blockaddress when // cloning a function.) if (BB.hasAddressTaken()) { - Constant *OldBBAddr = BlockAddress::get(const_cast(OldFunc), - const_cast(&BB)); + Constant *OldBBAddr = BlockAddress::get(const_cast(OldFunc), + const_cast(&BB)); VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); } @@ -225,30 +223,26 @@ "Subprogram should be in DIFinder->subprogram_count()..."); } + const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges; // Duplicate the metadata that is attached to the cloned function. // Subprograms/CUs/types that were already mapped to themselves won't be // duplicated. SmallVector, 1> MDs; OldFunc->getAllMetadata(MDs); for (auto MD : MDs) { - NewFunc->addMetadata( - MD.first, - *MapMetadata(MD.second, VMap, - ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, - TypeMapper, Materializer)); + NewFunc->addMetadata(MD.first, *MapMetadata(MD.second, VMap, RemapFlag, + TypeMapper, Materializer)); } - // Loop over all of the instructions in the function, fixing up operand - // references as we go. This uses VMap to do all the hard work. - for (Function::iterator BB = - cast(VMap[&OldFunc->front()])->getIterator(), - BE = NewFunc->end(); + // Loop over all of the instructions in the new function, fixing up operand + // references as we go. This uses VMap to do all the hard work. + for (Function::iterator + BB = cast(VMap[&OldFunc->front()])->getIterator(), + BE = NewFunc->end(); BB != BE; ++BB) // Loop over all instructions, fixing each one as we find it... for (Instruction &II : *BB) - RemapInstruction(&II, VMap, - ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, - TypeMapper, Materializer); + RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer); // Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the // same module, the compile unit will already be listed (or not). When @@ -266,7 +260,7 @@ // visiting the metadata attached to global values, which would allow this // code to be deleted. Alternatively, perhaps give responsibility for this // update to CloneFunctionInto's callers. - auto* NewModule = NewFunc->getParent(); + auto *NewModule = NewFunc->getParent(); auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu"); // Avoid multiple insertions of the same DICompileUnit to NMD. SmallPtrSet Visited; @@ -289,7 +283,7 @@ /// Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo) { - std::vector ArgTypes; + std::vector ArgTypes; // The user might be deleting arguments to the function by specifying them in // the VMap. If so, we need to not add the arguments to the arg ty vector