Skip to content

Commit 520f70e

Browse files
committed
[NFC] clang-format llvm/lib/Transforms/Utils/CloneFunction.cpp
Differential Revision: https://reviews.llvm.org/D98957
1 parent ab44ec1 commit 520f70e

File tree

1 file changed

+91
-90
lines changed

1 file changed

+91
-90
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
7272
}
7373

7474
if (CodeInfo) {
75-
CodeInfo->ContainsCalls |= hasCalls;
75+
CodeInfo->ContainsCalls |= hasCalls;
7676
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
7777
}
7878
return NewBB;
@@ -184,8 +184,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
184184
// implementation, which generates an invalid blockaddress when
185185
// cloning a function.)
186186
if (BB.hasAddressTaken()) {
187-
Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
188-
const_cast<BasicBlock*>(&BB));
187+
Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
188+
const_cast<BasicBlock *>(&BB));
189189
VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
190190
}
191191

@@ -234,11 +234,11 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
234234
TypeMapper, Materializer));
235235
}
236236

237-
// Loop over all of the instructions in the function, fixing up operand
238-
// references as we go. This uses VMap to do all the hard work.
239-
for (Function::iterator BB =
240-
cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
241-
BE = NewFunc->end();
237+
// Loop over all of the instructions in the new function, fixing up operand
238+
// references as we go. This uses VMap to do all the hard work.
239+
for (Function::iterator
240+
BB = cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
241+
BE = NewFunc->end();
242242
BB != BE; ++BB)
243243
// Loop over all instructions, fixing each one as we find it...
244244
for (Instruction &II : *BB)
@@ -260,7 +260,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
260260
// visiting the metadata attached to global values, which would allow this
261261
// code to be deleted. Alternatively, perhaps give responsibility for this
262262
// update to CloneFunctionInto's callers.
263-
auto* NewModule = NewFunc->getParent();
263+
auto *NewModule = NewFunc->getParent();
264264
auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
265265
// Avoid multiple insertions of the same DICompileUnit to NMD.
266266
SmallPtrSet<const void *, 8> Visited;
@@ -283,7 +283,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
283283
///
284284
Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
285285
ClonedCodeInfo *CodeInfo) {
286-
std::vector<Type*> ArgTypes;
286+
std::vector<Type *> ArgTypes;
287287

288288
// The user might be deleting arguments to the function by specifying them in
289289
// the VMap. If so, we need to not add the arguments to the arg ty vector
@@ -293,70 +293,70 @@ Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
293293
ArgTypes.push_back(I.getType());
294294

295295
// Create a new function type...
296-
FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
297-
ArgTypes, F->getFunctionType()->isVarArg());
296+
FunctionType *FTy =
297+
FunctionType::get(F->getFunctionType()->getReturnType(), ArgTypes,
298+
F->getFunctionType()->isVarArg());
298299

299300
// Create the new function...
300301
Function *NewF = Function::Create(FTy, F->getLinkage(), F->getAddressSpace(),
301302
F->getName(), F->getParent());
302303

303304
// Loop over the arguments, copying the names of the mapped arguments over...
304305
Function::arg_iterator DestI = NewF->arg_begin();
305-
for (const Argument & I : F->args())
306+
for (const Argument &I : F->args())
306307
if (VMap.count(&I) == 0) { // Is this argument preserved?
307308
DestI->setName(I.getName()); // Copy the name over...
308309
VMap[&I] = &*DestI++; // Add mapping to VMap
309310
}
310311

311-
SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
312+
SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
312313
CloneFunctionInto(NewF, F, VMap, CloneFunctionChangeType::LocalChangesOnly,
313314
Returns, "", CodeInfo);
314315

315316
return NewF;
316317
}
317318

318-
319-
320319
namespace {
321-
/// This is a private class used to implement CloneAndPruneFunctionInto.
322-
struct PruningFunctionCloner {
323-
Function *NewFunc;
324-
const Function *OldFunc;
325-
ValueToValueMapTy &VMap;
326-
bool ModuleLevelChanges;
327-
const char *NameSuffix;
328-
ClonedCodeInfo *CodeInfo;
329-
330-
public:
331-
PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
332-
ValueToValueMapTy &valueMap, bool moduleLevelChanges,
333-
const char *nameSuffix, ClonedCodeInfo *codeInfo)
334-
: NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
335-
ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
336-
CodeInfo(codeInfo) {}
337-
338-
/// The specified block is found to be reachable, clone it and
339-
/// anything that it can reach.
340-
void CloneBlock(const BasicBlock *BB,
341-
BasicBlock::const_iterator StartingInst,
342-
std::vector<const BasicBlock*> &ToClone);
343-
};
344-
}
320+
/// This is a private class used to implement CloneAndPruneFunctionInto.
321+
struct PruningFunctionCloner {
322+
Function *NewFunc;
323+
const Function *OldFunc;
324+
ValueToValueMapTy &VMap;
325+
bool ModuleLevelChanges;
326+
const char *NameSuffix;
327+
ClonedCodeInfo *CodeInfo;
328+
329+
public:
330+
PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
331+
ValueToValueMapTy &valueMap, bool moduleLevelChanges,
332+
const char *nameSuffix, ClonedCodeInfo *codeInfo)
333+
: NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
334+
ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
335+
CodeInfo(codeInfo) {}
336+
337+
/// The specified block is found to be reachable, clone it and
338+
/// anything that it can reach.
339+
void CloneBlock(const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
340+
std::vector<const BasicBlock *> &ToClone);
341+
};
342+
} // namespace
345343

346344
/// The specified block is found to be reachable, clone it and
347345
/// anything that it can reach.
348-
void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
349-
BasicBlock::const_iterator StartingInst,
350-
std::vector<const BasicBlock*> &ToClone){
346+
void PruningFunctionCloner::CloneBlock(
347+
const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
348+
std::vector<const BasicBlock *> &ToClone) {
351349
WeakTrackingVH &BBEntry = VMap[BB];
352350

353351
// Have we already cloned this block?
354-
if (BBEntry) return;
352+
if (BBEntry)
353+
return;
355354

356355
// Nope, clone it now.
357356
BasicBlock *NewBB;
358357
BBEntry = NewBB = BasicBlock::Create(BB->getContext());
359-
if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
358+
if (BB->hasName())
359+
NewBB->setName(BB->getName() + NameSuffix);
360360

361361
// It is only legal to clone a function if a block address within that
362362
// function is never referenced outside of the function. Given that, we
@@ -368,17 +368,17 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
368368
// Note that we don't need to fix the mapping for unreachable blocks;
369369
// the default mapping there is safe.
370370
if (BB->hasAddressTaken()) {
371-
Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
372-
const_cast<BasicBlock*>(BB));
371+
Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
372+
const_cast<BasicBlock *>(BB));
373373
VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
374374
}
375375

376376
bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
377377

378378
// Loop over all instructions, and copy them over, DCE'ing as we go. This
379379
// loop doesn't include the terminator.
380-
for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
381-
II != IE; ++II) {
380+
for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); II != IE;
381+
++II) {
382382

383383
Instruction *NewInst = II->clone();
384384

@@ -408,7 +408,7 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
408408
}
409409

410410
if (II->hasName())
411-
NewInst->setName(II->getName()+NameSuffix);
411+
NewInst->setName(II->getName() + NameSuffix);
412412
VMap[&*II] = NewInst; // Add instruction map to value.
413413
NewBB->getInstList().push_back(NewInst);
414414
hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
@@ -454,9 +454,9 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
454454
Value *V = VMap.lookup(SI->getCondition());
455455
Cond = dyn_cast_or_null<ConstantInt>(V);
456456
}
457-
if (Cond) { // Constant fold to uncond branch!
457+
if (Cond) { // Constant fold to uncond branch!
458458
SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
459-
BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
459+
BasicBlock *Dest = const_cast<BasicBlock *>(Case.getCaseSuccessor());
460460
VMap[OldTI] = BranchInst::Create(Dest, NewBB);
461461
ToClone.push_back(Dest);
462462
TerminatorDone = true;
@@ -466,9 +466,9 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
466466
if (!TerminatorDone) {
467467
Instruction *NewInst = OldTI->clone();
468468
if (OldTI->hasName())
469-
NewInst->setName(OldTI->getName()+NameSuffix);
469+
NewInst->setName(OldTI->getName() + NameSuffix);
470470
NewBB->getInstList().push_back(NewInst);
471-
VMap[OldTI] = NewInst; // Add instruction map to value.
471+
VMap[OldTI] = NewInst; // Add instruction map to value.
472472

473473
if (CodeInfo)
474474
if (auto *CB = dyn_cast<CallBase>(OldTI))
@@ -480,10 +480,10 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
480480
}
481481

482482
if (CodeInfo) {
483-
CodeInfo->ContainsCalls |= hasCalls;
483+
CodeInfo->ContainsCalls |= hasCalls;
484484
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
485-
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
486-
BB != &BB->getParent()->front();
485+
CodeInfo->ContainsDynamicAllocas |=
486+
hasStaticAllocas && BB != &BB->getParent()->front();
487487
}
488488
}
489489

@@ -521,7 +521,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
521521
}
522522

523523
// Clone the entry block, and anything recursively reachable from it.
524-
std::vector<const BasicBlock*> CloneWorklist;
524+
std::vector<const BasicBlock *> CloneWorklist;
525525
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
526526
while (!CloneWorklist.empty()) {
527527
const BasicBlock *BB = CloneWorklist.back();
@@ -534,11 +534,12 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
534534
// insert it into the new function in the right order. If not, ignore it.
535535
//
536536
// Defer PHI resolution until rest of function is resolved.
537-
SmallVector<const PHINode*, 16> PHIToResolve;
537+
SmallVector<const PHINode *, 16> PHIToResolve;
538538
for (const BasicBlock &BI : *OldFunc) {
539539
Value *V = VMap.lookup(&BI);
540540
BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
541-
if (!NewBB) continue; // Dead block.
541+
if (!NewBB)
542+
continue; // Dead block.
542543

543544
// Add the new block to the new function.
544545
NewFunc->getBasicBlockList().push_back(NewBB);
@@ -563,7 +564,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
563564

564565
// Defer PHI resolution until rest of function is resolved, PHI resolution
565566
// requires the CFG to be up-to-date.
566-
for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
567+
for (unsigned phino = 0, e = PHIToResolve.size(); phino != e;) {
567568
const PHINode *OPN = PHIToResolve[phino];
568569
unsigned NumPreds = OPN->getNumIncomingValues();
569570
const BasicBlock *OldBB = OPN->getParent();
@@ -572,21 +573,22 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
572573
// Map operands for blocks that are live and remove operands for blocks
573574
// that are dead.
574575
for (; phino != PHIToResolve.size() &&
575-
PHIToResolve[phino]->getParent() == OldBB; ++phino) {
576+
PHIToResolve[phino]->getParent() == OldBB;
577+
++phino) {
576578
OPN = PHIToResolve[phino];
577579
PHINode *PN = cast<PHINode>(VMap[OPN]);
578580
for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
579581
Value *V = VMap.lookup(PN->getIncomingBlock(pred));
580582
if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
581-
Value *InVal = MapValue(PN->getIncomingValue(pred),
582-
VMap,
583-
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
583+
Value *InVal =
584+
MapValue(PN->getIncomingValue(pred), VMap,
585+
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
584586
assert(InVal && "Unknown input value?");
585587
PN->setIncomingValue(pred, InVal);
586588
PN->setIncomingBlock(pred, MappedBlock);
587589
} else {
588590
PN->removeIncomingValue(pred, false);
589-
--pred; // Revisit the next entry.
591+
--pred; // Revisit the next entry.
590592
--e;
591593
}
592594
}
@@ -602,7 +604,7 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
602604
if (NumPreds != PN->getNumIncomingValues()) {
603605
assert(NumPreds < PN->getNumIncomingValues());
604606
// Count how many times each predecessor comes to this block.
605-
std::map<BasicBlock*, unsigned> PredCount;
607+
std::map<BasicBlock *, unsigned> PredCount;
606608
for (BasicBlock *Pred : predecessors(NewBB))
607609
--PredCount[Pred];
608610

@@ -722,11 +724,15 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
722724
}
723725

724726
BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
725-
if (!BI || BI->isConditional()) { ++I; continue; }
727+
if (!BI || BI->isConditional()) {
728+
++I;
729+
continue;
730+
}
726731

727732
BasicBlock *Dest = BI->getSuccessor(0);
728733
if (!Dest->getSinglePredecessor()) {
729-
++I; continue;
734+
++I;
735+
continue;
730736
}
731737

732738
// We shouldn't be able to get single-entry PHI nodes here, as instsimplify
@@ -759,21 +765,17 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
759765
Returns.push_back(RI);
760766
}
761767

762-
763768
/// This works exactly like CloneFunctionInto,
764769
/// except that it does some simple constant prop and DCE on the fly. The
765770
/// effect of this is to copy significantly less code in cases where (for
766771
/// example) a function call with constant arguments is inlined, and those
767772
/// constant arguments cause a significant amount of code in the callee to be
768773
/// dead. Since this doesn't produce an exact copy of the input, it can't be
769774
/// used for things like CloneFunction or CloneModule.
770-
void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
771-
ValueToValueMapTy &VMap,
772-
bool ModuleLevelChanges,
773-
SmallVectorImpl<ReturnInst*> &Returns,
774-
const char *NameSuffix,
775-
ClonedCodeInfo *CodeInfo,
776-
Instruction *TheCall) {
775+
void llvm::CloneAndPruneFunctionInto(
776+
Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap,
777+
bool ModuleLevelChanges, SmallVectorImpl<ReturnInst *> &Returns,
778+
const char *NameSuffix, ClonedCodeInfo *CodeInfo, Instruction *TheCall) {
777779
CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
778780
ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
779781
}
@@ -924,10 +926,9 @@ BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
924926
return NewBB;
925927
}
926928

927-
void llvm::cloneNoAliasScopes(
928-
ArrayRef<MDNode *> NoAliasDeclScopes,
929-
DenseMap<MDNode *, MDNode *> &ClonedScopes,
930-
StringRef Ext, LLVMContext &Context) {
929+
void llvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
930+
DenseMap<MDNode *, MDNode *> &ClonedScopes,
931+
StringRef Ext, LLVMContext &Context) {
931932
MDBuilder MDB(Context);
932933

933934
for (auto *ScopeList : NoAliasDeclScopes) {
@@ -950,9 +951,9 @@ void llvm::cloneNoAliasScopes(
950951
}
951952
}
952953

953-
void llvm::adaptNoAliasScopes(
954-
Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
955-
LLVMContext &Context) {
954+
void llvm::adaptNoAliasScopes(Instruction *I,
955+
const DenseMap<MDNode *, MDNode *> &ClonedScopes,
956+
LLVMContext &Context) {
956957
auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
957958
bool NeedsReplacement = false;
958959
SmallVector<Metadata *, 8> NewScopeList;
@@ -984,9 +985,9 @@ void llvm::adaptNoAliasScopes(
984985
replaceWhenNeeded(LLVMContext::MD_alias_scope);
985986
}
986987

987-
void llvm::cloneAndAdaptNoAliasScopes(
988-
ArrayRef<MDNode *> NoAliasDeclScopes,
989-
ArrayRef<BasicBlock *> NewBlocks, LLVMContext &Context, StringRef Ext) {
988+
void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
989+
ArrayRef<BasicBlock *> NewBlocks,
990+
LLVMContext &Context, StringRef Ext) {
990991
if (NoAliasDeclScopes.empty())
991992
return;
992993

@@ -1001,9 +1002,9 @@ void llvm::cloneAndAdaptNoAliasScopes(
10011002
adaptNoAliasScopes(&I, ClonedScopes, Context);
10021003
}
10031004

1004-
void llvm::cloneAndAdaptNoAliasScopes(
1005-
ArrayRef<MDNode *> NoAliasDeclScopes, Instruction *IStart,
1006-
Instruction *IEnd, LLVMContext &Context, StringRef Ext) {
1005+
void llvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
1006+
Instruction *IStart, Instruction *IEnd,
1007+
LLVMContext &Context, StringRef Ext) {
10071008
if (NoAliasDeclScopes.empty())
10081009
return;
10091010

0 commit comments

Comments
 (0)