Move compiler code out of method verifier.

We want to detect small methods for inlining at the end of
the method verification. Instead of adding more compiler
code to the runtime, we create a callback from the runtime
into the compiler, so that we can keep the code there.
Additionally, we move the compiler-related code that was
already in the method verifier to the compiler since it
doesn't really belong to the runtime in the first place.

Change-Id: I708ca13227c809e07917ff3879a89722017e83a9
diff --git a/compiler/Android.mk b/compiler/Android.mk
index a2419d5..d9a573f 100644
--- a/compiler/Android.mk
+++ b/compiler/Android.mk
@@ -57,6 +57,7 @@
 	dex/frontend.cc \
 	dex/mir_graph.cc \
 	dex/mir_analysis.cc \
+	dex/verified_methods_data.cc \
 	dex/vreg_analysis.cc \
 	dex/ssa_transformation.cc \
 	driver/compiler_driver.cc \
diff --git a/compiler/dex/quick/codegen_util.cc b/compiler/dex/quick/codegen_util.cc
index 5d78ed5..bae8ff3 100644
--- a/compiler/dex/quick/codegen_util.cc
+++ b/compiler/dex/quick/codegen_util.cc
@@ -19,6 +19,7 @@
 #include "gc_map.h"
 #include "mapping_table.h"
 #include "mir_to_lir-inl.h"
+#include "dex/verified_methods_data.h"
 #include "verifier/dex_gc_map.h"
 #include "verifier/method_verifier.h"
 
@@ -746,7 +747,8 @@
     }
   }
   MethodReference method_ref(cu_->dex_file, cu_->method_idx);
-  const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
+  const std::vector<uint8_t>* gc_map_raw =
+      cu_->compiler_driver->GetVerifiedMethodsData()->GetDexGcMap(method_ref);
   verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[0]);
   DCHECK_EQ(gc_map_raw->size(), dex_gc_map.RawSize());
   // Compute native offset to references size.
diff --git a/compiler/dex/verified_methods_data.cc b/compiler/dex/verified_methods_data.cc
new file mode 100644
index 0000000..454b92c
--- /dev/null
+++ b/compiler/dex/verified_methods_data.cc
@@ -0,0 +1,451 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#include "base/stl_util.h"
+#include "dex_file.h"
+#include "dex_instruction.h"
+#include "dex_instruction-inl.h"
+#include "base/mutex.h"
+#include "base/mutex-inl.h"
+#include "mirror/art_method.h"
+#include "mirror/art_method-inl.h"
+#include "mirror/class.h"
+#include "mirror/class-inl.h"
+#include "mirror/dex_cache.h"
+#include "mirror/dex_cache-inl.h"
+#include "mirror/object.h"
+#include "mirror/object-inl.h"
+#include "verified_methods_data.h"
+#include "verifier/dex_gc_map.h"
+#include "verifier/method_verifier.h"
+#include "verifier/method_verifier-inl.h"
+#include "verifier/register_line.h"
+#include "verifier/register_line-inl.h"
+
+namespace art {
+
+VerifiedMethodsData::VerifiedMethodsData()
+    : dex_gc_maps_lock_("compiler GC maps lock"),
+      dex_gc_maps_(),
+      safecast_map_lock_("compiler Cast Elision lock"),
+      safecast_map_(),
+      devirt_maps_lock_("compiler Devirtualization lock"),
+      devirt_maps_(),
+      rejected_classes_lock_("compiler rejected classes lock"),
+      rejected_classes_() {
+}
+
+VerifiedMethodsData::~VerifiedMethodsData() {
+  Thread* self = Thread::Current();
+  {
+    WriterMutexLock mu(self, dex_gc_maps_lock_);
+    STLDeleteValues(&dex_gc_maps_);
+  }
+  {
+    WriterMutexLock mu(self, safecast_map_lock_);
+    STLDeleteValues(&safecast_map_);
+  }
+  {
+    WriterMutexLock mu(self, devirt_maps_lock_);
+    STLDeleteValues(&devirt_maps_);
+  }
+}
+
+bool VerifiedMethodsData::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
+  MethodReference ref = method_verifier->GetMethodReference();
+  bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
+  if (compile) {
+    /* Generate a register map and add it to the method. */
+    const std::vector<uint8_t>* dex_gc_map = GenerateGcMap(method_verifier);
+    if (dex_gc_map == NULL) {
+      DCHECK(method_verifier->HasFailures());
+      return false;  // Not a real failure, but a failure to encode
+    }
+    if (kIsDebugBuild) {
+      VerifyGcMap(method_verifier, *dex_gc_map);
+    }
+    SetDexGcMap(ref, dex_gc_map);
+  }
+
+  if (method_verifier->HasCheckCasts()) {
+    MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet(method_verifier);
+    if (method_to_safe_casts != NULL) {
+      SetSafeCastMap(ref, method_to_safe_casts);
+    }
+  }
+
+  if (method_verifier->HasVirtualOrInterfaceInvokes()) {
+    PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap(method_verifier);
+    if (pc_to_concrete_method != NULL) {
+      SetDevirtMap(ref, pc_to_concrete_method);
+    }
+  }
+  return true;
+}
+
+const std::vector<uint8_t>* VerifiedMethodsData::GetDexGcMap(MethodReference ref) {
+  ReaderMutexLock mu(Thread::Current(), dex_gc_maps_lock_);
+  DexGcMapTable::const_iterator it = dex_gc_maps_.find(ref);
+  CHECK(it != dex_gc_maps_.end())
+    << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
+  CHECK(it->second != NULL);
+  return it->second;
+}
+
+const MethodReference* VerifiedMethodsData::GetDevirtMap(const MethodReference& ref,
+                                                                    uint32_t dex_pc) {
+  ReaderMutexLock mu(Thread::Current(), devirt_maps_lock_);
+  DevirtualizationMapTable::const_iterator it = devirt_maps_.find(ref);
+  if (it == devirt_maps_.end()) {
+    return NULL;
+  }
+
+  // Look up the PC in the map, get the concrete method to execute and return its reference.
+  PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
+  if (pc_to_concrete_method != it->second->end()) {
+    return &(pc_to_concrete_method->second);
+  } else {
+    return NULL;
+  }
+}
+
+bool VerifiedMethodsData::IsSafeCast(MethodReference ref, uint32_t pc) {
+  ReaderMutexLock mu(Thread::Current(), safecast_map_lock_);
+  SafeCastMap::const_iterator it = safecast_map_.find(ref);
+  if (it == safecast_map_.end()) {
+    return false;
+  }
+
+  // Look up the cast address in the set of safe casts
+  MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
+  return cast_it != it->second->end();
+}
+
+void VerifiedMethodsData::AddRejectedClass(ClassReference ref) {
+  {
+    WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
+    rejected_classes_.insert(ref);
+  }
+  DCHECK(IsClassRejected(ref));
+}
+
+bool VerifiedMethodsData::IsClassRejected(ClassReference ref) {
+  ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
+  return (rejected_classes_.find(ref) != rejected_classes_.end());
+}
+
+bool VerifiedMethodsData::IsCandidateForCompilation(MethodReference& method_ref,
+                                                    const uint32_t access_flags) {
+#ifdef ART_SEA_IR_MODE
+    bool use_sea = Runtime::Current()->IsSeaIRMode();
+    use_sea = use_sea && (std::string::npos != PrettyMethod(
+                          method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
+    if (use_sea) return true;
+#endif
+  // Don't compile class initializers, ever.
+  if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
+    return false;
+  }
+  return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
+}
+
+const std::vector<uint8_t>* VerifiedMethodsData::GenerateGcMap(
+    verifier::MethodVerifier* method_verifier) {
+  size_t num_entries, ref_bitmap_bits, pc_bits;
+  ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits);
+  // There's a single byte to encode the size of each bitmap
+  if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
+    // TODO: either a better GC map format or per method failures
+    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
+        << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers";
+    return NULL;
+  }
+  size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
+  // There are 2 bytes to encode the number of entries
+  if (num_entries >= 65536) {
+    // TODO: either a better GC map format or per method failures
+    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
+        << "Cannot encode GC map for method with " << num_entries << " entries";
+    return NULL;
+  }
+  size_t pc_bytes;
+  verifier::RegisterMapFormat format;
+  if (pc_bits <= 8) {
+    format = verifier::kRegMapFormatCompact8;
+    pc_bytes = 1;
+  } else if (pc_bits <= 16) {
+    format = verifier::kRegMapFormatCompact16;
+    pc_bytes = 2;
+  } else {
+    // TODO: either a better GC map format or per method failures
+    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
+        << "Cannot encode GC map for method with "
+        << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
+    return NULL;
+  }
+  size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
+  std::vector<uint8_t>* table = new std::vector<uint8_t>;
+  if (table == NULL) {
+    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
+        << "Failed to encode GC map (size=" << table_size << ")";
+    return NULL;
+  }
+  table->reserve(table_size);
+  // Write table header
+  table->push_back(format | ((ref_bitmap_bytes & ~0xFF) >> 5));
+  table->push_back(ref_bitmap_bytes & 0xFF);
+  table->push_back(num_entries & 0xFF);
+  table->push_back((num_entries >> 8) & 0xFF);
+  // Write table data
+  const DexFile::CodeItem* code_item = method_verifier->CodeItem();
+  for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
+    if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
+      table->push_back(i & 0xFF);
+      if (pc_bytes == 2) {
+        table->push_back((i >> 8) & 0xFF);
+      }
+      verifier::RegisterLine* line = method_verifier->GetRegLine(i);
+      line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
+    }
+  }
+  DCHECK_EQ(table->size(), table_size);
+  return table;
+}
+
+void VerifiedMethodsData::VerifyGcMap(verifier::MethodVerifier* method_verifier,
+                                      const std::vector<uint8_t>& data) {
+  // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
+  // that the table data is well formed and all references are marked (or not) in the bitmap
+  verifier::DexPcToReferenceMap map(&data[0]);
+  DCHECK_EQ(data.size(), map.RawSize());
+  size_t map_index = 0;
+  const DexFile::CodeItem* code_item = method_verifier->CodeItem();
+  for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
+    const uint8_t* reg_bitmap = map.FindBitMap(i, false);
+    if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
+      CHECK_LT(map_index, map.NumEntries());
+      CHECK_EQ(map.GetDexPc(map_index), i);
+      CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
+      map_index++;
+      verifier::RegisterLine* line = method_verifier->GetRegLine(i);
+      for (size_t j = 0; j < code_item->registers_size_; j++) {
+        if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
+          CHECK_LT(j / 8, map.RegWidth());
+          CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
+        } else if ((j / 8) < map.RegWidth()) {
+          CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
+        } else {
+          // If a register doesn't contain a reference then the bitmap may be shorter than the line
+        }
+      }
+    } else {
+      CHECK(reg_bitmap == NULL);
+    }
+  }
+}
+
+void VerifiedMethodsData::ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
+                                            size_t* gc_points, size_t* ref_bitmap_bits,
+                                            size_t* log2_max_gc_pc) {
+  size_t local_gc_points = 0;
+  size_t max_insn = 0;
+  size_t max_ref_reg = -1;
+  const DexFile::CodeItem* code_item = method_verifier->CodeItem();
+  for (size_t i = 0; i < code_item->insns_size_in_code_units_; i++) {
+    if (method_verifier->GetInstructionFlags(i).IsCompileTimeInfoPoint()) {
+      local_gc_points++;
+      max_insn = i;
+      verifier::RegisterLine* line = method_verifier->GetRegLine(i);
+      max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
+    }
+  }
+  *gc_points = local_gc_points;
+  *ref_bitmap_bits = max_ref_reg + 1;  // if max register is 0 we need 1 bit to encode (ie +1)
+  size_t i = 0;
+  while ((1U << i) <= max_insn) {
+    i++;
+  }
+  *log2_max_gc_pc = i;
+}
+
+void VerifiedMethodsData::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* gc_map) {
+  DCHECK(Runtime::Current()->IsCompiler());
+  {
+    WriterMutexLock mu(Thread::Current(), dex_gc_maps_lock_);
+    DexGcMapTable::iterator it = dex_gc_maps_.find(ref);
+    if (it != dex_gc_maps_.end()) {
+      delete it->second;
+      dex_gc_maps_.erase(it);
+    }
+    dex_gc_maps_.Put(ref, gc_map);
+  }
+  DCHECK(GetDexGcMap(ref) != NULL);
+}
+
+VerifiedMethodsData::MethodSafeCastSet* VerifiedMethodsData::GenerateSafeCastSet(
+    verifier::MethodVerifier* method_verifier) {
+  /*
+   * Walks over the method code and adds any cast instructions in which
+   * the type cast is implicit to a set, which is used in the code generation
+   * to elide these casts.
+   */
+  if (method_verifier->HasFailures()) {
+    return NULL;
+  }
+  UniquePtr<MethodSafeCastSet> mscs;
+  const DexFile::CodeItem* code_item = method_verifier->CodeItem();
+  const Instruction* inst = Instruction::At(code_item->insns_);
+  const Instruction* end = Instruction::At(code_item->insns_ +
+                                           code_item->insns_size_in_code_units_);
+
+  for (; inst < end; inst = inst->Next()) {
+    Instruction::Code code = inst->Opcode();
+    if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
+      uint32_t dex_pc = inst->GetDexPc(code_item->insns_);
+      const verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
+      bool is_safe_cast = false;
+      if (code == Instruction::CHECK_CAST) {
+        const verifier::RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
+        const verifier::RegType& cast_type =
+            method_verifier->ResolveCheckedClass(inst->VRegB_21c());
+        is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
+      } else {
+        const verifier::RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
+        // We only know its safe to assign to an array if the array type is precise. For example,
+        // an Object[] can have any type of object stored in it, but it may also be assigned a
+        // String[] in which case the stores need to be of Strings.
+        if (array_type.IsPreciseReference()) {
+          const verifier::RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
+          const verifier::RegType& component_type = method_verifier->GetRegTypeCache()
+              ->GetComponentType(array_type, method_verifier->GetClassLoader());
+          is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
+        }
+      }
+      if (is_safe_cast) {
+        if (mscs.get() == nullptr) {
+          mscs.reset(new MethodSafeCastSet());
+        }
+        mscs->insert(dex_pc);
+      }
+    }
+  }
+  return mscs.release();
+}
+
+void  VerifiedMethodsData::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
+  WriterMutexLock mu(Thread::Current(), safecast_map_lock_);
+  SafeCastMap::iterator it = safecast_map_.find(ref);
+  if (it != safecast_map_.end()) {
+    delete it->second;
+    safecast_map_.erase(it);
+  }
+  safecast_map_.Put(ref, cast_set);
+  DCHECK(safecast_map_.find(ref) != safecast_map_.end());
+}
+
+VerifiedMethodsData::PcToConcreteMethodMap* VerifiedMethodsData::GenerateDevirtMap(
+    verifier::MethodVerifier* method_verifier) {
+  // It is risky to rely on reg_types for sharpening in cases of soft
+  // verification, we might end up sharpening to a wrong implementation. Just abort.
+  if (method_verifier->HasFailures()) {
+    return NULL;
+  }
+
+  UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
+  const DexFile::CodeItem* code_item = method_verifier->CodeItem();
+  const uint16_t* insns = code_item->insns_;
+  const Instruction* inst = Instruction::At(insns);
+  const Instruction* end = Instruction::At(insns + code_item->insns_size_in_code_units_);
+
+  for (; inst < end; inst = inst->Next()) {
+    bool is_virtual   = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
+        (inst->Opcode() ==  Instruction::INVOKE_VIRTUAL_RANGE);
+    bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
+        (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
+
+    if (!is_interface && !is_virtual) {
+      continue;
+    }
+    // Get reg type for register holding the reference to the object that will be dispatched upon.
+    uint32_t dex_pc = inst->GetDexPc(insns);
+    verifier::RegisterLine* line = method_verifier->GetRegLine(dex_pc);
+    bool is_range = (inst->Opcode() ==  Instruction::INVOKE_VIRTUAL_RANGE) ||
+        (inst->Opcode() ==  Instruction::INVOKE_INTERFACE_RANGE);
+    const verifier::RegType&
+        reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
+
+    if (!reg_type.HasClass()) {
+      // We will compute devirtualization information only when we know the Class of the reg type.
+      continue;
+    }
+    mirror::Class* reg_class = reg_type.GetClass();
+    if (reg_class->IsInterface()) {
+      // We can't devirtualize when the known type of the register is an interface.
+      continue;
+    }
+    if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
+      // We can't devirtualize abstract classes except on arrays of abstract classes.
+      continue;
+    }
+    mirror::ArtMethod* abstract_method = method_verifier->GetDexCache()->GetResolvedMethod(
+        is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
+    if (abstract_method == NULL) {
+      // If the method is not found in the cache this means that it was never found
+      // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
+      continue;
+    }
+    // Find the concrete method.
+    mirror::ArtMethod* concrete_method = NULL;
+    if (is_interface) {
+      concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
+    }
+    if (is_virtual) {
+      concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
+    }
+    if (concrete_method == NULL || concrete_method->IsAbstract()) {
+      // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
+      continue;
+    }
+    if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
+        concrete_method->GetDeclaringClass()->IsFinal()) {
+      // If we knew exactly the class being dispatched upon, or if the target method cannot be
+      // overridden record the target to be used in the compiler driver.
+      if (pc_to_concrete_method_map.get() == NULL) {
+        pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
+      }
+      MethodReference concrete_ref(
+          concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
+          concrete_method->GetDexMethodIndex());
+      pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
+    }
+  }
+  return pc_to_concrete_method_map.release();
+}
+
+void  VerifiedMethodsData::SetDevirtMap(MethodReference ref,
+                                   const PcToConcreteMethodMap* devirt_map) {
+  WriterMutexLock mu(Thread::Current(), devirt_maps_lock_);
+  DevirtualizationMapTable::iterator it = devirt_maps_.find(ref);
+  if (it != devirt_maps_.end()) {
+    delete it->second;
+    devirt_maps_.erase(it);
+  }
+
+  devirt_maps_.Put(ref, devirt_map);
+  DCHECK(devirt_maps_.find(ref) != devirt_maps_.end());
+}
+
+}  // namespace art
diff --git a/compiler/dex/verified_methods_data.h b/compiler/dex/verified_methods_data.h
new file mode 100644
index 0000000..8d5058a
--- /dev/null
+++ b/compiler/dex/verified_methods_data.h
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2013 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_DEX_VERIFIED_METHODS_DATA_H_
+#define ART_COMPILER_DEX_VERIFIED_METHODS_DATA_H_
+
+#include <stdint.h>
+#include <set>
+#include <vector>
+
+#include "base/macros.h"
+#include "base/mutex.h"
+#include "class_reference.h"
+#include "method_reference.h"
+#include "safe_map.h"
+
+namespace art {
+
+namespace verifier {
+class MethodVerifier;
+}  // namespace verifier
+
+class VerifiedMethodsData {
+  public:
+    VerifiedMethodsData();
+    ~VerifiedMethodsData();
+
+    bool ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier)
+        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
+        LOCKS_EXCLUDED(dex_gc_maps_lock_, devirt_maps_lock_, safecast_map_lock_);
+
+    const std::vector<uint8_t>* GetDexGcMap(MethodReference ref)
+        LOCKS_EXCLUDED(dex_gc_maps_lock_);
+
+    const MethodReference* GetDevirtMap(const MethodReference& ref, uint32_t dex_pc)
+        LOCKS_EXCLUDED(devirt_maps_lock_);
+
+    // Returns true if the cast can statically be verified to be redundant
+    // by using the check-cast elision peephole optimization in the verifier
+    bool IsSafeCast(MethodReference ref, uint32_t pc) LOCKS_EXCLUDED(safecast_map_lock_);
+
+    void AddRejectedClass(ClassReference ref) LOCKS_EXCLUDED(rejected_classes_lock_);
+    bool IsClassRejected(ClassReference ref) LOCKS_EXCLUDED(rejected_classes_lock_);
+
+    static bool IsCandidateForCompilation(MethodReference& method_ref,
+                                          const uint32_t access_flags);
+
+  private:
+    /*
+     * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
+     * verification). For type-precise determination we have all the data we need, so we just need to
+     * encode it in some clever fashion.
+     * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
+     */
+    const std::vector<uint8_t>* GenerateGcMap(verifier::MethodVerifier* method_verifier);
+
+    // Verify that the GC map associated with method_ is well formed
+    void VerifyGcMap(verifier::MethodVerifier* method_verifier, const std::vector<uint8_t>& data);
+
+    // Compute sizes for GC map data
+    void ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
+                           size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
+
+    // All the GC maps that the verifier has created
+    typedef SafeMap<const MethodReference, const std::vector<uint8_t>*,
+        MethodReferenceComparator> DexGcMapTable;
+    ReaderWriterMutex dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
+    DexGcMapTable dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_);
+    void SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* dex_gc_map)
+        LOCKS_EXCLUDED(dex_gc_maps_lock_);
+
+    // Cast elision types.
+    typedef std::set<uint32_t> MethodSafeCastSet;
+    typedef SafeMap<MethodReference, const MethodSafeCastSet*,
+        MethodReferenceComparator> SafeCastMap;
+    MethodSafeCastSet* GenerateSafeCastSet(verifier::MethodVerifier* method_verifier)
+        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+    void SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* mscs)
+        LOCKS_EXCLUDED(safecast_map_lock_);
+    ReaderWriterMutex safecast_map_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
+    SafeCastMap safecast_map_ GUARDED_BY(safecast_map_lock_);
+
+    // Devirtualization map.
+    typedef SafeMap<uint32_t, MethodReference> PcToConcreteMethodMap;
+    typedef SafeMap<MethodReference, const PcToConcreteMethodMap*,
+        MethodReferenceComparator> DevirtualizationMapTable;
+    PcToConcreteMethodMap* GenerateDevirtMap(verifier::MethodVerifier* method_verifier)
+        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+    ReaderWriterMutex devirt_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
+    DevirtualizationMapTable devirt_maps_ GUARDED_BY(devirt_maps_lock_);
+    void SetDevirtMap(MethodReference ref, const PcToConcreteMethodMap* pc_method_map)
+          LOCKS_EXCLUDED(devirt_maps_lock_);
+
+    // Rejected classes
+    typedef std::set<ClassReference> RejectedClassesTable;
+    ReaderWriterMutex rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
+    RejectedClassesTable rejected_classes_ GUARDED_BY(rejected_classes_lock_);
+};
+
+}  // namespace art
+
+#endif  // ART_COMPILER_DEX_VERIFIED_METHODS_DATA_H_
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index af18c05..dbebd26 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -27,6 +27,7 @@
 #include "class_linker.h"
 #include "dex_compilation_unit.h"
 #include "dex_file-inl.h"
+#include "dex/verified_methods_data.h"
 #include "jni_internal.h"
 #include "object_utils.h"
 #include "runtime.h"
@@ -335,11 +336,13 @@
 extern "C" void compilerLLVMSetBitcodeFileName(art::CompilerDriver& driver,
                                                std::string const& filename);
 
-CompilerDriver::CompilerDriver(CompilerBackend compiler_backend, InstructionSet instruction_set,
+CompilerDriver::CompilerDriver(VerifiedMethodsData* verified_methods_data,
+                               CompilerBackend compiler_backend, InstructionSet instruction_set,
                                InstructionSetFeatures instruction_set_features,
                                bool image, DescriptorSet* image_classes, size_t thread_count,
                                bool dump_stats)
-    : compiler_backend_(compiler_backend),
+    : verified_methods_data_(verified_methods_data),
+      compiler_backend_(compiler_backend),
       instruction_set_(instruction_set),
       instruction_set_features_(instruction_set_features),
       freezing_constructor_lock_("freezing constructor lock"),
@@ -1253,7 +1256,7 @@
           // Did the verifier record a more precise invoke target based on its type information?
           const MethodReference caller_method(mUnit->GetDexFile(), mUnit->GetDexMethodIndex());
           const MethodReference* devirt_map_target =
-              verifier::MethodVerifier::GetDevirtMap(caller_method, dex_pc);
+              verified_methods_data_->GetDevirtMap(caller_method, dex_pc);
           if (devirt_map_target != NULL) {
             SirtRef<mirror::DexCache> target_dex_cache(soa.Self(), mUnit->GetClassLinker()->FindDexCache(*devirt_map_target->dex_file));
             SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
@@ -1301,7 +1304,7 @@
 }
 
 bool CompilerDriver::IsSafeCast(const MethodReference& mr, uint32_t dex_pc) {
-  bool result = verifier::MethodVerifier::IsSafeCast(mr, dex_pc);
+  bool result = verified_methods_data_->IsSafeCast(mr, dex_pc);
   if (result) {
     stats_->SafeCast();
   } else {
@@ -2240,7 +2243,7 @@
   }
   ClassReference ref(&dex_file, class_def_index);
   // Skip compiling classes with generic verifier failures since they will still fail at runtime
-  if (verifier::MethodVerifier::IsClassRejected(ref)) {
+  if (manager->GetCompiler()->verified_methods_data_->IsClassRejected(ref)) {
     return;
   }
   const byte* class_data = dex_file.GetClassData(class_def);
@@ -2323,7 +2326,7 @@
   } else if ((access_flags & kAccAbstract) != 0) {
   } else {
     MethodReference method_ref(&dex_file, method_idx);
-    bool compile = verifier::MethodVerifier::IsCandidateForCompilation(method_ref, access_flags);
+    bool compile = VerifiedMethodsData::IsCandidateForCompilation(method_ref, access_flags);
 
     if (compile) {
       CompilerFn compiler = compiler_;
diff --git a/compiler/driver/compiler_driver.h b/compiler/driver/compiler_driver.h
index 7e81849..14bfde6 100644
--- a/compiler/driver/compiler_driver.h
+++ b/compiler/driver/compiler_driver.h
@@ -43,6 +43,7 @@
 class DexCompilationUnit;
 class OatWriter;
 class TimingLogger;
+class VerifiedMethodsData;
 
 enum CompilerBackend {
   kQuick,
@@ -90,7 +91,8 @@
   // enabled.  "image_classes" lets the compiler know what classes it
   // can assume will be in the image, with NULL implying all available
   // classes.
-  explicit CompilerDriver(CompilerBackend compiler_backend, InstructionSet instruction_set,
+  explicit CompilerDriver(VerifiedMethodsData* verified_methods_data,
+                          CompilerBackend compiler_backend, InstructionSet instruction_set,
                           InstructionSetFeatures instruction_set_features,
                           bool image, DescriptorSet* image_classes,
                           size_t thread_count, bool dump_stats);
@@ -105,6 +107,10 @@
   void CompileOne(const mirror::ArtMethod* method, TimingLogger& timings)
       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
+  VerifiedMethodsData* GetVerifiedMethodsData() const {
+    return verified_methods_data_;
+  }
+
   const InstructionSet& GetInstructionSet() const {
     return instruction_set_;
   }
@@ -390,6 +396,8 @@
   std::vector<const PatchInformation*> code_to_patch_;
   std::vector<const PatchInformation*> methods_to_patch_;
 
+  VerifiedMethodsData* verified_methods_data_;
+
   CompilerBackend compiler_backend_;
 
   const InstructionSet instruction_set_;
diff --git a/compiler/llvm/compiler_llvm.cc b/compiler/llvm/compiler_llvm.cc
index d59afd4..35d1ecd 100644
--- a/compiler/llvm/compiler_llvm.cc
+++ b/compiler/llvm/compiler_llvm.cc
@@ -20,6 +20,7 @@
 #include "base/stl_util.h"
 #include "class_linker.h"
 #include "compiled_method.h"
+#include "dex/verified_methods_data.h"
 #include "driver/compiler_driver.h"
 #include "driver/dex_compilation_unit.h"
 #include "globals.h"
@@ -155,7 +156,8 @@
   MethodReference mref(dex_compilation_unit->GetDexFile(),
                        dex_compilation_unit->GetDexMethodIndex());
   return new CompiledMethod(*compiler_driver_, compiler_driver_->GetInstructionSet(),
-                            cunit->GetElfObject(), *verifier::MethodVerifier::GetDexGcMap(mref),
+                            cunit->GetElfObject(),
+                            *compiler_driver_->GetVerifiedMethodsData()->GetDexGcMap(mref),
                             cunit->GetDexCompilationUnit()->GetSymbol());
 }
 
diff --git a/compiler/oat_writer.cc b/compiler/oat_writer.cc
index 8382469..199a2b8 100644
--- a/compiler/oat_writer.cc
+++ b/compiler/oat_writer.cc
@@ -23,6 +23,7 @@
 #include "base/unix_file/fd_file.h"
 #include "class_linker.h"
 #include "dex_file-inl.h"
+#include "dex/verified_methods_data.h"
 #include "gc/space/space.h"
 #include "mirror/art_method-inl.h"
 #include "mirror/array.h"
@@ -217,7 +218,7 @@
       mirror::Class::Status status;
       if (compiled_class != NULL) {
         status = compiled_class->GetStatus();
-      } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
+      } else if (compiler_driver_->GetVerifiedMethodsData()->IsClassRejected(class_ref)) {
         status = mirror::Class::kStatusError;
       } else {
         status = mirror::Class::kStatusNotReady;
@@ -432,7 +433,7 @@
       mirror::Class::Status status;
       if (compiled_class != NULL) {
         status = compiled_class->GetStatus();
-      } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
+      } else if (compiler_driver_->GetVerifiedMethodsData()->IsClassRejected(class_ref)) {
         status = mirror::Class::kStatusError;
       } else {
         status = mirror::Class::kStatusNotReady;
diff --git a/compiler/sea_ir/frontend.cc b/compiler/sea_ir/frontend.cc
index 3512911..6c779c8 100644
--- a/compiler/sea_ir/frontend.cc
+++ b/compiler/sea_ir/frontend.cc
@@ -59,7 +59,7 @@
   std::string llvm_code = llvm_data->GetElf(compiler.GetInstructionSet());
   CompiledMethod* compiled_method =
       new CompiledMethod(compiler, compiler.GetInstructionSet(), llvm_code,
-                         *verifier::MethodVerifier::GetDexGcMap(mref), symbol);
+                         *compiler.GetVerifiedMethodsData()->GetDexGcMap(mref), symbol);
   LOG(INFO) << "Compiled SEA IR method " << PrettyMethod(method_idx, dex_file) << ".";
   return compiled_method;
 }
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 28d6649..b37cc54 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -30,7 +30,9 @@
 #include "base/timing_logger.h"
 #include "base/unix_file/fd_file.h"
 #include "class_linker.h"
+#include "compiler_callbacks.h"
 #include "dex_file-inl.h"
+#include "dex/verified_methods_data.h"
 #include "driver/compiler_driver.h"
 #include "elf_fixup.h"
 #include "elf_stripper.h"
@@ -162,12 +164,13 @@
                      InstructionSetFeatures instruction_set_features,
                      size_t thread_count)
       SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
-    if (!CreateRuntime(options, instruction_set)) {
+    UniquePtr<Dex2Oat> dex2oat(new Dex2Oat(compiler_backend, instruction_set,
+                                           instruction_set_features, thread_count));
+    if (!dex2oat->CreateRuntime(options, instruction_set)) {
       *p_dex2oat = NULL;
       return false;
     }
-    *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set,
-        instruction_set_features, thread_count);
+    *p_dex2oat = dex2oat.release();
     return true;
   }
 
@@ -261,7 +264,8 @@
       Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
     }
 
-    UniquePtr<CompilerDriver> driver(new CompilerDriver(compiler_backend_,
+    UniquePtr<CompilerDriver> driver(new CompilerDriver(verified_methods_data_.get(),
+                                                        compiler_backend_,
                                                         instruction_set_,
                                                         instruction_set_features_,
                                                         image,
@@ -337,21 +341,42 @@
   }
 
  private:
-  explicit Dex2Oat(Runtime* runtime,
-                   CompilerBackend compiler_backend,
+  class Dex2OatCompilerCallbacks : public CompilerCallbacks {
+    public:
+      explicit Dex2OatCompilerCallbacks(VerifiedMethodsData* verified_methods_data)
+          : verified_methods_data_(verified_methods_data) { }
+      virtual ~Dex2OatCompilerCallbacks() { }
+
+      virtual bool MethodVerified(verifier::MethodVerifier* verifier)
+          SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+        return verified_methods_data_->ProcessVerifiedMethod(verifier);
+      }
+      virtual void ClassRejected(ClassReference ref) {
+        verified_methods_data_->AddRejectedClass(ref);
+      }
+
+    private:
+      VerifiedMethodsData* verified_methods_data_;
+  };
+
+  explicit Dex2Oat(CompilerBackend compiler_backend,
                    InstructionSet instruction_set,
                    InstructionSetFeatures instruction_set_features,
                    size_t thread_count)
       : compiler_backend_(compiler_backend),
         instruction_set_(instruction_set),
         instruction_set_features_(instruction_set_features),
-        runtime_(runtime),
+        verified_methods_data_(new VerifiedMethodsData),
+        callbacks_(verified_methods_data_.get()),
+        runtime_(nullptr),
         thread_count_(thread_count),
         start_ns_(NanoTime()) {
   }
 
-  static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
+  bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set)
       SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
+    options.push_back(
+        std::make_pair("compilercallbacks", static_cast<CompilerCallbacks*>(&callbacks_)));
     if (!Runtime::Create(options, false)) {
       LOG(ERROR) << "Failed to create runtime";
       return false;
@@ -364,6 +389,7 @@
       }
     }
     runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
+    runtime_ = runtime;
     return true;
   }
 
@@ -405,6 +431,8 @@
   const InstructionSet instruction_set_;
   const InstructionSetFeatures instruction_set_features_;
 
+  UniquePtr<VerifiedMethodsData> verified_methods_data_;
+  Dex2OatCompilerCallbacks callbacks_;
   Runtime* runtime_;
   size_t thread_count_;
   uint64_t start_ns_;
@@ -899,7 +927,6 @@
   }
 
   Runtime::Options options;
-  options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
   std::vector<const DexFile*> boot_class_path;
   if (boot_image_option.empty()) {
     size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 643c183..fbb47bd 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -32,6 +32,7 @@
 #include "base/stl_util.h"
 #include "base/unix_file/fd_file.h"
 #include "class_linker-inl.h"
+#include "compiler_callbacks.h"
 #include "debugger.h"
 #include "dex_file-inl.h"
 #include "gc/accounting/card_table-inl.h"
@@ -2490,7 +2491,9 @@
         self->GetException(nullptr)->SetCause(cause.get());
       }
       ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex());
-      verifier::MethodVerifier::AddRejectedClass(ref);
+      if (Runtime::Current()->IsCompiler()) {
+        Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
+      }
       klass->SetStatus(mirror::Class::kStatusError, self);
       return;
     }
diff --git a/runtime/compiler_callbacks.h b/runtime/compiler_callbacks.h
new file mode 100644
index 0000000..9045f3a
--- /dev/null
+++ b/runtime/compiler_callbacks.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2013 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_RUNTIME_COMPILER_CALLBACKS_H_
+#define ART_RUNTIME_COMPILER_CALLBACKS_H_
+
+#include "class_reference.h"
+
+namespace art {
+
+namespace verifier {
+
+class MethodVerifier;
+
+}  // namespace verifier
+
+class CompilerCallbacks {
+  public:
+    virtual ~CompilerCallbacks() { }
+
+    virtual bool MethodVerified(verifier::MethodVerifier* verifier)
+        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
+    virtual void ClassRejected(ClassReference ref) = 0;
+
+  protected:
+    CompilerCallbacks() { }
+};
+
+}  // namespace art
+
+#endif  // ART_RUNTIME_COMPILER_CALLBACKS_H_
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index ff7b8f5..25623a1 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -70,7 +70,7 @@
 Runtime* Runtime::instance_ = NULL;
 
 Runtime::Runtime()
-    : is_compiler_(false),
+    : compiler_callbacks_(nullptr),
       is_zygote_(false),
       is_concurrent_gc_enabled_(true),
       is_explicit_gc_disabled_(false),
@@ -367,7 +367,7 @@
   parsed->low_memory_mode_ = false;
   parsed->use_tlab_ = false;
 
-  parsed->is_compiler_ = false;
+  parsed->compiler_callbacks_ = nullptr;
   parsed->is_zygote_ = false;
   parsed->interpreter_only_ = false;
   parsed->is_explicit_gc_disabled_ = false;
@@ -547,8 +547,9 @@
       parsed->properties_.push_back(option.substr(strlen("-D")));
     } else if (StartsWith(option, "-Xjnitrace:")) {
       parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
-    } else if (option == "compiler") {
-      parsed->is_compiler_ = true;
+    } else if (option == "compilercallbacks") {
+      parsed->compiler_callbacks_ =
+          reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
     } else if (option == "-Xzygote") {
       parsed->is_zygote_ = true;
     } else if (option == "-Xint") {
@@ -672,7 +673,7 @@
     parsed->boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
   }
 
-  if (!parsed->is_compiler_ && parsed->image_.empty()) {
+  if (parsed->compiler_callbacks_ == nullptr && parsed->image_.empty()) {
     parsed->image_ += GetAndroidRoot();
     parsed->image_ += "/framework/boot.art";
   }
@@ -885,7 +886,7 @@
   class_path_string_ = options->class_path_string_;
   properties_ = options->properties_;
 
-  is_compiler_ = options->is_compiler_;
+  compiler_callbacks_ = options->compiler_callbacks_;
   is_zygote_ = options->is_zygote_;
   is_explicit_gc_disabled_ = options->is_explicit_gc_disabled_;
 
diff --git a/runtime/runtime.h b/runtime/runtime.h
index ce64510..7b57dda 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -56,6 +56,7 @@
 class MethodVerifier;
 }
 class ClassLinker;
+class CompilerCallbacks;
 class DexFile;
 class InternTable;
 struct JavaVMExt;
@@ -100,7 +101,7 @@
     std::string image_;
     bool check_jni_;
     std::string jni_trace_;
-    bool is_compiler_;
+    CompilerCallbacks* compiler_callbacks_;
     bool is_zygote_;
     bool interpreter_only_;
     bool is_explicit_gc_disabled_;
@@ -148,7 +149,11 @@
       SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_);
 
   bool IsCompiler() const {
-    return is_compiler_;
+    return compiler_callbacks_ != nullptr;
+  }
+
+  CompilerCallbacks* GetCompilerCallbacks() {
+    return compiler_callbacks_;
   }
 
   bool IsZygote() const {
@@ -469,7 +474,7 @@
   // A pointer to the active runtime or NULL.
   static Runtime* instance_;
 
-  bool is_compiler_;
+  CompilerCallbacks* compiler_callbacks_;
   bool is_zygote_;
   bool is_concurrent_gc_enabled_;
   bool is_explicit_gc_disabled_;
diff --git a/runtime/verifier/dex_gc_map.h b/runtime/verifier/dex_gc_map.h
index 4570ae8..d77ea65 100644
--- a/runtime/verifier/dex_gc_map.h
+++ b/runtime/verifier/dex_gc_map.h
@@ -110,8 +110,6 @@
     return data_;
   }
 
-  friend class MethodVerifier;
-
   static const int kRegMapFormatShift = 5;
   static const uint8_t kRegMapFormatMask = 0x7;
 
diff --git a/runtime/verifier/method_verifier-inl.h b/runtime/verifier/method_verifier-inl.h
new file mode 100644
index 0000000..5cf234d
--- /dev/null
+++ b/runtime/verifier/method_verifier-inl.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2013 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_RUNTIME_VERIFIER_METHOD_VERIFIER_INL_H_
+#define ART_RUNTIME_VERIFIER_METHOD_VERIFIER_INL_H_
+
+#include "base/logging.h"
+#include "method_verifier.h"
+#include "mirror/class_loader.h"
+#include "mirror/dex_cache.h"
+
+namespace art {
+namespace verifier {
+
+const DexFile::CodeItem* MethodVerifier::CodeItem() const {
+  return code_item_;
+}
+
+RegisterLine* MethodVerifier::GetRegLine(uint32_t dex_pc) {
+  return reg_table_.GetLine(dex_pc);
+}
+
+const InstructionFlags& MethodVerifier::GetInstructionFlags(size_t index) const {
+  return insn_flags_[index];
+}
+
+mirror::ClassLoader* MethodVerifier::GetClassLoader() {
+  return class_loader_->get();
+}
+
+mirror::DexCache* MethodVerifier::GetDexCache() {
+  return dex_cache_->get();
+}
+
+MethodReference MethodVerifier::GetMethodReference() const {
+  return MethodReference(dex_file_, dex_method_idx_);
+}
+
+uint32_t MethodVerifier::GetAccessFlags() const {
+  return method_access_flags_;
+}
+
+bool MethodVerifier::HasCheckCasts() const {
+  return has_check_casts_;
+}
+
+bool MethodVerifier::HasVirtualOrInterfaceInvokes() const {
+  return has_virtual_or_interface_invokes_;
+}
+
+bool MethodVerifier::HasFailures() const {
+  return !failure_messages_.empty();
+}
+
+const RegType& MethodVerifier::ResolveCheckedClass(uint32_t class_idx) {
+  DCHECK(!HasFailures());
+  const RegType& result = ResolveClassAndCheckAccess(class_idx);
+  DCHECK(!HasFailures());
+  return result;
+}
+
+}  // namespace verifier
+}  // namespace art
+
+#endif  // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_INL_H_
diff --git a/runtime/verifier/method_verifier.cc b/runtime/verifier/method_verifier.cc
index 59ead89..f03cdcd 100644
--- a/runtime/verifier/method_verifier.cc
+++ b/runtime/verifier/method_verifier.cc
@@ -22,6 +22,7 @@
 #include "base/mutex-inl.h"
 #include "base/stringpiece.h"
 #include "class_linker.h"
+#include "compiler_callbacks.h"
 #include "dex_file-inl.h"
 #include "dex_instruction-inl.h"
 #include "dex_instruction_visitor.h"
@@ -110,7 +111,7 @@
     *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
     if (Runtime::Current()->IsCompiler()) {
       ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
-      AddRejectedClass(ref);
+      Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
     }
     return kHardFailure;
   }
@@ -441,7 +442,12 @@
   // Perform static instruction verification.
   result = result && VerifyInstructions();
   // Perform code-flow analysis and return.
-  return result && VerifyCodeFlow();
+  result = result && VerifyCodeFlow();
+  // Compute information for compiler.
+  if (result && Runtime::Current()->IsCompiler()) {
+    result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
+  }
+  return result;
 }
 
 std::ostream& MethodVerifier::Fail(VerifyError error) {
@@ -480,7 +486,7 @@
     case VERIFY_ERROR_BAD_CLASS_HARD: {
       if (Runtime::Current()->IsCompiler()) {
         ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
-        AddRejectedClass(ref);
+        Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
       }
       have_pending_hard_failure_ = true;
       break;
@@ -1063,38 +1069,6 @@
     DCHECK_NE(failures_.size(), 0U);
     return false;
   }
-
-  // Compute information for compiler.
-  if (Runtime::Current()->IsCompiler()) {
-    MethodReference ref(dex_file_, dex_method_idx_);
-    bool compile = IsCandidateForCompilation(ref, method_access_flags_);
-    if (compile) {
-      /* Generate a register map and add it to the method. */
-      const std::vector<uint8_t>* dex_gc_map = GenerateGcMap();
-      if (dex_gc_map == NULL) {
-        DCHECK_NE(failures_.size(), 0U);
-        return false;  // Not a real failure, but a failure to encode
-      }
-      if (kIsDebugBuild) {
-        VerifyGcMap(*dex_gc_map);
-      }
-      verifier::MethodVerifier::SetDexGcMap(ref, dex_gc_map);
-    }
-
-    if (has_check_casts_) {
-      MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
-      if (method_to_safe_casts != NULL) {
-        SetSafeCastMap(ref, method_to_safe_casts);
-      }
-    }
-
-    if (has_virtual_or_interface_invokes_) {
-      MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
-      if (pc_to_concrete_method != NULL) {
-        SetDevirtMap(ref, pc_to_concrete_method);
-      }
-    }
-  }
   return true;
 }
 
@@ -3910,325 +3884,6 @@
   return *declaring_class_;
 }
 
-void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
-                                       size_t* log2_max_gc_pc) {
-  size_t local_gc_points = 0;
-  size_t max_insn = 0;
-  size_t max_ref_reg = -1;
-  for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
-    if (insn_flags_[i].IsCompileTimeInfoPoint()) {
-      local_gc_points++;
-      max_insn = i;
-      RegisterLine* line = reg_table_.GetLine(i);
-      max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
-    }
-  }
-  *gc_points = local_gc_points;
-  *ref_bitmap_bits = max_ref_reg + 1;  // if max register is 0 we need 1 bit to encode (ie +1)
-  size_t i = 0;
-  while ((1U << i) <= max_insn) {
-    i++;
-  }
-  *log2_max_gc_pc = i;
-}
-
-MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
-  /*
-   * Walks over the method code and adds any cast instructions in which
-   * the type cast is implicit to a set, which is used in the code generation
-   * to elide these casts.
-   */
-  if (!failure_messages_.empty()) {
-    return NULL;
-  }
-  UniquePtr<MethodSafeCastSet> mscs;
-  const Instruction* inst = Instruction::At(code_item_->insns_);
-  const Instruction* end = Instruction::At(code_item_->insns_ +
-                                           code_item_->insns_size_in_code_units_);
-
-  for (; inst < end; inst = inst->Next()) {
-    Instruction::Code code = inst->Opcode();
-    if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
-      uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
-      RegisterLine* line = reg_table_.GetLine(dex_pc);
-      bool is_safe_cast = false;
-      if (code == Instruction::CHECK_CAST) {
-        const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
-        const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
-        is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
-      } else {
-        const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
-        // We only know its safe to assign to an array if the array type is precise. For example,
-        // an Object[] can have any type of object stored in it, but it may also be assigned a
-        // String[] in which case the stores need to be of Strings.
-        if (array_type.IsPreciseReference()) {
-          const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
-          const RegType& component_type(reg_types_.GetComponentType(array_type,
-                                                                    class_loader_->get()));
-          is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
-        }
-      }
-      if (is_safe_cast) {
-        if (mscs.get() == NULL) {
-          mscs.reset(new MethodSafeCastSet());
-        }
-        mscs->insert(dex_pc);
-      }
-    }
-  }
-  return mscs.release();
-}
-
-MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
-  // It is risky to rely on reg_types for sharpening in cases of soft
-  // verification, we might end up sharpening to a wrong implementation. Just abort.
-  if (!failure_messages_.empty()) {
-    return NULL;
-  }
-
-  UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
-  const uint16_t* insns = code_item_->insns_;
-  const Instruction* inst = Instruction::At(insns);
-  const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
-
-  for (; inst < end; inst = inst->Next()) {
-    bool is_virtual   = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
-        (inst->Opcode() ==  Instruction::INVOKE_VIRTUAL_RANGE);
-    bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
-        (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
-
-    if (!is_interface && !is_virtual) {
-      continue;
-    }
-    // Get reg type for register holding the reference to the object that will be dispatched upon.
-    uint32_t dex_pc = inst->GetDexPc(insns);
-    RegisterLine* line = reg_table_.GetLine(dex_pc);
-    bool is_range = (inst->Opcode() ==  Instruction::INVOKE_VIRTUAL_RANGE) ||
-        (inst->Opcode() ==  Instruction::INVOKE_INTERFACE_RANGE);
-    const RegType&
-        reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
-
-    if (!reg_type.HasClass()) {
-      // We will compute devirtualization information only when we know the Class of the reg type.
-      continue;
-    }
-    mirror::Class* reg_class = reg_type.GetClass();
-    if (reg_class->IsInterface()) {
-      // We can't devirtualize when the known type of the register is an interface.
-      continue;
-    }
-    if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
-      // We can't devirtualize abstract classes except on arrays of abstract classes.
-      continue;
-    }
-    mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod(
-        is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
-    if (abstract_method == NULL) {
-      // If the method is not found in the cache this means that it was never found
-      // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
-      continue;
-    }
-    // Find the concrete method.
-    mirror::ArtMethod* concrete_method = NULL;
-    if (is_interface) {
-      concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
-    }
-    if (is_virtual) {
-      concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
-    }
-    if (concrete_method == NULL || concrete_method->IsAbstract()) {
-      // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
-      continue;
-    }
-    if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
-        concrete_method->GetDeclaringClass()->IsFinal()) {
-      // If we knew exactly the class being dispatched upon, or if the target method cannot be
-      // overridden record the target to be used in the compiler driver.
-      if (pc_to_concrete_method_map.get() == NULL) {
-        pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
-      }
-      MethodReference concrete_ref(
-          concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
-          concrete_method->GetDexMethodIndex());
-      pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
-    }
-  }
-  return pc_to_concrete_method_map.release();
-}
-
-const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
-  size_t num_entries, ref_bitmap_bits, pc_bits;
-  ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
-  // There's a single byte to encode the size of each bitmap
-  if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
-    // TODO: either a better GC map format or per method failures
-    Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
-       << ref_bitmap_bits << " registers";
-    return NULL;
-  }
-  size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
-  // There are 2 bytes to encode the number of entries
-  if (num_entries >= 65536) {
-    // TODO: either a better GC map format or per method failures
-    Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
-       << num_entries << " entries";
-    return NULL;
-  }
-  size_t pc_bytes;
-  RegisterMapFormat format;
-  if (pc_bits <= 8) {
-    format = kRegMapFormatCompact8;
-    pc_bytes = 1;
-  } else if (pc_bits <= 16) {
-    format = kRegMapFormatCompact16;
-    pc_bytes = 2;
-  } else {
-    // TODO: either a better GC map format or per method failures
-    Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
-       << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
-    return NULL;
-  }
-  size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
-  std::vector<uint8_t>* table = new std::vector<uint8_t>;
-  if (table == NULL) {
-    Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
-    return NULL;
-  }
-  table->reserve(table_size);
-  // Write table header
-  table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
-                             ~DexPcToReferenceMap::kRegMapFormatMask));
-  table->push_back(ref_bitmap_bytes & 0xFF);
-  table->push_back(num_entries & 0xFF);
-  table->push_back((num_entries >> 8) & 0xFF);
-  // Write table data
-  for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
-    if (insn_flags_[i].IsCompileTimeInfoPoint()) {
-      table->push_back(i & 0xFF);
-      if (pc_bytes == 2) {
-        table->push_back((i >> 8) & 0xFF);
-      }
-      RegisterLine* line = reg_table_.GetLine(i);
-      line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
-    }
-  }
-  DCHECK_EQ(table->size(), table_size);
-  return table;
-}
-
-void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
-  // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
-  // that the table data is well formed and all references are marked (or not) in the bitmap
-  DexPcToReferenceMap map(&data[0]);
-  DCHECK_EQ(data.size(), map.RawSize());
-  size_t map_index = 0;
-  for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
-    const uint8_t* reg_bitmap = map.FindBitMap(i, false);
-    if (insn_flags_[i].IsCompileTimeInfoPoint()) {
-      CHECK_LT(map_index, map.NumEntries());
-      CHECK_EQ(map.GetDexPc(map_index), i);
-      CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
-      map_index++;
-      RegisterLine* line = reg_table_.GetLine(i);
-      for (size_t j = 0; j < code_item_->registers_size_; j++) {
-        if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
-          CHECK_LT(j / 8, map.RegWidth());
-          CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
-        } else if ((j / 8) < map.RegWidth()) {
-          CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
-        } else {
-          // If a register doesn't contain a reference then the bitmap may be shorter than the line
-        }
-      }
-    } else {
-      CHECK(reg_bitmap == NULL);
-    }
-  }
-}
-
-void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* gc_map) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  {
-    WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
-    DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
-    if (it != dex_gc_maps_->end()) {
-      delete it->second;
-      dex_gc_maps_->erase(it);
-    }
-    dex_gc_maps_->Put(ref, gc_map);
-  }
-  DCHECK(GetDexGcMap(ref) != NULL);
-}
-
-
-void  MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
-  SafeCastMap::iterator it = safecast_map_->find(ref);
-  if (it != safecast_map_->end()) {
-    delete it->second;
-    safecast_map_->erase(it);
-  }
-  safecast_map_->Put(ref, cast_set);
-  DCHECK(safecast_map_->find(ref) != safecast_map_->end());
-}
-
-bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
-  SafeCastMap::const_iterator it = safecast_map_->find(ref);
-  if (it == safecast_map_->end()) {
-    return false;
-  }
-
-  // Look up the cast address in the set of safe casts
-  MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
-  return cast_it != it->second->end();
-}
-
-const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
-  DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
-  CHECK(it != dex_gc_maps_->end())
-    << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
-  CHECK(it->second != NULL);
-  return it->second;
-}
-
-void  MethodVerifier::SetDevirtMap(MethodReference ref,
-                                   const PcToConcreteMethodMap* devirt_map) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
-  DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
-  if (it != devirt_maps_->end()) {
-    delete it->second;
-    devirt_maps_->erase(it);
-  }
-
-  devirt_maps_->Put(ref, devirt_map);
-  DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
-}
-
-const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
-                                                                    uint32_t dex_pc) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
-  DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
-  if (it == devirt_maps_->end()) {
-    return NULL;
-  }
-
-  // Look up the PC in the map, get the concrete method to execute and return its reference.
-  MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
-      = it->second->find(dex_pc);
-  if (pc_to_concrete_method != it->second->end()) {
-    return &(pc_to_concrete_method->second);
-  } else {
-    return NULL;
-  }
-}
-
 std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
   RegisterLine* line = reg_table_.GetLine(dex_pc);
   std::vector<int32_t> result;
@@ -4273,120 +3928,14 @@
   return result;
 }
 
-bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
-                                               const uint32_t access_flags) {
-#ifdef ART_SEA_IR_MODE
-    bool use_sea = Runtime::Current()->IsSeaIRMode();
-    use_sea = use_sea && (std::string::npos != PrettyMethod(
-                          method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
-    if (use_sea) return true;
-#endif
-  // Don't compile class initializers, ever.
-  if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
-    return false;
-  }
-  return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
-}
-
-ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
-MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
-
-ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
-MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
-
-ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
-MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
-
-ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
-MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
-
 void MethodVerifier::Init() {
-  if (Runtime::Current()->IsCompiler()) {
-    dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
-    Thread* self = Thread::Current();
-    {
-      WriterMutexLock mu(self, *dex_gc_maps_lock_);
-      dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
-    }
-
-    safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
-    {
-      WriterMutexLock mu(self, *safecast_map_lock_);
-      safecast_map_ = new MethodVerifier::SafeCastMap();
-    }
-
-    devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
-
-    {
-      WriterMutexLock mu(self, *devirt_maps_lock_);
-      devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
-    }
-
-    rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
-    {
-      WriterMutexLock mu(self, *rejected_classes_lock_);
-      rejected_classes_ = new MethodVerifier::RejectedClassesTable;
-    }
-  }
   art::verifier::RegTypeCache::Init();
 }
 
 void MethodVerifier::Shutdown() {
-  if (Runtime::Current()->IsCompiler()) {
-    Thread* self = Thread::Current();
-    {
-      WriterMutexLock mu(self, *dex_gc_maps_lock_);
-      STLDeleteValues(dex_gc_maps_);
-      delete dex_gc_maps_;
-      dex_gc_maps_ = NULL;
-    }
-    delete dex_gc_maps_lock_;
-    dex_gc_maps_lock_ = NULL;
-
-    {
-      WriterMutexLock mu(self, *safecast_map_lock_);
-      STLDeleteValues(safecast_map_);
-      delete safecast_map_;
-      safecast_map_ = NULL;
-    }
-    delete safecast_map_lock_;
-    safecast_map_lock_ = NULL;
-
-    {
-      WriterMutexLock mu(self, *devirt_maps_lock_);
-      STLDeleteValues(devirt_maps_);
-      delete devirt_maps_;
-      devirt_maps_ = NULL;
-    }
-    delete devirt_maps_lock_;
-    devirt_maps_lock_ = NULL;
-
-    {
-      WriterMutexLock mu(self, *rejected_classes_lock_);
-      delete rejected_classes_;
-      rejected_classes_ = NULL;
-    }
-    delete rejected_classes_lock_;
-    rejected_classes_lock_ = NULL;
-  }
   verifier::RegTypeCache::ShutDown();
 }
 
-void MethodVerifier::AddRejectedClass(ClassReference ref) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  {
-    WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
-    rejected_classes_->insert(ref);
-  }
-  CHECK(IsClassRejected(ref));
-}
-
-bool MethodVerifier::IsClassRejected(ClassReference ref) {
-  DCHECK(Runtime::Current()->IsCompiler());
-  ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
-  return (rejected_classes_->find(ref) != rejected_classes_->end());
-}
-
 void MethodVerifier::VisitRoots(RootVisitor* visitor, void* arg) {
   reg_types_.VisitRoots(visitor, arg);
 }
diff --git a/runtime/verifier/method_verifier.h b/runtime/verifier/method_verifier.h
index dffda96..ac36a7e 100644
--- a/runtime/verifier/method_verifier.h
+++ b/runtime/verifier/method_verifier.h
@@ -183,16 +183,6 @@
   // information
   void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
-  static const std::vector<uint8_t>* GetDexGcMap(MethodReference ref)
-      LOCKS_EXCLUDED(dex_gc_maps_lock_);
-
-  static const MethodReference* GetDevirtMap(const MethodReference& ref, uint32_t dex_pc)
-      LOCKS_EXCLUDED(devirt_maps_lock_);
-
-  // Returns true if the cast can statically be verified to be redundant
-  // by using the check-cast elision peephole optimization in the verifier
-  static bool IsSafeCast(MethodReference ref, uint32_t pc) LOCKS_EXCLUDED(safecast_map_lock_);
-
   // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
   // to the locks held at 'dex_pc' in method 'm'.
   static void FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
@@ -212,11 +202,6 @@
   static void Init() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
   static void Shutdown();
 
-  static void AddRejectedClass(ClassReference ref)
-      LOCKS_EXCLUDED(rejected_classes_lock_);
-  static bool IsClassRejected(ClassReference ref)
-      LOCKS_EXCLUDED(rejected_classes_lock_);
-
   bool CanLoadClasses() const {
     return can_load_classes_;
   }
@@ -236,11 +221,22 @@
   // Describe VRegs at the given dex pc.
   std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
 
-  static bool IsCandidateForCompilation(MethodReference& method_ref,
-                                        const uint32_t access_flags);
-
   void VisitRoots(RootVisitor* visitor, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
+  // Accessors used by the compiler via CompilerCallback
+  const DexFile::CodeItem* CodeItem() const;
+  RegisterLine* GetRegLine(uint32_t dex_pc);
+  const InstructionFlags& GetInstructionFlags(size_t index) const;
+  mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+  mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+  MethodReference GetMethodReference() const;
+  uint32_t GetAccessFlags() const;
+  bool HasCheckCasts() const;
+  bool HasVirtualOrInterfaceInvokes() const;
+  bool HasFailures() const;
+  const RegType& ResolveCheckedClass(uint32_t class_idx)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
  private:
   // Adds the given string to the beginning of the last failure message.
   void PrependToLastFailMessage(std::string);
@@ -612,57 +608,8 @@
   // Get a type representing the declaring class of the method.
   const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
-  /*
-   * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
-   * verification). For type-precise determination we have all the data we need, so we just need to
-   * encode it in some clever fashion.
-   * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
-   */
-  const std::vector<uint8_t>* GenerateGcMap();
-
-  // Verify that the GC map associated with method_ is well formed
-  void VerifyGcMap(const std::vector<uint8_t>& data);
-
-  // Compute sizes for GC map data
-  void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
-
   InstructionFlags* CurrentInsnFlags();
 
-  // All the GC maps that the verifier has created
-  typedef SafeMap<const MethodReference, const std::vector<uint8_t>*,
-      MethodReferenceComparator> DexGcMapTable;
-  static ReaderWriterMutex* dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
-  static DexGcMapTable* dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_);
-  static void SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* dex_gc_map)
-      LOCKS_EXCLUDED(dex_gc_maps_lock_);
-
-
-  // Cast elision types.
-  typedef std::set<uint32_t> MethodSafeCastSet;
-  typedef SafeMap<const MethodReference, const MethodSafeCastSet*,
-      MethodReferenceComparator> SafeCastMap;
-  MethodVerifier::MethodSafeCastSet* GenerateSafeCastSet()
-      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
-  static void SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* mscs);
-      LOCKS_EXCLUDED(safecast_map_lock_);
-  static ReaderWriterMutex* safecast_map_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
-  static SafeCastMap* safecast_map_ GUARDED_BY(safecast_map_lock_);
-
-  // Devirtualization map.
-  typedef SafeMap<const uint32_t, MethodReference> PcToConcreteMethodMap;
-  typedef SafeMap<const MethodReference, const PcToConcreteMethodMap*,
-      MethodReferenceComparator> DevirtualizationMapTable;
-  MethodVerifier::PcToConcreteMethodMap* GenerateDevirtMap()
-      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
-
-  static ReaderWriterMutex* devirt_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
-  static DevirtualizationMapTable* devirt_maps_ GUARDED_BY(devirt_maps_lock_);
-  static void SetDevirtMap(MethodReference ref,
-                           const PcToConcreteMethodMap* pc_method_map)
-        LOCKS_EXCLUDED(devirt_maps_lock_);
-  typedef std::set<ClassReference> RejectedClassesTable;
-  static ReaderWriterMutex* rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
-  static RejectedClassesTable* rejected_classes_ GUARDED_BY(rejected_classes_lock_);
 
   RegTypeCache reg_types_;