From ec1fbf229efec1f18aeac3048f9cb92f352f1756 Mon Sep 17 00:00:00 2001 From: Amir Ayupov Date: Tue, 23 Aug 2022 16:07:52 -0700 Subject: [PATCH] [BOLT][NFC] Move out handleExternalReference Move the large lambda out of BinaryFunction::disassemble, reducing its size from 338 to 295 LoC. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D132100 --- bolt/include/bolt/Core/BinaryFunction.h | 4 ++ bolt/lib/Core/BinaryFunction.cpp | 88 +++++++++++++++++---------------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h index f1e77fc..64dfc46 100644 --- a/bolt/include/bolt/Core/BinaryFunction.h +++ b/bolt/include/bolt/Core/BinaryFunction.h @@ -2046,6 +2046,10 @@ public: void handlePCRelOperand(MCInst &Instruction, uint64_t Address, uint64_t Size); + MCSymbol *handleExternalReference(MCInst &Instruction, uint64_t Size, + uint64_t Offset, uint64_t TargetAddress, + bool &IsCall); + /// Scan function for references to other functions. In relocation mode, /// add relocations for external references. /// diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp index e02a375..c55bca9 100644 --- a/bolt/lib/Core/BinaryFunction.cpp +++ b/bolt/lib/Core/BinaryFunction.cpp @@ -1047,6 +1047,51 @@ void BinaryFunction::handlePCRelOperand(MCInst &Instruction, uint64_t Address, Instruction, Expr, *BC.Ctx, 0))); } +MCSymbol *BinaryFunction::handleExternalReference(MCInst &Instruction, + uint64_t Size, + uint64_t Offset, + uint64_t TargetAddress, + bool &IsCall) { + auto &MIB = BC.MIB; + + const uint64_t AbsoluteInstrAddr = getAddress() + Offset; + BC.addInterproceduralReference(this, TargetAddress); + if (opts::Verbosity >= 2 && !IsCall && Size == 2 && !BC.HasRelocations) { + errs() << "BOLT-WARNING: relaxed tail call detected at 0x" + << Twine::utohexstr(AbsoluteInstrAddr) << " in function " << *this + << ". Code size will be increased.\n"; + } + + assert(!MIB->isTailCall(Instruction) && + "synthetic tail call instruction found"); + + // This is a call regardless of the opcode. + // Assign proper opcode for tail calls, so that they could be + // treated as calls. + if (!IsCall) { + if (!MIB->convertJmpToTailCall(Instruction)) { + assert(MIB->isConditionalBranch(Instruction) && + "unknown tail call instruction"); + if (opts::Verbosity >= 2) { + errs() << "BOLT-WARNING: conditional tail call detected in " + << "function " << *this << " at 0x" + << Twine::utohexstr(AbsoluteInstrAddr) << ".\n"; + } + } + IsCall = true; + } + + if (opts::Verbosity >= 2 && TargetAddress == 0) { + // We actually see calls to address 0 in presence of weak + // symbols originating from libraries. This code is never meant + // to be executed. + outs() << "BOLT-INFO: Function " << *this + << " has a call to address zero.\n"; + } + + return BC.getOrCreateGlobalSymbol(TargetAddress, "FUNCat"); +} + bool BinaryFunction::disassemble() { NamedRegionTimer T("disassemble", "Disassemble function", "buildfuncs", "Build Binary Functions", opts::TimeBuild); @@ -1065,49 +1110,6 @@ bool BinaryFunction::disassemble() { // basic block. Labels[0] = Ctx->createNamedTempSymbol("BB0"); - auto handleExternalReference = [&](MCInst &Instruction, uint64_t Size, - uint64_t Offset, uint64_t TargetAddress, - bool &IsCall) -> MCSymbol * { - const uint64_t AbsoluteInstrAddr = getAddress() + Offset; - MCSymbol *TargetSymbol = nullptr; - BC.addInterproceduralReference(this, TargetAddress); - if (opts::Verbosity >= 2 && !IsCall && Size == 2 && !BC.HasRelocations) { - errs() << "BOLT-WARNING: relaxed tail call detected at 0x" - << Twine::utohexstr(AbsoluteInstrAddr) << " in function " << *this - << ". Code size will be increased.\n"; - } - - assert(!MIB->isTailCall(Instruction) && - "synthetic tail call instruction found"); - - // This is a call regardless of the opcode. - // Assign proper opcode for tail calls, so that they could be - // treated as calls. - if (!IsCall) { - if (!MIB->convertJmpToTailCall(Instruction)) { - assert(MIB->isConditionalBranch(Instruction) && - "unknown tail call instruction"); - if (opts::Verbosity >= 2) { - errs() << "BOLT-WARNING: conditional tail call detected in " - << "function " << *this << " at 0x" - << Twine::utohexstr(AbsoluteInstrAddr) << ".\n"; - } - } - IsCall = true; - } - - TargetSymbol = BC.getOrCreateGlobalSymbol(TargetAddress, "FUNCat"); - if (opts::Verbosity >= 2 && TargetAddress == 0) { - // We actually see calls to address 0 in presence of weak - // symbols originating from libraries. This code is never meant - // to be executed. - outs() << "BOLT-INFO: Function " << *this - << " has a call to address zero.\n"; - } - - return TargetSymbol; - }; - auto handleIndirectBranch = [&](MCInst &Instruction, uint64_t Size, uint64_t Offset) { uint64_t IndirectTarget = 0; -- 2.7.4