ART: Move `CompiledMethod{,-Storage}` to dex2oat/.

Introduce a `CompiledCodeStorage` interface for callbacks to
`dex2oat` for storing code produced by `libart-compiler`.
Using this new interface, move `CompiledMethodStorage`
(this is a misnomer as it's also storing non-method thunk
code), `CompiledMethod` and `SwapSpace` to dex2oat/.

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing --jit
Test: run-gtests.sh
Test: testrunner.py --target --optimizing
Change-Id: Icb7ffc134d00e53c25bd665d044dd1c98bd49a64
diff --git a/compiler/Android.bp b/compiler/Android.bp
index 50555548..117e8dc 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -33,10 +33,8 @@
     defaults: ["art_defaults"],
     host_supported: true,
     srcs: [
-        "compiled_method.cc",
         "debug/elf_debug_writer.cc",
         "dex/inline_method_analyser.cc",
-        "driver/compiled_method_storage.cc",
         "driver/compiler_options.cc",
         "driver/dex_compilation_unit.cc",
         "jit/jit_compiler.cc",
@@ -96,7 +94,6 @@
         "trampolines/trampoline_compiler.cc",
         "utils/assembler.cc",
         "utils/jni_macro_assembler.cc",
-        "utils/swap_space.cc",
         "compiler.cc",
     ],
 
@@ -386,7 +383,6 @@
         "compiler_reflection_test.cc",
         "debug/dwarf/dwarf_test.cc",
         "debug/src_map_elem_test.cc",
-        "driver/compiled_method_storage_test.cc",
         "exception_test.cc",
         "jni/jni_compiler_test.cc",
         "linker/linker_patch_test.cc",
@@ -422,7 +418,6 @@
         "optimizing/suspend_check_test.cc",
         "utils/atomic_dex_ref_map_test.cc",
         "utils/dedupe_set_test.cc",
-        "utils/swap_space_test.cc",
 
         "jni/jni_cfi_test.cc",
         "optimizing/codegen_test.cc",
diff --git a/compiler/common_compiler_test.cc b/compiler/common_compiler_test.cc
index 99fb581..618d80b 100644
--- a/compiler/common_compiler_test.cc
+++ b/compiler/common_compiler_test.cc
@@ -28,9 +28,8 @@
 #include "base/memfd.h"
 #include "base/utils.h"
 #include "class_linker.h"
-#include "compiled_method-inl.h"
 #include "dex/descriptors_names.h"
-#include "driver/compiled_method_storage.h"
+#include "driver/compiled_code_storage.h"
 #include "driver/compiler_options.h"
 #include "jni/java_vm_ext.h"
 #include "interpreter/interpreter.h"
@@ -126,6 +125,65 @@
   DISALLOW_COPY_AND_ASSIGN(CodeAndMetadata);
 };
 
+class CommonCompilerTestImpl::OneCompiledMethodStorage final : public CompiledCodeStorage {
+ public:
+  OneCompiledMethodStorage() {}
+  ~OneCompiledMethodStorage() {}
+
+  CompiledMethod* CreateCompiledMethod(InstructionSet instruction_set,
+                                       ArrayRef<const uint8_t> code,
+                                       ArrayRef<const uint8_t> stack_map,
+                                       ArrayRef<const uint8_t> cfi ATTRIBUTE_UNUSED,
+                                       ArrayRef<const linker::LinkerPatch> patches,
+                                       bool is_intrinsic ATTRIBUTE_UNUSED) override {
+    // Supports only one method at a time.
+    CHECK_EQ(instruction_set_, InstructionSet::kNone);
+    CHECK_NE(instruction_set, InstructionSet::kNone);
+    instruction_set_ = instruction_set;
+    CHECK(code_.empty());
+    CHECK(!code.empty());
+    code_.assign(code.begin(), code.end());
+    CHECK(stack_map_.empty());
+    CHECK(!stack_map.empty());
+    stack_map_.assign(stack_map.begin(), stack_map.end());
+    CHECK(patches.empty()) << "Linker patches are unsupported for compiler gtests.";
+    return reinterpret_cast<CompiledMethod*>(this);
+  }
+
+  ArrayRef<const uint8_t> GetThunkCode(const linker::LinkerPatch& patch ATTRIBUTE_UNUSED,
+                                       /*out*/ std::string* debug_name  ATTRIBUTE_UNUSED) override {
+    LOG(FATAL) << "Unsupported.";
+    UNREACHABLE();
+  }
+
+  void SetThunkCode(const linker::LinkerPatch& patch ATTRIBUTE_UNUSED,
+                    ArrayRef<const uint8_t> code ATTRIBUTE_UNUSED,
+                    const std::string& debug_name ATTRIBUTE_UNUSED) override{
+    LOG(FATAL) << "Unsupported.";
+    UNREACHABLE();
+  }
+
+  InstructionSet GetInstructionSet() const {
+    CHECK_NE(instruction_set_, InstructionSet::kNone);
+    return instruction_set_;
+  }
+
+  ArrayRef<const uint8_t> GetCode() const {
+    CHECK(!code_.empty());
+    return ArrayRef<const uint8_t>(code_);
+  }
+
+  ArrayRef<const uint8_t> GetStackMap() const {
+    CHECK(!stack_map_.empty());
+    return ArrayRef<const uint8_t>(stack_map_);
+  }
+
+ private:
+  InstructionSet instruction_set_ = InstructionSet::kNone;
+  std::vector<uint8_t> code_;
+  std::vector<uint8_t> stack_map_;
+};
+
 std::unique_ptr<CompilerOptions> CommonCompilerTestImpl::CreateCompilerOptions(
     InstructionSet instruction_set, const std::string& variant) {
   std::unique_ptr<CompilerOptions> compiler_options = std::make_unique<CompilerOptions>();
@@ -212,7 +270,7 @@
   CHECK(method != nullptr);
   TimingLogger timings("CommonCompilerTestImpl::CompileMethod", false, false);
   TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
-  CompiledMethodStorage storage(/*swap_fd=*/ -1);
+  OneCompiledMethodStorage storage;
   CompiledMethod* compiled_method = nullptr;
   {
     DCHECK(!Runtime::Current()->IsStarted());
@@ -240,17 +298,16 @@
                                           dex_cache);
     }
     CHECK(compiled_method != nullptr) << "Failed to compile " << method->PrettyMethod();
-    CHECK_NE(compiled_method->GetQuickCode().size(), 0u);
+    CHECK_EQ(reinterpret_cast<OneCompiledMethodStorage*>(compiled_method), &storage);
   }
   {
     TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
-    const void* method_code = MakeExecutable(compiled_method->GetQuickCode(),
-                                             compiled_method->GetVmapTable(),
-                                             compiled_method->GetInstructionSet());
+    const void* method_code = MakeExecutable(storage.GetCode(),
+                                             storage.GetStackMap(),
+                                             storage.GetInstructionSet());
     LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
     GetRuntime()->GetInstrumentation()->InitializeMethodsCode(method, /*aot_code=*/ method_code);
   }
-  CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&storage, compiled_method);
 }
 
 void CommonCompilerTestImpl::ClearBootImageOption() {
diff --git a/compiler/common_compiler_test.h b/compiler/common_compiler_test.h
index be2e483..1175522 100644
--- a/compiler/common_compiler_test.h
+++ b/compiler/common_compiler_test.h
@@ -90,6 +90,8 @@
 
  private:
   class CodeAndMetadata;
+  class OneCompiledMethodStorage;
+
   std::vector<CodeAndMetadata> code_and_metadata_;
 };
 
diff --git a/compiler/compiler.cc b/compiler/compiler.cc
index 98d7339..727b9dd 100644
--- a/compiler/compiler.cc
+++ b/compiler/compiler.cc
@@ -28,7 +28,7 @@
 namespace art {
 
 Compiler* Compiler::Create(const CompilerOptions& compiler_options,
-                           CompiledMethodStorage* storage,
+                           CompiledCodeStorage* storage,
                            Compiler::Kind kind) {
   // Check that oat version when runtime was compiled matches the oat version of the compiler.
   constexpr std::array<uint8_t, 4> compiler_oat_version = OatHeader::kOatVersion;
diff --git a/compiler/compiler.h b/compiler/compiler.h
index afa0dba..6115d02 100644
--- a/compiler/compiler.h
+++ b/compiler/compiler.h
@@ -38,8 +38,8 @@
 }  // namespace mirror
 
 class ArtMethod;
+class CompiledCodeStorage;
 class CompiledMethod;
-class CompiledMethodStorage;
 class CompilerOptions;
 class DexFile;
 template<class T> class Handle;
@@ -53,7 +53,7 @@
   };
 
   static Compiler* Create(const CompilerOptions& compiler_options,
-                          CompiledMethodStorage* storage,
+                          CompiledCodeStorage* storage,
                           Kind kind);
 
   virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const = 0;
@@ -99,7 +99,7 @@
 
  protected:
   Compiler(const CompilerOptions& compiler_options,
-           CompiledMethodStorage* storage,
+           CompiledCodeStorage* storage,
            uint64_t warning) :
       compiler_options_(compiler_options),
       storage_(storage),
@@ -110,13 +110,13 @@
     return compiler_options_;
   }
 
-  CompiledMethodStorage* GetCompiledMethodStorage() const {
+  CompiledCodeStorage* GetCompiledCodeStorage() const {
     return storage_;
   }
 
  private:
   const CompilerOptions& compiler_options_;
-  CompiledMethodStorage* const storage_;
+  CompiledCodeStorage* const storage_;
   const uint64_t maximum_compilation_time_before_warning_;
 
   DISALLOW_COPY_AND_ASSIGN(Compiler);
diff --git a/compiler/driver/compiled_code_storage.h b/compiler/driver/compiled_code_storage.h
new file mode 100644
index 0000000..dfabeb4
--- /dev/null
+++ b/compiler/driver/compiled_code_storage.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_COMPILER_DRIVER_COMPILED_CODE_STORAGE_H_
+#define ART_COMPILER_DRIVER_COMPILED_CODE_STORAGE_H_
+
+#include <string>
+
+#include "base/array_ref.h"
+
+namespace art {
+
+namespace linker {
+class LinkerPatch;
+}  // namespace linker
+
+class CompiledMethod;
+enum class InstructionSet;
+
+// Interface for storing AOT-compiled artifacts.
+// These artifacts include compiled method code and related stack maps and
+// linker patches as well as the compiled thunk code required for some kinds
+// of linker patches.
+//
+// This interface is used for passing AOT-compiled code and metadata produced
+// by the `libart-compiler` to `dex2oat`. The `CompiledMethod` created by
+// `dex2oat` is completely opaque to the `libart-compiler`.
+class CompiledCodeStorage {
+ public:
+  virtual CompiledMethod* CreateCompiledMethod(InstructionSet instruction_set,
+                                               ArrayRef<const uint8_t> code,
+                                               ArrayRef<const uint8_t> stack_map,
+                                               ArrayRef<const uint8_t> cfi,
+                                               ArrayRef<const linker::LinkerPatch> patches,
+                                               bool is_intrinsic) = 0;
+
+  // TODO: Rewrite the interface for passing thunks to the `dex2oat` to reduce
+  // locking. The `OptimizingCompiler` is currently calling `GetThunkCode()`
+  // and locking a mutex there for every `LinkerPatch` that needs a thunk to
+  // check whether we need to compile it. Using a thunk compiler interface,
+  // we could drive this from the `dex2oat` side and lock the mutex at most
+  // once per `CreateCompiledMethod()` for any number of patches.
+  virtual ArrayRef<const uint8_t> GetThunkCode(const linker::LinkerPatch& patch,
+                                               /*out*/ std::string* debug_name = nullptr) = 0;
+  virtual void SetThunkCode(const linker::LinkerPatch& patch,
+                            ArrayRef<const uint8_t> code,
+                            const std::string& debug_name) = 0;
+
+ protected:
+  CompiledCodeStorage() {}
+  ~CompiledCodeStorage() {}
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(CompiledCodeStorage);
+};
+
+}  // namespace art
+
+#endif  // ART_COMPILER_DRIVER_COMPILED_CODE_STORAGE_H_
diff --git a/compiler/libart-compiler.map b/compiler/libart-compiler.map
index b2ddd9e..f66052a 100644
--- a/compiler/libart-compiler.map
+++ b/compiler/libart-compiler.map
@@ -17,15 +17,12 @@
 ART_COMPILER {
   global:
     extern "C++" {
+      art::debug::MakeMiniDebugInfo*;
       *art::debug::WriteDebugInfo*;
-      art::CompiledCode::*;
-      art::CompiledMethod::*;
-      art::CompiledMethodStorage::*;
       art::Compiler::Create*;
       art::CompilerOptions::*;
       art::CreateTrampoline*;
       art::IntrinsicObjects::*;
-      art::debug::MakeMiniDebugInfo*;
       art::linker::operator*art::linker::LinkerPatch::Type*;
       art::operator*art::Whence*;
     };
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index 902b37a..b4bf477 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -33,12 +33,11 @@
 #include "base/timing_logger.h"
 #include "builder.h"
 #include "code_generator.h"
-#include "compiled_method.h"
 #include "compiler.h"
 #include "debug/elf_debug_writer.h"
 #include "debug/method_debug_info.h"
 #include "dex/dex_file_types.h"
-#include "driver/compiled_method_storage.h"
+#include "driver/compiled_code_storage.h"
 #include "driver/compiler_options.h"
 #include "driver/dex_compilation_unit.h"
 #include "graph_checker.h"
@@ -269,7 +268,7 @@
 class OptimizingCompiler final : public Compiler {
  public:
   explicit OptimizingCompiler(const CompilerOptions& compiler_options,
-                              CompiledMethodStorage* storage);
+                              CompiledCodeStorage* storage);
   ~OptimizingCompiler() override;
 
   bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const override;
@@ -363,6 +362,7 @@
   CompiledMethod* Emit(ArenaAllocator* allocator,
                        CodeVectorAllocator* code_allocator,
                        CodeGenerator* codegen,
+                       bool is_intrinsic,
                        const dex::CodeItem* item) const;
 
   // Try compiling a method and return the code generator used for
@@ -412,7 +412,7 @@
 static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
 
 OptimizingCompiler::OptimizingCompiler(const CompilerOptions& compiler_options,
-                                       CompiledMethodStorage* storage)
+                                       CompiledCodeStorage* storage)
     : Compiler(compiler_options, storage, kMaximumCompilationTimeBeforeWarning) {
   // Enable C1visualizer output.
   const std::string& cfg_file_name = compiler_options.GetDumpCfgFileName();
@@ -710,18 +710,19 @@
 CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* allocator,
                                          CodeVectorAllocator* code_allocator,
                                          CodeGenerator* codegen,
+                                         bool is_intrinsic,
                                          const dex::CodeItem* code_item_for_osr_check) const {
   ArenaVector<linker::LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
   ScopedArenaVector<uint8_t> stack_map = codegen->BuildStackMaps(code_item_for_osr_check);
 
-  CompiledMethodStorage* storage = GetCompiledMethodStorage();
-  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
-      storage,
+  CompiledCodeStorage* storage = GetCompiledCodeStorage();
+  CompiledMethod* compiled_method = storage->CreateCompiledMethod(
       codegen->GetInstructionSet(),
       code_allocator->GetMemory(),
       ArrayRef<const uint8_t>(stack_map),
       ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
-      ArrayRef<const linker::LinkerPatch>(linker_patches));
+      ArrayRef<const linker::LinkerPatch>(linker_patches),
+      is_intrinsic);
 
   for (const linker::LinkerPatch& patch : linker_patches) {
     if (codegen->NeedsThunkCode(patch) && storage->GetThunkCode(patch).empty()) {
@@ -1078,10 +1079,8 @@
     compiled_method = Emit(&allocator,
                            &code_allocator,
                            codegen.get(),
+                           compiled_intrinsic,
                            compiled_intrinsic ? nullptr : code_item);
-    if (compiled_intrinsic) {
-      compiled_method->MarkAsIntrinsic();
-    }
 
     if (kArenaAllocatorCountAllocations) {
       codegen.reset();  // Release codegen's ScopedArenaAllocator for memory accounting.
@@ -1172,12 +1171,11 @@
                               method,
                               &handles));
       if (codegen != nullptr) {
-        CompiledMethod* compiled_method = Emit(&allocator,
-                                               &code_allocator,
-                                               codegen.get(),
-                                               /* item= */ nullptr);
-        compiled_method->MarkAsIntrinsic();
-        return compiled_method;
+        return Emit(&allocator,
+                    &code_allocator,
+                    codegen.get(),
+                    /*is_intrinsic=*/ true,
+                    /*item=*/ nullptr);
       }
     }
   }
@@ -1192,17 +1190,17 @@
                         jni_compiled_method,
                         jni_compiled_method.GetCode().size(),
                         compiler_options.GetDebuggable() && compiler_options.IsJitCompiler());
-  return CompiledMethod::SwapAllocCompiledMethod(
-      GetCompiledMethodStorage(),
+  return GetCompiledCodeStorage()->CreateCompiledMethod(
       jni_compiled_method.GetInstructionSet(),
       jni_compiled_method.GetCode(),
       ArrayRef<const uint8_t>(stack_map),
       jni_compiled_method.GetCfi(),
-      /* patches= */ ArrayRef<const linker::LinkerPatch>());
+      /*patches=*/ ArrayRef<const linker::LinkerPatch>(),
+      /*is_intrinsic=*/ false);
 }
 
 Compiler* CreateOptimizingCompiler(const CompilerOptions& compiler_options,
-                                   CompiledMethodStorage* storage) {
+                                   CompiledCodeStorage* storage) {
   return new OptimizingCompiler(compiler_options, storage);
 }
 
diff --git a/compiler/optimizing/optimizing_compiler.h b/compiler/optimizing/optimizing_compiler.h
index cd6d684..95c5c0a 100644
--- a/compiler/optimizing/optimizing_compiler.h
+++ b/compiler/optimizing/optimizing_compiler.h
@@ -23,13 +23,13 @@
 namespace art {
 
 class ArtMethod;
+class CompiledCodeStorage;
 class Compiler;
-class CompiledMethodStorage;
 class CompilerOptions;
 class DexFile;
 
 Compiler* CreateOptimizingCompiler(const CompilerOptions& compiler_options,
-                                   CompiledMethodStorage* storage);
+                                   CompiledCodeStorage* storage);
 
 bool EncodeArtMethodInInlineInfo(ArtMethod* method);
 
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index f248205..5f7735f 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -30,6 +30,8 @@
     srcs: [
         "dex/quick_compiler_callbacks.cc",
         "dex/verification_results.cc",
+        "driver/compiled_method.cc",
+        "driver/compiled_method_storage.cc",
         "driver/compiler_driver.cc",
         "linker/code_info_table_deduper.cc",
         "linker/elf_writer.cc",
@@ -38,6 +40,7 @@
         "linker/multi_oat_relative_patcher.cc",
         "linker/oat_writer.cc",
         "linker/relative_patcher.cc",
+        "utils/swap_space.cc",
     ],
 
     codegen: {
@@ -510,6 +513,7 @@
         "dex2oat_test.cc",
         "dex2oat_vdex_test.cc",
         "dex2oat_image_test.cc",
+        "driver/compiled_method_storage_test.cc",
         "driver/compiler_driver_test.cc",
         "linker/code_info_table_deduper_test.cc",
         "linker/elf_writer_test.cc",
@@ -519,6 +523,7 @@
         "linker/multi_oat_relative_patcher_test.cc",
         "linker/oat_writer_test.cc",
         "verifier_deps_test.cc",
+        "utils/swap_space_test.cc",
     ],
     target: {
         host: {
diff --git a/compiler/compiled_method-inl.h b/dex2oat/driver/compiled_method-inl.h
similarity index 90%
rename from compiler/compiled_method-inl.h
rename to dex2oat/driver/compiled_method-inl.h
index e60b30f..77ac85c 100644
--- a/compiler/compiled_method-inl.h
+++ b/dex2oat/driver/compiled_method-inl.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_COMPILED_METHOD_INL_H_
-#define ART_COMPILER_COMPILED_METHOD_INL_H_
+#ifndef ART_DEX2OAT_DRIVER_COMPILED_METHOD_INL_H_
+#define ART_DEX2OAT_DRIVER_COMPILED_METHOD_INL_H_
 
 #include "compiled_method.h"
 
@@ -52,4 +52,4 @@
 
 }  // namespace art
 
-#endif  // ART_COMPILER_COMPILED_METHOD_INL_H_
+#endif  // ART_DEX2OAT_DRIVER_COMPILED_METHOD_INL_H_
diff --git a/compiler/compiled_method.cc b/dex2oat/driver/compiled_method.cc
similarity index 100%
rename from compiler/compiled_method.cc
rename to dex2oat/driver/compiled_method.cc
diff --git a/compiler/compiled_method.h b/dex2oat/driver/compiled_method.h
similarity index 97%
rename from compiler/compiled_method.h
rename to dex2oat/driver/compiled_method.h
index ed31ca7..a92c757 100644
--- a/compiler/compiled_method.h
+++ b/dex2oat/driver/compiled_method.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_COMPILED_METHOD_H_
-#define ART_COMPILER_COMPILED_METHOD_H_
+#ifndef ART_DEX2OAT_DRIVER_COMPILED_METHOD_H_
+#define ART_DEX2OAT_DRIVER_COMPILED_METHOD_H_
 
 #include <memory>
 #include <string>
@@ -158,4 +158,4 @@
 
 }  // namespace art
 
-#endif  // ART_COMPILER_COMPILED_METHOD_H_
+#endif  // ART_DEX2OAT_DRIVER_COMPILED_METHOD_H_
diff --git a/compiler/driver/compiled_method_storage.cc b/dex2oat/driver/compiled_method_storage.cc
similarity index 94%
rename from compiler/driver/compiled_method_storage.cc
rename to dex2oat/driver/compiled_method_storage.cc
index 4857ec0..0e46f4e 100644
--- a/compiler/driver/compiled_method_storage.cc
+++ b/dex2oat/driver/compiled_method_storage.cc
@@ -253,6 +253,21 @@
   return ThunkMapKey(linker_patch.GetType(), custom_value1, custom_value2);
 }
 
+CompiledMethod* CompiledMethodStorage::CreateCompiledMethod(
+    InstructionSet instruction_set,
+    ArrayRef<const uint8_t> code,
+    ArrayRef<const uint8_t> stack_map,
+    ArrayRef<const uint8_t> cfi,
+    ArrayRef<const linker::LinkerPatch> patches,
+    bool is_intrinsic) {
+  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
+      this, instruction_set, code, stack_map, cfi, patches);
+  if (is_intrinsic) {
+    compiled_method->MarkAsIntrinsic();
+  }
+  return compiled_method;
+}
+
 ArrayRef<const uint8_t> CompiledMethodStorage::GetThunkCode(const linker::LinkerPatch& linker_patch,
                                                             /*out*/ std::string* debug_name) {
   ThunkMapKey key = GetThunkMapKey(linker_patch);
diff --git a/compiler/driver/compiled_method_storage.h b/dex2oat/driver/compiled_method_storage.h
similarity index 83%
rename from compiler/driver/compiled_method_storage.h
rename to dex2oat/driver/compiled_method_storage.h
index f9f3401..3b0304e 100644
--- a/compiler/driver/compiled_method_storage.h
+++ b/dex2oat/driver/compiled_method_storage.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
-#define ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
+#ifndef ART_DEX2OAT_DRIVER_COMPILED_METHOD_STORAGE_H_
+#define ART_DEX2OAT_DRIVER_COMPILED_METHOD_STORAGE_H_
 
 #include <iosfwd>
 #include <map>
@@ -24,6 +24,7 @@
 #include "base/array_ref.h"
 #include "base/length_prefixed_array.h"
 #include "base/macros.h"
+#include "driver/compiled_code_storage.h"
 #include "utils/dedupe_set.h"
 #include "utils/swap_space.h"
 
@@ -33,7 +34,8 @@
 class LinkerPatch;
 }  // namespace linker
 
-class CompiledMethodStorage {
+// TODO: Find a better name. This stores both method and non-method (thunks) code.
+class CompiledMethodStorage final : public CompiledCodeStorage {
  public:
   explicit CompiledMethodStorage(int swap_fd);
   ~CompiledMethodStorage();
@@ -68,16 +70,23 @@
   void ReleaseLinkerPatches(const LengthPrefixedArray<linker::LinkerPatch>* linker_patches);
   size_t UniqueLinkerPatchesEntries() const;
 
+  CompiledMethod* CreateCompiledMethod(InstructionSet instruction_set,
+                                       ArrayRef<const uint8_t> code,
+                                       ArrayRef<const uint8_t> stack_map,
+                                       ArrayRef<const uint8_t> cfi,
+                                       ArrayRef<const linker::LinkerPatch> patches,
+                                       bool is_intrinsic) override;
+
   // Returns the code associated with the given patch.
   // If the code has not been set, returns empty data.
   // If `debug_name` is not null, stores the associated debug name in `*debug_name`.
   ArrayRef<const uint8_t> GetThunkCode(const linker::LinkerPatch& linker_patch,
-                                       /*out*/ std::string* debug_name = nullptr);
+                                       /*out*/ std::string* debug_name = nullptr) override;
 
   // Sets the code and debug name associated with the given patch.
   void SetThunkCode(const linker::LinkerPatch& linker_patch,
                     ArrayRef<const uint8_t> code,
-                    const std::string& debug_name);
+                    const std::string& debug_name) override;
 
  private:
   class ThunkMapKey;
@@ -132,4 +141,4 @@
 
 }  // namespace art
 
-#endif  // ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
+#endif  // ART_DEX2OAT_DRIVER_COMPILED_METHOD_STORAGE_H_
diff --git a/compiler/driver/compiled_method_storage_test.cc b/dex2oat/driver/compiled_method_storage_test.cc
similarity index 100%
rename from compiler/driver/compiled_method_storage_test.cc
rename to dex2oat/driver/compiled_method_storage_test.cc
diff --git a/dex2oat/linker/arm/relative_patcher_arm_base.cc b/dex2oat/linker/arm/relative_patcher_arm_base.cc
index d074432..1cb72f6 100644
--- a/dex2oat/linker/arm/relative_patcher_arm_base.cc
+++ b/dex2oat/linker/arm/relative_patcher_arm_base.cc
@@ -17,9 +17,9 @@
 #include "linker/arm/relative_patcher_arm_base.h"
 
 #include "base/stl_util.h"
-#include "compiled_method-inl.h"
 #include "debug/method_debug_info.h"
 #include "dex/dex_file_types.h"
+#include "driver/compiled_method-inl.h"
 #include "linker/linker_patch.h"
 #include "oat.h"
 #include "oat_quick_method_header.h"
diff --git a/dex2oat/linker/arm/relative_patcher_thumb2.cc b/dex2oat/linker/arm/relative_patcher_thumb2.cc
index 99728cf..45a4e8b 100644
--- a/dex2oat/linker/arm/relative_patcher_thumb2.cc
+++ b/dex2oat/linker/arm/relative_patcher_thumb2.cc
@@ -22,7 +22,7 @@
 #include "art_method.h"
 #include "base/bit_utils.h"
 #include "base/malloc_arena_pool.h"
-#include "compiled_method.h"
+#include "driver/compiled_method.h"
 #include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "linker/linker_patch.h"
 #include "lock_word.h"
diff --git a/dex2oat/linker/arm64/relative_patcher_arm64.cc b/dex2oat/linker/arm64/relative_patcher_arm64.cc
index 5794040..6b84472 100644
--- a/dex2oat/linker/arm64/relative_patcher_arm64.cc
+++ b/dex2oat/linker/arm64/relative_patcher_arm64.cc
@@ -21,7 +21,7 @@
 #include "art_method.h"
 #include "base/bit_utils.h"
 #include "base/malloc_arena_pool.h"
-#include "compiled_method-inl.h"
+#include "driver/compiled_method-inl.h"
 #include "driver/compiler_driver.h"
 #include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "heap_poisoning.h"
diff --git a/dex2oat/linker/multi_oat_relative_patcher_test.cc b/dex2oat/linker/multi_oat_relative_patcher_test.cc
index 2a05816..a393eb8 100644
--- a/dex2oat/linker/multi_oat_relative_patcher_test.cc
+++ b/dex2oat/linker/multi_oat_relative_patcher_test.cc
@@ -16,8 +16,8 @@
 
 #include "multi_oat_relative_patcher.h"
 
-#include "compiled_method.h"
 #include "debug/method_debug_info.h"
+#include "driver/compiled_method.h"
 #include "gtest/gtest.h"
 #include "linker/linker_patch.h"
 #include "stream/vector_output_stream.h"
diff --git a/dex2oat/linker/oat_writer.cc b/dex2oat/linker/oat_writer.cc
index b12ef4f..cf86006 100644
--- a/dex2oat/linker/oat_writer.cc
+++ b/dex2oat/linker/oat_writer.cc
@@ -37,7 +37,6 @@
 #include "class_linker.h"
 #include "class_table-inl.h"
 #include "code_info_table_deduper.h"
-#include "compiled_method-inl.h"
 #include "debug/method_debug_info.h"
 #include "dex/art_dex_file_loader.h"
 #include "dex/class_accessor-inl.h"
@@ -49,6 +48,7 @@
 #include "dex/verification_results.h"
 #include "dex_container.h"
 #include "dexlayout.h"
+#include "driver/compiled_method-inl.h"
 #include "driver/compiler_driver-inl.h"
 #include "driver/compiler_options.h"
 #include "gc/space/image_space.h"
diff --git a/dex2oat/linker/oat_writer_test.cc b/dex2oat/linker/oat_writer_test.cc
index 2810bed..b688e49 100644
--- a/dex2oat/linker/oat_writer_test.cc
+++ b/dex2oat/linker/oat_writer_test.cc
@@ -24,7 +24,6 @@
 #include "base/unix_file/fd_file.h"
 #include "class_linker.h"
 #include "common_compiler_driver_test.h"
-#include "compiled_method-inl.h"
 #include "compiler.h"
 #include "debug/method_debug_info.h"
 #include "dex/class_accessor-inl.h"
@@ -32,6 +31,7 @@
 #include "dex/quick_compiler_callbacks.h"
 #include "dex/test_dex_file_builder.h"
 #include "dex/verification_results.h"
+#include "driver/compiled_method-inl.h"
 #include "driver/compiler_driver.h"
 #include "driver/compiler_options.h"
 #include "entrypoints/quick/quick_entrypoints.h"
diff --git a/dex2oat/linker/relative_patcher_test.h b/dex2oat/linker/relative_patcher_test.h
index f25b501..2900523 100644
--- a/dex2oat/linker/relative_patcher_test.h
+++ b/dex2oat/linker/relative_patcher_test.h
@@ -24,9 +24,9 @@
 #include "base/array_ref.h"
 #include "base/globals.h"
 #include "base/macros.h"
-#include "compiled_method-inl.h"
 #include "dex/method_reference.h"
 #include "dex/string_reference.h"
+#include "driver/compiled_method-inl.h"
 #include "driver/compiled_method_storage.h"
 #include "linker/relative_patcher.h"
 #include "oat_quick_method_header.h"
diff --git a/compiler/utils/swap_space.cc b/dex2oat/utils/swap_space.cc
similarity index 100%
rename from compiler/utils/swap_space.cc
rename to dex2oat/utils/swap_space.cc
diff --git a/compiler/utils/swap_space.h b/dex2oat/utils/swap_space.h
similarity index 97%
rename from compiler/utils/swap_space.h
rename to dex2oat/utils/swap_space.h
index 827e9a6..aba6485 100644
--- a/compiler/utils/swap_space.h
+++ b/dex2oat/utils/swap_space.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_COMPILER_UTILS_SWAP_SPACE_H_
-#define ART_COMPILER_UTILS_SWAP_SPACE_H_
+#ifndef ART_DEX2OAT_UTILS_SWAP_SPACE_H_
+#define ART_DEX2OAT_UTILS_SWAP_SPACE_H_
 
 #include <stddef.h>
 #include <stdint.h>
@@ -239,4 +239,4 @@
 
 }  // namespace art
 
-#endif  // ART_COMPILER_UTILS_SWAP_SPACE_H_
+#endif  // ART_DEX2OAT_UTILS_SWAP_SPACE_H_
diff --git a/compiler/utils/swap_space_test.cc b/dex2oat/utils/swap_space_test.cc
similarity index 100%
rename from compiler/utils/swap_space_test.cc
rename to dex2oat/utils/swap_space_test.cc