aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2021-08-22 19:00:43 +0000
committerDimitry Andric <dim@FreeBSD.org>2021-11-13 20:39:49 +0000
commitfe6060f10f634930ff71b7c50291ddc610da2475 (patch)
tree1483580c790bd4d27b6500a7542b5ee00534d3cc /contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp
parentb61bce17f346d79cecfd8f195a64b10f77be43b1 (diff)
parent344a3780b2e33f6ca763666c380202b18aab72a3 (diff)
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp')
-rw-r--r--contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp357
1 files changed, 200 insertions, 157 deletions
diff --git a/contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp b/contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp
index 6ab061510a60..0ac9a5aaa425 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -44,7 +44,6 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
const Twine &NameSuffix, Function *F,
ClonedCodeInfo *CodeInfo,
DebugInfoFinder *DIFinder) {
- DenseMap<const MDNode *, MDNode *> Cache;
BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
if (BB->hasName())
NewBB->setName(BB->getName() + NameSuffix);
@@ -72,7 +71,7 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
}
if (CodeInfo) {
- CodeInfo->ContainsCalls |= hasCalls;
+ CodeInfo->ContainsCalls |= hasCalls;
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
}
return NewBB;
@@ -83,8 +82,8 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
//
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
ValueToValueMapTy &VMap,
- bool ModuleLevelChanges,
- SmallVectorImpl<ReturnInst*> &Returns,
+ CloneFunctionChangeType Changes,
+ SmallVectorImpl<ReturnInst *> &Returns,
const char *NameSuffix, ClonedCodeInfo *CodeInfo,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
@@ -95,6 +94,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
assert(VMap.count(&I) && "No mapping from source argument specified!");
#endif
+ bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
+
// Copy all attributes other than those stored in the AttributeList. We need
// to remap the parameter indices of the AttributeList.
AttributeList NewAttrs = NewFunc->getAttributes();
@@ -123,45 +124,54 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(),
OldAttrs.getRetAttributes(), NewArgAttrs));
- bool MustCloneSP =
- OldFunc->getParent() && OldFunc->getParent() == NewFunc->getParent();
- DISubprogram *SP = OldFunc->getSubprogram();
- if (SP) {
- assert(!MustCloneSP || ModuleLevelChanges);
- // Add mappings for some DebugInfo nodes that we don't want duplicated
- // even if they're distinct.
- auto &MD = VMap.MD();
- MD[SP->getUnit()].reset(SP->getUnit());
- MD[SP->getType()].reset(SP->getType());
- MD[SP->getFile()].reset(SP->getFile());
- // If we're not cloning into the same module, no need to clone the
- // subprogram
- if (!MustCloneSP)
- MD[SP].reset(SP);
- }
-
// 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, 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 information about dbg.value and dbg.declare to avoid
- // duplicating the types.
- DebugInfoFinder DIFinder;
+ // 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
+ // information about dbg.value and dbg.declare to avoid duplicating the
+ // types.
+ Optional<DebugInfoFinder> DIFinder;
+
+ // Track the subprogram attachment that needs to be cloned to fine-tune the
+ // mapping within the same module.
+ DISubprogram *SPClonedWithinModule = nullptr;
+ if (Changes < CloneFunctionChangeType::DifferentModule) {
+ assert((NewFunc->getParent() == nullptr ||
+ NewFunc->getParent() == OldFunc->getParent()) &&
+ "Expected NewFunc to have the same parent, or no parent");
+
+ // Need to find subprograms, types, and compile units.
+ DIFinder.emplace();
+
+ SPClonedWithinModule = OldFunc->getSubprogram();
+ if (SPClonedWithinModule)
+ DIFinder->processSubprogram(SPClonedWithinModule);
+ } else {
+ assert((NewFunc->getParent() == nullptr ||
+ NewFunc->getParent() != OldFunc->getParent()) &&
+ "Expected NewFunc to have different parents, or no parent");
+
+ if (Changes == CloneFunctionChangeType::DifferentModule) {
+ assert(NewFunc->getParent() &&
+ "Need parent of new function to maintain debug info invariants");
+
+ // Need to find all the compile units.
+ DIFinder.emplace();
+ }
+ }
// 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,
- ModuleLevelChanges ? &DIFinder : nullptr);
+ DIFinder ? &*DIFinder : nullptr);
// Add basic block mapping.
VMap[&BB] = CBB;
@@ -173,8 +183,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
// implementation, which generates an invalid blockaddress when
// cloning a function.)
if (BB.hasAddressTaken()) {
- Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
- const_cast<BasicBlock*>(&BB));
+ Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
+ const_cast<BasicBlock *>(&BB));
VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
}
@@ -183,54 +193,83 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
Returns.push_back(RI);
}
- for (DISubprogram *ISP : DIFinder.subprograms())
- if (ISP != SP)
- VMap.MD()[ISP].reset(ISP);
-
- for (DICompileUnit *CU : DIFinder.compile_units())
- VMap.MD()[CU].reset(CU);
-
- for (DIType *Type : DIFinder.types())
- VMap.MD()[Type].reset(Type);
+ if (Changes < CloneFunctionChangeType::DifferentModule &&
+ DIFinder->subprogram_count() > 0) {
+ // Turn on module-level changes, since we need to clone (some of) the
+ // debug info metadata.
+ //
+ // FIXME: Metadata effectively owned by a function should be made
+ // local, and only that local metadata should be cloned.
+ ModuleLevelChanges = true;
+
+ auto mapToSelfIfNew = [&VMap](MDNode *N) {
+ // Avoid clobbering an existing mapping.
+ (void)VMap.MD().try_emplace(N, N);
+ };
+
+ // Avoid cloning types, compile units, and (other) subprograms.
+ for (DISubprogram *ISP : DIFinder->subprograms())
+ if (ISP != SPClonedWithinModule)
+ mapToSelfIfNew(ISP);
+
+ for (DICompileUnit *CU : DIFinder->compile_units())
+ mapToSelfIfNew(CU);
+
+ for (DIType *Type : DIFinder->types())
+ mapToSelfIfNew(Type);
+ } else {
+ assert(!SPClonedWithinModule &&
+ "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<std::pair<unsigned, MDNode *>, 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<BasicBlock>(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<BasicBlock>(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);
-
- // Register all DICompileUnits of the old parent module in the new parent module
- auto* OldModule = OldFunc->getParent();
- auto* NewModule = NewFunc->getParent();
- if (OldModule && NewModule && OldModule != NewModule && DIFinder.compile_unit_count()) {
- auto* NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
- // Avoid multiple insertions of the same DICompileUnit to NMD.
- SmallPtrSet<const void*, 8> Visited;
- for (auto* Operand : NMD->operands())
- Visited.insert(Operand);
- for (auto* Unit : DIFinder.compile_units())
- // VMap.MD()[Unit] == Unit
- if (Visited.insert(Unit).second)
- NMD->addOperand(Unit);
+ 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
+ // cloning a module, CloneModule() will handle creating the named metadata.
+ if (Changes != CloneFunctionChangeType::DifferentModule)
+ return;
+
+ // Update !llvm.dbg.cu with compile units added to the new module if this
+ // function is being cloned in isolation.
+ //
+ // FIXME: This is making global / module-level changes, which doesn't seem
+ // like the right encapsulation Consider dropping the requirement to update
+ // !llvm.dbg.cu (either obsoleting the node, or restricting it to
+ // non-discardable compile units) instead of discovering compile units by
+ // 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 *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
+ // Avoid multiple insertions of the same DICompileUnit to NMD.
+ SmallPtrSet<const void *, 8> Visited;
+ for (auto *Operand : NMD->operands())
+ Visited.insert(Operand);
+ for (auto *Unit : DIFinder->compile_units()) {
+ MDNode *MappedUnit =
+ MapMetadata(Unit, VMap, RF_None, TypeMapper, Materializer);
+ if (Visited.insert(MappedUnit).second)
+ NMD->addOperand(MappedUnit);
}
}
@@ -243,7 +282,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
///
Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
ClonedCodeInfo *CodeInfo) {
- std::vector<Type*> ArgTypes;
+ std::vector<Type *> 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
@@ -253,8 +292,9 @@ Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
ArgTypes.push_back(I.getType());
// Create a new function type...
- FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
- ArgTypes, F->getFunctionType()->isVarArg());
+ FunctionType *FTy =
+ FunctionType::get(F->getFunctionType()->getReturnType(), ArgTypes,
+ F->getFunctionType()->isVarArg());
// Create the new function...
Function *NewF = Function::Create(FTy, F->getLinkage(), F->getAddressSpace(),
@@ -262,61 +302,60 @@ Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
// Loop over the arguments, copying the names of the mapped arguments over...
Function::arg_iterator DestI = NewF->arg_begin();
- for (const Argument & I : F->args())
+ for (const Argument &I : F->args())
if (VMap.count(&I) == 0) { // Is this argument preserved?
DestI->setName(I.getName()); // Copy the name over...
VMap[&I] = &*DestI++; // Add mapping to VMap
}
- SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
- CloneFunctionInto(NewF, F, VMap, F->getSubprogram() != nullptr, Returns, "",
- CodeInfo);
+ SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
+ CloneFunctionInto(NewF, F, VMap, CloneFunctionChangeType::LocalChangesOnly,
+ Returns, "", CodeInfo);
return NewF;
}
-
-
namespace {
- /// This is a private class used to implement CloneAndPruneFunctionInto.
- struct PruningFunctionCloner {
- Function *NewFunc;
- const Function *OldFunc;
- ValueToValueMapTy &VMap;
- bool ModuleLevelChanges;
- const char *NameSuffix;
- ClonedCodeInfo *CodeInfo;
-
- public:
- PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
- ValueToValueMapTy &valueMap, bool moduleLevelChanges,
- const char *nameSuffix, ClonedCodeInfo *codeInfo)
- : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
- ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
- CodeInfo(codeInfo) {}
-
- /// The specified block is found to be reachable, clone it and
- /// anything that it can reach.
- void CloneBlock(const BasicBlock *BB,
- BasicBlock::const_iterator StartingInst,
- std::vector<const BasicBlock*> &ToClone);
- };
-}
+/// This is a private class used to implement CloneAndPruneFunctionInto.
+struct PruningFunctionCloner {
+ Function *NewFunc;
+ const Function *OldFunc;
+ ValueToValueMapTy &VMap;
+ bool ModuleLevelChanges;
+ const char *NameSuffix;
+ ClonedCodeInfo *CodeInfo;
+
+public:
+ PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
+ ValueToValueMapTy &valueMap, bool moduleLevelChanges,
+ const char *nameSuffix, ClonedCodeInfo *codeInfo)
+ : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
+ ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
+ CodeInfo(codeInfo) {}
+
+ /// The specified block is found to be reachable, clone it and
+ /// anything that it can reach.
+ void CloneBlock(const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
+ std::vector<const BasicBlock *> &ToClone);
+};
+} // namespace
/// The specified block is found to be reachable, clone it and
/// anything that it can reach.
-void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
- BasicBlock::const_iterator StartingInst,
- std::vector<const BasicBlock*> &ToClone){
+void PruningFunctionCloner::CloneBlock(
+ const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
+ std::vector<const BasicBlock *> &ToClone) {
WeakTrackingVH &BBEntry = VMap[BB];
// Have we already cloned this block?
- if (BBEntry) return;
+ if (BBEntry)
+ return;
// Nope, clone it now.
BasicBlock *NewBB;
BBEntry = NewBB = BasicBlock::Create(BB->getContext());
- if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
+ if (BB->hasName())
+ NewBB->setName(BB->getName() + NameSuffix);
// It is only legal to clone a function if a block address within that
// function is never referenced outside of the function. Given that, we
@@ -328,8 +367,8 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
// Note that we don't need to fix the mapping for unreachable blocks;
// the default mapping there is safe.
if (BB->hasAddressTaken()) {
- Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
- const_cast<BasicBlock*>(BB));
+ Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
+ const_cast<BasicBlock *>(BB));
VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
}
@@ -337,8 +376,8 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
// Loop over all instructions, and copy them over, DCE'ing as we go. This
// loop doesn't include the terminator.
- for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
- II != IE; ++II) {
+ for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); II != IE;
+ ++II) {
Instruction *NewInst = II->clone();
@@ -368,15 +407,17 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
}
if (II->hasName())
- NewInst->setName(II->getName()+NameSuffix);
+ NewInst->setName(II->getName() + NameSuffix);
VMap[&*II] = NewInst; // Add instruction map to value.
NewBB->getInstList().push_back(NewInst);
hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
- if (CodeInfo)
+ if (CodeInfo) {
+ CodeInfo->OrigVMap[&*II] = NewInst;
if (auto *CB = dyn_cast<CallBase>(&*II))
if (CB->hasOperandBundles())
CodeInfo->OperandBundleCallSites.push_back(NewInst);
+ }
if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
if (isa<ConstantInt>(AI->getArraySize()))
@@ -414,9 +455,9 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
Value *V = VMap.lookup(SI->getCondition());
Cond = dyn_cast_or_null<ConstantInt>(V);
}
- if (Cond) { // Constant fold to uncond branch!
+ if (Cond) { // Constant fold to uncond branch!
SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
- BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
+ BasicBlock *Dest = const_cast<BasicBlock *>(Case.getCaseSuccessor());
VMap[OldTI] = BranchInst::Create(Dest, NewBB);
ToClone.push_back(Dest);
TerminatorDone = true;
@@ -426,24 +467,26 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
if (!TerminatorDone) {
Instruction *NewInst = OldTI->clone();
if (OldTI->hasName())
- NewInst->setName(OldTI->getName()+NameSuffix);
+ NewInst->setName(OldTI->getName() + NameSuffix);
NewBB->getInstList().push_back(NewInst);
- VMap[OldTI] = NewInst; // Add instruction map to value.
+ VMap[OldTI] = NewInst; // Add instruction map to value.
- if (CodeInfo)
+ if (CodeInfo) {
+ CodeInfo->OrigVMap[OldTI] = NewInst;
if (auto *CB = dyn_cast<CallBase>(OldTI))
if (CB->hasOperandBundles())
CodeInfo->OperandBundleCallSites.push_back(NewInst);
+ }
// Recursively clone any reachable successor blocks.
append_range(ToClone, successors(BB->getTerminator()));
}
if (CodeInfo) {
- CodeInfo->ContainsCalls |= hasCalls;
+ CodeInfo->ContainsCalls |= hasCalls;
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
- CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
- BB != &BB->getParent()->front();
+ CodeInfo->ContainsDynamicAllocas |=
+ hasStaticAllocas && BB != &BB->getParent()->front();
}
}
@@ -481,7 +524,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
}
// Clone the entry block, and anything recursively reachable from it.
- std::vector<const BasicBlock*> CloneWorklist;
+ std::vector<const BasicBlock *> CloneWorklist;
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
while (!CloneWorklist.empty()) {
const BasicBlock *BB = CloneWorklist.back();
@@ -494,11 +537,12 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
// insert it into the new function in the right order. If not, ignore it.
//
// Defer PHI resolution until rest of function is resolved.
- SmallVector<const PHINode*, 16> PHIToResolve;
+ SmallVector<const PHINode *, 16> PHIToResolve;
for (const BasicBlock &BI : *OldFunc) {
Value *V = VMap.lookup(&BI);
BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
- if (!NewBB) continue; // Dead block.
+ if (!NewBB)
+ continue; // Dead block.
// Add the new block to the new function.
NewFunc->getBasicBlockList().push_back(NewBB);
@@ -523,7 +567,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
// Defer PHI resolution until rest of function is resolved, PHI resolution
// requires the CFG to be up-to-date.
- for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
+ for (unsigned phino = 0, e = PHIToResolve.size(); phino != e;) {
const PHINode *OPN = PHIToResolve[phino];
unsigned NumPreds = OPN->getNumIncomingValues();
const BasicBlock *OldBB = OPN->getParent();
@@ -532,21 +576,22 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
// Map operands for blocks that are live and remove operands for blocks
// that are dead.
for (; phino != PHIToResolve.size() &&
- PHIToResolve[phino]->getParent() == OldBB; ++phino) {
+ PHIToResolve[phino]->getParent() == OldBB;
+ ++phino) {
OPN = PHIToResolve[phino];
PHINode *PN = cast<PHINode>(VMap[OPN]);
for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
Value *V = VMap.lookup(PN->getIncomingBlock(pred));
if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
- Value *InVal = MapValue(PN->getIncomingValue(pred),
- VMap,
- ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
+ Value *InVal =
+ MapValue(PN->getIncomingValue(pred), VMap,
+ ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
assert(InVal && "Unknown input value?");
PN->setIncomingValue(pred, InVal);
PN->setIncomingBlock(pred, MappedBlock);
} else {
PN->removeIncomingValue(pred, false);
- --pred; // Revisit the next entry.
+ --pred; // Revisit the next entry.
--e;
}
}
@@ -562,10 +607,9 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
if (NumPreds != PN->getNumIncomingValues()) {
assert(NumPreds < PN->getNumIncomingValues());
// Count how many times each predecessor comes to this block.
- std::map<BasicBlock*, unsigned> PredCount;
- for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
- PI != E; ++PI)
- --PredCount[*PI];
+ std::map<BasicBlock *, unsigned> PredCount;
+ for (BasicBlock *Pred : predecessors(NewBB))
+ --PredCount[Pred];
// Figure out how many entries to remove from each PHI.
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
@@ -683,11 +727,15 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
}
BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
- if (!BI || BI->isConditional()) { ++I; continue; }
+ if (!BI || BI->isConditional()) {
+ ++I;
+ continue;
+ }
BasicBlock *Dest = BI->getSuccessor(0);
if (!Dest->getSinglePredecessor()) {
- ++I; continue;
+ ++I;
+ continue;
}
// We shouldn't be able to get single-entry PHI nodes here, as instsimplify
@@ -720,7 +768,6 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
Returns.push_back(RI);
}
-
/// This works exactly like CloneFunctionInto,
/// except that it does some simple constant prop and DCE on the fly. The
/// effect of this is to copy significantly less code in cases where (for
@@ -728,13 +775,10 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
/// constant arguments cause a significant amount of code in the callee to be
/// dead. Since this doesn't produce an exact copy of the input, it can't be
/// used for things like CloneFunction or CloneModule.
-void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
- ValueToValueMapTy &VMap,
- bool ModuleLevelChanges,
- SmallVectorImpl<ReturnInst*> &Returns,
- const char *NameSuffix,
- ClonedCodeInfo *CodeInfo,
- Instruction *TheCall) {
+void llvm::CloneAndPruneFunctionInto(
+ Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap,
+ bool ModuleLevelChanges, SmallVectorImpl<ReturnInst *> &Returns,
+ const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
}
@@ -885,10 +929,9 @@ BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
return NewBB;
}
-void llvm::cloneNoAliasScopes(
- ArrayRef<MDNode *> NoAliasDeclScopes,
- DenseMap<MDNode *, MDNode *> &ClonedScopes,
- StringRef Ext, LLVMContext &Context) {
+void llvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
+ DenseMap<MDNode *, MDNode *> &ClonedScopes,
+ StringRef Ext, LLVMContext &Context) {
MDBuilder MDB(Context);
for (auto *ScopeList : NoAliasDeclScopes) {
@@ -911,9 +954,9 @@ void llvm::cloneNoAliasScopes(
}
}
-void llvm::adaptNoAliasScopes(
- Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
- LLVMContext &Context) {
+void llvm::adaptNoAliasScopes(Instruction *I,
+ const DenseMap<MDNode *, MDNode *> &ClonedScopes,
+ LLVMContext &Context) {
auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
bool NeedsReplacement = false;
SmallVector<Metadata *, 8> NewScopeList;
@@ -945,9 +988,9 @@ void llvm::adaptNoAliasScopes(
replaceWhenNeeded(LLVMContext::MD_alias_scope);
}
-void llvm::cloneAndAdaptNoAliasScopes(
- ArrayRef<MDNode *> NoAliasDeclScopes,
- ArrayRef<BasicBlock *> NewBlocks, LLVMContext &Context, StringRef Ext) {
+void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
+ ArrayRef<BasicBlock *> NewBlocks,
+ LLVMContext &Context, StringRef Ext) {
if (NoAliasDeclScopes.empty())
return;
@@ -962,9 +1005,9 @@ void llvm::cloneAndAdaptNoAliasScopes(
adaptNoAliasScopes(&I, ClonedScopes, Context);
}
-void llvm::cloneAndAdaptNoAliasScopes(
- ArrayRef<MDNode *> NoAliasDeclScopes, Instruction *IStart,
- Instruction *IEnd, LLVMContext &Context, StringRef Ext) {
+void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
+ Instruction *IStart, Instruction *IEnd,
+ LLVMContext &Context, StringRef Ext) {
if (NoAliasDeclScopes.empty())
return;