Refactor Thumb2 entrypoint adjustment handling. am: 467d570d3f am: f4d74398a5 am: a9627341cc am: d379e98afd

Original change: https://android-review.googlesource.com/c/platform/art/+/2238555

Change-Id: I52ff5892b21e391741dc4637d02e87245791318b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/compiler/compiled_method.cc b/compiler/compiled_method.cc
index e7cdd6e..21d5ed9 100644
--- a/compiler/compiled_method.cc
+++ b/compiler/compiled_method.cc
@@ -54,25 +54,8 @@
   return RoundUp(offset, GetInstructionSetCodeAlignment(instruction_set));
 }
 
-size_t CompiledCode::CodeDelta() const {
-  return CodeDelta(GetInstructionSet());
-}
-
-size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
-  switch (instruction_set) {
-    case InstructionSet::kArm:
-    case InstructionSet::kArm64:
-    case InstructionSet::kX86:
-    case InstructionSet::kX86_64:
-      return 0;
-    case InstructionSet::kThumb2: {
-      // +1 to set the low-order bit so a BLX will switch to Thumb mode
-      return 1;
-    }
-    default:
-      LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
-      UNREACHABLE();
-  }
+size_t CompiledCode::GetEntryPointAdjustment() const {
+  return GetInstructionSetEntryPointAdjustment(GetInstructionSet());
 }
 
 const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
diff --git a/compiler/compiled_method.h b/compiler/compiled_method.h
index e92777f..fa80d67 100644
--- a/compiler/compiled_method.h
+++ b/compiler/compiled_method.h
@@ -58,10 +58,9 @@
   size_t AlignCode(size_t offset) const;
   static size_t AlignCode(size_t offset, InstructionSet instruction_set);
 
-  // returns the difference between the code address and a usable PC.
-  // mainly to cope with kThumb2 where the lower bit must be set.
-  size_t CodeDelta() const;
-  static size_t CodeDelta(InstructionSet instruction_set);
+  // Returns the difference between the code address and a usable PC.
+  // Mainly to cope with `kThumb2` where the lower bit must be set.
+  size_t GetEntryPointAdjustment() const;
 
   // Returns a pointer suitable for invoking the code at the argument
   // code_pointer address.  Mainly to cope with kThumb2 where the
diff --git a/compiler/debug/elf_debug_loc_writer.h b/compiler/debug/elf_debug_loc_writer.h
index 37ab948..9be1e24 100644
--- a/compiler/debug/elf_debug_loc_writer.h
+++ b/compiler/debug/elf_debug_loc_writer.h
@@ -21,7 +21,6 @@
 #include <map>
 
 #include "arch/instruction_set.h"
-#include "compiled_method.h"
 #include "debug/method_debug_info.h"
 #include "dwarf/debug_info_entry_writer.h"
 #include "dwarf/register.h"
diff --git a/compiler/debug/elf_debug_writer.cc b/compiler/debug/elf_debug_writer.cc
index 765a81d..d8007c0 100644
--- a/compiler/debug/elf_debug_writer.cc
+++ b/compiler/debug/elf_debug_writer.cc
@@ -208,7 +208,8 @@
     using Reader = ElfDebugReader<ElfTypes>;
     Reader reader(buffer);
     reader.VisitFunctionSymbols([&](Elf_Sym sym, const char*) {
-      DCHECK_EQ(sym.st_value, method_info.code_address + CompiledMethod::CodeDelta(isa));
+      DCHECK_EQ(sym.st_value,
+                method_info.code_address + GetInstructionSetEntryPointAdjustment(isa));
       DCHECK_EQ(sym.st_size, method_info.code_size);
       num_syms++;
     });
diff --git a/compiler/debug/elf_symtab_writer.h b/compiler/debug/elf_symtab_writer.h
index 410f704..d2acd64 100644
--- a/compiler/debug/elf_symtab_writer.h
+++ b/compiler/debug/elf_symtab_writer.h
@@ -153,7 +153,7 @@
     uint64_t address = info.code_address;
     address += info.is_code_address_text_relative ? text->GetAddress() : 0;
     // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
-    address += CompiledMethod::CodeDelta(info.isa);
+    address += GetInstructionSetEntryPointAdjustment(info.isa);
     symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
   }
   // Add symbols for dex files.
diff --git a/compiler/jit/jit_logger.h b/compiler/jit/jit_logger.h
index f4ef75a..61a4dcf 100644
--- a/compiler/jit/jit_logger.h
+++ b/compiler/jit/jit_logger.h
@@ -21,7 +21,6 @@
 
 #include "base/mutex.h"
 #include "base/os.h"
-#include "compiled_method.h"
 
 namespace art {
 
diff --git a/compiler/optimizing/code_generator.cc b/compiler/optimizing/code_generator.cc
index 252e756..bd8db30 100644
--- a/compiler/optimizing/code_generator.cc
+++ b/compiler/optimizing/code_generator.cc
@@ -39,7 +39,6 @@
 #include "base/leb128.h"
 #include "class_linker.h"
 #include "class_root-inl.h"
-#include "compiled_method.h"
 #include "dex/bytecode_utils.h"
 #include "dex/code_item_accessors-inl.h"
 #include "graph_visualizer.h"
diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc
index 7fb6d3c..b34b613 100644
--- a/compiler/optimizing/code_generator_arm64.cc
+++ b/compiler/optimizing/code_generator_arm64.cc
@@ -27,7 +27,6 @@
 #include "class_root-inl.h"
 #include "class_table.h"
 #include "code_generator_utils.h"
-#include "compiled_method.h"
 #include "entrypoints/quick/quick_entrypoints.h"
 #include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "gc/accounting/card_table.h"
diff --git a/compiler/optimizing/code_generator_arm_vixl.cc b/compiler/optimizing/code_generator_arm_vixl.cc
index f0cca90..8cea5f9 100644
--- a/compiler/optimizing/code_generator_arm_vixl.cc
+++ b/compiler/optimizing/code_generator_arm_vixl.cc
@@ -26,7 +26,6 @@
 #include "class_table.h"
 #include "code_generator_utils.h"
 #include "common_arm.h"
-#include "compiled_method.h"
 #include "entrypoints/quick/quick_entrypoints.h"
 #include "gc/accounting/card_table.h"
 #include "gc/space/image_space.h"
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index ce27083..04373c5 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -20,7 +20,6 @@
 #include "art_method-inl.h"
 #include "class_table.h"
 #include "code_generator_utils.h"
-#include "compiled_method.h"
 #include "entrypoints/quick/quick_entrypoints.h"
 #include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "gc/accounting/card_table.h"
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index b1db993..40a1cd2 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -21,7 +21,6 @@
 #include "class_root-inl.h"
 #include "class_table.h"
 #include "code_generator_utils.h"
-#include "compiled_method.h"
 #include "entrypoints/quick/quick_entrypoints.h"
 #include "gc/accounting/card_table.h"
 #include "gc/space/image_space.h"
diff --git a/dex2oat/linker/arm/relative_patcher_arm_base.cc b/dex2oat/linker/arm/relative_patcher_arm_base.cc
index 6d91322..d074432 100644
--- a/dex2oat/linker/arm/relative_patcher_arm_base.cc
+++ b/dex2oat/linker/arm/relative_patcher_arm_base.cc
@@ -501,7 +501,8 @@
         if (!result.first) {
           break;
         }
-        uint32_t target_offset = result.second - CompiledCode::CodeDelta(instruction_set_);
+        uint32_t target_offset =
+            result.second - GetInstructionSetEntryPointAdjustment(instruction_set_);
         if (target_offset >= patch_offset) {
           DCHECK_LE(target_offset - patch_offset, max_positive_displacement);
         } else if (patch_offset - target_offset > max_negative_displacement) {
diff --git a/dex2oat/linker/elf_writer_quick.cc b/dex2oat/linker/elf_writer_quick.cc
index 424c252..61e5783 100644
--- a/dex2oat/linker/elf_writer_quick.cc
+++ b/dex2oat/linker/elf_writer_quick.cc
@@ -25,7 +25,6 @@
 #include "base/globals.h"
 #include "base/leb128.h"
 #include "base/utils.h"
-#include "compiled_method.h"
 #include "debug/elf_debug_writer.h"
 #include "debug/method_debug_info.h"
 #include "driver/compiler_options.h"
diff --git a/dex2oat/linker/image_writer.cc b/dex2oat/linker/image_writer.cc
index 07afa36..483165a 100644
--- a/dex2oat/linker/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -35,7 +35,6 @@
 #include "base/unix_file/fd_file.h"
 #include "class_linker-inl.h"
 #include "class_root-inl.h"
-#include "compiled_method.h"
 #include "dex/dex_file-inl.h"
 #include "dex/dex_file_types.h"
 #include "driver/compiler_options.h"
diff --git a/dex2oat/linker/oat_writer.cc b/dex2oat/linker/oat_writer.cc
index fcec2e2..b12ef4f 100644
--- a/dex2oat/linker/oat_writer.cc
+++ b/dex2oat/linker/oat_writer.cc
@@ -1353,7 +1353,7 @@
 
     ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
     uint32_t code_size = quick_code.size() * sizeof(uint8_t);
-    uint32_t thumb_offset = compiled_method->CodeDelta();
+    uint32_t thumb_offset = compiled_method->GetEntryPointAdjustment();
 
     // Deduplicate code arrays if we are not producing debuggable code.
     bool deduped = true;
@@ -1800,8 +1800,9 @@
       }
       DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader),
                            GetInstructionSetCodeAlignment(compiled_method->GetInstructionSet()));
-      DCHECK_EQ(method_offsets.code_offset_,
-                offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
+      DCHECK_EQ(
+          method_offsets.code_offset_,
+          offset_ + sizeof(OatQuickMethodHeader) + compiled_method->GetEntryPointAdjustment())
           << dex_file_->PrettyMethod(method_ref.index);
       const OatQuickMethodHeader& method_header =
           oat_class->method_headers_[method_offsets_index];
@@ -2398,22 +2399,22 @@
     const bool generate_debug_info = GetCompilerOptions().GenerateAnyDebugInfo();
     size_t adjusted_offset = offset;
 
-    #define DO_TRAMPOLINE(field, fn_name)                                   \
-      /* Pad with at least four 0xFFs so we can do DCHECKs in OatQuickMethodHeader */ \
-      offset = CompiledCode::AlignCode(offset + 4, instruction_set);        \
-      adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set);  \
-      oat_header_->Set ## fn_name ## Offset(adjusted_offset);               \
-      (field) = compiler_driver_->Create ## fn_name();                      \
-      if (generate_debug_info) {                                            \
-        debug::MethodDebugInfo info = {};                                   \
-        info.custom_name = #fn_name;                                        \
-        info.isa = instruction_set;                                         \
-        info.is_code_address_text_relative = true;                          \
-        /* Use the code offset rather than the `adjusted_offset`. */        \
-        info.code_address = offset - oat_header_->GetExecutableOffset();    \
-        info.code_size = (field)->size();                                   \
-        method_info_.push_back(std::move(info));                            \
-      }                                                                     \
+    #define DO_TRAMPOLINE(field, fn_name)                                                 \
+      /* Pad with at least four 0xFFs so we can do DCHECKs in OatQuickMethodHeader */     \
+      offset = CompiledCode::AlignCode(offset + 4, instruction_set);                      \
+      adjusted_offset = offset + GetInstructionSetEntryPointAdjustment(instruction_set);  \
+      oat_header_->Set ## fn_name ## Offset(adjusted_offset);                             \
+      (field) = compiler_driver_->Create ## fn_name();                                    \
+      if (generate_debug_info) {                                                          \
+        debug::MethodDebugInfo info = {};                                                 \
+        info.custom_name = #fn_name;                                                      \
+        info.isa = instruction_set;                                                       \
+        info.is_code_address_text_relative = true;                                        \
+        /* Use the code offset rather than the `adjusted_offset`. */                      \
+        info.code_address = offset - oat_header_->GetExecutableOffset();                  \
+        info.code_size = (field)->size();                                                 \
+        method_info_.push_back(std::move(info));                                          \
+      }                                                                                   \
       offset += (field)->size();
 
     DO_TRAMPOLINE(jni_dlsym_lookup_trampoline_, JniDlsymLookupTrampoline);
diff --git a/dex2oat/linker/relative_patcher_test.h b/dex2oat/linker/relative_patcher_test.h
index 56e0dc1..f25b501 100644
--- a/dex2oat/linker/relative_patcher_test.h
+++ b/dex2oat/linker/relative_patcher_test.h
@@ -132,7 +132,7 @@
       offset += alignment_size;
 
       offset += sizeof(OatQuickMethodHeader);
-      uint32_t quick_code_offset = offset + compiled_method->CodeDelta();
+      uint32_t quick_code_offset = offset + compiled_method->GetEntryPointAdjustment();
       const auto code = compiled_method->GetQuickCode();
       offset += code.size();
 
@@ -172,7 +172,8 @@
           if (patch.GetType() == LinkerPatch::Type::kCallRelative) {
             auto result = method_offset_map_.FindMethodOffset(patch.TargetMethod());
             uint32_t target_offset =
-                result.first ? result.second : kTrampolineOffset + compiled_method->CodeDelta();
+                result.first ? result.second
+                             : kTrampolineOffset + compiled_method->GetEntryPointAdjustment();
             patcher_->PatchCall(&patched_code_,
                                 patch.LiteralOffset(),
                                 offset + patch.LiteralOffset(),
@@ -227,7 +228,7 @@
 
     auto result = method_offset_map_.FindMethodOffset(method_ref);
     CHECK(result.first);  // Must have been linked.
-    size_t offset = result.second - compiled_methods_[idx]->CodeDelta();
+    size_t offset = result.second - compiled_methods_[idx]->GetEntryPointAdjustment();
     CHECK_LT(offset, output_.size());
     CHECK_LE(offset + expected_code.size(), output_.size());
     ArrayRef<const uint8_t> linked_code(&output_[offset], expected_code.size());
diff --git a/dex2oat/linker/x86/relative_patcher_x86.cc b/dex2oat/linker/x86/relative_patcher_x86.cc
index e3b94b0..a444446 100644
--- a/dex2oat/linker/x86/relative_patcher_x86.cc
+++ b/dex2oat/linker/x86/relative_patcher_x86.cc
@@ -16,7 +16,6 @@
 
 #include "linker/x86/relative_patcher_x86.h"
 
-#include "compiled_method.h"
 #include "linker/linker_patch.h"
 
 namespace art {
diff --git a/dex2oat/linker/x86_64/relative_patcher_x86_64.cc b/dex2oat/linker/x86_64/relative_patcher_x86_64.cc
index 0b9d07e..629affc 100644
--- a/dex2oat/linker/x86_64/relative_patcher_x86_64.cc
+++ b/dex2oat/linker/x86_64/relative_patcher_x86_64.cc
@@ -16,7 +16,6 @@
 
 #include "linker/x86_64/relative_patcher_x86_64.h"
 
-#include "compiled_method.h"
 #include "linker/linker_patch.h"
 
 namespace art {
diff --git a/libartbase/arch/instruction_set.h b/libartbase/arch/instruction_set.h
index 69983d2..e68ff08 100644
--- a/libartbase/arch/instruction_set.h
+++ b/libartbase/arch/instruction_set.h
@@ -99,6 +99,21 @@
   InstructionSetAbort(isa);
 }
 
+constexpr bool IsValidInstructionSet(InstructionSet isa) {
+  switch (isa) {
+    case InstructionSet::kArm:
+    case InstructionSet::kThumb2:
+    case InstructionSet::kArm64:
+    case InstructionSet::kX86:
+    case InstructionSet::kX86_64:
+      return true;
+
+    case InstructionSet::kNone:
+      return false;
+  }
+  return false;
+}
+
 constexpr size_t GetInstructionSetInstructionAlignment(InstructionSet isa) {
   switch (isa) {
     case InstructionSet::kArm:
@@ -118,21 +133,6 @@
   InstructionSetAbort(isa);
 }
 
-constexpr bool IsValidInstructionSet(InstructionSet isa) {
-  switch (isa) {
-    case InstructionSet::kArm:
-    case InstructionSet::kThumb2:
-    case InstructionSet::kArm64:
-    case InstructionSet::kX86:
-    case InstructionSet::kX86_64:
-      return true;
-
-    case InstructionSet::kNone:
-      return false;
-  }
-  return false;
-}
-
 constexpr size_t GetInstructionSetCodeAlignment(InstructionSet isa) {
   switch (isa) {
     case InstructionSet::kArm:
@@ -152,6 +152,26 @@
   InstructionSetAbort(isa);
 }
 
+// Returns the difference between the code address and a usable PC.
+// Mainly to cope with `kThumb2` where the lower bit must be set.
+constexpr size_t GetInstructionSetEntryPointAdjustment(InstructionSet isa) {
+  switch (isa) {
+    case InstructionSet::kArm:
+    case InstructionSet::kArm64:
+    case InstructionSet::kX86:
+    case InstructionSet::kX86_64:
+      return 0;
+    case InstructionSet::kThumb2: {
+      // +1 to set the low-order bit so a BLX will switch to Thumb mode
+      return 1;
+    }
+
+    case InstructionSet::kNone:
+      break;
+  }
+  InstructionSetAbort(isa);
+}
+
 constexpr bool Is64BitInstructionSet(InstructionSet isa) {
   switch (isa) {
     case InstructionSet::kArm:
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index e69b32a..0d163bd 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -45,7 +45,6 @@
 #include "class_linker-inl.h"
 #include "class_linker.h"
 #include "class_root-inl.h"
-#include "compiled_method.h"
 #include "debug/debug_info.h"
 #include "debug/elf_debug_writer.h"
 #include "debug/method_debug_info.h"
@@ -183,17 +182,17 @@
     builder_->WriteDynamicSection();
 
     const OatHeader& oat_header = oat_file_->GetOatHeader();
-    #define DO_TRAMPOLINE(fn_name)                                                \
-      if (oat_header.Get ## fn_name ## Offset() != 0) {                           \
-        debug::MethodDebugInfo info = {};                                         \
-        info.custom_name = #fn_name;                                              \
-        info.isa = oat_header.GetInstructionSet();                                \
-        info.is_code_address_text_relative = true;                                \
-        size_t code_offset = oat_header.Get ## fn_name ## Offset();               \
-        code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet());   \
-        info.code_address = code_offset - oat_header.GetExecutableOffset();       \
-        info.code_size = 0;  /* The symbol lasts until the next symbol. */        \
-        method_debug_infos_.push_back(std::move(info));                           \
+    #define DO_TRAMPOLINE(fn_name)                                                            \
+      if (oat_header.Get ## fn_name ## Offset() != 0) {                                       \
+        debug::MethodDebugInfo info = {};                                                     \
+        info.custom_name = #fn_name;                                                          \
+        info.isa = oat_header.GetInstructionSet();                                            \
+        info.is_code_address_text_relative = true;                                            \
+        size_t code_offset = oat_header.Get ## fn_name ## Offset();                           \
+        code_offset -= GetInstructionSetEntryPointAdjustment(oat_header.GetInstructionSet()); \
+        info.code_address = code_offset - oat_header.GetExecutableOffset();                   \
+        info.code_size = 0;  /* The symbol lasts until the next symbol. */                    \
+        method_debug_infos_.push_back(std::move(info));                                       \
       }
     DO_TRAMPOLINE(JniDlsymLookupTrampoline);
     DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);