}
if (CodeInfo) {
- CodeInfo->ContainsCalls |= hasCalls;
+ CodeInfo->ContainsCalls |= hasCalls;
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
}
return NewBB;
// 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);
}
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)
// 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<const void *, 8> Visited;
///
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
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(),
// 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.
+ 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
// 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);
}
// 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();
}
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));
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;
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 (auto *CB = dyn_cast<CallBase>(OldTI))
}
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();
}
}
}
// 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();
// 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);
// 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();
// 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;
}
}
if (NumPreds != PN->getNumIncomingValues()) {
assert(NumPreds < PN->getNumIncomingValues());
// Count how many times each predecessor comes to this block.
- std::map<BasicBlock*, unsigned> PredCount;
+ std::map<BasicBlock *, unsigned> PredCount;
for (BasicBlock *Pred : predecessors(NewBB))
--PredCount[Pred];
}
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
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
/// 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, Instruction *TheCall) {
CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
}
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) {
}
}
-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;
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;
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;