[BOLT][NFC] Simplify postProcessJumpTables
authorAmir Ayupov <aaupov@fb.com>
Fri, 30 Jun 2023 05:21:44 +0000 (22:21 -0700)
committerAmir Aupov <amir.aupov@gmail.com>
Fri, 30 Jun 2023 05:47:16 +0000 (22:47 -0700)
Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D154115

bolt/lib/Core/BinaryFunction.cpp

index dac32c4..bde25ef 100644 (file)
@@ -1615,39 +1615,6 @@ void BinaryFunction::postProcessJumpTables() {
                 "detected in function "
              << *this << '\n';
     }
-    if (JT.Entries.empty()) {
-      bool HasOneParent = (JT.Parents.size() == 1);
-      for (unsigned I = 0; I < JT.EntriesAsAddress.size(); ++I) {
-        uint64_t EntryAddress = JT.EntriesAsAddress[I];
-        // builtin_unreachable does not belong to any function
-        // Need to handle separately
-        bool IsBuiltIn = false;
-        for (BinaryFunction *Parent : JT.Parents) {
-          if (EntryAddress == Parent->getAddress() + Parent->getSize()) {
-            IsBuiltIn = true;
-            // Specify second parameter as true to accept builtin_unreachable
-            MCSymbol *Label = getOrCreateLocalLabel(EntryAddress, true);
-            JT.Entries.push_back(Label);
-            break;
-          }
-        }
-        if (IsBuiltIn)
-          continue;
-        // Create local label for targets cannot be reached by other fragments
-        // Otherwise, secondary entry point to target function
-        BinaryFunction *TargetBF =
-            BC.getBinaryFunctionContainingAddress(EntryAddress);
-        if (TargetBF->getAddress() != EntryAddress) {
-          MCSymbol *Label =
-              (HasOneParent && TargetBF == this)
-                  ? getOrCreateLocalLabel(JT.EntriesAsAddress[I], true)
-                  : TargetBF->addEntryPointAtOffset(EntryAddress -
-                                                    TargetBF->getAddress());
-          JT.Entries.push_back(Label);
-        }
-      }
-    }
-
     const uint64_t BDSize =
         BC.getBinaryDataAtAddress(JT.getAddress())->getSize();
     if (!BDSize) {
@@ -1656,6 +1623,33 @@ void BinaryFunction::postProcessJumpTables() {
       assert(BDSize >= JT.getSize() &&
              "jump table cannot be larger than the containing object");
     }
+    if (!JT.Entries.empty())
+      continue;
+
+    bool HasOneParent = (JT.Parents.size() == 1);
+    for (uint64_t EntryAddress : JT.EntriesAsAddress) {
+      // builtin_unreachable does not belong to any function
+      // Need to handle separately
+      bool IsBuiltinUnreachable =
+          llvm::any_of(JT.Parents, [&](const BinaryFunction *Parent) {
+            return EntryAddress == Parent->getAddress() + Parent->getSize();
+          });
+      if (IsBuiltinUnreachable) {
+        MCSymbol *Label = getOrCreateLocalLabel(EntryAddress, true);
+        JT.Entries.push_back(Label);
+        continue;
+      }
+      // Create local label for targets cannot be reached by other fragments
+      // Otherwise, secondary entry point to target function
+      BinaryFunction *TargetBF =
+          BC.getBinaryFunctionContainingAddress(EntryAddress);
+      if (uint64_t Offset = EntryAddress - TargetBF->getAddress()) {
+        MCSymbol *Label = (HasOneParent && TargetBF == this)
+                              ? getOrCreateLocalLabel(EntryAddress, true)
+                              : TargetBF->addEntryPointAtOffset(Offset);
+        JT.Entries.push_back(Label);
+      }
+    }
   }
 
   // Add TakenBranches from JumpTables.