Remove CodeItem accessor functions

These are replaced by the accessor helpers.

Bug: 63756964
Test: test-art-host
Test: test/testrunner/testrunner.py --host -j30

Change-Id: Ic93d60b68b684eeb5f69be286b4e15b8f8f97542
diff --git a/compiler/dex/dex_to_dex_compiler.cc b/compiler/dex/dex_to_dex_compiler.cc
index ead909a..7e41c53 100644
--- a/compiler/dex/dex_to_dex_compiler.cc
+++ b/compiler/dex/dex_to_dex_compiler.cc
@@ -114,7 +114,8 @@
 
 void DexCompiler::Compile() {
   DCHECK_EQ(dex_to_dex_compilation_level_, DexToDexCompilationLevel::kOptimize);
-  IterationRange<DexInstructionIterator> instructions = unit_.GetCodeItem()->Instructions();
+  IterationRange<DexInstructionIterator> instructions(unit_.GetCodeItemAccessor().begin(),
+                                                      unit_.GetCodeItemAccessor().end());
   for (DexInstructionIterator it = instructions.begin(); it != instructions.end(); ++it) {
     const uint32_t dex_pc = it.DexPc();
     Instruction* inst = const_cast<Instruction*>(&it.Inst());
@@ -364,7 +365,7 @@
     if (kIsDebugBuild) {
       // Double check that the counts line up with the size of the quicken info.
       size_t quicken_count = 0;
-      for (const DexInstructionPcPair& pair : code_item->Instructions()) {
+      for (const DexInstructionPcPair& pair : unit.GetCodeItemAccessor()) {
         if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
           ++quicken_count;
         }
@@ -376,7 +377,7 @@
       // Dex pc is not serialized, only used for checking the instructions. Since we access the
       // array based on the index of the quickened instruction, the indexes must line up perfectly.
       // The reader side uses the NeedsIndexForInstruction function too.
-      const Instruction& inst = code_item->InstructionAt(info.dex_pc);
+      const Instruction& inst = unit.GetCodeItemAccessor().InstructionAt(info.dex_pc);
       CHECK(QuickenInfoTable::NeedsIndexForInstruction(&inst)) << inst.Opcode();
       // Add the index.
       quicken_data.push_back(static_cast<uint8_t>(info.dex_member_index >> 0));
diff --git a/compiler/dex/dex_to_dex_decompiler_test.cc b/compiler/dex/dex_to_dex_decompiler_test.cc
index 979c4c4..89a63c0 100644
--- a/compiler/dex/dex_to_dex_decompiler_test.cc
+++ b/compiler/dex/dex_to_dex_decompiler_test.cc
@@ -99,8 +99,10 @@
         if (compiled_method != nullptr) {
           table = compiled_method->GetVmapTable();
         }
-        optimizer::ArtDecompileDEX(
-            *it.GetMethodCodeItem(), table, /* decompile_return_instruction */ true);
+        optimizer::ArtDecompileDEX(updated_dex_file,
+                                   *it.GetMethodCodeItem(),
+                                   table,
+                                   /* decompile_return_instruction */ true);
         it.Next();
       }
       DCHECK(!it.HasNext());
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index 68f963e..5d4ed46 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -702,6 +702,7 @@
 //       stable order.
 
 static void ResolveConstStrings(Handle<mirror::DexCache> dex_cache,
+                                const DexFile* dex_file,
                                 const DexFile::CodeItem* code_item)
       REQUIRES_SHARED(Locks::mutator_lock_) {
   if (code_item == nullptr) {
@@ -710,7 +711,7 @@
   }
 
   ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
-  for (const DexInstructionPcPair& inst : code_item->Instructions()) {
+  for (const DexInstructionPcPair& inst : CodeItemInstructionAccessor(dex_file, code_item)) {
     switch (inst->Opcode()) {
       case Instruction::CONST_STRING:
       case Instruction::CONST_STRING_JUMBO: {
@@ -772,7 +773,7 @@
           continue;
         }
         previous_method_idx = method_idx;
-        ResolveConstStrings(dex_cache, it.GetMethodCodeItem());
+        ResolveConstStrings(dex_cache, dex_file, it.GetMethodCodeItem());
         it.Next();
       }
       DCHECK(!it.HasNext());
@@ -2352,9 +2353,7 @@
     // Intern strings seen in <clinit>.
     ArtMethod* clinit = klass->FindClassInitializer(class_linker->GetImagePointerSize());
     if (clinit != nullptr) {
-      const DexFile::CodeItem* code_item = clinit->GetCodeItem();
-      DCHECK(code_item != nullptr);
-      for (const DexInstructionPcPair& inst : code_item->Instructions()) {
+      for (const DexInstructionPcPair& inst : clinit->DexInstructions()) {
         if (inst->Opcode() == Instruction::CONST_STRING) {
           ObjPtr<mirror::String> s = class_linker->ResolveString(
               dex::StringIndex(inst->VRegB_21c()), dex_cache);
diff --git a/compiler/driver/dex_compilation_unit.cc b/compiler/driver/dex_compilation_unit.cc
index 7e8e812..76e1299 100644
--- a/compiler/driver/dex_compilation_unit.cc
+++ b/compiler/driver/dex_compilation_unit.cc
@@ -16,6 +16,7 @@
 
 #include "dex_compilation_unit.h"
 
+#include "code_item_accessors-inl.h"
 #include "mirror/dex_cache.h"
 #include "utils.h"
 
@@ -38,7 +39,8 @@
       dex_method_idx_(method_idx),
       access_flags_(access_flags),
       verified_method_(verified_method),
-      dex_cache_(dex_cache) {
+      dex_cache_(dex_cache),
+      code_item_accessor_(&dex_file, code_item) {
 }
 
 const std::string& DexCompilationUnit::GetSymbol() {
diff --git a/compiler/driver/dex_compilation_unit.h b/compiler/driver/dex_compilation_unit.h
index 24a9a5b..cdc505f 100644
--- a/compiler/driver/dex_compilation_unit.h
+++ b/compiler/driver/dex_compilation_unit.h
@@ -20,6 +20,7 @@
 #include <stdint.h>
 
 #include "base/arena_object.h"
+#include "code_item_accessors.h"
 #include "dex_file.h"
 #include "handle.h"
 #include "jni.h"
@@ -112,6 +113,10 @@
     return dex_cache_;
   }
 
+  const CodeItemDataAccessor& GetCodeItemAccessor() const {
+    return code_item_accessor_;
+  }
+
  private:
   const Handle<mirror::ClassLoader> class_loader_;
 
@@ -127,6 +132,8 @@
 
   const Handle<mirror::DexCache> dex_cache_;
 
+  const CodeItemDataAccessor code_item_accessor_;
+
   std::string symbol_;
 };
 
diff --git a/compiler/optimizing/code_generator.cc b/compiler/optimizing/code_generator.cc
index dee74e9..729b08e 100644
--- a/compiler/optimizing/code_generator.cc
+++ b/compiler/optimizing/code_generator.cc
@@ -45,6 +45,7 @@
 #include "base/casts.h"
 #include "bytecode_utils.h"
 #include "class_linker.h"
+#include "code_item_accessors-inl.h"
 #include "compiled_method.h"
 #include "dex/verified_method.h"
 #include "driver/compiler_driver.h"
@@ -910,7 +911,8 @@
   }
   ArenaVector<size_t> covered(
       loop_headers.size(), 0, graph.GetAllocator()->Adapter(kArenaAllocMisc));
-  for (const DexInstructionPcPair& pair : code_item.Instructions()) {
+  for (const DexInstructionPcPair& pair : CodeItemInstructionAccessor(&graph.GetDexFile(),
+                                                                      &code_item)) {
     const uint32_t dex_pc = pair.DexPc();
     const Instruction& instruction = pair.Inst();
     if (instruction.IsBranch()) {
diff --git a/dex2oat/dex2oat_test.cc b/dex2oat/dex2oat_test.cc
index c8a70c0..b338d08 100644
--- a/dex2oat/dex2oat_test.cc
+++ b/dex2oat/dex2oat_test.cc
@@ -30,6 +30,7 @@
 #include "base/macros.h"
 #include "base/mutex-inl.h"
 #include "bytecode_utils.h"
+#include "code_item_accessors-inl.h"
 #include "dex2oat_environment_test.h"
 #include "dex2oat_return_codes.h"
 #include "dex_file-inl.h"
@@ -943,7 +944,7 @@
                class_it.Next()) {
             if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
               for (const DexInstructionPcPair& inst :
-                       class_it.GetMethodCodeItem()->Instructions()) {
+                       CodeItemInstructionAccessor(dex_file.get(), class_it.GetMethodCodeItem())) {
                 ASSERT_FALSE(inst->IsQuickened());
               }
             }
diff --git a/dexlayout/dexlayout_test.cc b/dexlayout/dexlayout_test.cc
index f994fd6..360f234 100644
--- a/dexlayout/dexlayout_test.cc
+++ b/dexlayout/dexlayout_test.cc
@@ -22,6 +22,7 @@
 #include <unistd.h>
 
 #include "base/unix_file/fd_file.h"
+#include "code_item_accessors-inl.h"
 #include "common_runtime_test.h"
 #include "dex_file-inl.h"
 #include "dex_file_loader.h"
@@ -698,7 +699,7 @@
       while (it.HasNextMethod()) {
         DexFile::CodeItem* item = const_cast<DexFile::CodeItem*>(it.GetMethodCodeItem());
         if (item != nullptr) {
-          IterationRange<DexInstructionIterator> instructions = item->Instructions();
+          CodeItemInstructionAccessor instructions(dex, item);
           if (instructions.begin() != instructions.end()) {
             DexInstructionIterator last_instruction = instructions.begin();
             for (auto dex_it = instructions.begin(); dex_it != instructions.end(); ++dex_it) {
diff --git a/profman/profman.cc b/profman/profman.cc
index 0bef205..10b9f64 100644
--- a/profman/profman.cc
+++ b/profman/profman.cc
@@ -39,6 +39,7 @@
 #include "base/unix_file/fd_file.h"
 #include "boot_image_profile.h"
 #include "bytecode_utils.h"
+#include "code_item_accessors-inl.h"
 #include "dex_file.h"
 #include "dex_file_loader.h"
 #include "dex_file_types.h"
@@ -726,7 +727,7 @@
     const DexFile::CodeItem* code_item = dex_file->GetCodeItem(offset);
 
     bool found_invoke = false;
-    for (const DexInstructionPcPair& inst : code_item->Instructions()) {
+    for (const DexInstructionPcPair& inst : CodeItemInstructionAccessor(dex_file, code_item)) {
       if (inst->Opcode() == Instruction::INVOKE_VIRTUAL) {
         if (found_invoke) {
           LOG(ERROR) << "Multiple invoke INVOKE_VIRTUAL found: "
diff --git a/runtime/code_item_accessors-no_art-inl.h b/runtime/code_item_accessors-no_art-inl.h
index ebdd78b..38ce8fb 100644
--- a/runtime/code_item_accessors-no_art-inl.h
+++ b/runtime/code_item_accessors-no_art-inl.h
@@ -39,6 +39,7 @@
 inline void CodeItemInstructionAccessor::Init(const DexFile* dex_file,
                                               const DexFile::CodeItem* code_item) {
   if (code_item != nullptr) {
+    DCHECK(dex_file->HasAddress(code_item));
     DCHECK(dex_file != nullptr);
     if (dex_file->IsCompactDexFile()) {
       Init(down_cast<const CompactDexFile::CodeItem&>(*code_item));
diff --git a/runtime/code_item_accessors_test.cc b/runtime/code_item_accessors_test.cc
index ef5d246..57a5573 100644
--- a/runtime/code_item_accessors_test.cc
+++ b/runtime/code_item_accessors_test.cc
@@ -86,17 +86,17 @@
     EXPECT_EQ(data_accessor.TriesSize(), kTriesSize);
   };
 
-  uint8_t buffer1[sizeof(CompactDexFile::CodeItem) + kInsnsSizeInCodeUnits * sizeof(uint16_t)] = {};
-  CompactDexFile::CodeItem* dex_code_item = reinterpret_cast<CompactDexFile::CodeItem*>(buffer1);
+  StandardDexFile::CodeItem* dex_code_item =
+      reinterpret_cast<StandardDexFile::CodeItem*>(const_cast<uint8_t*>(standard_dex->Begin()));
   dex_code_item->registers_size_ = kRegisterSize;
   dex_code_item->ins_size_ = kInsSize;
   dex_code_item->outs_size_ = kOutsSize;
   dex_code_item->tries_size_ = kTriesSize;
   dex_code_item->insns_size_in_code_units_ = kInsnsSizeInCodeUnits;
-  verify_code_item(compact_dex.get(), dex_code_item, dex_code_item->insns_);
+  verify_code_item(standard_dex.get(), dex_code_item, dex_code_item->insns_);
 
-  uint8_t buffer2[sizeof(CompactDexFile::CodeItem) + kInsnsSizeInCodeUnits * sizeof(uint16_t)] = {};
-  CompactDexFile::CodeItem* cdex_code_item = reinterpret_cast<CompactDexFile::CodeItem*>(buffer2);
+  CompactDexFile::CodeItem* cdex_code_item =
+      reinterpret_cast<CompactDexFile::CodeItem*>(const_cast<uint8_t*>(compact_dex->Begin()));
   cdex_code_item->registers_size_ = kRegisterSize;
   cdex_code_item->ins_size_ = kInsSize;
   cdex_code_item->outs_size_ = kOutsSize;
diff --git a/runtime/dex_file-inl.h b/runtime/dex_file-inl.h
index a6f7621..7cc8b87 100644
--- a/runtime/dex_file-inl.h
+++ b/runtime/dex_file-inl.h
@@ -133,10 +133,6 @@
   return StringDataByIdx(proto_id.shorty_idx_);
 }
 
-inline const DexFile::TryItem* DexFile::GetTryItems(const CodeItem& code_item, uint32_t offset) {
-  return GetTryItems(code_item.Instructions().end(), offset);
-}
-
 inline const DexFile::TryItem* DexFile::GetTryItems(const DexInstructionIterator& code_item_end,
                                                     uint32_t offset) {
   return reinterpret_cast<const TryItem*>
diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc
index d136dc3..d6469c6 100644
--- a/runtime/dex_file.cc
+++ b/runtime/dex_file.cc
@@ -221,7 +221,10 @@
   uintptr_t code_item_start = reinterpret_cast<uintptr_t>(&code_item);
   uint32_t insns_size = code_item.insns_size_in_code_units_;
   uint32_t tries_size = code_item.tries_size_;
-  const uint8_t* handler_data = GetCatchHandlerData(code_item, 0);
+  const uint8_t* handler_data = GetCatchHandlerData(
+      DexInstructionIterator(code_item.insns_, code_item.insns_size_in_code_units_),
+      code_item.tries_size_,
+      0);
 
   if (tries_size == 0 || handler_data == nullptr) {
     uintptr_t insns_end = reinterpret_cast<uintptr_t>(&code_item.insns_[insns_size]);
@@ -511,11 +514,6 @@
   return -1;
 }
 
-int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
-  int32_t try_item = FindTryItem(GetTryItems(code_item, 0), code_item.tries_size_, address);
-  return (try_item == -1) ? -1 : DexFile::GetTryItems(code_item, try_item)->handler_off_;
-}
-
 bool DexFile::LineNumForPcCb(void* raw_context, const PositionInfo& entry) {
   LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
 
diff --git a/runtime/dex_file.h b/runtime/dex_file.h
index 6c6ae2d..b9bf119 100644
--- a/runtime/dex_file.h
+++ b/runtime/dex_file.h
@@ -303,16 +303,6 @@
 
   // Raw code_item.
   struct CodeItem {
-    IterationRange<DexInstructionIterator> Instructions(uint32_t start_dex_pc = 0u) const {
-      DCHECK_LE(start_dex_pc, insns_size_in_code_units_);
-      return { DexInstructionIterator(insns_, start_dex_pc),
-               DexInstructionIterator(insns_, insns_size_in_code_units_) };
-    }
-
-    const Instruction& InstructionAt(uint32_t dex_pc) const {
-      return *Instruction::At(insns_ + dex_pc);
-    }
-
     // Used when quickening / unquickening.
     void SetDebugInfoOffset(uint32_t new_offset) {
       debug_info_off_ = new_offset;
@@ -784,7 +774,6 @@
   }
 
   static const TryItem* GetTryItems(const DexInstructionIterator& code_item_end, uint32_t offset);
-  static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset);
 
   // Get the base of the encoded data for the given DexCode.
   static const uint8_t* GetCatchHandlerData(const DexInstructionIterator& code_item_end,
@@ -794,16 +783,10 @@
         reinterpret_cast<const uint8_t*>(GetTryItems(code_item_end, tries_size));
     return handler_data + offset;
   }
-  static const uint8_t* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
-    return GetCatchHandlerData(code_item.Instructions().end(), code_item.tries_size_, offset);
-  }
 
   // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
   static int32_t FindTryItem(const TryItem* try_items, uint32_t tries_size, uint32_t address);
 
-  // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none.
-  static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address);
-
   // Get the pointer to the start of the debugging data
   const uint8_t* GetDebugInfoStream(uint32_t debug_info_off) const {
     // Check that the offset is in bounds.
@@ -1044,6 +1027,10 @@
   ALWAYS_INLINE const StandardDexFile* AsStandardDexFile() const;
   ALWAYS_INLINE const CompactDexFile* AsCompactDexFile() const;
 
+  bool HasAddress(const void* addr) const {
+    return Begin() <= addr && addr < Begin() + Size();
+  }
+
  protected:
   // First Dex format version supporting default methods.
   static const uint32_t kDefaultMethodsVersion = 37;
diff --git a/runtime/dex_to_dex_decompiler.cc b/runtime/dex_to_dex_decompiler.cc
index a4e4fb5..dc7e11b 100644
--- a/runtime/dex_to_dex_decompiler.cc
+++ b/runtime/dex_to_dex_decompiler.cc
@@ -21,6 +21,7 @@
 #include "base/macros.h"
 #include "base/mutex.h"
 #include "bytecode_utils.h"
+#include "code_item_accessors-inl.h"
 #include "dex_file-inl.h"
 #include "dex_instruction-inl.h"
 #include "quicken_info.h"
@@ -30,10 +31,11 @@
 
 class DexDecompiler {
  public:
-  DexDecompiler(const DexFile::CodeItem& code_item,
+  DexDecompiler(const DexFile* dex_file,
+                const DexFile::CodeItem& code_item,
                 const ArrayRef<const uint8_t>& quickened_info,
                 bool decompile_return_instruction)
-    : code_item_(code_item),
+    : code_item_accessor_(dex_file, &code_item),
       quicken_info_(quickened_info.data()),
       quicken_info_number_of_indices_(QuickenInfoTable::NumberOfIndices(quickened_info.size())),
       decompile_return_instruction_(decompile_return_instruction) {}
@@ -76,7 +78,7 @@
     return ret;
   }
 
-  const DexFile::CodeItem& code_item_;
+  const CodeItemInstructionAccessor code_item_accessor_;
   const QuickenInfoTable quicken_info_;
   const size_t quicken_info_number_of_indices_;
   const bool decompile_return_instruction_;
@@ -91,7 +93,7 @@
   // because the RETURN_VOID quickening is not encoded in the quickening data. Because
   // unquickening is a rare need and not performance sensitive, it is not worth the
   // added storage to also add the RETURN_VOID quickening in the quickened data.
-  for (const DexInstructionPcPair& pair : code_item_.Instructions()) {
+  for (const DexInstructionPcPair& pair : code_item_accessor_) {
     Instruction* inst = const_cast<Instruction*>(&pair.Inst());
 
     switch (inst->Opcode()) {
@@ -194,13 +196,14 @@
   return true;
 }
 
-bool ArtDecompileDEX(const DexFile::CodeItem& code_item,
+bool ArtDecompileDEX(const DexFile* dex_file,
+                     const DexFile::CodeItem& code_item,
                      const ArrayRef<const uint8_t>& quickened_info,
                      bool decompile_return_instruction) {
   if (quickened_info.size() == 0 && !decompile_return_instruction) {
     return true;
   }
-  DexDecompiler decompiler(code_item, quickened_info, decompile_return_instruction);
+  DexDecompiler decompiler(dex_file, code_item, quickened_info, decompile_return_instruction);
   return decompiler.Decompile();
 }
 
diff --git a/runtime/dex_to_dex_decompiler.h b/runtime/dex_to_dex_decompiler.h
index d7cb164..804b7e8 100644
--- a/runtime/dex_to_dex_decompiler.h
+++ b/runtime/dex_to_dex_decompiler.h
@@ -29,7 +29,8 @@
 // to non-const has too many repercussions on the code base. We make it
 // consistent with DexToDexCompiler, but we should really change it to
 // DexFile::CodeItem*.
-bool ArtDecompileDEX(const DexFile::CodeItem& code_item,
+bool ArtDecompileDEX(const DexFile* dex_file,
+                     const DexFile::CodeItem& code_item,
                      const ArrayRef<const uint8_t>& quickened_data,
                      bool decompile_return_instruction);
 
diff --git a/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc b/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc
index a4d14ec..7192058 100644
--- a/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc
+++ b/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc
@@ -2564,8 +2564,7 @@
   // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
   ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
   uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
-  const DexFile::CodeItem* code = caller_method->GetCodeItem();
-  const Instruction& inst = code->InstructionAt(dex_pc);
+  const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
   DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
          inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
   const uint32_t proto_idx = inst.VRegH();
diff --git a/runtime/vdex_file.cc b/runtime/vdex_file.cc
index fe768a1..a9af97d 100644
--- a/runtime/vdex_file.cc
+++ b/runtime/vdex_file.cc
@@ -254,6 +254,7 @@
                                            quickening_info));
           }
           optimizer::ArtDecompileDEX(
+              &target_dex_file,
               *code_item,
               GetQuickeningInfoAt(quickening_info, quickening_offset),
               decompile_return_instruction);
diff --git a/test/983-source-transform-verify/source_transform.cc b/test/983-source-transform-verify/source_transform.cc
index 3010d7a..831e01c 100644
--- a/test/983-source-transform-verify/source_transform.cc
+++ b/test/983-source-transform-verify/source_transform.cc
@@ -27,6 +27,7 @@
 
 #include "base/macros.h"
 #include "bytecode_utils.h"
+#include "code_item_accessors-inl.h"
 #include "dex_file.h"
 #include "dex_file_loader.h"
 #include "dex_instruction.h"
@@ -88,7 +89,8 @@
       if (!it.IsAtMethod() || it.GetMethodCodeItem() == nullptr) {
         continue;
       }
-      for (const DexInstructionPcPair& pair : it.GetMethodCodeItem()->Instructions()) {
+      for (const DexInstructionPcPair& pair :
+          art::CodeItemInstructionAccessor(dex.get(), it.GetMethodCodeItem())) {
         const Instruction& inst = pair.Inst();
         int forbiden_flags = (Instruction::kVerifyError | Instruction::kVerifyRuntimeOnly);
         if (inst.Opcode() == Instruction::RETURN_VOID_NO_BARRIER ||