[BOLT][NFC] Move out handleExternalReference
authorAmir Ayupov <aaupov@fb.com>
Tue, 23 Aug 2022 23:07:52 +0000 (16:07 -0700)
committerAmir Ayupov <aaupov@fb.com>
Wed, 24 Aug 2022 00:36:41 +0000 (17:36 -0700)
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
bolt/lib/Core/BinaryFunction.cpp

index f1e77fc..64dfc46 100644 (file)
@@ -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.
   ///
index e02a375..c55bca9 100644 (file)
@@ -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;