From 9a884543f167717cb46cf4e6e92d9e0ef521bddd Mon Sep 17 00:00:00 2001 From: Amir Ayupov Date: Fri, 7 May 2021 18:43:25 -0700 Subject: [PATCH] [BOLT][NFC] Avoid unnecessary copies with push_back Summary: Small refactoring inspired by clang-tidy modernize-use-emplace (cherry picked from FBD28307493) --- bolt/src/BinaryContext.cpp | 5 ++--- bolt/src/BinaryEmitter.cpp | 2 +- bolt/src/BinaryFunction.h | 2 +- bolt/src/BoltAddressTranslation.cpp | 2 +- bolt/src/CacheMetrics.cpp | 2 +- bolt/src/DataAggregator.cpp | 2 +- bolt/src/DebugData.cpp | 6 +++--- bolt/src/Passes/BinaryFunctionCallGraph.cpp | 4 ++-- bolt/src/Passes/BinaryPasses.cpp | 6 +++--- bolt/src/Passes/ExtTSPReorderAlgorithm.cpp | 10 ++++------ bolt/src/Passes/FrameOptimizer.cpp | 2 +- bolt/src/Passes/HFSortPlus.cpp | 4 +--- bolt/src/Passes/IndirectCallPromotion.cpp | 6 +++--- bolt/src/Passes/Instrumentation.cpp | 2 +- bolt/src/Passes/LongJmp.cpp | 10 +++++----- bolt/src/Passes/ReorderData.cpp | 2 +- bolt/src/RewriteInstance.cpp | 4 ++-- bolt/src/Target/X86/X86MCPlusBuilder.cpp | 14 +++++++------- 18 files changed, 40 insertions(+), 45 deletions(-) diff --git a/bolt/src/BinaryContext.cpp b/bolt/src/BinaryContext.cpp index 66cab26d263b..8fd34fb107eb 100644 --- a/bolt/src/BinaryContext.cpp +++ b/bolt/src/BinaryContext.cpp @@ -1326,15 +1326,14 @@ void BinaryContext::fixBinaryDataHoles() { while (Itr != End) { if (Itr->second->getAddress() > EndAddress) { uint64_t Gap = Itr->second->getAddress() - EndAddress; - Holes.push_back(std::make_pair(EndAddress, Gap)); + Holes.emplace_back(EndAddress, Gap); } EndAddress = Itr->second->getEndAddress(); ++Itr; } if (EndAddress < Section.getEndAddress()) { - Holes.push_back(std::make_pair(EndAddress, - Section.getEndAddress() - EndAddress)); + Holes.emplace_back(EndAddress, Section.getEndAddress() - EndAddress); } // If there is already a symbol at the start of the hole, grow that symbol diff --git a/bolt/src/BinaryEmitter.cpp b/bolt/src/BinaryEmitter.cpp index c07bb035e5dd..4538842962e4 100644 --- a/bolt/src/BinaryEmitter.cpp +++ b/bolt/src/BinaryEmitter.cpp @@ -461,7 +461,7 @@ void BinaryEmitter::emitFunctionBody(BinaryFunction &BF, bool EmitColdPart, const auto Offset = BC.MIB->getAnnotationAs(Instr, "Offset"); MCSymbol *LocSym = BC.Ctx->createTempSymbol(); Streamer.emitLabel(LocSym); - BB->getLocSyms().emplace_back(std::make_pair(Offset, LocSym)); + BB->getLocSyms().emplace_back(Offset, LocSym); } Streamer.emitInstruction(Instr, *BC.STI); diff --git a/bolt/src/BinaryFunction.h b/bolt/src/BinaryFunction.h index 73943be59d58..fbe87c99b640 100644 --- a/bolt/src/BinaryFunction.h +++ b/bolt/src/BinaryFunction.h @@ -1610,7 +1610,7 @@ public: BB->setIndex(BasicBlocks.size() - 1); if (CurrentState == State::Disassembled) { - BasicBlockOffsets.emplace_back(std::make_pair(Offset, BB)); + BasicBlockOffsets.emplace_back(Offset, BB); } else if (CurrentState == State::CFG) { BB->setLayoutIndex(layout_size()); BasicBlocksLayout.emplace_back(BB); diff --git a/bolt/src/BoltAddressTranslation.cpp b/bolt/src/BoltAddressTranslation.cpp index 74e1de1117ac..698a414f1d31 100644 --- a/bolt/src/BoltAddressTranslation.cpp +++ b/bolt/src/BoltAddressTranslation.cpp @@ -266,7 +266,7 @@ BoltAddressTranslation::getFallthroughsInTrace( } if (Iter->second & BRANCHENTRY) break; - Res.emplace_back(std::make_pair(Src, Iter->first)); + Res.emplace_back(Src, Iter->first); } return Res; diff --git a/bolt/src/CacheMetrics.cpp b/bolt/src/CacheMetrics.cpp index 362823d0623a..3bb1cf520085 100644 --- a/bolt/src/CacheMetrics.cpp +++ b/bolt/src/CacheMetrics.cpp @@ -138,7 +138,7 @@ extractFunctionCalls(const std::vector &BinaryFunctions) { continue; // Record the call - Calls[DstFunction].push_back(std::make_pair(SrcFunction, Count)); + Calls[DstFunction].emplace_back(SrcFunction, Count); } } } diff --git a/bolt/src/DataAggregator.cpp b/bolt/src/DataAggregator.cpp index b5fb1e24d83a..2fd386942a12 100644 --- a/bolt/src/DataAggregator.cpp +++ b/bolt/src/DataAggregator.cpp @@ -969,7 +969,7 @@ bool DataAggregator::recordTrace( } else { Offset = BB->getOffset(); } - Branches->emplace_back(std::make_pair(Offset, NextBB->getOffset())); + Branches->emplace_back(Offset, NextBB->getOffset()); } BB = NextBB; diff --git a/bolt/src/DebugData.cpp b/bolt/src/DebugData.cpp index 6791ee007305..00a2f1d02972 100644 --- a/bolt/src/DebugData.cpp +++ b/bolt/src/DebugData.cpp @@ -175,11 +175,11 @@ DebugLocWriter::addList(const DebugLocationsVector &LocList) { void SimpleBinaryPatcher::addBinaryPatch(uint32_t Offset, const std::string &NewValue) { - Patches.emplace_back(std::make_pair(Offset, NewValue)); + Patches.emplace_back(Offset, NewValue); } void SimpleBinaryPatcher::addBytePatch(uint32_t Offset, uint8_t Value) { - Patches.emplace_back(std::make_pair(Offset, std::string(1, Value))); + Patches.emplace_back(Offset, std::string(1, Value)); } void SimpleBinaryPatcher::addLEPatch(uint32_t Offset, uint64_t NewValue, @@ -189,7 +189,7 @@ void SimpleBinaryPatcher::addLEPatch(uint32_t Offset, uint64_t NewValue, LE64[I] = NewValue & 0xff; NewValue >>= 8; } - Patches.emplace_back(std::make_pair(Offset, LE64)); + Patches.emplace_back(Offset, LE64); } void SimpleBinaryPatcher::addUDataPatch(uint32_t Offset, uint64_t Value, uint64_t Size) { diff --git a/bolt/src/Passes/BinaryFunctionCallGraph.cpp b/bolt/src/Passes/BinaryFunctionCallGraph.cpp index a613c94a0336..ddf15e70526f 100644 --- a/bolt/src/Passes/BinaryFunctionCallGraph.cpp +++ b/bolt/src/Passes/BinaryFunctionCallGraph.cpp @@ -184,11 +184,11 @@ BinaryFunctionCallGraph buildCallGraph(BinaryContext &BC, BC.MIB->getAnnotationAs(Inst, "CallProfile"); for (const IndirectCallProfile &CSI : ICSP) { if (CSI.Symbol) - Counts.push_back(std::make_pair(CSI.Symbol, CSI.Count)); + Counts.emplace_back(CSI.Symbol, CSI.Count); } } else { const uint64_t Count = BB->getExecutionCount(); - Counts.push_back(std::make_pair(DstSym, Count)); + Counts.emplace_back(DstSym, Count); } return Counts; diff --git a/bolt/src/Passes/BinaryPasses.cpp b/bolt/src/Passes/BinaryPasses.cpp index 8e9d2ac8c686..cc10d6483792 100644 --- a/bolt/src/Passes/BinaryPasses.cpp +++ b/bolt/src/Passes/BinaryPasses.cpp @@ -549,8 +549,8 @@ void LowerAnnotations::runOnFunctions(BinaryContext &BC) { for (auto II = BB->begin(); II != BB->end(); ++II) { if (BF.requiresAddressTranslation() && BC.MIB->hasAnnotation(*II, "Offset")) { - PreservedOffsetAnnotations.push_back(std::make_pair( - &(*II), BC.MIB->getAnnotationAs(*II, "Offset"))); + PreservedOffsetAnnotations.emplace_back( + &(*II), BC.MIB->getAnnotationAs(*II, "Offset")); } BC.MIB->stripAnnotations(*II); } @@ -823,7 +823,7 @@ uint64_t SimplifyConditionalTailCalls::fixTailCalls(BinaryContext &BC, // the target for the unconditional branch or add a unconditional // branch to the old target. This has to be done manually since // fixupBranches is not called after SCTC. - NeedsUncondBranch.emplace_back(std::make_pair(PredBB, CondSucc)); + NeedsUncondBranch.emplace_back(PredBB, CondSucc); Count = PredBB->getFallthroughBranchInfo().Count; } else { // Change destination of the conditional branch. diff --git a/bolt/src/Passes/ExtTSPReorderAlgorithm.cpp b/bolt/src/Passes/ExtTSPReorderAlgorithm.cpp index ca023e5510b7..8e0c9788eb29 100644 --- a/bolt/src/Passes/ExtTSPReorderAlgorithm.cpp +++ b/bolt/src/Passes/ExtTSPReorderAlgorithm.cpp @@ -301,9 +301,7 @@ public: } } - void addEdge(Chain *Other, Edge *Edge) { - Edges.push_back(std::make_pair(Other, Edge)); - } + void addEdge(Chain *Other, Edge *Edge) { Edges.emplace_back(Other, Edge); } void merge(Chain *Other, const std::vector &MergedBlocks) { Blocks = MergedBlocks; @@ -364,7 +362,7 @@ public: } void appendJump(Block *SrcBlock, Block *DstBlock, uint64_t EC) { - Jumps.push_back(std::make_pair(std::make_pair(SrcBlock, DstBlock), EC)); + Jumps.emplace_back(std::make_pair(SrcBlock, DstBlock), EC); } void moveJumps(Edge *Other) { @@ -552,9 +550,9 @@ private: class Block &SuccBlock = AllBlocks[SuccBB->getLayoutIndex()]; uint64_t Count = BI->Count; SuccBlock.InWeight += Count; - SuccBlock.InJumps.push_back(std::make_pair(&Block, Count)); + SuccBlock.InJumps.emplace_back(&Block, Count); Block.OutWeight += Count; - Block.OutJumps.push_back(std::make_pair(&SuccBlock, Count)); + Block.OutJumps.emplace_back(&SuccBlock, Count); NumEdges++; } ++BI; diff --git a/bolt/src/Passes/FrameOptimizer.cpp b/bolt/src/Passes/FrameOptimizer.cpp index 57d407dd67b7..013e759a96c5 100644 --- a/bolt/src/Passes/FrameOptimizer.cpp +++ b/bolt/src/Passes/FrameOptimizer.cpp @@ -209,7 +209,7 @@ void FrameOptimizerPass::removeUnusedStores(const FrameAnalysis &FA, LLVM_DEBUG(dbgs() << "FIE offset = " << FIEX->StackOffset << " size = " << (int)FIEX->Size << "\n"); // Delete it! - ToErase.push_back(std::make_pair(&BB, &Inst)); + ToErase.emplace_back(&BB, &Inst); Prev = &Inst; } } diff --git a/bolt/src/Passes/HFSortPlus.cpp b/bolt/src/Passes/HFSortPlus.cpp index e089b191ca32..8c2f01c4ed6b 100644 --- a/bolt/src/Passes/HFSortPlus.cpp +++ b/bolt/src/Passes/HFSortPlus.cpp @@ -123,9 +123,7 @@ public: } } - void addEdge(Chain *Other, Edge *Edge) { - Edges.push_back(std::make_pair(Other, Edge)); - } + void addEdge(Chain *Other, Edge *Edge) { Edges.emplace_back(Other, Edge); } void merge(Chain *Other) { Nodes.insert(Nodes.end(), Other->Nodes.begin(), Other->Nodes.end()); diff --git a/bolt/src/Passes/IndirectCallPromotion.cpp b/bolt/src/Passes/IndirectCallPromotion.cpp index 6ca8f42f8b51..01ea53fa556d 100644 --- a/bolt/src/Passes/IndirectCallPromotion.cpp +++ b/bolt/src/Passes/IndirectCallPromotion.cpp @@ -657,7 +657,7 @@ IndirectCallPromotion::findCallTargetSymbols( assert(Target.To.Sym && "All ICP targets must be to known symbols"); assert(!Target.JTIndices.empty() && "Jump tables must have indices"); for (uint64_t Idx : Target.JTIndices) { - SymTargets.push_back(std::make_pair(Target.To.Sym, Idx)); + SymTargets.emplace_back(Target.To.Sym, Idx); ++I; } } @@ -667,7 +667,7 @@ IndirectCallPromotion::findCallTargetSymbols( "All ICP targets must be to known symbols"); assert(Targets[I].JTIndices.empty() && "Can't have jump table indices for non-jump tables"); - SymTargets.push_back(std::make_pair(Targets[I].To.Sym, 0)); + SymTargets.emplace_back(Targets[I].To.Sym, 0); } } @@ -773,7 +773,7 @@ IndirectCallPromotion::maybeGetVtableSyms( if (Itr != MethodToVtable.end()) { if (BinaryData *BD = BC.getBinaryDataContainingAddress(Itr->second)) { const uint64_t Addend = Itr->second - BD->getAddress(); - VtableSyms.push_back(std::make_pair(BD->getSymbol(), Addend)); + VtableSyms.emplace_back(BD->getSymbol(), Addend); continue; } } diff --git a/bolt/src/Passes/Instrumentation.cpp b/bolt/src/Passes/Instrumentation.cpp index bc9bac0d0fc0..21f03974a520 100644 --- a/bolt/src/Passes/Instrumentation.cpp +++ b/bolt/src/Passes/Instrumentation.cpp @@ -286,7 +286,7 @@ bool Instrumentation::instrumentOneTarget( return true; } // Critical edge, create BB and put counter there - SplitWorklist.emplace_back(std::make_pair(&FromBB, TargetBB)); + SplitWorklist.emplace_back(&FromBB, TargetBB); SplitInstrs.emplace_back(std::move(CounterInstrs)); return true; } diff --git a/bolt/src/Passes/LongJmp.cpp b/bolt/src/Passes/LongJmp.cpp index f0aed52b14cc..90589e30778a 100644 --- a/bolt/src/Passes/LongJmp.cpp +++ b/bolt/src/Passes/LongJmp.cpp @@ -572,11 +572,11 @@ bool LongJmpPass::relax(BinaryFunction &Func) { InsertionPoint = &*std::prev(Func.end()); // Create a stub to handle a far-away target - Insertions.emplace_back(std::make_pair( - InsertionPoint, - replaceTargetWithStub(BB, Inst, DotAddress, - InsertionPoint == Frontier ? FrontierAddress - : DotAddress))); + Insertions.emplace_back(InsertionPoint, + replaceTargetWithStub(BB, Inst, DotAddress, + InsertionPoint == Frontier + ? FrontierAddress + : DotAddress)); } } diff --git a/bolt/src/Passes/ReorderData.cpp b/bolt/src/Passes/ReorderData.cpp index 6d3150e9deb7..9b195bb515d9 100644 --- a/bolt/src/Passes/ReorderData.cpp +++ b/bolt/src/Passes/ReorderData.cpp @@ -177,7 +177,7 @@ DataOrder ReorderData::baseOrder(BinaryContext &BC, continue; auto BDCI = BinaryDataCounts.find(BD); uint64_t BDCount = BDCI == BinaryDataCounts.end() ? 0 : BDCI->second; - Order.push_back(std::make_pair(BD, BDCount)); + Order.emplace_back(BD, BDCount); } return Order; } diff --git a/bolt/src/RewriteInstance.cpp b/bolt/src/RewriteInstance.cpp index 403079d25012..c324e79e854e 100644 --- a/bolt/src/RewriteInstance.cpp +++ b/bolt/src/RewriteInstance.cpp @@ -3887,14 +3887,14 @@ std::vector RewriteInstance::getOutputSections( auto addSection = [&](const std::string &Name, const ELFShdrTy &Section) { ELFShdrTy NewSection = Section; NewSection.sh_name = SHStrTab.getOffset(Name); - OutputSections.emplace_back(std::make_pair(Name, std::move(NewSection))); + OutputSections.emplace_back(Name, std::move(NewSection)); }; // Copy over entries for original allocatable sections using modified name. for (const ELFShdrTy &Section : Sections) { // Always ignore this section. if (Section.sh_type == ELF::SHT_NULL) { - OutputSections.emplace_back(std::make_pair("", Section)); + OutputSections.emplace_back("", Section); continue; } diff --git a/bolt/src/Target/X86/X86MCPlusBuilder.cpp b/bolt/src/Target/X86/X86MCPlusBuilder.cpp index 973a3fcb43e0..1e48b737fc2b 100644 --- a/bolt/src/Target/X86/X86MCPlusBuilder.cpp +++ b/bolt/src/Target/X86/X86MCPlusBuilder.cpp @@ -3369,7 +3369,7 @@ public: }; for (unsigned int i = 0; i < Targets.size(); ++i) { - Results.push_back(std::make_pair(NextTarget, std::vector())); + Results.emplace_back(NextTarget, std::vector()); std::vector* NewCall = &Results.back().second; if (MinimizeCodeSize && !LoadElim) { @@ -3495,8 +3495,8 @@ public: Jne.addOperand(MCOperand::createImm(X86::COND_NE)); // Call specific target directly. - Results.push_back(std::make_pair(Ctx->createNamedTempSymbol(), - std::vector())); + Results.emplace_back(Ctx->createNamedTempSymbol(), + std::vector()); NewCall = &Results.back().second; NewCall->push_back(CallInst); MCInst &CallOrJmp = NewCall->back(); @@ -3545,7 +3545,7 @@ public: } // Cold call block. - Results.push_back(std::make_pair(NextTarget, std::vector())); + Results.emplace_back(NextTarget, std::vector()); std::vector &NewCall = Results.back().second; for (const MCInst *Inst : MethodFetchInsns) { if (Inst != &CallInst) @@ -3558,7 +3558,7 @@ public: jumpToMergeBlock(NewCall); // Record merge block - Results.push_back(std::make_pair(MergeBlock, std::vector())); + Results.emplace_back(MergeBlock, std::vector()); } return Results; @@ -3581,7 +3581,7 @@ public: MCSymbol* NextTarget = nullptr; for (unsigned int i = 0; i < Targets.size(); ++i) { - Results.push_back(std::make_pair(NextTarget, std::vector())); + Results.emplace_back(NextTarget, std::vector()); std::vector* CurBB = &Results.back().second; // Compare current index to a specific index. @@ -3615,7 +3615,7 @@ public: } // Cold call block. - Results.push_back(std::make_pair(NextTarget, std::vector())); + Results.emplace_back(NextTarget, std::vector()); std::vector &CurBB = Results.back().second; for (const MCInst *Inst : TargetFetchInsns) { if (Inst != &IJmpInst)